8000 [Console] Add autocompletion for security commands · symfony/symfony@534c766 · GitHub
[go: up one dir, main page]

Skip to content

Commit 534c766

Browse files
[Console] Add autocompletion for security commands
1 parent 47e284b commit 534c766

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

src/Symfony/Bundle/SecurityBundle/Command/UserPasswordEncoderCommand.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Bundle\SecurityBundle\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;
@@ -172,6 +174,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
172174
return 0;
173175
}
174176

177+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
178+
{
179+
if ($input->mustSuggestArgumentValuesFor('user-class')) {
180+
$suggestions->suggestValues($this->userClasses);
181+
182+
return;
183+
}
184+
}
185+
175186
/**
176187
* Create the password question to ask the user for the password to be encoded.
177188
*/

src/Symfony/Bundle/SecurityBundle/Tests/Functional/UserPasswordEncoderCommandTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bundle\FrameworkBundle\Console\Application;
1515
use Symfony\Bundle\SecurityBundle\Command\UserPasswordEncoderCommand;
1616
use Symfony\Component\Console\Application as ConsoleApplication;
17+
use Symfony\Component\Console\Tester\CommandCompletionTester;
1718
use Symfony\Component\Console\Tester\CommandTester;
1819
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
1920
use Symfony\Component\Security\Core\Encoder\NativePasswordEncoder;
@@ -315,6 +316,29 @@ public function testThrowsExceptionOnNoConfiguredEncoders()
315316
], ['interactive' => false]);
316317
}
317318

319+
/**
320+
* @dataProvider provideCompletionSuggestions
321+
*/
322+
public function testCompletionSuggestions(array $input, array $expectedSuggestions)
323+
{
324+
if (!class_exists(CommandCompletionTester::class)) {
325+
$this->markTestSkipped('Test command completion requires symfony/console 5.4+.');
326+
}
327+
328+
$command = new UserPasswordEncoderCommand($this->createMock(EncoderFactoryInterface::class), ['App\Entity\User']);
329+
$tester = new CommandCompletionTester($command);
330+
$suggestions = $tester->complete($input);
331+
$this->assertSame($expectedSuggestions, $suggestions);
332+
}
333+
334+
public function provideCompletionSuggestions()
335+
{
336+
yield 'user_class' => [
337+
['App'],
338+
['App\Entity\User'],
339+
];
340+
}
341+
318342
protected function setUp(): void
319343
{
320344
$this->colSize = getenv('COLUMNS');

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\Compl 6D40 etion\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: 24 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,29 @@ 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+
$suggestions = $tester->complete($input);
302+
$this->assertSame($expectedSuggestions, $suggestions);
303+
}
304+
305+
public function provideCompletionSuggestions()
306+
{
307+
yield 'user_class' => [
308+
['App'],
309+
['App\Entity\User'],
310+
];
311+
}
312+
289313
protected function setUp(): void
290314
{
291315
$this->colSize = getenv('COLUMNS');

0 commit comments

Comments
 (0)
0