Skip to content

Commit

Permalink
Make PHPUnit 10.1 XML coverage report and test cases names with provi…
Browse files Browse the repository at this point in the history
…der compatible with Infection and old format (#1854)

In PHPUnit >=10 data providers with keys are stored as `Class\\test_method#some key`

In PHPUnit <10 data providers with keys are stored as `Class\\test_method with data set "some key"`.

We need to translate to the old format because this is what PHPUnit <10 and >=10 understand from CLI `--filter` option.

In other words, PHPUnit 10 has a BC break in XML coverage reports that this PR workarounds.
  • Loading branch information
maks-rafalko committed May 13, 2023
1 parent f669c4e commit ebbe59e
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/TestFramework/AbstractTestFrameworkAdapter.php
Expand Up @@ -99,7 +99,8 @@ public function getMutantCommandLine(
$mutationOriginalFilePath
),
$extraOptions,
$coverageTests
$coverageTests,
$this->getVersion()
)
);
}
Expand Down
Expand Up @@ -52,5 +52,5 @@ public function buildForInitialTestsRun(string $configPath, string $extraOptions
*
* @return string[]
*/
public function buildForMutant(string $configPath, string $extraOptions, array $tests): array;
public function buildForMutant(string $configPath, string $extraOptions, array $tests, string $testFrameworkVersion): array;
}
Expand Up @@ -46,7 +46,8 @@
use function ltrim;
use function preg_quote;
use function rtrim;
use function Safe\sprintf;
use function sprintf;
use function version_compare;

/**
* @internal
Expand Down Expand Up @@ -77,7 +78,7 @@ public function buildForInitialTestsRun(string $configPath, string $extraOptions
/**
* @param TestLocation[] $tests
*/
public function buildForMutant(string $configPath, string $extraOptions, array $tests): array
public function buildForMutant(string $configPath, string $extraOptions, array $tests, string $testFrameworkVersion): array
{
$options = $this->buildForInitialTestsRun($configPath, $extraOptions);

Expand All @@ -91,8 +92,14 @@ public function buildForMutant(string $configPath, string $extraOptions, array $
$partsDelimitedByColons = explode('::', $testCaseString, 2);

if (count($partsDelimitedByColons) > 1) {
$parts = explode('\\', $partsDelimitedByColons[0]);
$testCaseString = sprintf('%s::%s', end($parts), $partsDelimitedByColons[1]);
$methodNameWithDataProvider = $this->getMethodNameWithDataProvider($partsDelimitedByColons[1], $testFrameworkVersion);

$testClassFullyQualifiedClassName = $partsDelimitedByColons[0];

$parts = explode('\\', $testClassFullyQualifiedClassName);
$classNameWithoutNamespace = end($parts);

$testCaseString = sprintf('%s::%s', $classNameWithoutNamespace, $methodNameWithDataProvider);
}

if (array_key_exists($testCaseString, $usedTestCases)) {
Expand All @@ -112,4 +119,28 @@ public function buildForMutant(string $configPath, string $extraOptions, array $

return $options;
}

private function getMethodNameWithDataProvider(string $methodNameWithDataProvider, string $testFrameworkVersion): string
{
$methodNameWithDataProviderResult = $methodNameWithDataProvider;

/*
* in PHPUnit >=10 data providers with keys are stored as `Class\\test_method#some key`
* in PHPUnit <10 data providers with keys are stored as `Class\\test_method with data set "some key"`
*
* we need to translate to the old format because this is what PHPUnit <10 and >=10 understands from CLI `--filter` option
*/
if (version_compare($testFrameworkVersion, '10', '>=')) {
$methodNameParts = explode('#', $methodNameWithDataProviderResult, 2);

if (count($methodNameParts) > 1) {
$methodName = $methodNameParts[0];
$dataProviderKey = $methodNameParts[1];

$methodNameWithDataProviderResult = sprintf('%s with data set "%s"', $methodName, $dataProviderKey);
}
}

return $methodNameWithDataProviderResult;
}
}
Expand Up @@ -98,7 +98,7 @@ public function test_it_can_build_the_command_with_extra_options_that_contains_s
/**
* @dataProvider provideTestCases
*/
public function test_it_can_build_the_command_with_filter_option_for_covering_tests_for_mutant(bool $executeOnlyCoveringTestCases, array $testCases, ?string $expectedFilterOptionValue = null): void
public function test_it_can_build_the_command_with_filter_option_for_covering_tests_for_mutant(bool $executeOnlyCoveringTestCases, array $testCases, string $phpUnitVersion, ?string $expectedFilterOptionValue = null): void
{
$configPath = '/the config/path';

Expand All @@ -123,7 +123,8 @@ public function test_it_can_build_the_command_with_filter_option_for_covering_te
array_map(
static fn (string $testCase): TestLocation => TestLocation::forTestMethod($testCase),
$testCases
)
),
$phpUnitVersion
)
);
}
Expand All @@ -135,13 +136,15 @@ public function provideTestCases(): Generator
[
'App\Test::test_case1',
],
'9.5',
];

yield '1 test case' => [
true,
[
'App\Test::test_case1',
],
'9.5',
'/Test\:\:test_case1/',
];

Expand All @@ -151,6 +154,7 @@ public function provideTestCases(): Generator
'App\Test::test_case1',
'App\Test::test_case2',
],
'9.5',
'/Test\:\:test_case1|Test\:\:test_case2/',
];

Expand All @@ -160,6 +164,7 @@ public function provideTestCases(): Generator
'App\Test::test_case1 with data set "With special character >"',
'App\Test::test_case2',
],
'9.5',
'/Test\:\:test_case1 with data set "With special character \\>"|Test\:\:test_case2/',
];

Expand All @@ -169,6 +174,7 @@ public function provideTestCases(): Generator
'App\Test::test_case1 with data set "With special character @"',
'App\Test::test_case2',
],
'9.5',
'/Test\:\:test_case1 with data set "With special character @"|Test\:\:test_case2/',
];

Expand All @@ -178,6 +184,7 @@ public function provideTestCases(): Generator
'App\Test::test_case1 with data set "#1"',
'App\Test::test_case1 with data set "#2"',
],
'9.5',
'/Test\:\:test_case1 with data set "\#1"|Test\:\:test_case1 with data set "\#2"/',
];

Expand All @@ -186,6 +193,7 @@ public function provideTestCases(): Generator
[
'App\Test::test_case1 with data set "With special char \\"',
],
'9.5',
'/Test\:\:test_case1 with data set "With special char \\\\"/',
];

Expand All @@ -194,6 +202,74 @@ public function provideTestCases(): Generator
[
'App\Test::test_case1 with data set "With special chars ::"',
],
'9.5',
'/Test\:\:test_case1 with data set "With special chars \:\:"/',
];

yield '1 test case - PHPUnit10' => [
true,
[
'App\Test::test_case1',
],
'10.1',
'/Test\:\:test_case1/',
];

yield '2 test cases - PHPUnit10' => [
true,
[
'App\Test::test_case1',
'App\Test::test_case2',
],
'10.1',
'/Test\:\:test_case1|Test\:\:test_case2/',
];

yield '1 simple test case, 1 with data set and special character > - PHPUnit10' => [
true,
[
'App\Test::test_case1#With special character >',
'App\Test::test_case2',
],
'10.1',
'/Test\:\:test_case1 with data set "With special character \\>"|Test\:\:test_case2/',
];

yield '1 simple test case, 1 with data set and special character @ - PHPUnit10' => [
true,
[
'App\Test::test_case1#With special character @',
'App\Test::test_case2',
],
'10.1',
'/Test\:\:test_case1 with data set "With special character @"|Test\:\:test_case2/',
];

yield '2 data sets from data provider for the same test case - PHPUnit10' => [
true,
[
'App\Test::test_case1##1',
'App\Test::test_case1##2',
],
'10.1',
'/Test\:\:test_case1 with data set "\#1"|Test\:\:test_case1 with data set "\#2"/',
];

yield '1 data set from data provider "With special char \\" - PHPUnit10' => [
true,
[
'App\Test::test_case1#With special char \\"',
],
'10.1',
'/Test\:\:test_case1 with data set "With special char \\\\""/',
];

yield '1 data set from data provider "With special chars ::" - PHPUnit10' => [
true,
[
'App\Test::test_case1#With special chars ::',
],
'10.1',
'/Test\:\:test_case1 with data set "With special chars \:\:"/',
];
}
Expand Down

0 comments on commit ebbe59e

Please sign in to comment.