From 5a390894ea6db80dd2b66926056a7750813ca4d6 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 13 Dec 2025 16:55:31 +0100 Subject: [PATCH] Use Ask attribute to simplify RenameApiKeyCommand --- .../src/Command/Api/RenameApiKeyCommand.php | 41 +++---------------- .../Command/Api/RenameApiKeyCommandTest.php | 9 ---- 2 files changed, 5 insertions(+), 45 deletions(-) diff --git a/module/CLI/src/Command/Api/RenameApiKeyCommand.php b/module/CLI/src/Command/Api/RenameApiKeyCommand.php index fcbca1ce..fc0ec9bb 100644 --- a/module/CLI/src/Command/Api/RenameApiKeyCommand.php +++ b/module/CLI/src/Command/Api/RenameApiKeyCommand.php @@ -4,19 +4,14 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI\Command\Api; -use Shlinkio\Shlink\Core\Exception\InvalidArgumentException; use Shlinkio\Shlink\Core\Model\Renaming; -use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; use Symfony\Component\Console\Attribute\Argument; use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Attribute\Ask; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; -use function Shlinkio\Shlink\Core\ArrayUtils\map; - #[AsCommand( name: RenameApiKeyCommand::NAME, description: 'Renames an API key by name', @@ -30,38 +25,12 @@ class RenameApiKeyCommand extends Command parent::__construct(); } - protected function interact(InputInterface $input, OutputInterface $output): void - { - $io = new SymfonyStyle($input, $output); - $oldName = $input->getArgument('old-name'); - $newName = $input->getArgument('new-name'); - - if ($oldName === null) { - $apiKeys = $this->apiKeyService->listKeys(); - $requestedOldName = $io->choice( - 'What API key do you want to rename?', - map($apiKeys, static fn (ApiKey $apiKey) => $apiKey->name), - ); - - $input->setArgument('old-name', $requestedOldName); - } - - if ($newName === null) { - $requestedNewName = $io->ask( - 'What is the new name you want to set?', - validator: static fn (string|null $value): string => $value !== null - ? $value - : throw new InvalidArgumentException('The new name cannot be empty'), - ); - - $input->setArgument('new-name', $requestedNewName); - } - } - public function __invoke( SymfonyStyle $io, - #[Argument(description: 'Current name of the API key to rename')] string $oldName, - #[Argument(description: 'New name to set to the API key')] string $newName, + #[Argument(description: 'Current name of the API key to rename'), Ask('What API key do you want to rename?')] + string $oldName, + #[Argument(description: 'New name to set to the API key'), Ask('What is the new name you want to set?')] + string $newName, ): int { $this->apiKeyService->renameApiKey(Renaming::fromNames($oldName, $newName)); $io->success('API key properly renamed'); diff --git a/module/CLI/test/Command/Api/RenameApiKeyCommandTest.php b/module/CLI/test/Command/Api/RenameApiKeyCommandTest.php index d8c5f07f..2fe4fb9d 100644 --- a/module/CLI/test/Command/Api/RenameApiKeyCommandTest.php +++ b/module/CLI/test/Command/Api/RenameApiKeyCommandTest.php @@ -9,8 +9,6 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\CLI\Command\Api\RenameApiKeyCommand; use Shlinkio\Shlink\Core\Model\Renaming; -use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta; -use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use Symfony\Component\Console\Tester\CommandTester; @@ -32,11 +30,6 @@ class RenameApiKeyCommandTest extends TestCase $oldName = 'old name'; $newName = 'new name'; - $this->apiKeyService->expects($this->once())->method('listKeys')->willReturn([ - ApiKey::fromMeta(ApiKeyMeta::fromParams(name: 'foo')), - ApiKey::fromMeta(ApiKeyMeta::fromParams(name: $oldName)), - ApiKey::fromMeta(ApiKeyMeta::fromParams(name: 'bar')), - ]); $this->apiKeyService->expects($this->once())->method('renameApiKey')->with( Renaming::fromNames($oldName, $newName), ); @@ -53,7 +46,6 @@ class RenameApiKeyCommandTest extends TestCase $oldName = 'old name'; $newName = 'new name'; - $this->apiKeyService->expects($this->never())->method('listKeys'); $this->apiKeyService->expects($this->once())->method('renameApiKey')->with( Renaming::fromNames($oldName, $newName), ); @@ -70,7 +62,6 @@ class RenameApiKeyCommandTest extends TestCase $oldName = 'old name'; $newName = 'new name'; - $this->apiKeyService->expects($this->never())->method('listKeys'); $this->apiKeyService->expects($this->once())->method('renameApiKey')->with( Renaming::fromNames($oldName, $newName), );