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

124 lines
4.1 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(
2023-02-09 11:32:38 +03:00
callable $createInput,
2021-01-10 22:24:13 +03:00
array $expectedRoles,
int $expectedDomainCalls,
2021-01-10 22:24:13 +03:00
): void {
2023-02-09 11:32:38 +03:00
$input = $createInput($this);
$this->domainService->expects($this->exactly($expectedDomainCalls))->method('getOrCreate')->with(
'example.com',
2023-02-09 11:32:38 +03:00
)->willReturn(self::domainWithId(Domain::withAuthority('example.com')));
2021-01-10 22:24:13 +03:00
$result = $this->resolver->determineRoles($input);
self::assertEquals($expectedRoles, $result);
}
2023-02-09 11:32:38 +03:00
public static function provideRoles(): iterable
2021-01-10 22:24:13 +03:00
{
2023-02-09 11:32:38 +03:00
$domain = self::domainWithId(Domain::withAuthority('example.com'));
$buildInput = static fn (array $definition) => function (TestCase $test) use ($definition): InputInterface {
$input = $test->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
2023-02-09 11:32:38 +03:00
private static function domainWithId(Domain $domain): Domain
2022-10-24 21:11:25 +03:00
{
$domain->setId('1');
return $domain;
}
2021-01-10 22:24:13 +03:00
}