Skip to content

Instantly share code, notes, and snippets.

@tscheepers
Last active January 27, 2021 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tscheepers/66400c46b117c652f608 to your computer and use it in GitHub Desktop.
Save tscheepers/66400c46b117c652f608 to your computer and use it in GitHub Desktop.
Laravel and Laravel Behat Extension code coverage trait
<?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');
}
}
@klapifoch
Copy link

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!

@tscheepers
Copy link
Author

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?

@klapifoch
Copy link

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!

@praditha
Copy link

praditha commented Jun 8, 2016

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.

@jaceju
Copy link

jaceju commented Dec 14, 2016

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