Skip to content

Commit

Permalink
When IgnoreMutator decorator class is used, work with underlying clas…
Browse files Browse the repository at this point in the history
…s of mutator to get its definition (#1972)

* When IgnoreMutator decorator class is used, work with underlying class of mutator to get its definition

Fixes:

```
DomainException: The class "Infection\Mutator\IgnoreMutator" does not have a definition
```

on StrykerHtmlReportBuilder.php

Bug has been introduced in https://github.com/infection/infection/pull/1686/files#diff-01143bfb1cd8c30fa7f368e58fa5b9bebf3f35de2a4bc509123ed81358b59ff8R249

* Fix phpstan type error
  • Loading branch information
maks-rafalko authored May 27, 2024
1 parent 0b13941 commit e2200ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/Logger/Html/StrykerHtmlReportBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@
use Infection\Mutant\MutantExecutionResult;
use Infection\Mutator\FunctionSignature\ProtectedVisibility;
use Infection\Mutator\FunctionSignature\PublicVisibility;
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorFactory;
use Infection\Mutator\MutatorResolver;
use Infection\Mutator\ProfileList;
use Infection\Mutator\Removal\MethodCallRemoval;
use Infection\Str;
use function ltrim;
use function md5;
use const PHP_EOL;
use PhpParser\NodeAbstract;
use function Safe\file_get_contents;
use function Safe\preg_match;
use function Safe\preg_split;
Expand Down Expand Up @@ -246,7 +249,7 @@ function (MutantExecutionResult $result) use ($originalCode): array {
'id' => $result->getMutantHash(),
'mutatorName' => $result->getMutatorName(),
'replacement' => Str::convertToUtf8(Str::trimLineReturns(ltrim($replacement))),
'description' => $this->getMutatorDescription($result->getMutatorClass()),
'description' => $this->getMutatorDescription($result->getMutatorName(), $result->getMutatorClass()),
'location' => [
'start' => ['line' => $result->getOriginalStartingLine(), 'column' => $startingColumn],
'end' => ['line' => $endingLine, 'column' => $endingColumn],
Expand Down Expand Up @@ -312,10 +315,13 @@ private function getTestsCompleted(string $processOutput): int
return 0;
}

private function getMutatorDescription(string $mutatorClass): string
private function getMutatorDescription(string $mutatorName, string $mutatorClass): string
{
Assert::true(MutatorResolver::isValidMutator($mutatorClass), sprintf('Unknown mutator "%s"', $mutatorClass));

/** @var class-string<Mutator<NodeAbstract>> $mutatorClass */
$mutatorClass = ProfileList::ALL_MUTATORS[$mutatorName] ?? $mutatorClass;

$definition = $mutatorClass::getDefinition();

Assert::notNull($definition);
Expand Down
8 changes: 6 additions & 2 deletions tests/phpunit/Logger/Html/StrykerHtmlReportBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use Infection\Mutant\DetectionStatus;
use Infection\Mutant\MutantExecutionResult;
use Infection\Mutator\FunctionSignature\PublicVisibility;
use Infection\Mutator\IgnoreMutator;
use Infection\Mutator\Removal\ArrayItemRemoval;
use Infection\Mutator\Removal\MethodCallRemoval;
use Infection\Testing\MutatorName;
Expand Down Expand Up @@ -301,7 +302,7 @@ final class ForHtmlReport
$this->inner('3');
DIFF,
'32f68ca331c9262cc97322271d88d06d',
PublicVisibility::class,
IgnoreMutator::class,
realpath(__DIR__ . '/../../Fixtures/ForHtmlReport.php'),
13,
35,
Expand All @@ -312,6 +313,8 @@ final class ForHtmlReport
// check that duplicate values are moved in the report
new TestLocation('TestClass::test_method1', '/infection/path/to/TestClass.php', 0.123),
],
'PHPUnit output. Tests: 1, Assertions: 3',
'PublicVisibility',
),
// this tests diff on the one-line method call removal
self::createMutantExecutionResult(
Expand Down Expand Up @@ -472,6 +475,7 @@ private static function createMutantExecutionResult(
int $originalEndFilePosition,
array $testLocations,
?string $processOutput = 'PHPUnit output. Tests: 1, Assertions: 3',
?string $mutatorName = null,
): MutantExecutionResult {
return new MutantExecutionResult(
'bin/phpunit --configuration infection-tmp-phpunit.xml --filter "tests/Acme/FooTest.php"',
Expand All @@ -480,7 +484,7 @@ private static function createMutantExecutionResult(
now(normalize_trailing_spaces($diff)),
$mutantHash,
$mutatorClassName,
MutatorName::getName($mutatorClassName),
$mutatorName ?? MutatorName::getName($mutatorClassName),
$originalFileRealPath,
$originalStartingLine,
$originalEndingLine,
Expand Down

0 comments on commit e2200ae

Please sign in to comment.