Skip to content

Commit 3d42741

Browse files
[Console] Add completion to server:dump command
1 parent 31aae3b commit 3d42741

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

Command/ServerDumpCommand.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\VarDumper\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\Input\InputInterface;
1719
use Symfony\Component\Console\Input\InputOption;
@@ -55,10 +57,8 @@ public function __construct(DumpServer $server, array $descriptors = [])
5557

5658
protected function configure()
5759
{
58-
$availableFormats = implode(', ', array_keys($this->descriptors));
59-
6060
$this
61-
->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format (%s)', $availableFormats), 'cli')
61+
->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format (%s)', implode(', ', $this->getAvailableFormats())), 'cli')
6262
->setDescription(self::$defaultDescription)
6363
->setHelp(<<<'EOF'
6464
<info>%command.name%</info> starts a dump server that collects and displays
@@ -99,4 +99,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9999

100100
return 0;
101101
}
102+
103+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
104+
{
105+
if ($input->mustSuggestOptionValuesFor('format')) {
106+
$suggestions->suggestValues($this->getAvailableFormats());
107+
}
108+
}
109+
110+
private function getAvailableFormats(): array
111+
{
112+
return array_keys($this->descriptors);
113+
}
102114
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Symfony\Component\VarDumper\Tests\Command;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Console\Tester\CommandCompletionTester;
7+
use Symfony\Component\VarDumper\Command\ServerDumpCommand;
8+
use Symfony\Component\VarDumper\Server\DumpServer;
9+
10+
class ServerDumpCommandTest extends TestCase
11+
{
12+
/**
13+
* @dataProvider provideCompletionSuggestions
14+
*/
15+
public function testComplete(array $input, array $expectedSuggestions)
16+
{
17+
$tester = new CommandCompletionTester($this->createCommand());
18+
19+
$this->assertSame($expectedSuggestions, $tester->complete($input));
20+
}
21+
22+
public function provideCompletionSuggestions()
23+
{
24+
yield 'option --format' => [
25+
['--format', ''],
26+
['cli', 'html'],
27+
];
28+
}
29+
30+
private function createCommand(): ServerDumpCommand
31+
{
32+
return new ServerDumpCommand($this->createMock(DumpServer::class));
33+
}
34+
}

0 commit comments

Comments
 (0)