Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/PhpunitMerger/Command/CoverageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,21 @@ protected function configure()
null,
InputOption::VALUE_REQUIRED,
'The highLowerBound value to be used for HTML format'
);
)
->addOption(
'projectRoot',
null,
InputOption::VALUE_REQUIRED,
'The path to the project source code'
)
->addOption(
'fixRoot',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
'The root path that will be replaced with the project root path',
[]
)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand All @@ -61,12 +75,20 @@ protected function execute(InputInterface $input, OutputInterface $output)

$codeCoverage = $this->getCodeCoverage();

$projectRoot = $input->getOption('projectRoot');
$fixRoot = $input->getOption('fixRoot');

foreach ($finder as $file) {
$coverage = require $file->getRealPath();
if (!$coverage instanceof CodeCoverage) {
throw new \RuntimeException($file->getRealPath() . ' doesn\'t return a valid ' . CodeCoverage::class . ' object!');
}
$this->normalizeCoverage($coverage);

if (!empty($projectRoot) && !empty($fixRoot)) {
$this->fixPaths($coverage, $projectRoot, $fixRoot);
}

$codeCoverage->merge($coverage);
}

Expand Down Expand Up @@ -97,6 +119,26 @@ private function normalizeCoverage(CodeCoverage $coverage)
$coverage->setTests($tests);
}

private function fixPaths(CodeCoverage $coverage, string $projectRoot, array $paths)
{
$data = $coverage->getData();
$lineCoverage = [];
foreach ($data->lineCoverage() as $fileName => $coverageData) {
$newName = $fileName;
foreach ($paths as $path) {
$newName = str_replace($path, $projectRoot, $fileName);
if ($newName != $fileName) {
break;
}
}

$lineCoverage[$newName] = $coverageData;
}

$data->setLineCoverage($lineCoverage);
$coverage->setData($data);
}

private function writeCodeCoverage(CodeCoverage $codeCoverage, OutputInterface $output, $file = null)
{
$writer = new Clover();
Expand Down