Files
pest/tests/Features/Init.php
T
2026-11-26 19:34:03 +05:30

240 lines
7.4 KiB
PHP

<?php
declare(strict_types=1);
use Symfony\Component\Process\Process;
test('init creates a runnable suite when the tests directory is missing', function (): void {
$root = makeInitProject();
try {
$process = runInitProject($root, '--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()
->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',
"<?php\n\nreturn require_once {$autoload};\n",
);
file_put_contents($root.DIRECTORY_SEPARATOR.'run.php', <<<PHP
<?php
declare(strict_types=1);
use Pest\Kernel;
use Pest\Panic;
use Pest\TestSuite;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
require {$autoload};
\$arguments = array_slice(\$argv, 1);
\$GLOBALS['_composer_bin_dir'] = getcwd().'/vendor/bin';
\$_SERVER['argv'] = ['pest', ...\$arguments];
\$_SERVER['COLLISION_PRINTER'] = 'DefaultPrinter';
\$input = new ArgvInput(\$_SERVER['argv']);
\$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, false);
\$testSuite = TestSuite::getInstance(getcwd(), 'tests');
try {
\$kernel = Kernel::boot(\$testSuite, \$input, \$output);
\$result = \$kernel->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'
<?php
return [
'root' => ['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('<?php __HALT_COMPILER();');
PHP,
$phar,
]);
$build->mustRun();
$loader = $root.DIRECTORY_SEPARATOR.'tool-loader.php';
file_put_contents($loader, sprintf(
"<?php\n\nrequire_once %s;\n\n(new Composer\\Autoload\\ClassLoader(%s))->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<int, string> $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);
}