Skip to content

Commit

Permalink
[Console] Add autocompletion for security commands
Browse files Browse the repository at this point in the history
  • Loading branch information
noniagriconomie committed Oct 22, 2021
1 parent 0f2ca6e commit 487d9a5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Command/UserPasswordHashCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down
29 changes: 29 additions & 0 deletions Tests/Command/UserPasswordHashCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 487d9a5

Please sign in to comment.