-
-
Save tscheepers/66400c46b117c652f608 to your computer and use it in GitHub Desktop.
<?php | |
trait CodeCoverage { | |
/** | |
* @var PHP_CodeCoverage | |
*/ | |
protected static $coverage; | |
/** | |
* @BeforeSuite | |
*/ | |
public static function setupCoverage() | |
{ | |
if (self::isCodeCoverageEnabled()) { | |
$filter = new PHP_CodeCoverage_Filter(); | |
$filter->addDirectoryToBlacklist(__DIR__ . '/../../vendor'); | |
$filter->addDirectoryToWhitelist(__DIR__ . '/../../app'); | |
self::$coverage = new PHP_CodeCoverage(null, $filter); | |
self::$coverage->start('Behat Test'); | |
} | |
} | |
/** | |
* @AfterSuite | |
*/ | |
public static function writeCoverageFiles() | |
{ | |
if (self::isCodeCoverageEnabled()) { | |
self::$coverage->stop(); | |
// Use Clover to analyze your coverage with a tool | |
if (getenv('CODE_COVERAGE_CLOVER')) { | |
$writer = new PHP_CodeCoverage_Report_Clover; | |
$writer->process( | |
self::$coverage, | |
__DIR__ . '/../../clover-behat.xml' | |
); | |
} | |
// Use HTML to take a look at your test coverage locally | |
if (getenv('CODE_COVERAGE_HTML')) { | |
$writer = new PHP_CodeCoverage_Report_HTML; | |
$writer->process( | |
self::$coverage, | |
__DIR__ . '/../../coverage-behat' | |
); | |
} | |
// use the text report to append the coverage results | |
// to stdout at the end of the test run | |
// so you can view them from the command line | |
$writer = new PHP_CodeCoverage_Report_Text(75, 90, false, false); | |
fwrite(STDOUT, $writer->process(self::$coverage, true)); | |
} | |
} | |
private static function isCodeCoverageEnabled() | |
{ | |
return | |
getenv('CODE_COVERAGE') || | |
getenv('CODE_COVERAGE_HTML') || | |
getenv('CODE_COVERAGE_CLOVER'); | |
} | |
} |
Hi @klapifoch, have you added phpunit's code coverage plugin to you composer.json?
{
"require-dev": {
"phpunit/php-code-coverage": "~2.0"
}
}
And if its something else could you send the error messages?
My bad, one thing is, i was called trait in the wrong place(outside the class not inside), and the other one is you need to have installed XDebug.
Thanks for your help!
Hi @tscheepers, I have a problem and I hope you can help me.
I already configure everything and try to run CODE_COVERAGE_HTML=on ./vendor/bin/behat
and great, it's work.
The coverage-behat folder is generated.
But the problem is, when I open the index.html, I found that all of my controller, model and another files that I've created is marked as Not Executed. Even though the test is successfully and I'm sure it's already call the correct controller, model and etc.
Thanks.
This is an implement for phpunit/php-code-coverage: ~4.0
:
https://gist.github.com/jaceju/e74464e9bbc19e1df84888cc3181861e
I hope you can help me with this:
I downloaded https://github.com/laracasts/Behat-Laravel-Extension-Example-App
Inside features\bootstrap i created a file called CodeCoverage.php and copy the trait content in this gist.
After that added use CodeCoverage; to FeatureContext.
But when i run this:
CODE_COVERAGE=on ./vendor/bin/behat
It's not working for me, what am i doing wrong? Thanks!