diff --git a/src/Symfony/Component/Uid/Command/GenerateUlidCommand.php b/src/Symfony/Component/Uid/Command/GenerateUlidCommand.php index 2aa87e980466b..1f99b4f49c9a6 100644 --- a/src/Symfony/Component/Uid/Command/GenerateUlidCommand.php +++ b/src/Symfony/Component/Uid/Command/GenerateUlidCommand.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Uid\Command; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Completion\CompletionInput; +use Symfony\Component\Console\Completion\CompletionSuggestions; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\ConsoleOutputInterface; @@ -83,14 +85,14 @@ protected function execute(InputInterface $input, OutputInterface $output) } } - switch ($input->getOption('format')) { - case 'base32': $format = 'toBase32'; break; - case 'base58': $format = 'toBase58'; break; - case 'rfc4122': $format = 'toRfc4122'; break; - default: - $io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $input->getOption('format'))); + $formatOption = $input->getOption('format'); - return 1; + if (\in_array($formatOption, $this->getAvailableFormatOptions())) { + $format = 'to'.ucfirst($formatOption); + } else { + $io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $input->getOption('format'))); + + return 1; } $count = (int) $input->getOption('count'); @@ -106,4 +108,20 @@ protected function execute(InputInterface $input, OutputInterface $output) return 0; } + + public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void + { + if ($input->mustSuggestOptionValuesFor('format')) { + $suggestions->suggestValues($this->getAvailableFormatOptions()); + } + } + + private function getAvailableFormatOptions(): array + { + return [ + 'base32', + 'base58', + 'rfc4122', + ]; + } } diff --git a/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php b/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php index b6992151ec03d..d218b227aa7a4 100644 --- a/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php +++ b/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Uid\Command; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Completion\CompletionInput; +use Symfony\Component\Console\Completion\CompletionSuggestions; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\ConsoleOutputInterface; @@ -174,14 +176,14 @@ protected function execute(InputInterface $input, OutputInterface $output) break; } - switch ($input->getOption('format')) { - case 'base32': $format = 'toBase32'; break; - case 'base58': $format = 'toBase58'; break; - case 'rfc4122': $format = 'toRfc4122'; break; - default: - $io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $input->getOption('format'))); + $formatOption = $input->getOption('format'); + + if (\in_array($formatOption, $this->getAvailableFormatOptions())) { + $format = 'to'.ucfirst($formatOption); + } else { + $io->error(sprintf('Invalid format "%s", did you mean "base32", "base58" or "rfc4122"?', $formatOption)); - return 1; + return 1; } $count = (int) $input->getOption('count'); @@ -197,4 +199,20 @@ protected function execute(InputInterface $input, OutputInterface $output) return 0; } + + public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void + { + if ($input->mustSuggestOptionValuesFor('format')) { + $suggestions->suggestValues($this->getAvailableFormatOptions()); + } + } + + private function getAvailableFormatOptions(): array + { + return [ + 'base32', + 'base58', + 'rfc4122', + ]; + } } diff --git a/src/Symfony/Component/Uid/Tests/Command/GenerateUlidCommandTest.php b/src/Symfony/Component/Uid/Tests/Command/GenerateUlidCommandTest.php index 789a557107fe2..c304915ad73b0 100644 --- a/src/Symfony/Component/Uid/Tests/Command/GenerateUlidCommandTest.php +++ b/src/Symfony/Component/Uid/Tests/Command/GenerateUlidCommandTest.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Uid\Tests\Command; use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandCompletionTester; use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Uid\Command\GenerateUlidCommand; use Symfony\Component\Uid\Ulid; @@ -100,4 +102,24 @@ public function testUlidsAreDifferentWhenGeneratingSeveralNow() $this->assertNotSame($ulids[0], $ulids[1]); } + + /** + * @dataProvider provideCompletionSuggestions + */ + public function testComplete(array $input, array $expectedSuggestions) + { + $application = new Application(); + $application->add(new GenerateUlidCommand()); + $tester = new CommandCompletionTester($application->get('ulid:generate')); + $suggestions = $tester->complete($input, 2); + $this->assertSame($expectedSuggestions, $suggestions); + } + + public function provideCompletionSuggestions(): iterable + { + yield 'option --format' => [ + ['--format', ''], + ['base32', 'base58', 'rfc4122'], + ]; + } } diff --git a/src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php b/src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php index 408fd47d31600..d95d2c4e0dba5 100644 --- a/src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php +++ b/src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Uid\Tests\Command; use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandCompletionTester; use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Uid\Command\GenerateUuidCommand; use Symfony\Component\Uid\Factory\UuidFactory; @@ -229,4 +231,24 @@ public function testNamespacePredefinedKeyword() $this->assertSame('9c7d0eda-982d-5708-b4bd-79b3b179725d', (string) Uuid::fromRfc4122(trim($commandTester->getDisplay()))); } + + /** + * @dataProvider provideCompletionSuggestions + */ + public function testComplete(array $input, array $expectedSuggestions) + { + $application = new Application(); + $application->add(new GenerateUuidCommand()); + $tester = new CommandCompletionTester($application->get('uuid:generate')); + $suggestions = $tester->complete($input, 2); + $this->assertSame($expectedSuggestions, $suggestions); + } + + public function provideCompletionSuggestions(): iterable + { + yield 'option --format' => [ + ['--format', ''], + ['base32', 'base58', 'rfc4122'], + ]; + } }