Skip to content

[Uid] Allow use autocompletion #43639

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 22, 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
32 changes: 25 additions & 7 deletions src/Symfony/Component/Uid/Command/GenerateUlidCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message could use the list.

Copy link
Contributor Author

@StaffNowa StaffNowa Oct 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I made PR #43849


return 1;
}

$count = (int) $input->getOption('count');
Expand All @@ -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',
];
}
}
32 changes: 25 additions & 7 deletions src/Symfony/Component/Uid/Command/GenerateUuidCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced by a const in d0f6b98

{
return [
'base32',
'base58',
'rfc4122',
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'],
];
}
}