Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] add suggestions for debug commands: firewall, form, messenger, router #43598

Merged
merged 1 commit into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -131,4 +133,18 @@ private function findRouteNameContaining(string $name, RouteCollection $routes):

return $foundRoutesNames;
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestArgumentValuesFor('name')) {
$suggestions->suggestValues(array_keys($this->router->getRouteCollection()->all()));

return;
}

if ($input->mustSuggestOptionValuesFor('format')) {
$helper = new DescriptorHelper();
$suggestions->suggestValues($helper->getFormats());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;

/**
Expand Down Expand Up @@ -69,6 +70,37 @@ public function testSearchWithThrow()
$tester->execute(['name' => 'gerard'], ['interactive' => true]);
}

/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
if (!class_exists(CommandCompletionTester::class)) {
$this->markTestSkipped('Test command completion requires symfony/console 5.4+.');
}

$tester = new CommandCompletionTester($this->application->get('debug:router'));
$this->assertSame($expectedSuggestions, $tester->complete($input));
}

public function provideCompletionSuggestions()
{
yield 'option --format' => [
['--format', ''],
['txt', 'xml', 'json', 'md'],
];

yield 'route_name' => [
[''],
[
'routerdebug_session_welcome',
'routerdebug_session_welcome_name',
'routerdebug_session_logout',
'routerdebug_test',
],
];
}

private function createCommandTester(): CommandTester
{
$command = $this->application->get('debug:router');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Symfony\Bundle\SecurityBundle\Security\FirewallContext;
use Symfony\Bundle\SecurityBundle\Security\LazyFirewallContext;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -277,4 +279,11 @@ private function getExampleName(): string

return $name;
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestArgumentValuesFor('name')) {
$suggestions->suggestValues($this->firewallNames);
}
}
}
73 changes: 60 additions & 13 deletions src/Symfony/Component/Form/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Form\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -159,19 +161,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

private function getFqcnTypeClass(InputInterface $input, SymfonyStyle $io, string $shortClassName): string
{
$classes = [];
sort($this->namespaces);
foreach ($this->namespaces as $namespace) {
if (class_exists($fqcn = $namespace.'\\'.$shortClassName)) {
$classes[] = $fqcn;
} elseif (class_exists($fqcn = $namespace.'\\'.ucfirst($shortClassName))) {
$classes[] = $fqcn;
} elseif (class_exists($fqcn = $namespace.'\\'.ucfirst($shortClassName).'Type')) {
$classes[] = $fqcn;
} elseif (str_ends_with($shortClassName, 'type') && class_exists($fqcn = $namespace.'\\'.ucfirst(substr($shortClassName, 0, -4).'Type'))) {
$classes[] = $fqcn;
}
}
$classes = $this->getFqcnTypeClasses($shortClassName);

if (0 === $count = \count($classes)) {
$message = sprintf("Could not find type \"%s\" into the following namespaces:\n %s", $shortClassName, implode("\n ", $this->namespaces));
Expand All @@ -198,6 +188,25 @@ private function getFqcnTypeClass(InputInterface $input, SymfonyStyle $io, strin
return $io->choice(sprintf("The type \"%s\" is ambiguous.\n\nSelect one of the following form types to display its information:", $shortClassName), $classes, $classes[0]);
}

private function getFqcnTypeClasses(string $shortClassName): array
{
$classes = [];
sort($this->namespaces);
foreach ($this->namespaces as $namespace) {
if (class_exists($fqcn = $namespace.'\\'.$shortClassName)) {
$classes[] = $fqcn;
} elseif (class_exists($fqcn = $namespace.'\\'.ucfirst($shortClassName))) {
$classes[] = $fqcn;
} elseif (class_exists($fqcn = $namespace.'\\'.ucfirst($shortClassName).'Type')) {
$classes[] = $fqcn;
} elseif (str_ends_with($shortClassName, 'type') && class_exists($fqcn = $namespace.'\\'.ucfirst(substr($shortClassName, 0, -4).'Type'))) {
$classes[] = $fqcn;
}
}

return $classes;
}

private function getCoreTypes(): array
{
$coreExtension = new CoreExtension();
Expand Down Expand Up @@ -242,4 +251,42 @@ private function findAlternatives(string $name, array $collection): array

return array_keys($alternatives);
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestArgumentValuesFor('class')) {
$suggestions->suggestValues(array_merge($this->getCoreTypes(), $this->types));

return;
}

if ($input->mustSuggestArgumentValuesFor('option') && null !== $class = $input->getArgument('class')) {
$this->completeOptions($class, $suggestions);

return;
}

if ($input->mustSuggestOptionValuesFor('format')) {
$helper = new DescriptorHelper();
$suggestions->suggestValues($helper->getFormats());
}
}

private function completeOptions(string $class, CompletionSuggestions $suggestions): void
{
if (!class_exists($class) || !is_subclass_of($class, FormTypeInterface::class)) {
$classes = $this->getFqcnTypeClasses($class);

if (1 === count($classes)) {
$class = $classes[0];
}
}

if (!$this->formRegistry->hasType($class)) {
return;
}

$resolvedType = $this->formRegistry->getType($class);
$suggestions->suggestValues($resolvedType->getOptionsResolver()->getDefinedOptions());
}
}
93 changes: 93 additions & 0 deletions src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Command\DebugCommand;
use Symfony\Component\Form\Extension\Core\CoreExtension;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormRegistry;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\Form\ResolvedFormTypeFactory;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -186,6 +189,96 @@ class:%s
, $tester->getDisplay(true));
}

/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
if (!class_exists(CommandCompletionTester::class)) {
$this->markTestSkipped('Test command completion requires symfony/console 5.4+.');
}

$formRegistry = new FormRegistry([], new ResolvedFormTypeFactory());
$command = new DebugCommand($formRegistry);
$application = new Application();
$application->add($command);
$tester = new CommandCompletionTester($application->get('debug:form'));
$this->assertSame($expectedSuggestions, $tester->complete($input));
}

public function provideCompletionSuggestions(): iterable
{
yield 'option --format' => [
['--format', ''],
['txt', 'json'],
];

yield 'form_type' => [
[''],
$this->getCoreTypes(),
];

yield 'option for FQCN' => [
['Symfony\\Component\\Form\\Extension\\Core\\Type\\ButtonType', ''],
[
'block_name',
'block_prefix',
'disabled',
'label',
'label_format',
'row_attr',
'label_html',
'label_translation_parameters',
'attr_translation_parameters',
'attr',
'translation_domain',
'auto_initialize',
'priority',
],
];

yield 'option for short name' => [
['ButtonType', ''],
[
'block_name',
'block_prefix',
'disabled',
'label',
'label_format',
'row_attr',
'label_html',
'label_translation_parameters',
'attr_translation_parameters',
'attr',
'translation_domain',
'auto_initialize',
'priority',
],
];

yield 'option for ambiguous form type' => [
['Type', ''],
[],
];

yield 'option for invalid form type' => [
['NotExistingFormType', ''],
[],
];
}

private function getCoreTypes(): array
{
$coreExtension = new CoreExtension();
$loadTypesRefMethod = (new \ReflectionObject($coreExtension))->getMethod('loadTypes');
$loadTypesRefMethod->setAccessible(true);
$coreTypes = $loadTypesRefMethod->invoke($coreExtension);
$coreTypes = array_map(function (FormTypeInterface $type) { return \get_class($type); }, $coreTypes);
sort($coreTypes);

return $coreTypes;
}

private function createCommandTester(array $namespaces = ['Symfony\Component\Form\Extension\Core\Type'], array $types = [])
{
$formRegistry = new FormRegistry([], new ResolvedFormTypeFactory());
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Messenger/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Messenger\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -138,4 +140,11 @@ private static function getClassDescription(string $class): string

return '';
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestArgumentValuesFor('bus')) {
$suggestions->suggestValues(array_keys($this->mapping));
}
}
}
26 changes: 26 additions & 0 deletions src/Symfony/Component/Messenger/Tests/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace Symfony\Component\Messenger\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Messenger\Command\DebugCommand;
use Symfony\Component\Messenger\Tests\Fixtures\DummyCommand;
Expand Down Expand Up @@ -166,4 +168,28 @@ public function testExceptionOnUnknownBusArgument()
$tester = new CommandTester($command);
$tester->execute(['bus' => 'unknown_bus'], ['decorated' => false]);
}

/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
fabpot marked this conversation as resolved.
Show resolved Hide resolved
if (!class_exists(CommandCompletionTester::class)) {
$this->markTestSkipped('Test command completion requires symfony/console 5.4+.');
}

$command = new DebugCommand(['command_bus' => [], 'query_bus' => []]);
$application = new Application();
$application->add($command);
$tester = new CommandCompletionTester($application->get('debug:messenger'));
$this->assertSame($expectedSuggestions, $tester->complete($input));
}

public function provideCompletionSuggestions(): iterable
{
yield 'bus' => [
[''],
['command_bus', 'query_bus'],
];
}
}