10000 [Console] Add autocompletion for security commands · symfony/symfony@49f45a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 49f45a9

Browse files
[Console] Add autocompletion for security commands
1 parent 47e284b commit 49f45a9

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/Symfony/Component/PasswordHasher/Command/UserPasswordHashCommand.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\PasswordHasher\Command;
1313

1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1517
use Symfony\Component\Console\Exception\InvalidArgumentException;
1618
use Symfony\Component\Console\Exception\RuntimeException;
1719
use Symfony\Component\Console\Input\InputArgument;
@@ -168,6 +170,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
168170
return 0;
169171
}
170172

173+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
174+
{
175+
if ($input->mustSuggestArgumentValuesFor('user-class')) {
176+
$suggestions->suggestValues($this->userClasses);
177+
178+
return;
179+
}
180+
}
181+
171182
/**
172183
* Create the password question to ask the user for the password to be hashed.
173184
*/

src/Symfony/Component/PasswordHasher/Tests/Command/UserPasswordHashCommandTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\PasswordHasher\Tests\Command;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Tester\CommandCompletionTester;
1516
use Symfony\Component\Console\Tester\CommandTester;
1617
use Symfony\Component\PasswordHasher\Command\UserPasswordHashCommand;
1718
use Symfony\Component\PasswordHasher\Hasher\NativePasswordHasher;
@@ -286,6 +287,34 @@ public function testThrowsExceptionOnNoConfiguredHashers()
286287
], ['interactive' => false]);
287288
}
288289

290+
/**
291+
* @dataProvider provideCompletionSuggestions
292+
*/
293+
public function testCompletionSuggestions(array $input, array $expectedSuggestions)
294+
{
295+
if (!class_exists(CommandCompletionTester::class)) {
296+
$this->markTestSkipped('Test command completion requires symfony/console 5.4+.');
297+
}
298+
299+
$command = new UserPasswordHashCommand($this->createMock(PasswordHasherFactoryInterface::class), ['App\Entity\User']);
300+
$tester = new CommandCompletionTester($command);
301+
302+
$this->assertSame($expectedSuggestions, $tester->complete($input));
303+
}
304+
305+
public function provideCompletionSuggestions()
306+
{
307+
yield 'user_class_empty' => [
308+
[''],
309+
['App\Entity\User'],
310+
];
311+
312+
yield 'user_class_given' => [
313+
['App'],
314+
['App\Entity\User'],
315+
];
316+
}
317+
289318
protected function setUp(): void
290319
{
291320
$this->colSize = getenv('COLUMNS');

0 commit comments

Comments
 (0)
0