|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Atk4\Ui\Behat; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use SebastianBergmann\CodeCoverage\CodeCoverage; |
| 9 | +use SebastianBergmann\CodeCoverage\Driver\Selector as DriverSelector; |
| 10 | +use SebastianBergmann\CodeCoverage\Filter; |
| 11 | +use SebastianBergmann\CodeCoverage\Report; |
| 12 | + |
| 13 | +class CoverageUtil |
| 14 | +{ |
| 15 | + private static ?CodeCoverage $coverage = null; |
| 16 | + |
| 17 | + private function __construct() |
| 18 | + { |
| 19 | + // zeroton |
| 20 | + } |
| 21 | + |
| 22 | + public static function start(Filter $filter): void |
| 23 | + { |
| 24 | + if (self::$coverage !== null) { |
| 25 | + throw new \Error('Coverage already started'); |
| 26 | + } |
| 27 | + |
| 28 | + self::$coverage = new CodeCoverage((new DriverSelector())->forLineCoverage($filter), $filter); |
| 29 | + self::$coverage->cacheStaticAnalysis(sys_get_temp_dir() . '/phpunit-coverage.' . md5(__DIR__) . '.cache'); |
| 30 | + self::$coverage->start(self::class); |
| 31 | + } |
| 32 | + |
| 33 | + public static function startFromPhpunitConfig(string $phpunitConfigDir): void |
| 34 | + { |
| 35 | + $filter = new Filter(); |
| 36 | + |
| 37 | + $phpunitCoverageConfig = simplexml_load_file($phpunitConfigDir . '/phpunit.xml.dist')->coverage; |
| 38 | + foreach ($phpunitCoverageConfig->include->directory as $path) { |
| 39 | + $filter->includeDirectory($phpunitConfigDir . '/' . $path); |
| 40 | + } |
| 41 | + foreach ($phpunitCoverageConfig->exclude->directory as $path) { |
| 42 | + $filter->excludeDirectory($phpunitConfigDir . '/' . $path); |
| 43 | + } |
| 44 | + |
| 45 | + static::start($filter); |
| 46 | + } |
| 47 | + |
| 48 | + public static function saveData(string $outputDir): void |
| 49 | + { |
| 50 | + $outputFile = $outputDir . '/' . basename($_SERVER['SCRIPT_NAME'] ?? 'unknown', '.php') . '-' . hash('sha256', microtime(true) . random_bytes(64)) . '.cov'; |
| 51 | + |
| 52 | + self::$coverage->stop(); |
| 53 | + $writer = new Report\PHP(); |
| 54 | + $writer->process(self::$coverage, $outputFile); |
| 55 | + self::$coverage = null; |
| 56 | + } |
| 57 | + |
| 58 | + public static function isCalledFromPhpunit(): bool |
| 59 | + { |
| 60 | + foreach (debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) { |
| 61 | + if (is_a($frame['class'] ?? null, TestCase::class, true)) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return false; |
| 67 | + } |
| 68 | +} |
0 commit comments