BREAKING: project commands implementing CommandInterface must define static describe(): string (intentional, forces documentation).
133 lines
6.6 KiB
PHP
133 lines
6.6 KiB
PHP
<?php
|
|
|
|
use Toolbox\Commands\AddCommand;
|
|
use Toolbox\Commands\InstallCommand;
|
|
use Toolbox\Console;
|
|
use Toolbox\Lock;
|
|
|
|
Harness::suite('group8');
|
|
|
|
$GLOBALS['g8_repo'] = dirname(__DIR__);
|
|
|
|
Harness::it('el binario global usa el motor empaquetado en commands', function () {
|
|
$repo = $GLOBALS['g8_repo'];
|
|
exec('php ' . escapeshellarg($repo . '/bin/duckbrain') . ' --version 2>&1', $out, $code);
|
|
eq(0, $code);
|
|
ok(str_contains(implode("\n", $out), 'duckbrain-toolbox'), 'el binario global arranca');
|
|
});
|
|
|
|
Harness::it('commands:toolbox es un componente opcional que se copia y hashea', function () {
|
|
$repo = $GLOBALS['g8_repo'];
|
|
$proj = tmpdir('proj');
|
|
|
|
(new AddCommand(new Console()))->run(['commands:toolbox', '--root=' . $proj, '--toolbox=' . $repo, '--yes']);
|
|
|
|
ok(is_file($proj . '/src/Toolbox/Installer.php'), 'motor copiado a ROOT_CORE/Toolbox');
|
|
$lock = Lock::load($proj);
|
|
ok($lock->has('commands:toolbox'), 'lock registra el componente opcional');
|
|
|
|
rrmdir($proj);
|
|
});
|
|
|
|
Harness::it('el CLI del proyecto gestiona addons sin el instalador global', function () {
|
|
$repo = $GLOBALS['g8_repo'];
|
|
$core = g7_core('0.1.0');
|
|
$proj = tmpdir('proj');
|
|
|
|
// Core + CLI + motor (todo vía el instalador global, como bootstrap).
|
|
(new InstallCommand(new Console()))->run([$proj, '--toolbox=' . $repo, '--core-dir=' . $core, '--yes']);
|
|
(new AddCommand(new Console()))->run(['commands:cli', 'commands:toolbox', '--root=' . $proj, '--toolbox=' . $repo, '--yes']);
|
|
|
|
ok(is_file($proj . '/duckbrain'), 'binario del proyecto instalado');
|
|
|
|
$cmd = 'cd ' . escapeshellarg($proj) . ' && php duckbrain --version 2>&1';
|
|
exec($cmd, $out, $code);
|
|
eq(0, $code, 'el binario del proyecto arranca: ' . implode("\n", $out));
|
|
ok(str_contains(implode("\n", $out), 'duckbrain-toolbox'), 'usa el motor del proyecto');
|
|
|
|
rrmdir($proj);
|
|
rrmdir($core);
|
|
});
|
|
|
|
Harness::it('la actualización del motor surte efecto en la siguiente invocación', function () {
|
|
$repo = $GLOBALS['g8_repo'];
|
|
$core = g7_core('0.1.0');
|
|
$proj = tmpdir('proj');
|
|
|
|
(new InstallCommand(new Console()))->run([$proj, '--toolbox=' . $repo, '--core-dir=' . $core, '--yes']);
|
|
(new AddCommand(new Console()))->run(['commands:cli', 'commands:toolbox', '--root=' . $proj, '--toolbox=' . $repo, '--yes']);
|
|
|
|
$before = trim((string) shell_exec('cd ' . escapeshellarg($proj) . ' && php duckbrain --version 2>&1'));
|
|
|
|
// Simula una actualización del motor reescribiendo su archivo en disco.
|
|
$config = (string) file_get_contents($proj . '/src/Toolbox/Config.php');
|
|
$config = preg_replace("/VERSION = '[^']*'/", "VERSION = '0.9.9'", $config);
|
|
write_file($proj . '/src/Toolbox/Config.php', $config);
|
|
|
|
$after = trim((string) shell_exec('cd ' . escapeshellarg($proj) . ' && php duckbrain --version 2>&1'));
|
|
|
|
ok(str_contains($before, \Toolbox\Config::VERSION), 'antes usa la versión vieja');
|
|
ok(str_contains($after, '0.9.9'), 'después usa la versión nueva sin recompilar');
|
|
|
|
rrmdir($proj);
|
|
rrmdir($core);
|
|
});
|
|
|
|
Harness::it('el bin despacha por clase, alias, usage y guard de colisiones', function () {
|
|
$repo = $GLOBALS['g8_repo'];
|
|
$core = g7_core('0.1.0');
|
|
$proj = tmpdir('proj');
|
|
|
|
(new InstallCommand(new Console()))->run([$proj, '--toolbox=' . $repo, '--core-dir=' . $core, '--yes']);
|
|
(new AddCommand(new Console()))->run(['commands:cli', 'commands:toolbox', '--root=' . $proj, '--toolbox=' . $repo, '--yes']);
|
|
|
|
write_file($proj . '/src/Commands/Eco.php', "<?php\n\nnamespace Commands;\n\nclass Eco implements \\Interfaces\\CommandInterface\n{\n public static function describe(): string { return 'Comando eco de prueba'; }\n public function run(): void { echo 'ECO_OK' . PHP_EOL; }\n}\n");
|
|
|
|
$o = []; exec('cd ' . escapeshellarg($proj) . ' && php duckbrain eco 2>&1', $o, $c1);
|
|
eq(0, $c1); ok(str_contains(implode('', $o), 'ECO_OK'), 'dispatch por nombre de clase');
|
|
|
|
$o = []; exec('cd ' . escapeshellarg($proj) . ' && php duckbrain list 2>&1', $o, $c2);
|
|
eq(0, $c2); ok(str_contains(implode('', $o), 'commands:cli'), 'dispatch por alias del motor');
|
|
|
|
$o = []; exec('cd ' . escapeshellarg($proj) . ' && php duckbrain 2>&1', $o, $c3);
|
|
eq(0, $c3); $usage = implode('', $o);
|
|
ok(str_contains($usage, 'eco') && str_contains($usage, 'Comando eco de prueba'), 'usage lista clase y describe');
|
|
ok(str_contains($usage, 'toolbox, add'), 'usage lista aliases del entry del motor');
|
|
|
|
$o = []; exec('cd ' . escapeshellarg($proj) . ' && php duckbrain noexiste 2>&1', $o, $c4);
|
|
eq(1, $c4); ok(str_contains(implode('', $o), 'Comando desconocido'), 'argumento inexistente falla limpio');
|
|
|
|
// alias-vs-alias: 'add' también declarado por Eco
|
|
write_file($proj . '/src/Commands/Eco.php', str_replace(
|
|
" public function run(): void",
|
|
" public static function aliases(): array { return ['add']; }\n public function run(): void",
|
|
(string) file_get_contents($proj . '/src/Commands/Eco.php')
|
|
));
|
|
$o = []; exec('cd ' . escapeshellarg($proj) . ' && php duckbrain list 2>&1', $o, $c5);
|
|
eq(1, $c5); ok(str_contains(implode('', $o), 'Conflicto de alias'), 'guard alias duplicado con ambos dueños');
|
|
|
|
// alias-vs-clase ajena: 'eco' declarado por Toolbox está prohibido; usar un tercero
|
|
write_file($proj . '/src/Commands/Ladro.php', "<?php\n\nnamespace Commands;\n\nclass Ladro implements \\Interfaces\\CommandInterface\n{\n public static function describe(): string { return 'ladrador'; }\n public static function aliases(): array { return ['eco']; }\n public function run(): void {}\n}\n");
|
|
write_file($proj . '/src/Commands/Eco.php', str_replace(" public static function aliases(): array { return ['add']; }\n", '', (string) file_get_contents($proj . '/src/Commands/Eco.php')));
|
|
$o = []; exec('cd ' . escapeshellarg($proj) . ' && php duckbrain 2>&1', $o, $c6);
|
|
eq(1, $c6); ok(str_contains(implode('', $o), 'colisiona con el comando'), 'guard alias que roba nombre de clase');
|
|
|
|
rrmdir($proj);
|
|
rrmdir($core);
|
|
});
|
|
|
|
Harness::it('bin nuevo sin el motor: los aliases del motor son comando desconocido', function () {
|
|
$repo = $GLOBALS['g8_repo'];
|
|
$core = g7_core('0.1.0');
|
|
$proj = tmpdir('proj');
|
|
|
|
(new InstallCommand(new Console()))->run([$proj, '--toolbox=' . $repo, '--core-dir=' . $core, '--yes']);
|
|
(new AddCommand(new Console()))->run(['commands:cli', '--root=' . $proj, '--toolbox=' . $repo, '--yes']);
|
|
|
|
$o = []; exec('cd ' . escapeshellarg($proj) . ' && php duckbrain catalog 2>&1', $o, $c);
|
|
eq(1, $c); ok(str_contains(implode('', $o), 'Comando desconocido'), 'sin Commands\\Toolbox no hay delegación');
|
|
|
|
rrmdir($proj);
|
|
rrmdir($core);
|
|
});
|