From 119ca3f38cde8256a51e787e1ad50e7fb8d1c266 Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Mon, 5 Jul 2021 23:20:13 +0300 Subject: [PATCH] Add more tests, remove redundant `.*` from regular expression --- .../ArgumentsAndOptionsBuilder.php | 5 +- .../ArgumentsAndOptionsBuilderTest.php | 51 ++++++++++++++++--- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilder.php b/src/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilder.php index dbe8c778a..db4054db7 100644 --- a/src/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilder.php +++ b/src/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilder.php @@ -45,6 +45,7 @@ use Infection\AbstractTestFramework\Coverage\TestLocation; use Infection\TestFramework\CommandLineArgumentsAndOptionsBuilder; use function ltrim; +use function Safe\sprintf; /** * @internal @@ -74,8 +75,6 @@ public function buildForMutant(string $configPath, string $extraOptions, array $ { $options = $this->buildForInitialTestsRun($configPath, $extraOptions); -// preg_replace('/\swith data set (.*)/', '', $test->getMethod()) - if (count($tests) > 0) { $escapedTests = array_map( static fn (TestLocation $testLocation): string => escapeshellcmd($testLocation->getMethod()), @@ -83,7 +82,7 @@ public function buildForMutant(string $configPath, string $extraOptions, array $ ); $options[] = '--filter'; - $options[] = implode('|', array_unique($escapedTests)); + $options[] = sprintf('/%s/', implode('|', array_unique($escapedTests))); } return $options; diff --git a/tests/phpunit/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilderTest.php b/tests/phpunit/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilderTest.php index cfa4b48e9..8bfe9c213 100644 --- a/tests/phpunit/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilderTest.php +++ b/tests/phpunit/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilderTest.php @@ -35,6 +35,8 @@ namespace Infection\Tests\TestFramework\PhpUnit\CommandLine; +use function array_map; +use Generator; use Infection\AbstractTestFramework\Coverage\TestLocation; use Infection\TestFramework\PhpUnit\CommandLine\ArgumentsAndOptionsBuilder; use PHPUnit\Framework\TestCase; @@ -93,7 +95,10 @@ public function test_it_can_build_the_command_with_extra_options_that_contains_s ); } - public function test_it_can_build_the_command_with_filter_option_for_covering_tests_for_mutant(): void + /** + * @dataProvider provideTestCases + */ + public function test_it_can_build_the_command_with_filter_option_for_covering_tests_for_mutant(array $testCases, string $expectedFilterOptionValue): void { $configPath = '/the config/path'; @@ -103,16 +108,50 @@ public function test_it_can_build_the_command_with_filter_option_for_covering_te $configPath, '--path=/a path/with spaces', '--filter', - 'App\\\\Test::test_case1|App\\\\Test::test_case2', + $expectedFilterOptionValue, ], $this->builder->buildForMutant( $configPath, '--path=/a path/with spaces', - [ - TestLocation::forTestMethod('App\Test::test_case1'), - TestLocation::forTestMethod('App\Test::test_case2'), - ] + array_map( + static fn (string $testCase): TestLocation => TestLocation::forTestMethod($testCase), + $testCases + ) ) ); } + + public function provideTestCases(): Generator + { + yield '1 test case' => [ + [ + 'App\Test::test_case1', + ], + '/App\\\\Test::test_case1/', + ]; + + yield '2 test cases' => [ + [ + 'App\Test::test_case1', + 'App\Test::test_case2', + ], + '/App\\\\Test::test_case1|App\\\\Test::test_case2/', + ]; + + yield '2 simple test cases, 1 with data set and special character >' => [ + [ + 'App\Test::test_case1 with data set "With special character >"', + 'App\Test::test_case2', + ], + '/App\\\\Test::test_case1 with data set "With special character \\>"|App\\\\Test::test_case2/', + ]; + + yield '2 simple test cases, 1 with data set and special character @' => [ + [ + 'App\Test::test_case1 with data set "With special character @"', + 'App\Test::test_case2', + ], + '/App\\\\Test::test_case1 with data set "With special character @"|App\\\\Test::test_case2/', + ]; + } }