Skip to content

Commit

Permalink
Fix coverage report for root directory and file reports (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz authored Jul 13, 2021
1 parent 0932bf9 commit 199f146
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions bin/code-coverage-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +23,6 @@

$loader = require $autoloaderFile;


$phpunitBridgeDirectories = [
dirname(realpath($autoloaderFile)) . '/bin/.phpunit',
dirname(dirname(realpath($autoloaderFile))) . '/bin/.phpunit',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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')) {
Expand Down

0 comments on commit 199f146

Please sign in to comment.