getExitCode())->toBe(0) ->and($process->getErrorOutput())->not->toContain('cannot be resolved') ->and($process->getOutput())->toContain('File created.') ->and($root.DIRECTORY_SEPARATOR.'phpunit.xml')->toBeFile() ->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Pest.php')->toBeFile() ->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'TestCase.php')->toBeFile() ->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Unit'.DIRECTORY_SEPARATOR.'ExampleTest.php')->toBeFile(); $suite = runInitProject($root, '--colors=never'); expect($suite->getExitCode())->toBe(0) ->and($suite->getOutput())->toContain('2 passed (2 assertions)'); } finally { removeInitProject($root); } }); test('init creates a suite when --init is not the first argument', function (): void { $root = makeInitProject(); try { $process = runInitProject($root, '--colors=never', '--init'); expect($process->getExitCode())->toBe(0) ->and($process->getErrorOutput())->not->toContain('cannot be resolved') ->and($process->getOutput())->toContain('File created.') ->and($root.DIRECTORY_SEPARATOR.'phpunit.xml')->toBeFile() ->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Pest.php')->toBeFile(); } finally { removeInitProject($root); } }); test('init creates a suite when --init is passed with a value', function (): void { $root = makeInitProject(); try { $process = runInitProject($root, '--init=pest'); expect($process->getExitCode())->toBe(0) ->and($process->getErrorOutput())->not->toContain('cannot be resolved') ->and($process->getOutput())->toContain('File created.') ->and($root.DIRECTORY_SEPARATOR.'phpunit.xml')->toBeFile() ->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Pest.php')->toBeFile(); } finally { removeInitProject($root); } }); test('a missing tests directory does not crash while resolving tia state', function (): void { $root = makeInitProject(); try { $process = runInitProject($root); expect($process->getExitCode())->not->toBe(0) ->and($process->getOutput())->toContain('does not exist') ->and($process->getErrorOutput())->not->toContain('cannot be resolved') ->and($process->getOutput())->not->toContain('cannot be resolved'); } finally { removeInitProject($root); } }); test('init writes the plain stubs when only a tool bundled in a PHAR installs Laravel', function (): void { $root = makeInitProject(); try { $toolLoader = makeLaravelToolPhar($root); $process = runInitProjectWithPhpOptions(['-d', 'auto_prepend_file='.$toolLoader], $root, '--init'); expect($process->getExitCode())->toBe(0) ->and(file_get_contents($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Pest.php')) ->toContain("require_once __DIR__.'/TestCase.php';") ->not->toContain('RefreshDatabase'); } finally { removeInitProject($root); } })->skipOnWindows(); function makeInitProject(): string { $root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-init-'.bin2hex(random_bytes(4)); mkdir($root.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'bin', 0777, true); copy( dirname(__DIR__, 2).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'pest-plugins.json', $root.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'pest-plugins.json', ); $autoload = var_export( dirname(__DIR__, 2).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php', true, ); file_put_contents( $root.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php', "handle(\$_SERVER['argv'], \$_SERVER['argv']); \$kernel->terminate(); } catch (Throwable \$throwable) { Panic::with(\$throwable); } exit(\$result); PHP); return $root; } function makeLaravelToolPhar(string $root): string { $phar = $root.DIRECTORY_SEPARATOR.'tool.phar'; $build = new Process([ PHP_BINARY, '-d', 'phar.readonly=0', '-r', <<<'PHP' $phar = new Phar($argv[1]); $phar->addFromString('vendor/composer/installed.php', <<<'INSTALLED' ['name' => 'tool/dumper', 'install_path' => __DIR__.'/../../'], 'versions' => ['laravel/framework' => ['pretty_version' => 'v12.0.0', 'version' => '12.0.0.0', 'dev_requirement' => false]], ]; INSTALLED); $phar->setStub('mustRun(); $loader = $root.DIRECTORY_SEPARATOR.'tool-loader.php'; file_put_contents($loader, sprintf( "register();\n", var_export(dirname(__DIR__, 2).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'composer'.DIRECTORY_SEPARATOR.'ClassLoader.php', true), var_export('phar://'.$phar.'/vendor', true), )); return $loader; } function runInitProject(string $root, string ...$command): Process { return runInitProjectWithPhpOptions([], $root, ...$command); } /** * @param array $phpOptions */ function runInitProjectWithPhpOptions(array $phpOptions, string $root, string ...$command): Process { $process = new Process( [PHP_BINARY, ...$phpOptions, 'run.php', ...$command], $root, [ 'PEST_NO_SUPPORT' => 'true', 'COLLISION_PRINTER' => 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true', ], ); $process->setTimeout(60); $process->run(); return $process; } function removeInitProject(string $root): void { if (! is_dir($root)) { return; } $items = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST, ); foreach ($items as $item) { if ($item->isDir() && ! $item->isLink()) { rmdir($item->getPathname()); continue; } unlink($item->getPathname()); } rmdir($root); }