|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +|-------------------------------------------------------------------------- |
| 5 | +| Leaf CLI test helpers |
| 6 | +|-------------------------------------------------------------------------- |
| 7 | +| |
| 8 | +| Tests run inside a throwaway sandbox with a fake `composer` on PATH, |
| 9 | +| so commands build their real shell strings without ever touching the |
| 10 | +| network or a real project. |
| 11 | +| |
| 12 | +*/ |
| 13 | + |
| 14 | +function sandboxSetup(): void |
| 15 | +{ |
| 16 | + $GLOBALS['__cliTestCwd'] = getcwd(); |
| 17 | + |
| 18 | + $base = PHP_OS_FAMILY === 'Windows' ? sys_get_temp_dir() : '/tmp'; |
| 19 | + $sandbox = $base . '/leaf-cli-test-' . substr(str_shuffle('abcdefghijklmnopqrstuvwxyz'), 0, 10); |
| 20 | + |
| 21 | + mkdir("$sandbox/bin", 0777, true); |
| 22 | + file_put_contents("$sandbox/bin/composer", "#!/bin/bash\necho \"COMPOSER CALLED WITH: \$@\"\n"); |
| 23 | + chmod("$sandbox/bin/composer", 0755); |
| 24 | + chdir($sandbox); |
| 25 | + |
| 26 | + $GLOBALS['__cliSandbox'] = $sandbox; |
| 27 | +} |
| 28 | + |
| 29 | +function sandboxTeardown(): void |
| 30 | +{ |
| 31 | + chdir($GLOBALS['__cliTestCwd']); |
| 32 | + removeDirRecursive($GLOBALS['__cliSandbox']); |
| 33 | +} |
| 34 | + |
| 35 | +function removeDirRecursive(string $dir): void |
| 36 | +{ |
| 37 | + if (!is_dir($dir)) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + foreach (scandir($dir) as $item) { |
| 42 | + if ($item === '.' || $item === '..') { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + $path = "$dir/$item"; |
| 47 | + is_dir($path) && !is_link($path) ? removeDirRecursive($path) : unlink($path); |
| 48 | + } |
| 49 | + |
| 50 | + rmdir($dir); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Run the real leaf binary inside the current sandbox, with the fake |
| 55 | + * composer first on PATH. |
| 56 | + * @return array{0: int, 1: string} exit code and combined output |
| 57 | + */ |
| 58 | +function leaf(string $args): array |
| 59 | +{ |
| 60 | + $bin = dirname(__DIR__) . '/bin/leaf'; |
| 61 | + $path = $GLOBALS['__cliSandbox'] . '/bin:' . getenv('PATH'); |
| 62 | + |
| 63 | + exec('PATH=' . escapeshellarg($path) . ' ' . escapeshellarg(PHP_BINARY) . ' ' . escapeshellarg($bin) . " $args 2>&1", $output, $exit); |
| 64 | + |
| 65 | + return [$exit, implode("\n", $output)]; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Write a minimal fixture project into the sandbox for `leaf context` |
| 70 | + * to scan: routes with every registration shape the parser claims to |
| 71 | + * understand, a model, a schema file, and env keys. |
| 72 | + */ |
| 73 | +function fixtureProject(): string |
| 74 | +{ |
| 75 | + $root = getcwd(); |
| 76 | + |
| 77 | + mkdir("$root/app/routes", 0777, true); |
| 78 | + mkdir("$root/app/models", 0777, true); |
| 79 | + mkdir("$root/app/database", 0777, true); |
| 80 | + |
| 81 | + file_put_contents("$root/composer.json", json_encode([ |
| 82 | + 'name' => 'fixture/app', |
| 83 | + 'require' => ['leafs/leaf' => '^5.0', 'leafs/db' => '^5.0'], |
| 84 | + ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
| 85 | + |
| 86 | + file_put_contents("$root/app/routes/index.php", <<<'PHP' |
| 87 | +<?php |
| 88 | +
|
| 89 | +app()->get('/', function () { |
| 90 | + response()->json(['ok' => true]); |
| 91 | +}); |
| 92 | +
|
| 93 | +app()->get('/users/{id}', 'UsersController@show'); |
| 94 | +
|
| 95 | +app()->post('/users', ['middleware' => 'auth.required', 'UsersController@store']); |
| 96 | +
|
| 97 | +app()->group('/admin', function () { |
| 98 | + app()->get('/dashboard', 'AdminController@index'); |
| 99 | +}); |
| 100 | +
|
| 101 | +app()->resource('/posts', 'PostsController'); |
| 102 | +PHP); |
| 103 | + |
| 104 | + file_put_contents("$root/app/models/User.php", "<?php\n\nclass User extends Model\n{\n}\n"); |
| 105 | + file_put_contents("$root/app/database/users.yml", "users:\n id: id\n name: string\n"); |
| 106 | + file_put_contents("$root/.env.example", "APP_KEY=\nDB_HOST=localhost\nDB_PASSWORD=\n"); |
| 107 | + file_put_contents("$root/.env", "APP_KEY=secret-value-should-never-print\nDB_HOST=localhost\nDB_PASSWORD=supersecret\n"); |
| 108 | + |
| 109 | + return $root; |
| 110 | +} |
0 commit comments