Last active
January 27, 2021 14:11
-
-
Save tscheepers/66400c46b117c652f608 to your computer and use it in GitHub Desktop.
Laravel and Laravel Behat Extension code coverage trait
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 @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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!