Skip to content

[VarDumper] Add completion to server:dump command #43683

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

Merged
merged 1 commit into from
Oct 24, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/Symfony/Component/VarDumper/Command/ServerDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\VarDumper\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\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -55,10 +57,8 @@ public function __construct(DumpServer $server, array $descriptors = [])

protected function configure()
{
$availableFormats = implode(', ', array_keys($this->descriptors));

$this
->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format (%s)', $availableFormats), 'cli')
->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format (%s)', implode(', ', $this->getAvailableFormats())), 'cli')
->setDescription(self::$defaultDescription)
->setHelp(<<<'EOF'
<info>%command.name%</info> starts a dump server that collects and displays
Expand Down Expand Up @@ -99,4 +99,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int

return 0;
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestOptionValuesFor('format')) {
$suggestions->suggestValues($this->getAvailableFormats());
}
}

private function getAvailableFormats(): array
{
return array_keys($this->descriptors);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Symfony\Component\VarDumper\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\VarDumper\Command\ServerDumpCommand;
use Symfony\Component\VarDumper\Server\DumpServer;

class ServerDumpCommandTest extends TestCase
{
/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
$tester = new CommandCompletionTester($this->createCommand());

$this->assertSame($expectedSuggestions, $tester->complete($input));
}

public function provideCompletionSuggestions()
{
yield 'option --format' => [
['--format', ''],
['cli', 'html'],
];
}

private function createCommand(): ServerDumpCommand
{
return new ServerDumpCommand($this->createMock(DumpServer::class));
}
}