Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| CoverageSummaryLoader | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |
100.00% |
1 / 1 |
| load | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * Fast Forward Development Tools for PHP projects. |
| 7 | * |
| 8 | * This file is part of fast-forward/dev-tools project. |
| 9 | * |
| 10 | * @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 11 | * @license https://opensource.org/licenses/MIT MIT License |
| 12 | * |
| 13 | * @see https://github.com/php-fast-forward/ |
| 14 | * @see https://github.com/php-fast-forward/dev-tools |
| 15 | * @see https://github.com/php-fast-forward/dev-tools/issues |
| 16 | * @see https://php-fast-forward.github.io/dev-tools/ |
| 17 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 18 | */ |
| 19 | |
| 20 | namespace FastForward\DevTools\PhpUnit\Coverage; |
| 21 | |
| 22 | use RuntimeException; |
| 23 | use SebastianBergmann\CodeCoverage\CodeCoverage; |
| 24 | |
| 25 | use function is_file; |
| 26 | |
| 27 | /** |
| 28 | * Loads line coverage metrics from the serialized PHPUnit `coverage-php` output. |
| 29 | */ |
| 30 | class CoverageSummaryLoader implements CoverageSummaryLoaderInterface |
| 31 | { |
| 32 | /** |
| 33 | * @param string $coverageReportPath Path to the PHPUnit `coverage-php` report file |
| 34 | * |
| 35 | * @return CoverageSummary Extracted line coverage summary |
| 36 | * |
| 37 | * @throws RuntimeException When the report file does not exist or contains invalid data |
| 38 | */ |
| 39 | public function load(string $coverageReportPath): CoverageSummary |
| 40 | { |
| 41 | if (! is_file($coverageReportPath)) { |
| 42 | throw new RuntimeException(\sprintf('PHPUnit coverage report not found: %s', $coverageReportPath)); |
| 43 | } |
| 44 | |
| 45 | /** @var mixed $coverage */ |
| 46 | $coverage = require $coverageReportPath; |
| 47 | |
| 48 | if (! $coverage instanceof CodeCoverage) { |
| 49 | throw new RuntimeException(\sprintf('PHPUnit coverage report is invalid: %s', $coverageReportPath)); |
| 50 | } |
| 51 | |
| 52 | $report = $coverage->getReport(); |
| 53 | |
| 54 | return new CoverageSummary($report->numberOfExecutedLines(), $report->numberOfExecutableLines()); |
| 55 | } |
| 56 | } |