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

Skip to content

Commit ca1e869

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

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

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

+11
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 8000 \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

+24
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

+11
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

+25
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,30 @@ public function testThrowsExceptionOnNoConfiguredHashers()
286287
], ['interactive' => false]);
287288
}
288289

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

0 commit comments

Comments
 (0)
0