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');
}
}
@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