From 49f45a97798f5df69418739d9bc87cd8829ba359 Mon Sep 17 00:00:00 2001 From: noniagriconomie Date: Fri, 22 Oct 2021 12:59:09 +0200 Subject: [PATCH] [Console] Add autocompletion for security commands --- .../Command/UserPasswordHashCommand.php | 11 +++++++ .../Command/UserPasswordHashCommandTest.php | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/Symfony/Component/PasswordHasher/Command/UserPasswordHashCommand.php b/src/Symfony/Component/PasswordHasher/Command/UserPasswordHashCommand.php index 1643ed855831b..1d0e1d09fa71e 100644 --- a/src/Symfony/Component/PasswordHasher/Command/UserPasswordHashCommand.php +++ b/src/Symfony/Component/PasswordHasher/Command/UserPasswordHashCommand.php @@ -12,6 +12,8 @@ namespace Symfony\Component\PasswordHasher\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\Exception\RuntimeException; use Symfony\Component\Console\Input\InputArgument; @@ -168,6 +170,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } + public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void + { + if ($input->mustSuggestArgumentValuesFor('user-class')) { + $suggestions->suggestValues($this->userClasses); + + return; + } + } + /** * Create the password question to ask the user for the password to be hashed. */ diff --git a/src/Symfony/Component/PasswordHasher/Tests/Command/UserPasswordHashCommandTest.php b/src/Symfony/Component/PasswordHasher/Tests/Command/UserPasswordHashCommandTest.php index 5c24ce9539e45..29fdae5e8dc27 100644 --- a/src/Symfony/Component/PasswordHasher/Tests/Command/UserPasswordHashCommandTest.php +++ b/src/Symfony/Component/PasswordHasher/Tests/Command/UserPasswordHashCommandTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\PasswordHasher\Tests\Command; use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Tester\CommandCompletionTester; use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\PasswordHasher\Command\UserPasswordHashCommand; use Symfony\Component\PasswordHasher\Hasher\NativePasswordHasher; @@ -286,6 +287,34 @@ public function testThrowsExceptionOnNoConfiguredHashers() ], ['interactive' => false]); } + /** + * @dataProvider provideCompletionSuggestions + */ + public function testCompletionSuggestions(array $input, array $expectedSuggestions) + { + if (!class_exists(CommandCompletionTester::class)) { + $this->markTestSkipped('Test command completion requires symfony/console 5.4+.'); + } + + $command = new UserPasswordHashCommand($this->createMock(PasswordHasherFactoryInterface::class), ['App\Entity\User']); + $tester = new CommandCompletionTester($command); + + $this->assertSame($expectedSuggestions, $tester->complete($input)); + } + + public function provideCompletionSuggestions() + { + yield 'user_class_empty' => [ + [''], + ['App\Entity\User'], + ]; + + yield 'user_class_given' => [ + ['App'], + ['App\Entity\User'], + ]; + } + protected function setUp(): void { $this->colSize = getenv('COLUMNS');