shlink/module/CLI/test/ApiKey/RoleResolverTest.php

123 lines
4 KiB
PHP
Raw Normal View History

2021-01-10 22:24:13 +03:00
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\ApiKey;
use PHPUnit\Framework\MockObject\MockObject;
2021-01-10 22:24:13 +03:00
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\ApiKey\RoleResolver;
use Shlinkio\Shlink\CLI\Exception\InvalidRoleConfigException;
2021-01-10 22:24:13 +03:00
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
2021-01-10 22:24:13 +03:00
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
use Shlinkio\Shlink\Rest\ApiKey\Role;
2021-01-10 22:24:13 +03:00
use Symfony\Component\Console\Input\InputInterface;
use function Functional\map;
2021-01-10 22:24:13 +03:00
class RoleResolverTest extends TestCase
{
private RoleResolver $resolver;
2022-10-24 20:53:13 +03:00
private MockObject & DomainServiceInterface $domainService;
2021-01-10 22:24:13 +03:00
protected function setUp(): void
{
$this->domainService = $this->createMock(DomainServiceInterface::class);
$this->resolver = new RoleResolver($this->domainService, 'default.com');
2021-01-10 22:24:13 +03:00
}
/**
* @test
* @dataProvider provideRoles
*/
public function properRolesAreResolvedBasedOnInput(
InputInterface $input,
array $expectedRoles,
int $expectedDomainCalls,
2021-01-10 22:24:13 +03:00
): void {
$this->domainService->expects($this->exactly($expectedDomainCalls))->method('getOrCreate')->with(
'example.com',
2022-10-24 21:11:25 +03:00
)->willReturn($this->domainWithId(Domain::withAuthority('example.com')));
2021-01-10 22:24:13 +03:00
$result = $this->resolver->determineRoles($input);
self::assertEquals($expectedRoles, $result);
}
public function provideRoles(): iterable
{
2022-10-24 21:11:25 +03:00
$domain = $this->domainWithId(Domain::withAuthority('example.com'));
2021-01-10 22:24:13 +03:00
$buildInput = function (array $definition): InputInterface {
$input = $this->createStub(InputInterface::class);
$input->method('getOption')->willReturnMap(
map($definition, static fn (mixed $returnValue, string $param) => [$param, $returnValue]),
);
2021-01-10 22:24:13 +03:00
return $input;
2021-01-10 22:24:13 +03:00
};
yield 'no roles' => [
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => null, Role::AUTHORED_SHORT_URLS->paramName() => false]),
2021-01-10 22:24:13 +03:00
[],
0,
];
yield 'domain role only' => [
$buildInput(
[Role::DOMAIN_SPECIFIC->paramName() => 'example.com', Role::AUTHORED_SHORT_URLS->paramName() => false],
),
[RoleDefinition::forDomain($domain)],
2021-01-10 22:24:13 +03:00
1,
];
2021-07-17 21:58:24 +03:00
yield 'false domain role' => [
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => false]),
2021-07-17 21:58:24 +03:00
[],
0,
];
yield 'true domain role' => [
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => true]),
2021-07-17 21:58:24 +03:00
[],
0,
];
yield 'string array domain role' => [
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => ['foo', 'bar']]),
2021-07-17 21:58:24 +03:00
[],
0,
];
2021-01-10 22:24:13 +03:00
yield 'author role only' => [
$buildInput([Role::DOMAIN_SPECIFIC->paramName() => null, Role::AUTHORED_SHORT_URLS->paramName() => true]),
2021-01-10 22:24:13 +03:00
[RoleDefinition::forAuthoredShortUrls()],
0,
];
yield 'both roles' => [
$buildInput(
[Role::DOMAIN_SPECIFIC->paramName() => 'example.com', Role::AUTHORED_SHORT_URLS->paramName() => true],
),
[RoleDefinition::forAuthoredShortUrls(), RoleDefinition::forDomain($domain)],
2021-01-10 22:24:13 +03:00
1,
];
}
/** @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],
]);
$this->expectException(InvalidRoleConfigException::class);
$this->resolver->determineRoles($input);
}
2022-10-24 21:11:25 +03:00
private function domainWithId(Domain $domain): Domain
{
$domain->setId('1');
return $domain;
}
2021-01-10 22:24:13 +03:00
}