Migrate GenerateKeyCommand to symfony/console attributes

This commit is contained in:
Alejandro Celaya
2025-12-13 16:49:52 +01:00
parent c53f538c79
commit 5df3abbce9
6 changed files with 92 additions and 127 deletions

View File

@@ -9,12 +9,12 @@ use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\ApiKey\RoleResolver;
use Shlinkio\Shlink\CLI\Command\Api\Input\ApiKeyInput;
use Shlinkio\Shlink\CLI\Exception\InvalidRoleConfigException;
use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
use Shlinkio\Shlink\Rest\ApiKey\Role;
use Symfony\Component\Console\Input\InputInterface;
class RoleResolverTest extends TestCase
@@ -30,11 +30,10 @@ class RoleResolverTest extends TestCase
#[Test, DataProvider('provideRoles')]
public function properRolesAreResolvedBasedOnInput(
callable $createInput,
ApiKeyInput $input,
array $expectedRoles,
int $expectedDomainCalls,
): void {
$input = $createInput($this);
$this->domainService->expects($this->exactly($expectedDomainCalls))->method('getOrCreate')->with(
'example.com',
)->willReturn(self::domainWithId(Domain::withAuthority('example.com')));
@@ -60,43 +59,39 @@ class RoleResolverTest extends TestCase
};
yield 'no roles' => [
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => null, Role::AUTHORED_SHORT_URLS->paramName() => false]),
new ApiKeyInput(),
[],
0,
];
yield 'domain role only' => [
$buildInput(
[Role::DOMAIN_SPECIFIC->paramName() => 'example.com', Role::AUTHORED_SHORT_URLS->paramName() => false],
),
(function (): ApiKeyInput {
$input = new ApiKeyInput();
$input->domain = 'example.com';
return $input;
})(),
[RoleDefinition::forDomain($domain)],
1,
];
yield 'false domain role' => [
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => false]),
[],
0,
];
yield 'true domain role' => [
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => true]),
[],
0,
];
yield 'string array domain role' => [
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => ['foo', 'bar']]),
[],
0,
];
yield 'author role only' => [
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => null, Role::AUTHORED_SHORT_URLS->paramName() => true]),
(function (): ApiKeyInput {
$input = new ApiKeyInput();
$input->authorOnly = true;
return $input;
})(),
[RoleDefinition::forAuthoredShortUrls()],
0,
];
yield 'all roles' => [
$buildInput([
Role::DOMAIN_SPECIFIC->paramName() => 'example.com',
Role::AUTHORED_SHORT_URLS->paramName() => true,
Role::NO_ORPHAN_VISITS->paramName() => true,
]),
(function (): ApiKeyInput {
$input = new ApiKeyInput();
$input->domain = 'example.com';
$input->authorOnly = true;
$input->noOrphanVisits = true;
return $input;
})(),
[
RoleDefinition::forAuthoredShortUrls(),
RoleDefinition::forDomain($domain),
@@ -109,13 +104,8 @@ class RoleResolverTest extends TestCase
#[Test]
public function exceptionIsThrownWhenTryingToAddDomainOnlyLinkedToDefaultDomain(): void
{
$input = $this->createStub(InputInterface::class);
$input
->method('getOption')
->willReturnMap([
[Role::DOMAIN_SPECIFIC->paramName(), 'default.com'],
[Role::AUTHORED_SHORT_URLS->paramName(), null],
]);
$input = new ApiKeyInput();
$input->domain = 'default.com';
$this->expectException(InvalidRoleConfigException::class);