From 199f1467ddb599ba842f435f4439993df3705426 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Tue, 13 Jul 2021 09:02:46 +0200 Subject: [PATCH] Fix coverage report for root directory and file reports (#6) --- bin/code-coverage-checker.php | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/bin/code-coverage-checker.php b/bin/code-coverage-checker.php index e3eaeb8..512e7bf 100644 --- a/bin/code-coverage-checker.php +++ b/bin/code-coverage-checker.php @@ -4,6 +4,7 @@ use SebastianBergmann\CodeCoverage\CodeCoverage; use SebastianBergmann\CodeCoverage\Node\Directory; +use SebastianBergmann\CodeCoverage\Node\File; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\TableStyle; use Symfony\Component\Console\Input\ArgvInput; @@ -22,7 +23,6 @@ $loader = require $autoloaderFile; - $phpunitBridgeDirectories = [ dirname(realpath($autoloaderFile)) . '/bin/.phpunit', dirname(dirname(realpath($autoloaderFile))) . '/bin/.phpunit', @@ -85,7 +85,7 @@ // Check all root paths if no paths are given if (empty($paths)) { - /** @var Directory $report */ + /** @var Directory|File $report */ foreach ($coverage->getReport() as $report) { if (\method_exists($report, 'getPath')) { // PHPUNIT <= 8 @@ -192,7 +192,10 @@ function assertCodeCoverage(CodeCoverage $coverage, string $path, string $metric return 0; } -function printCodeCoverageReport(Directory $pathReport): void +/** + * @param Directory|File $pathReport + */ +function printCodeCoverageReport($pathReport): void { global $io; @@ -259,10 +262,25 @@ function printCodeCoverageReport(Directory $pathReport): void $io->newLine(1); } -function getReportForPath(Directory $rootReport, string $path): ?Directory +/** + * @return Directory|File|null + */ +function getReportForPath(Directory $rootReport, string $path) { $currentPath = getcwd() . DIRECTORY_SEPARATOR . $path; + if (\method_exists($rootReport, 'getPath')) { + // PHPUNIT <= 8 + $rootPath = $rootReport->getPath(); + } else { + // PHPUNIT 9 + $rootPath = $rootReport->pathAsString(); + } + + if (0 === mb_strpos($rootPath, $currentPath)) { + return $rootReport; + } + /** @var Directory $report */ foreach ($rootReport as $report) { if (\method_exists($report, 'getPath')) {