mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Add tests for RenameApiKeyCOmmand and ApiKeyMeta
This commit is contained in:
83
module/CLI/test/Command/Api/RenameApiKeyCommandTest.php
Normal file
83
module/CLI/test/Command/Api/RenameApiKeyCommandTest.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\CLI\Command\Api;
|
||||
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
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;
|
||||
|
||||
class RenameApiKeyCommandTest extends TestCase
|
||||
{
|
||||
private CommandTester $commandTester;
|
||||
private MockObject & ApiKeyServiceInterface $apiKeyService;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->apiKeyService = $this->createMock(ApiKeyServiceInterface::class);
|
||||
$this->commandTester = CliTestUtils::testerForCommand(new RenameApiKeyCommand($this->apiKeyService));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function oldNameIsRequestedIfNotProvided(): void
|
||||
{
|
||||
$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),
|
||||
);
|
||||
|
||||
$this->commandTester->setInputs([$oldName]);
|
||||
$this->commandTester->execute([
|
||||
'newName' => $newName,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function newNameIsRequestedIfNotProvided(): void
|
||||
{
|
||||
$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),
|
||||
);
|
||||
|
||||
$this->commandTester->setInputs([$newName]);
|
||||
$this->commandTester->execute([
|
||||
'oldName' => $oldName,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function apiIsRenamedWithProvidedNames(): void
|
||||
{
|
||||
$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),
|
||||
);
|
||||
|
||||
$this->commandTester->execute([
|
||||
'oldName' => $oldName,
|
||||
'newName' => $newName,
|
||||
]);
|
||||
}
|
||||
}
|
||||
35
module/Rest/test/ApiKey/Model/ApiKeyMetaTest.php
Normal file
35
module/Rest/test/ApiKey/Model/ApiKeyMetaTest.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Rest\ApiKey\Model;
|
||||
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
|
||||
|
||||
use function sprintf;
|
||||
use function substr;
|
||||
|
||||
class ApiKeyMetaTest extends TestCase
|
||||
{
|
||||
#[Test, DataProvider('provideNames')]
|
||||
public function nameIsInferredWhenNotProvided(string|null $key, string|null $name, callable $getExpectedName): void
|
||||
{
|
||||
$meta = ApiKeyMeta::fromParams(key: $key, name: $name);
|
||||
$expectedName = $getExpectedName($meta);
|
||||
|
||||
self::assertEquals($expectedName, $meta->name);
|
||||
}
|
||||
|
||||
public static function provideNames(): iterable
|
||||
{
|
||||
yield 'name' => [null, 'the name', static fn (ApiKeyMeta $meta) => 'the name'];
|
||||
yield 'key' => ['the key', null, static fn (ApiKeyMeta $meta) => 'the key'];
|
||||
yield 'generated key' => [null, null, static fn (ApiKeyMeta $meta) => sprintf(
|
||||
'%s-****-****-****-************',
|
||||
substr($meta->key, offset: 0, length: 8),
|
||||
)];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user