Files
duckbrain-toolbox/tests/group8_project_cli_test.php
T

210 lines
10 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);
});
Harness::it('list muestra la descripción del lock por componente, con — si falta', function () {
$root = tmpdir('proj');
$tool = tmpdir('tool');
write_file($tool . '/packages/mix/duckbrain.json', json_encode([
'name' => 'mix',
'version' => '1.0.0',
'description' => 'Descripción del paquete',
'components' => [
'own' => ['files' => ['src/Libs/Own.php'], 'description' => 'Descripción propia'],
'inh' => ['files' => ['src/Libs/Inh.php']],
'old' => ['files' => ['src/Libs/Old.php']],
],
'default' => ['own', 'inh', 'old'],
]));
foreach (['Own', 'Inh', 'Old'] as $c) {
write_file($tool . "/packages/mix/src/Libs/{$c}.php", "<?php\n");
}
(new Toolbox\Installer($root, $tool, new Console()))->add(['mix'], ['yes' => true]);
// simular lock viejo: quitar la descripción de `old`
$raw = json_decode((string) file_get_contents($root . '/.duckbrain/lock.json'), true);
unset($raw['components']['mix:old']['description']);
file_put_contents($root . '/.duckbrain/lock.json', json_encode($raw));
$out = fopen('php://memory', 'rw');
$code = (new Toolbox\Commands\ListCommand(new Console($out)))->run(['--root=' . $root, '--toolbox=' . $tool]);
rewind($out);
$text = (string) stream_get_contents($out);
eq(0, $code);
ok((bool) preg_match('/mix:own.*Descripción propia/', $text), 'fila propia con su descripción');
ok((bool) preg_match('/mix:inh.*Descripción del paquete/', $text), 'fila heredada con la del paquete');
ok((bool) preg_match('/mix:old.*—/', $text), 'lock viejo sin campo muestra —');
rrmdir($root);
rrmdir($tool);
});
Harness::it('commands lista la superficie con origen y reporta 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', 'commands:toolbox', '--root=' . $proj, '--toolbox=' . $repo, '--yes']);
$eco = "<?php\n\nnamespace Commands;\n\nclass Eco implements \\Interfaces\\CommandInterface\n{\n public static function describe(): string { return 'eco local'; }\n public static function aliases(): array { return ['hola']; }\n public function run(): void {}\n}\n";
write_file($proj . '/src/Commands/Eco.php', $eco);
// sin colisión: funciona desde el bin del proyecto
$o = []; exec('cd ' . escapeshellarg($proj) . ' && php duckbrain commands 2>&1', $o, $code);
eq(0, $code, 'bin del proyecto sirve commands');
$text = implode("\n", $o);
ok(str_contains($text, 'eco') && str_contains($text, 'hola'), 'comando propio con su alias');
ok(str_contains($text, 'proyecto'), 'origen proyecto');
ok(str_contains($text, 'commands:migration@'), 'origen componente@versión desde el lock');
// con colisión (roba el nombre de clase migration): el motor REPORTA, el bin aborta
write_file($proj . '/src/Commands/Eco.php', str_replace("['hola']", "['migration']", $eco));
$out = fopen('php://memory', 'rw');
$code = (new Toolbox\Commands\CommandsCommand(new Console($out, $out)))->run(['--root=' . $proj]);
rewind($out);
$text = (string) stream_get_contents($out);
eq(0, $code, 'la vista no aborta ante colisiones');
ok(str_contains($text, 'migration!'), 'fila marcada con !');
ok(str_contains($text, "WARN: alias 'migration'"), 'warn con los dueños');
$o = []; exec('cd ' . escapeshellarg($proj) . ' && php duckbrain list 2>&1', $o, $c2);
eq(1, $c2, 'el despachador sí aborta ante colisión (guard del bin)');
rrmdir($proj);
rrmdir($core);
});