Created
December 14, 2016 08:16
-
-
Save jaceju/e74464e9bbc19e1df84888cc3181861e to your computer and use it in GitHub Desktop.
Behat Coverage implement of http://www.label305.com/laravel/mink/behat/coverage/2015/02/17/Code-coverage-for-the-Laravel-Behat-Extension.html
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 | |
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