diff --git a/bin/pest b/bin/pest index 088755ff..4d8f344f 100755 --- a/bin/pest +++ b/bin/pest @@ -192,6 +192,7 @@ use Symfony\Component\Console\Output\ConsoleOutput; include_once $autoloadPath; + $GLOBALS['_composer_autoload_path'] = $autoloadPath; $GLOBALS['_composer_bin_dir'] ??= dirname($autoloadPath).'/bin'; $rootPath = ComposerProject::rootPath($autoloadPath); diff --git a/bin/worker.php b/bin/worker.php index cef6f9c1..e72fea9f 100644 --- a/bin/worker.php +++ b/bin/worker.php @@ -43,9 +43,11 @@ $bootPest = (static function (string $rootPath): void { 'testdox-columns:', 'testdox-summary', 'phpunit-argv:', + 'composer-autoload:', ]); $composerAutoloadFiles = [ + ...(isset($getopt['composer-autoload']) && is_string($getopt['composer-autoload']) ? [$getopt['composer-autoload']] : []), dirname(__DIR__, 3).DIRECTORY_SEPARATOR.'autoload.php', dirname(__DIR__, 2).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php', dirname(__DIR__).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php', diff --git a/src/Plugins/Parallel/Paratest/WrapperRunner.php b/src/Plugins/Parallel/Paratest/WrapperRunner.php index 38109afb..289d8bbf 100644 --- a/src/Plugins/Parallel/Paratest/WrapperRunner.php +++ b/src/Plugins/Parallel/Paratest/WrapperRunner.php @@ -156,6 +156,10 @@ final class WrapperRunner implements RunnerInterface $parameters[] = $wrapper; $parameters[] = '--test-directory='.TestSuite::getInstance()->testPath; + if (is_string($GLOBALS['_composer_autoload_path'] ?? null)) { + $parameters[] = '--composer-autoload='.$GLOBALS['_composer_autoload_path']; + } + $this->parameters = $parameters; $this->codeCoverageFilterRegistry = new CodeCoverageFilterRegistry; } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 28134166..b15e01b5 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2646,6 +2646,9 @@ ✓ parallel counts a shared issue only once across recycled workers with ('notice', 'shares notice', 'notice') ✓ parallel combines stop-on counts from different workers and cleans up their files + PASS Tests\Visual\ParallelWorkerAutoload + ✓ loads the autoloader of the parent process in a parallel worker + PASS Tests\Visual\Shard ✓ an empty shard runs no tests with dataset "sequential" ✓ an empty shard runs no tests with dataset "parallel" @@ -2690,4 +2693,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, 39 skipped, 1977 passed (4474 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1978 passed (4476 assertions) \ No newline at end of file diff --git a/tests/Fixtures/Tia/Project.php b/tests/Fixtures/Tia/Project.php index ba65d6e6..f17a3203 100644 --- a/tests/Fixtures/Tia/Project.php +++ b/tests/Fixtures/Tia/Project.php @@ -423,29 +423,12 @@ final class Project public function scaffoldVendor(string $directory): void { - $pestRoot = dirname(__DIR__, 3); - $pest = $directory.'/vendor/pestphp/pest'; + $this->scaffoldPest($directory.'/vendor/pestphp/pest', $directory.'/vendor', '/pestphp/pest/src'); + } - foreach (['src', 'overrides', 'resources', 'stubs'] as $tree) { - self::mirror($pestRoot.'/'.$tree, $pest.'/'.$tree); - } - - foreach (['pest', 'worker.php'] as $binary) { - self::mirror($pestRoot.'/bin/'.$binary, $pest.'/bin/'.$binary); - } - - self::mirror($pestRoot.'/composer.json', $pest.'/composer.json'); - - file_put_contents($directory.'/vendor/autoload.php', sprintf( - "addPsr4('Pest\\\\', __DIR__.'/pestphp/pest/src', true);\n\nreturn \$loader;\n", - var_export($pestRoot.'/vendor/autoload.php', true), - )); - - if (! is_dir($directory.'/vendor/bin') && ! @mkdir($directory.'/vendor/bin', 0755, true)) { - throw new RuntimeException(sprintf('Unable to create [%s].', $directory.'/vendor/bin')); - } - - self::mirror($pestRoot.'/vendor/pest-plugins.json', $directory.'/vendor/pest-plugins.json'); + public function scaffoldCheckout(string $directory): void + { + $this->scaffoldPest($directory, $directory.'/vendor', '/../src'); } public function destroy(): void @@ -512,6 +495,33 @@ final class Project return $project; } + private function scaffoldPest(string $pest, string $vendor, string $sourceFromVendor): void + { + $pestRoot = dirname(__DIR__, 3); + + foreach (['src', 'overrides', 'resources', 'stubs'] as $tree) { + self::mirror($pestRoot.'/'.$tree, $pest.'/'.$tree); + } + + foreach (['pest', 'worker.php'] as $binary) { + self::mirror($pestRoot.'/bin/'.$binary, $pest.'/bin/'.$binary); + } + + self::mirror($pestRoot.'/composer.json', $pest.'/composer.json'); + + if (! is_dir($vendor.'/bin') && ! @mkdir($vendor.'/bin', 0755, true)) { + throw new RuntimeException(sprintf('Unable to create [%s].', $vendor.'/bin')); + } + + file_put_contents($vendor.'/autoload.php', sprintf( + "addPsr4('Pest\\\\', __DIR__.%s, true);\n\nreturn \$loader;\n", + var_export($pestRoot.'/vendor/autoload.php', true), + var_export($sourceFromVendor, true), + )); + + self::mirror($pestRoot.'/vendor/pest-plugins.json', $vendor.'/pest-plugins.json'); + } + private static function mirror(string $from, string $to): void { if (is_dir($from)) { diff --git a/tests/Visual/ParallelWorkerAutoload.php b/tests/Visual/ParallelWorkerAutoload.php new file mode 100644 index 00000000..d86cc494 --- /dev/null +++ b/tests/Visual/ParallelWorkerAutoload.php @@ -0,0 +1,49 @@ +path('checkout'); + $project->scaffoldCheckout($checkout); + + $project->write('vendor/autoload.php', <<<'DECOY' + write('checkout/phpunit.xml', <<<'CONFIGURATION' + + + CONFIGURATION); + + $test = 'checkout/tests/WorkerAutoloadTest.php'; + $project->write($test, <<<'FIXTURE' + toBe('1') + ->and(realpath((new ReflectionClass(Pest\TestSuite::class))->getFileName()))->toStartWith(dirname(__DIR__).DIRECTORY_SEPARATOR.'src'); + }); + FIXTURE); + + $process = new Process( + [PHP_BINARY, $checkout.'/bin/pest', '--configuration='.$project->path('checkout/phpunit.xml'), '--colors=never', $project->path($test), '--parallel', '--processes=2'], + $checkout, + ['PARATEST' => '0', 'PAO_DISABLE' => '1'], + ); + + expect($process->run())->toBe(0, $process->getOutput().$process->getErrorOutput()) + ->and($process->getOutput())->toContain('1 passed (2 assertions)'); + } finally { + Project::destroyAll(); + } +});