diff --git a/src/TestFramework/AbstractTestFrameworkAdapter.php b/src/TestFramework/AbstractTestFrameworkAdapter.php index 1c2e68364..302efcc22 100644 --- a/src/TestFramework/AbstractTestFrameworkAdapter.php +++ b/src/TestFramework/AbstractTestFrameworkAdapter.php @@ -99,7 +99,8 @@ public function getMutantCommandLine( $mutationOriginalFilePath ), $extraOptions, - $coverageTests + $coverageTests, + $this->getVersion() ) ); } diff --git a/src/TestFramework/CommandLineArgumentsAndOptionsBuilder.php b/src/TestFramework/CommandLineArgumentsAndOptionsBuilder.php index 817c5b40f..393231369 100644 --- a/src/TestFramework/CommandLineArgumentsAndOptionsBuilder.php +++ b/src/TestFramework/CommandLineArgumentsAndOptionsBuilder.php @@ -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; } diff --git a/src/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilder.php b/src/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilder.php index 5355bdad9..92aaccae6 100644 --- a/src/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilder.php +++ b/src/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilder.php @@ -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 @@ -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); @@ -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)) { @@ -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; + } } diff --git a/tests/phpunit/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilderTest.php b/tests/phpunit/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilderTest.php index fd56910d9..f0cfcca1b 100644 --- a/tests/phpunit/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilderTest.php +++ b/tests/phpunit/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilderTest.php @@ -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'; @@ -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 ) ); } @@ -135,6 +136,7 @@ public function provideTestCases(): Generator [ 'App\Test::test_case1', ], + '9.5', ]; yield '1 test case' => [ @@ -142,6 +144,7 @@ public function provideTestCases(): Generator [ 'App\Test::test_case1', ], + '9.5', '/Test\:\:test_case1/', ]; @@ -151,6 +154,7 @@ public function provideTestCases(): Generator 'App\Test::test_case1', 'App\Test::test_case2', ], + '9.5', '/Test\:\:test_case1|Test\:\:test_case2/', ]; @@ -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/', ]; @@ -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/', ]; @@ -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"/', ]; @@ -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 \\\\"/', ]; @@ -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 \:\:"/', ]; }