Skip to content

Instantly share code, notes, and snippets.

@jaceju
Created December 14, 2016 08:16
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 jaceju/e74464e9bbc19e1df84888cc3181861e to your computer and use it in GitHub Desktop.
Save jaceju/e74464e9bbc19e1df84888cc3181861e to your computer and use it in GitHub Desktop.
<?php
namespace Feature\Helper;
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\CodeCoverage as PHPUnitCodeCoverage;
use SebastianBergmann\CodeCoverage\InvalidArgumentException;
use SebastianBergmann\CodeCoverage\Report\Clover;
use SebastianBergmann\CodeCoverage\Report\Html\Facade as Html;
use SebastianBergmann\CodeCoverage\Report\Text;
use SebastianBergmann\CodeCoverage\RuntimeException;
trait CodeCoverage
{
/**
* @var PHPUnitCodeCoverage
*/
protected static $coverage;
/**
* @BeforeSuite
* @throws RuntimeException
* @throws InvalidArgumentException
*/
public static function setupCoverage()
{
if (self::isCodeCoverageEnabled()) {
$filter = new Filter();
$filter->addDirectoryToWhitelist(__DIR__ . '/../../app');
$filter->addDirectoryToWhitelist(__DIR__ . '/../../src');
self::$coverage = new PHPUnitCodeCoverage(null, $filter);
self::$coverage->start('Behat Test');
}
}
/**
* @AfterSuite
* @throws InvalidArgumentException
*/
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 Clover;
$writer->process(
self::$coverage,
__DIR__ . '/../../reports/coverage/clover.xml'
);
}
// Use HTML to take a look at your test coverage locally
if (getenv('CODE_COVERAGE_HTML')) {
$writer = new Html;
$writer->process(
self::$coverage,
__DIR__ . '/../../reports/coverage'
);
}
// 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 Text(75, 90, false, false);
fwrite(STDOUT, $writer->process(self::$coverage, true));
}
}
/**
* @return bool
*/
private static function isCodeCoverageEnabled()
{
return
getenv('CODE_COVERAGE') ||
getenv('CODE_COVERAGE_HTML') ||
getenv('CODE_COVERAGE_CLOVER');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment