Skip to content

Commit 76ed6f4

Browse files
StaffNowafabpot
authored andcommitted
Add completion for debug:twig
1 parent cdf6571 commit 76ed6f4

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Command/DebugCommand.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Bridge\Twig\Command;
1313

1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1517
use Symfony\Component\Console\Exception\InvalidArgumentException;
1618
use Symfony\Component\Console\Formatter\OutputFormatter;
1719
use Symfony\Component\Console\Input\InputArgument;
@@ -110,6 +112,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
110112
return 0;
111113
}
112114

115+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
116+
{
117+
if ($input->mustSuggestArgumentValuesFor('name')) {
118+
$suggestions->suggestValues(array_keys($this->getLoaderPaths()));
119+
}
120+
121+
if ($input->mustSuggestOptionValuesFor('format')) {
122+
$suggestions->suggestValues(['text', 'json']);
123+
}
124+
}
125+
113126
private function displayPathsText(SymfonyStyle $io, string $name)
114127
{
115128
$file = new \ArrayIterator($this->findTemplateFiles($name));

Tests/Command/DebugCommandTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bridge\Twig\Command\DebugCommand;
1616
use Symfony\Component\Console\Application;
1717
use Symfony\Component\Console\Exception\InvalidArgumentException;
18+
use Symfony\Component\Console\Tester\CommandCompletionTester;
1819
use Symfony\Component\Console\Tester\CommandTester;
1920
use Twig\Environment;
2021
use Twig\Loader\ChainLoader;
@@ -293,6 +294,33 @@ public function testWithFilter()
293294
$this->assertNotSame($display1, $display2);
294295
}
295296

297+
/**
298+
* @dataProvider provideCompletionSuggestions
299+
*/
300+
public function testComplete(array $input, array $expectedSuggestions)
301+
{
302+
if (!class_exists(CommandCompletionTester::class)) {
303+
$this->markTestSkipped('Test command completion requires symfony/console 5.4+.');
304+
}
305+
306+
$projectDir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
307+
$loader = new FilesystemLoader([], $projectDir);
308+
$environment = new Environment($loader);
309+
310+
$application = new Application();
311+
$application->add(new DebugCommand($environment, $projectDir, [], null, null));
312+
313+
$tester = new CommandCompletionTester($application->find('debug:twig'));
314+
$suggestions = $tester->complete($input, 2);
315+
$this->assertSame($expectedSuggestions, $suggestions);
316+
}
317+
318+
public function provideCompletionSuggestions(): iterable
319+
{
320+
yield 'name' => [['email'], []];
321+
yield 'option --format' => [['--format', ''], ['text', 'json']];
322+
}
323+
296324
private function createCommandTester(array $paths = [], array $bundleMetadata = [], string $defaultPath = null, bool $useChainLoader = false, array $globals = []): CommandTester
297325
{
298326
$projectDir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';

0 commit comments

Comments
 (0)