diff --git a/bin/worker.php b/bin/worker.php index 8a4574dbd..aba8a96df 100644 --- a/bin/worker.php +++ b/bin/worker.php @@ -111,7 +111,10 @@ exit; } - $exitCode = $application->runTest(realpath(trim($testPath))); + [$testFile, $testFilter] = array_pad(explode("\0", trim($testPath), 2), 2, null); + $testPath = realpath($testFile).($testFilter === null ? '' : "\0".$testFilter); + + $exitCode = $application->runTest($testPath); fwrite($statusFile, (string) $exitCode); fflush($statusFile); diff --git a/overrides/Runner/TestSuiteLoader.php b/overrides/Runner/TestSuiteLoader.php index 084474bde..16ec2183d 100644 --- a/overrides/Runner/TestSuiteLoader.php +++ b/overrides/Runner/TestSuiteLoader.php @@ -41,6 +41,11 @@ final class TestSuiteLoader */ private static array $declaredClasses = []; + /** + * @psalm-var array + */ + private static array $classesBySuiteClassFile = []; + public function __construct() { if (empty(self::$declaredClasses)) { @@ -53,6 +58,10 @@ public function __construct() */ public function load(string $suiteClassFile): ReflectionClass { + if (array_key_exists($suiteClassFile, self::$classesBySuiteClassFile)) { + return self::$classesBySuiteClassFile[$suiteClassFile]; + } + $suiteClassName = $this->classNameFromFileName($suiteClassFile); (static function () use ($suiteClassFile) { @@ -151,7 +160,7 @@ public function load(string $suiteClassFile): ReflectionClass // @codeCoverageIgnoreEnd if ($class->isSubclassOf(TestCase::class) && ! $class->isAbstract()) { - return $class; + return self::$classesBySuiteClassFile[$suiteClassFile] = $class; } if ($class->hasMethod('suite')) { @@ -164,7 +173,7 @@ public function load(string $suiteClassFile): ReflectionClass // @codeCoverageIgnoreEnd if (! $method->isAbstract() && $method->isPublic() && $method->isStatic()) { - return $class; + return self::$classesBySuiteClassFile[$suiteClassFile] = $class; } } diff --git a/overrides/TextUI/TestSuiteFilterProcessor.php b/overrides/TextUI/TestSuiteFilterProcessor.php index 9c9b618d7..95f4e53c3 100644 --- a/overrides/TextUI/TestSuiteFilterProcessor.php +++ b/overrides/TextUI/TestSuiteFilterProcessor.php @@ -14,6 +14,7 @@ use Pest\Plugins\Only; use Pest\Runner\Filter\EnsureTestCaseIsInitiatedFilter; +use Pest\TestSuite as PestTestSuite; use PHPUnit\Event; use PHPUnit\Framework\TestSuite; use PHPUnit\Metadata\Api\Groups; @@ -36,6 +37,8 @@ public function __construct(private Event\Emitter $emitter) {} */ public function process(Configuration $configuration, TestSuite $suite): void { + PestTestSuite::getInstance()->filteredTestSuite = $suite; + $factory = new Factory; // @phpstan-ignore-next-line diff --git a/src/Plugins/Parallel/Paratest/WrapperRunner.php b/src/Plugins/Parallel/Paratest/WrapperRunner.php index 2cb01a10b..7237a1830 100644 --- a/src/Plugins/Parallel/Paratest/WrapperRunner.php +++ b/src/Plugins/Parallel/Paratest/WrapperRunner.php @@ -6,6 +6,7 @@ use const DIRECTORY_SEPARATOR; +use Generator; use NunoMaduro\Collision\Adapters\Phpunit\Support\ResultReflection; use ParaTest\JUnit\LogMerger; use ParaTest\JUnit\Writer; @@ -14,13 +15,17 @@ use ParaTest\WrapperRunner\MissingResultsException; use ParaTest\WrapperRunner\SuiteLoader; use ParaTest\WrapperRunner\WrapperWorker; +use Pest\Contracts\HasPrintableTestCaseName; use Pest\Plugins\Tia; use Pest\Result; use Pest\TestSuite; use PHPUnit\Event\Facade as EventFacade; use PHPUnit\Event\Test\AfterLastTestMethodFailed; use PHPUnit\Event\TestRunner\WarningTriggered; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\TestSuite as PHPUnitTestSuite; use PHPUnit\Runner\CodeCoverage; +use PHPUnit\Runner\Phpt\TestCase as PhptTestCase; use PHPUnit\Runner\TestRunHistory\DefaultTestRunHistory; use PHPUnit\TestRunner\TestResult\Facade as TestResultFacade; use PHPUnit\TestRunner\TestResult\TestResult; @@ -41,6 +46,8 @@ use function array_merge; use function array_merge_recursive; use function array_shift; +use function array_unique; +use function array_values; use function assert; use function bin2hex; use function count; @@ -48,11 +55,16 @@ use function file_get_contents; use function filesize; use function is_file; +use function is_string; +use function iterator_to_array; use function max; use function mkdir; +use function preg_quote; +use function property_exists; use function random_bytes; use function realpath; use function rmdir; +use function rtrim; use function sprintf; use function str_starts_with; use function unlink; @@ -638,6 +650,10 @@ private function clearFiles(array $files): void */ private function getTestFiles(SuiteLoader $suiteLoader): array { + if ($this->options->functional) { + return $this->getFunctionalTests($suiteLoader); + } + /** @var array $files */ $files = []; @@ -661,6 +677,58 @@ private function getTestFiles(SuiteLoader $suiteLoader): array return array_keys($files); } + /** + * @return array + */ + private function getFunctionalTests(SuiteLoader $suiteLoader): array + { + $testSuite = TestSuite::getInstance()->filteredTestSuite; + + assert($testSuite instanceof PHPUnitTestSuite); + + $testCases = iterator_to_array($this->testCasesOf($testSuite), false); + + assert(count($testCases) === count($suiteLoader->tests)); + + $tests = []; + + foreach ($testCases as $index => $testCase) { + $tests[] = $testCase instanceof HasPrintableTestCaseName + ? $this->getFunctionalPestTest($testCase) + : $suiteLoader->tests[$index]; + } + + return array_values(array_unique($tests)); + } + + /** + * @return non-empty-string + */ + private function getFunctionalPestTest(HasPrintableTestCaseName&TestCase $testCase): string + { + $className = $testCase::class; + + assert(property_exists($className, '__filename') && is_string($className::$__filename)); + + $name = rtrim($testCase->getPrintableTestCaseMethodName().$testCase->dataSetAsString()); + + return $className::$__filename."\0/^[^:]+::".preg_quote($name, '/').'$/'; + } + + /** + * @return Generator + */ + private function testCasesOf(PHPUnitTestSuite $testSuite): Generator + { + foreach ($testSuite as $test) { + if ($test instanceof PHPUnitTestSuite) { + yield from $this->testCasesOf($test); + } elseif ($test instanceof TestCase || $test instanceof PhptTestCase) { + yield $test; + } + } + } + private function shouldIncludeBootstrappedTestFile(string $filename): bool { if (! $this->options->configuration->hasCliArguments()) { diff --git a/src/Plugins/Shard.php b/src/Plugins/Shard.php index c98376c8c..0bf1773fb 100644 --- a/src/Plugins/Shard.php +++ b/src/Plugins/Shard.php @@ -192,7 +192,7 @@ private function removeParallelArguments(array $arguments): array { return array_values(array_filter( $arguments, - fn (string $argument): bool => ! in_array($argument, ['--parallel', '-p'], strict: true) + fn (string $argument): bool => ! in_array($argument, ['--parallel', '-p', '--functional'], strict: true) && ! str_starts_with($argument, '--processes'), )); } diff --git a/src/TestSuite.php b/src/TestSuite.php index c56a74f65..bfe728f2c 100644 --- a/src/TestSuite.php +++ b/src/TestSuite.php @@ -13,6 +13,7 @@ use Pest\Repositories\TestRepository; use Pest\Support\Str; use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\TestSuite as PHPUnitTestSuite; /** * @internal @@ -33,6 +34,8 @@ final class TestSuite public SnapshotRepository $snapshots; + public ?PHPUnitTestSuite $filteredTestSuite = null; + public string $rootPath; private static ?TestSuite $instance = null; diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index f9116164c..9301d037c 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2259,6 +2259,13 @@ ✓ parallel reports invalid datasets as failures ✓ parallel can have multiple exclude-groups ✓ parallel can have multiple groups + ✓ parallel functional gives each test to a worker on its own + ✓ parallel functional runs the tests of a file whose name gives no class name + ✓ parallel functional runs the tests of its shard + ✓ parallel functional runs the selected cases of tests that share a description across files with dataset "without a filter" + ✓ parallel functional runs the selected cases of tests that share a description across files with dataset "filtered by the description" + ✓ parallel functional runs the selected cases of tests that share a description across files with dataset "filtered by the file" + ✓ parallel functional runs the selected cases of tests that share a description across files with dataset "filtered by the data set" PASS Tests\Visual\ParallelNestedDatasets ✓ parallel loads nested datasets from nested directories @@ -2292,4 +2299,4 @@ ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 36 skipped, 1624 passed (3587 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 36 skipped, 1631 passed (3602 assertions) \ No newline at end of file diff --git a/tests/Fixtures/Suites/ParallelFunctional/SameDescription/FirstTest.php b/tests/Fixtures/Suites/ParallelFunctional/SameDescription/FirstTest.php new file mode 100644 index 000000000..f398c5b00 --- /dev/null +++ b/tests/Fixtures/Suites/ParallelFunctional/SameDescription/FirstTest.php @@ -0,0 +1,5 @@ +toBeInt(); +})->with(['only first' => 1, 'shared' => 2]); diff --git a/tests/Fixtures/Suites/ParallelFunctional/SameDescription/SecondTest.php b/tests/Fixtures/Suites/ParallelFunctional/SameDescription/SecondTest.php new file mode 100644 index 000000000..8b74e48c8 --- /dev/null +++ b/tests/Fixtures/Suites/ParallelFunctional/SameDescription/SecondTest.php @@ -0,0 +1,5 @@ +toBeInt()->toBeGreaterThan(2); +})->with(['only second' => 3, 'shared' => 4]); diff --git a/tests/Fixtures/Suites/ParallelFunctional/Spread/PestTest.php b/tests/Fixtures/Suites/ParallelFunctional/Spread/PestTest.php new file mode 100644 index 000000000..646a5f718 --- /dev/null +++ b/tests/Fixtures/Suites/ParallelFunctional/Spread/PestTest.php @@ -0,0 +1,17 @@ +toBeTrue(); +}); + +it('runs the second test', function () { + expect(true)->toBeTrue(); +}); + +it('runs the third test', function () { + expect(true)->toBeTrue(); +}); + +test('calls Foo::it runs the third test', function () { + expect(true)->toBeTrue(); +}); diff --git a/tests/Fixtures/Suites/ParallelFunctional/Spread/PhpUnitTest.php b/tests/Fixtures/Suites/ParallelFunctional/Spread/PhpUnitTest.php new file mode 100644 index 000000000..7baaba0f9 --- /dev/null +++ b/tests/Fixtures/Suites/ParallelFunctional/Spread/PhpUnitTest.php @@ -0,0 +1,30 @@ +assertTrue(true); + } + + #[DataProvider('values')] + public function test_it_runs_a_data_set(int $value): void + { + $this->assertIsInt($value); + } + + /** + * @return array + */ + public static function values(): array + { + return ['one' => [1], 'two' => [2]]; + } +} diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 572ff80bf..c441a9d28 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -56,3 +56,35 @@ ->toContain('Tests: 2 passed (2 assertions)') ->toContain('Parallel: 3 processes'); })->skipOnWindows(); + +test('parallel functional gives each test to a worker on its own', function () use ($run): void { + $output = $run('tests/Fixtures/Suites/ParallelFunctional/Spread', '--functional', '--verbose'); + + expect(preg_match_all('/executing: .+(PestTest|PhpUnitTest)\.php.+(runs the \w+ test|runs_the_first_test|runs_a_data_set)/', (string) $output))->toBe(7) + ->and($output) + ->toContain('Tests: 7 passed (7 assertions)') + ->toContain('Parallel: 3 processes'); +})->skipOnWindows(); + +test('parallel functional runs the tests of a file whose name gives no class name', function () use ($run): void { + expect($run('tests/PHPUnit/CustomAffixes/@#$%^&()-_=+.php', '--functional')) + ->toContain('Tests: 1 passed (1 assertions)') + ->toContain('Parallel: 3 processes'); +})->skipOnWindows(); + +test('parallel functional runs the tests of its shard', function () use ($run): void { + expect($run('tests/Fixtures/Suites/ParallelFunctional', '--functional', '--shard=1/2')) + ->toContain('Tests: 6 passed (6 assertions)') + ->toContain('Shard: 1 of 2 — 2 files ran, out of 4.'); +})->skipOnWindows(); + +test('parallel functional runs the selected cases of tests that share a description across files', function (array $arguments, string $expected) use ($run): void { + expect($run('tests/Fixtures/Suites/ParallelFunctional/SameDescription', '--functional', ...$arguments)) + ->toContain("Tests: {$expected}") + ->toContain('Parallel: 3 processes'); +})->with([ + 'without a filter' => [[], '4 passed (6 assertions)'], + 'filtered by the description' => [['--filter=shares a description'], '4 passed (6 assertions)'], + 'filtered by the file' => [['--filter=SecondTest'], '2 passed (4 assertions)'], + 'filtered by the data set' => [['--filter=shared'], '2 passed (3 assertions)'], +])->skipOnWindows();