diff --git a/bin/pest b/bin/pest index ef9b1a84..ba6d9810 100755 --- a/bin/pest +++ b/bin/pest @@ -6,6 +6,7 @@ declare(strict_types=1); use Pest\Contracts\Restarter; use Pest\Kernel; use Pest\Panic; +use Pest\Support\ComposerProject; use Pest\Support\Container; use Pest\TestCaseFilters\GitDirtyTestCaseFilter; use Pest\TestCaseMethodFilters\AssigneeTestCaseFilter; @@ -129,21 +130,16 @@ use Symfony\Component\Console\Output\ConsoleOutput; } // Used when Pest is required using composer. - $vendorPath = dirname(__DIR__, 4).'/vendor/autoload.php'; + $autoloadPath = $GLOBALS['_composer_autoload_path'] ?? dirname(__DIR__, 3).'/autoload.php'; - // Used when Pest maintainers are running Pest tests. - $localPath = dirname(__DIR__).'/vendor/autoload.php'; - - if (file_exists($vendorPath)) { - include_once $vendorPath; - $autoloadPath = $vendorPath; - } else { - include_once $localPath; - $autoloadPath = $localPath; + if (! is_file($autoloadPath)) { + $autoloadPath = dirname(__DIR__).'/vendor/autoload.php'; } - // Get $rootPath based on $autoloadPath - $rootPath = dirname($autoloadPath, 2); + include_once $autoloadPath; + + $GLOBALS['_composer_bin_dir'] ??= dirname($autoloadPath).'/bin'; + $rootPath = ComposerProject::rootPath($autoloadPath); $input = new ArgvInput; diff --git a/bin/worker.php b/bin/worker.php index c4bce856..cef6f9c1 100644 --- a/bin/worker.php +++ b/bin/worker.php @@ -7,6 +7,7 @@ use ParaTest\WrapperRunner\WrapperWorker; use Pest\Kernel; use Pest\Plugins\Actions\CallsHandleArguments; use Pest\Plugins\Parallel\Paratest\StopConditions; +use Pest\Support\ComposerProject; use Pest\Support\Container; use Pest\TestSuite; use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry; @@ -14,10 +15,9 @@ use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\OutputInterface; -$bootPest = (static function (): void { +$bootPest = (static function (string $rootPath): void { $workerArgv = new ArgvInput; - $rootPath = dirname(PHPUNIT_COMPOSER_INSTALL, 2); $testSuite = TestSuite::getInstance($rootPath, $workerArgv->getParameterOption( '--test-directory', 'tests' @@ -56,13 +56,14 @@ $bootPest = (static function (): void { if (file_exists($file)) { require_once $file; define('PHPUNIT_COMPOSER_INSTALL', $file); + $GLOBALS['_composer_bin_dir'] ??= dirname($file).'/bin'; break; } } $container = Container::getInstance(); - $rootPath = dirname(PHPUNIT_COMPOSER_INSTALL, 2); + $rootPath = ComposerProject::rootPath(PHPUNIT_COMPOSER_INSTALL); foreach (Kernel::RESTARTERS as $restarterClass) { $restarter = $container->get($restarterClass); @@ -85,7 +86,7 @@ $bootPest = (static function (): void { $phpunitArgv = unserialize($getopt['phpunit-argv'], ['allowed_classes' => false]); assert(is_array($phpunitArgv)); - $bootPest(); + $bootPest($rootPath); $phpunitArgv = CallsHandleArguments::execute($phpunitArgv); diff --git a/src/Support/ComposerProject.php b/src/Support/ComposerProject.php new file mode 100644 index 00000000..598def43 --- /dev/null +++ b/src/Support/ComposerProject.php @@ -0,0 +1,28 @@ +path('build')); + rename($project->path('vendor'), $project->path($vendor)); + + $project->write($vendor.'/composer/installed.php', <<<'METADATA' + ['install_path' => __DIR__.'/../../..']]; + METADATA); + + $project->write($vendor.'/bin/pest', <<<'PROXY' + write($vendor.'/pestphp/pest/.temp/.gitkeep', ''); + $project->write('phpunit.xml', <<<'CONFIGURATION' + + + CONFIGURATION); + + $test = 'tests/NestedVendorTest.php'; + $project->write($test, <<<'FIXTURE' + rootPath)->toBe(realpath(__DIR__.'/..')); + + if (($_SERVER['PARATEST'] ?? null) === '1') { + expect($_SERVER['PEST_PARALLEL'] ?? null)->toBe('1'); + } + }); + FIXTURE); + + $process = new Process( + [PHP_BINARY, $project->path($vendor.'/'.$binary), '--configuration='.$project->path('phpunit.xml'), '--colors=never', $project->path($test), ...$arguments], + dirname($project->path), + ['PARATEST' => '0', 'PAO_DISABLE' => '1'], + ); + + expect($process->run())->toBe(0) + ->and($process->getOutput())->toContain("1 passed ($assertions assertions)"); + } finally { + Project::destroyAll(); + } +})->with([ + 'Composer proxy' => ['bin/pest', [], 1], + 'Composer proxy with workers' => ['bin/pest', ['--parallel', '--processes=2'], 2], + 'direct execution with workers' => ['pestphp/pest/bin/pest', ['--parallel', '--processes=2'], 2], +]);