Files

114 lines
3.4 KiB
PHP
Raw Permalink Normal View History

2021-01-10 20:24:13 +01:00
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\ApiKey;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
2021-01-10 20:24:13 +01:00
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;
2021-01-10 20:24:13 +01:00
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
2021-01-10 20:24:13 +01:00
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
class RoleResolverTest extends TestCase
{
private RoleResolver $resolver;
private MockObject&DomainServiceInterface $domainService;
2021-01-10 20:24:13 +01:00
protected function setUp(): void
{
$this->domainService = $this->createMock(DomainServiceInterface::class);
$this->resolver = new RoleResolver($this->domainService, new UrlShortenerOptions('default.com'));
2021-01-10 20:24:13 +01:00
}
#[Test, DataProvider('provideRoles')]
2021-01-10 20:24:13 +01:00
public function properRolesAreResolvedBasedOnInput(
ApiKeyInput $input,
2021-01-10 20:24:13 +01:00
array $expectedRoles,
int $expectedDomainCalls,
2021-01-10 20:24:13 +01:00
): void {
$this->domainService
->expects($this->exactly($expectedDomainCalls))
->method('getOrCreate')
->with(
'example.com',
)
->willReturn(self::domainWithId(Domain::withAuthority('example.com')));
2021-01-10 20:24:13 +01:00
2023-09-19 09:10:17 +02:00
$result = [...$this->resolver->determineRoles($input)];
2021-01-10 20:24:13 +01:00
self::assertEquals($expectedRoles, $result);
}
2023-02-09 09:32:38 +01:00
public static function provideRoles(): iterable
2021-01-10 20:24:13 +01:00
{
2023-02-09 09:32:38 +01:00
$domain = self::domainWithId(Domain::withAuthority('example.com'));
2021-01-10 20:24:13 +01:00
yield 'no roles' => [
new ApiKeyInput(),
2021-01-10 20:24:13 +01:00
[],
0,
];
yield 'domain role only' => [
2026-05-16 11:21:43 +02:00
(static function (): ApiKeyInput {
$input = new ApiKeyInput();
$input->domain = 'example.com';
return $input;
})(),
[RoleDefinition::forDomain($domain)],
2021-01-10 20:24:13 +01:00
1,
];
yield 'author role only' => [
2026-05-16 11:21:43 +02:00
(static function (): ApiKeyInput {
$input = new ApiKeyInput();
$input->authorOnly = true;
return $input;
})(),
2021-01-10 20:24:13 +01:00
[RoleDefinition::forAuthoredShortUrls()],
0,
];
2025-11-17 12:33:08 +01:00
yield 'all roles' => [
2026-05-16 11:21:43 +02:00
(static function (): ApiKeyInput {
$input = new ApiKeyInput();
$input->domain = 'example.com';
$input->authorOnly = true;
$input->noOrphanVisits = true;
return $input;
})(),
2025-11-17 12:33:08 +01:00
[
RoleDefinition::forAuthoredShortUrls(),
RoleDefinition::forDomain($domain),
RoleDefinition::forNoOrphanVisits(),
],
2021-01-10 20:24:13 +01:00
1,
];
}
#[Test]
public function exceptionIsThrownWhenTryingToAddDomainOnlyLinkedToDefaultDomain(): void
{
$input = new ApiKeyInput();
$input->domain = 'default.com';
$this->domainService->expects($this->never())->method('getOrCreate');
$this->expectException(InvalidRoleConfigException::class);
2023-09-19 09:10:17 +02:00
[...$this->resolver->determineRoles($input)];
}
2022-10-24 20:11:25 +02:00
2023-02-09 09:32:38 +01:00
private static function domainWithId(Domain $domain): Domain
2022-10-24 20:11:25 +02:00
{
$domain->setId('1');
return $domain;
}
2021-01-10 20:24:13 +01:00
}