rob-revisited/ide/tests/SymfonyConsoleTest.php

144 lines
11 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Ide;
use JumPHP\Ide\Symfony\ConsoleCommands;
use JumPHP\Ide\Workspace\LocalWorkspace;
use PHPUnit\Framework\TestCase;
/** The Console window: `bin/console list --format=json` into groups and forms, and the command lines it makes. */
final class SymfonyConsoleTest extends TestCase
{
/** @return array<string, mixed> what Symfony's JsonDescriptor prints (cut down) */
private static function listJson(): array
{
$global = [
'help' => ['name' => '--help', 'shortcut' => '-h', 'accept_value' => false, 'is_value_required' => false, 'is_multiple' => false, 'description' => 'Display help for the given command.', 'default' => false],
'no-interaction' => ['name' => '--no-interaction', 'shortcut' => '-n', 'accept_value' => false, 'is_value_required' => false, 'is_multiple' => false, 'description' => 'Do not ask any interactive question', 'default' => false],
'env' => ['name' => '--env', 'shortcut' => '-e', 'accept_value' => true, 'is_value_required' => true, 'is_multiple' => false, 'description' => 'The Environment name.', 'default' => 'dev'],
];
return [
'application' => ['name' => 'Symfony', 'version' => '7.3.0 (env: dev, debug: true)'],
'commands' => [
['name' => 'list', 'hidden' => false, 'usage' => ['list [--raw] [--format FORMAT] [--] [<namespace>]'], 'description' => 'List commands', 'help' => '', 'definition' => [
'arguments' => ['namespace' => ['name' => 'namespace', 'is_required' => false, 'is_array' => false, 'description' => 'The namespace name', 'default' => null]],
'options' => $global + ['raw' => ['name' => '--raw', 'shortcut' => '', 'accept_value' => false, 'is_value_required' => false, 'is_multiple' => false, 'description' => 'To output raw command list', 'default' => false]],
]],
['name' => 'cache:clear', 'hidden' => false, 'usage' => ['cache:clear [--no-warmup] [--no-optional-warmers]'], 'description' => 'Clear the cache', 'help' => 'The <info>%command.name%</info> command clears the cache', 'definition' => [
'arguments' => [], 'options' => $global + ['no-warmup' => ['name' => '--no-warmup', 'shortcut' => '', 'accept_value' => false, 'is_value_required' => false, 'is_multiple' => false, 'description' => 'Do not warm up the cache', 'default' => false]],
]],
['name' => 'make:entity', 'hidden' => false, 'aliases' => [], 'usage' => [], 'description' => 'Create or update a Doctrine entity class', 'help' => '', 'definition' => [
'arguments' => ['name' => ['name' => 'name', 'is_required' => false, 'is_array' => false, 'description' => 'Class name of the entity', 'default' => null]],
'options' => $global + [
'regenerate' => ['name' => '--regenerate', 'shortcut' => '', 'accept_value' => false, 'is_value_required' => false, 'is_multiple' => false, 'description' => 'Regenerate', 'default' => false],
'api-resource' => ['name' => '--api-resource', 'shortcut' => '-a', 'accept_value' => false, 'is_value_required' => false, 'is_multiple' => false, 'description' => 'Mark as API Platform resource', 'default' => false],
],
]],
['name' => 'doctrine:migrations:migrate', 'hidden' => false, 'usage' => [], 'description' => 'Execute a migration', 'help' => '', 'definition' => [
'arguments' => ['version' => ['name' => 'version', 'is_required' => false, 'is_array' => false, 'description' => 'The version', 'default' => 'latest']],
'options' => $global + [
'configuration' => ['name' => '--configuration', 'shortcut' => '', 'accept_value' => true, 'is_value_required' => true, 'is_multiple' => false, 'description' => 'The path', 'default' => null],
'filter-expression' => ['name' => '--filter-expression', 'shortcut' => '', 'accept_value' => true, 'is_value_required' => true, 'is_multiple' => true, 'description' => 'Tables', 'default' => []],
],
]],
['name' => 'messenger:consume', 'hidden' => false, 'usage' => [], 'description' => 'Consume messages', 'help' => '', 'definition' => [
'arguments' => ['receivers' => ['name' => 'receivers', 'is_required' => true, 'is_array' => true, 'description' => 'Names of the receivers', 'default' => []]],
'options' => $global,
]],
['name' => '_complete', 'hidden' => true, 'description' => 'Internal', 'definition' => ['arguments' => [], 'options' => []]],
],
'namespaces' => [],
];
}
public function testTheCommandsAreGroupedByNamespaceWithoutTheHiddenOnes(): void
{
$list = ConsoleCommands::parse(self::listJson());
self::assertSame('Symfony', $list['application']['name']);
self::assertSame(5, $list['count']);
self::assertSame(['_global', 'cache', 'doctrine', 'make', 'messenger'], array_column($list['groups'], 'id'));
self::assertSame('doctrine:migrations:migrate', $list['groups'][2]['commands'][0]['name']);
self::assertSame(['help', 'no-interaction', 'env'], array_column($list['globals'], 'name'));
}
public function testACommandBecomesAFormDefinition(): void
{
$list = ConsoleCommands::parse(self::listJson());
$cache = $list['groups'][1]['commands'][0];
// its own options only, without the global ones; the help without Symfony's tags
self::assertSame(['no-warmup'], array_column($cache['options'], 'name'));
self::assertSame('The %command.name% command clears the cache', $cache['help']);
$consume = $list['groups'][4]['commands'][0];
self::assertTrue($consume['arguments'][0]['required']);
self::assertTrue($consume['arguments'][0]['array']);
$migrate = $list['groups'][2]['commands'][0];
self::assertSame('latest', $migrate['arguments'][0]['default']);
[$conf, $filter] = $migrate['options'];
self::assertTrue($conf['value'] && $conf['valueRequired'] && !$conf['multiple']);
self::assertTrue($filter['multiple']);
}
public function testTheFormMakesACommandLineWithQuoting(): void
{
$list = ConsoleCommands::parse(self::listJson());
$entity = $list['groups'][3]['commands'][0];
self::assertSame(['make:entity', '--regenerate', 'Product'], ConsoleCommands::argv($entity, ['arguments' => ['name' => 'Product'], 'options' => ['regenerate' => true, 'api-resource' => false]]));
$migrate = $list['groups'][2]['commands'][0];
$argv = ConsoleCommands::argv($migrate, ['arguments' => ['version' => ''], 'options' => ['configuration' => "my conf's.php", 'filter-expression' => 'users posts', 'no-interaction' => true]]);
self::assertSame(['doctrine:migrations:migrate', "--configuration=my conf's.php", '--filter-expression=users', '--filter-expression=posts', '--no-interaction'], $argv);
self::assertSame("doctrine:migrations:migrate '--configuration=my conf'\\''s.php' --filter-expression=users --filter-expression=posts --no-interaction", ConsoleCommands::line($argv));
$consume = $list['groups'][4]['commands'][0];
self::assertSame('messenger:consume async failed', ConsoleCommands::line(ConsoleCommands::argv($consume, ['arguments' => ['receivers' => 'async failed']])));
// an argument that starts with "-" goes after --
self::assertSame(['make:entity', '--', '-x'], ConsoleCommands::argv($entity, ['arguments' => ['name' => '-x']]));
self::assertSame("''", ConsoleCommands::quote(''));
}
public function testDockerRunsThroughTheSitesWebConsoleSoThePhpIsTheProjects(): void
{
$toolkit = static fn (array $c): array => ['sh', '-c', 'exec ' . implode(' ', array_map(fn ($a) => $a === '@jumphp' ? '"$tk/bin/jumphp"' : escapeshellarg($a), $c))];
$docker = ['runner' => 'docker', 'tool' => 'bin/console'];
self::assertSame(['sh', '-c', 'exec "$tk/bin/jumphp" \'web:console\' \'/home/me/shop\' \'list\' \'--format=json\''],
ConsoleCommands::runCommand('/home/me/shop', $docker, ['list', '--format=json'], null, $toolkit));
// a typed line goes after it, as the shell reads it
self::assertSame('exec "$tk/bin/jumphp" \'web:console\' \'/home/me/shop\' make:entity Foo --no-interaction',
ConsoleCommands::runCommand('/home/me/shop', $docker, [], 'make:entity Foo --no-interaction', $toolkit)[2]);
self::assertSame("/home/me/.jumphp/toolkit/bin/jumphp web:console /home/me/shop make:entity 'My Thing'",
ConsoleCommands::terminalCommand('/home/me/shop', $docker, "make:entity 'My Thing'", '/home/me/.jumphp/toolkit/'));
}
public function testLocalPhpRunsTheConsoleWithTheComputersPhp(): void
{
$local = ['runner' => 'local', 'binary' => '/opt/php 8.5/bin/php', 'tool' => 'artisan'];
$none = static fn (array $c): array => throw new \LogicException('no toolkit for the local runner');
self::assertSame(['/opt/php 8.5/bin/php', 'artisan', 'list', '--format=json'], ConsoleCommands::runCommand('/srv/app', $local, ['list', '--format=json'], null, $none));
self::assertSame(['sh', '-c', "'/opt/php 8.5/bin/php' artisan migrate --pretend"], ConsoleCommands::runCommand('/srv/app', $local, [], 'migrate --pretend', $none));
self::assertSame("cd /srv/app && '/opt/php 8.5/bin/php' artisan migrate", ConsoleCommands::terminalCommand('/srv/app', $local, 'migrate', '/tk'));
}
public function testAPlatformCheckErrorSaysWhichPhpTheDependenciesNeed(): void
{
$e = ConsoleCommands::error("bin/console list did not answer: Composer detected issues in your platform:\n\nYour Composer dependencies require a PHP version \">= 8.5.12\". You are running 8.4.27.");
self::assertSame(['need' => '>= 8.5.12', 'version' => '8.5', 'running' => '8.4.27'], $e['platform']);
self::assertArrayNotHasKey('platform', ConsoleCommands::error('Could not open input file: bin/console'));
}
public function testTheIdeOffersTheConsoleWindowWhenTheProjectHasBinConsole(): void
{
$dir = sys_get_temp_dir() . '/jumphp-sfc-' . bin2hex(random_bytes(4));
mkdir("$dir/bin", 0777, true);
file_put_contents("$dir/composer.json", '{"require": {"symfony/console": "^7"}}');
try {
self::assertNull(ConsoleCommands::tool($dir));
file_put_contents("$dir/bin/console", "#!/usr/bin/env php\n<?php\n");
self::assertSame('bin/console', ConsoleCommands::tool($dir));
$ide = new Ide(new LocalWorkspace($dir));
self::assertSame('bin/console', $ide->handle('project.info', [])['capabilities']['console']);
self::assertSame('php bin/console cache:clear --no-warmup', $ide->handle('console.preview', ['argv' => ['cache:clear', '--no-warmup']])['line']);
self::assertSame('php bin/console make:entity Foo', $ide->handle('console.preview', ['line' => 'make:entity Foo'])['line']);
} finally {
exec('rm -rf ' . escapeshellarg($dir));
}
}
}