Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel 5, Laravel Behat Extension code coverage trait. More in this blog article: http://www.label305.com/laravel/mink/behat/coverage/2015/02/17/Code-coverage-for-the-Laravel-Behat-Extension.html
<?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

This comment has been minimized.

Copy link

klapifoch commented Feb 18, 2015

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

This comment has been minimized.

Copy link
Owner Author

tscheepers commented Feb 18, 2015

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

This comment has been minimized.

Copy link

klapifoch commented Feb 19, 2015

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

This comment has been minimized.

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

This comment has been minimized.

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
You can’t perform that action at this time.