Skip to content
Closed
Show file tree
Hide file tree
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
59 changes: 57 additions & 2 deletions src/Tester/MutationTestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ public function run(): int
}
}

$coveredLines = array_map(fn (array $lines): array => array_filter($lines, fn (?array $tests): bool => $tests !== [] && $tests !== null), $coverageData->lineCoverage());
$coveredLines = array_filter($coveredLines, fn (array $lines): bool => $lines !== []);
$coveredLines = $this->coveredLines($coverageData);

$files = FileFinder::files($this->getConfiguration()->paths, $this->getConfiguration()->pathsToIgnore);

Expand Down Expand Up @@ -192,6 +191,62 @@ classesToMutate: $this->getConfiguration()->everything ? [] : $this->getConfigur
return $this->isMinScoreIsReached($mutationSuite) ? 0 : 1;
}

/**
* Returns the tests covering each line, as `[file => [line => [testId, ...]]]`.
*
* @return array<string, array<int, array<int, string>>>
*/
private function coveredLines(ProcessedCodeCoverageData $coverageData): array
{
/** @var array<int, string> $testIds */
$testIds = method_exists($coverageData, 'testIds') ? $coverageData->testIds() : []; // @phpstan-ignore function.alreadyNarrowedType

$coveredLines = [];

foreach ($coverageData->lineCoverage() as $file => $lines) {
foreach ($lines as $line => $tests) {
$ids = $this->testIdsCoveringLine($tests ?? [], $testIds);

if ($ids === []) {
continue;
}

$coveredLines[$file][$line] = $ids;
}
}

return $coveredLines;
}

/**
* Resolves the test ids a single line holds.
*
* Up to phpunit/php-code-coverage 14.2 a line held the test ids themselves. Since
* 14.3 it holds hit counts, keyed by an index into the coverage data's test ids.
*
* @param array<int|string, mixed> $tests
* @param array<int, string> $testIds
* @return array<int, string>
*/
private function testIdsCoveringLine(array $tests, array $testIds): array
{
$ids = [];

foreach ($tests as $index => $test) {
if (is_string($test)) {
$ids[] = $test;

continue;
}

if (is_int($index) && isset($testIds[$index])) {
$ids[] = $testIds[$index];
}
}

return $ids;
}

private function getConfiguration(): Configuration
{
return Container::getInstance()->get(ConfigurationRepository::class)->mergedConfiguration(); // @phpstan-ignore-line
Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/Tester/MutationTestRunnerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

use Pest\Mutate\Tester\MutationTestRunner;

/**
* @param array<int|string, mixed> $tests
* @param array<int, string> $testIds
* @param array<int, string> $expected
*/
it('resolves the tests covering a line from either code coverage shape', function (array $tests, array $testIds, array $expected): void {
$runner = new MutationTestRunner;

$method = new ReflectionClass($runner)->getMethod('testIdsCoveringLine');

expect($method->invoke($runner, $tests, $testIds))->toBe($expected);
})->with([
'ids in the values, up to php-code-coverage 14.2' => [
['P\Tests\Unit\FooTest::__pest_evaluable_it_works', 'Tests\Unit\BarTest::test_baz'],
[],
['P\Tests\Unit\FooTest::__pest_evaluable_it_works', 'Tests\Unit\BarTest::test_baz'],
],
'hit counts keyed by index, since php-code-coverage 14.3' => [
[0 => 1, 2 => 4],
[
0 => 'P\Tests\Unit\FooTest::__pest_evaluable_it_works',
1 => 'Tests\Unit\OtherTest::test_untouched',
2 => 'Tests\Unit\BarTest::test_baz',
],
['P\Tests\Unit\FooTest::__pest_evaluable_it_works', 'Tests\Unit\BarTest::test_baz'],
],
'an index with no test id is dropped' => [[7 => 1], [0 => 'Tests\Unit\FooTest::test_baz'], []],
'nothing covers the line' => [[], [], []],
]);