Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Symfony/Bridge/Twig/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Bridge\Twig\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\Formatter\OutputFormatter;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -110,6 +112,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 0;
}

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

if ($input->mustSuggestOptionValuesFor('format')) {
$suggestions->suggestValues(['text', 'json']);
}
}

private function displayPathsText(SymfonyStyle $io, string $name)
{
$file = new \ArrayIterator($this->findTemplateFiles($name));
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bridge\Twig\Command\DebugCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;
use Twig\Environment;
use Twig\Loader\ChainLoader;
Expand Down Expand Up @@ -293,6 +294,33 @@ public function testWithFilter()
$this->assertNotSame($display1, $display2);
}

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

$projectDir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
$loader = new FilesystemLoader([], $projectDir);
$environment = new Environment($loader);

$application = new Application();
$application->add(new DebugCommand($environment, $projectDir, [], null, null));

$tester = new CommandCompletionTester($application->find('debug:twig'));
$suggestions = $tester->complete($input, 2);
$this->assertSame($expectedSuggestions, $suggestions);
}

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

private function createCommandTester(array $paths = [], array $bundleMetadata = [], string $defaultPath = null, bool $useChainLoader = false, array $globals = []): CommandTester
{
$projectDir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
Expand Down