Make sure a unique name is required by api-key:generate command

This commit is contained in:
Alejandro Celaya
2024-11-07 14:52:06 +01:00
parent 4c1ff72438
commit 9e6f129de6
2 changed files with 31 additions and 6 deletions

View File

@@ -10,6 +10,7 @@ use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\ApiKey\RoleResolverInterface;
use Shlinkio\Shlink\CLI\Command\Api\GenerateKeyCommand;
use Shlinkio\Shlink\CLI\Util\ExitCode;
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
@@ -64,8 +65,27 @@ class GenerateKeyCommandTest extends TestCase
$this->callback(fn (ApiKeyMeta $meta) => $meta->name === 'Alice'),
)->willReturn(ApiKey::create());
$this->commandTester->execute([
$exitCode = $this->commandTester->execute([
'--name' => 'Alice',
]);
self::assertEquals(ExitCode::EXIT_SUCCESS, $exitCode);
}
#[Test]
public function warningIsPrintedIfProvidedNameAlreadyExists(): void
{
$name = 'The API key';
$this->apiKeyService->expects($this->never())->method('create');
$this->apiKeyService->expects($this->once())->method('existsWithName')->with($name)->willReturn(true);
$exitCode = $this->commandTester->execute([
'--name' => $name,
]);
$output = $this->commandTester->getDisplay();
self::assertEquals(ExitCode::EXIT_WARNING, $exitCode);
self::assertStringContainsString('An API key with name "The API key" already exists.', $output);
}
}