Used PHPUnit mocks in RoleResolverTest instead of prophezy

This commit is contained in:
Alejandro Celaya 2022-10-12 12:47:58 +02:00
parent b59cbeceac
commit bcd5d2848d
7 changed files with 61 additions and 47 deletions

View File

@ -7,6 +7,7 @@ namespace Shlinkio\Shlink\CLI\ApiKey;
use Shlinkio\Shlink\CLI\Exception\InvalidRoleConfigException; use Shlinkio\Shlink\CLI\Exception\InvalidRoleConfigException;
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface; use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition; use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
use Shlinkio\Shlink\Rest\ApiKey\Role;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use function is_string; use function is_string;
@ -19,8 +20,8 @@ class RoleResolver implements RoleResolverInterface
public function determineRoles(InputInterface $input): array public function determineRoles(InputInterface $input): array
{ {
$domainAuthority = $input->getOption(self::DOMAIN_ONLY_PARAM); $domainAuthority = $input->getOption(Role::DOMAIN_SPECIFIC->paramName());
$author = $input->getOption(self::AUTHOR_ONLY_PARAM); $author = $input->getOption(Role::AUTHORED_SHORT_URLS->paramName());
$roleDefinitions = []; $roleDefinitions = [];
if ($author) { if ($author) {

View File

@ -9,9 +9,6 @@ use Symfony\Component\Console\Input\InputInterface;
interface RoleResolverInterface interface RoleResolverInterface
{ {
public const AUTHOR_ONLY_PARAM = 'author-only';
public const DOMAIN_ONLY_PARAM = 'domain-only';
/** /**
* @return RoleDefinition[] * @return RoleDefinition[]
*/ */

View File

@ -32,8 +32,8 @@ class GenerateKeyCommand extends Command
protected function configure(): void protected function configure(): void
{ {
$authorOnly = RoleResolverInterface::AUTHOR_ONLY_PARAM; $authorOnly = Role::AUTHORED_SHORT_URLS->paramName();
$domainOnly = RoleResolverInterface::DOMAIN_ONLY_PARAM; $domainOnly = Role::DOMAIN_SPECIFIC->paramName();
$help = <<<HELP $help = <<<HELP
The <info>%command.name%</info> generates a new valid API key. The <info>%command.name%</info> generates a new valid API key.

View File

@ -62,8 +62,8 @@ class ListKeysCommand extends Command
$rowData[] = $apiKey->isAdmin() ? 'Admin' : implode("\n", $apiKey->mapRoles( $rowData[] = $apiKey->isAdmin() ? 'Admin' : implode("\n", $apiKey->mapRoles(
fn (Role $role, array $meta) => fn (Role $role, array $meta) =>
empty($meta) empty($meta)
? Role::toFriendlyName($role) ? $role->toFriendlyName()
: sprintf('%s: %s', Role::toFriendlyName($role), Role::domainAuthorityFromMeta($meta)), : sprintf('%s: %s', $role->toFriendlyName(), Role::domainAuthorityFromMeta($meta)),
)); ));
return $rowData; return $rowData;

View File

@ -4,27 +4,27 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\ApiKey; namespace ShlinkioTest\Shlink\CLI\ApiKey;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\ApiKey\RoleResolver; use Shlinkio\Shlink\CLI\ApiKey\RoleResolver;
use Shlinkio\Shlink\CLI\Exception\InvalidRoleConfigException; use Shlinkio\Shlink\CLI\Exception\InvalidRoleConfigException;
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface; use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Core\Domain\Entity\Domain;
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition; use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
use Shlinkio\Shlink\Rest\ApiKey\Role;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use function Functional\map;
class RoleResolverTest extends TestCase class RoleResolverTest extends TestCase
{ {
use ProphecyTrait;
private RoleResolver $resolver; private RoleResolver $resolver;
private ObjectProphecy $domainService; private MockObject $domainService;
protected function setUp(): void protected function setUp(): void
{ {
$this->domainService = $this->prophesize(DomainServiceInterface::class); $this->domainService = $this->createMock(DomainServiceInterface::class);
$this->resolver = new RoleResolver($this->domainService->reveal(), 'default.com'); $this->resolver = new RoleResolver($this->domainService, 'default.com');
} }
/** /**
@ -36,61 +36,65 @@ class RoleResolverTest extends TestCase
array $expectedRoles, array $expectedRoles,
int $expectedDomainCalls, int $expectedDomainCalls,
): void { ): void {
$getDomain = $this->domainService->getOrCreate('example.com')->willReturn( $this->domainService
Domain::withAuthority('example.com')->setId('1'), ->expects($this->exactly($expectedDomainCalls))
); ->method('getOrCreate')
->with($this->equalTo('example.com'))
->willReturn(Domain::withAuthority('example.com')->setId('1'));
$result = $this->resolver->determineRoles($input); $result = $this->resolver->determineRoles($input);
self::assertEquals($expectedRoles, $result); self::assertEquals($expectedRoles, $result);
$getDomain->shouldHaveBeenCalledTimes($expectedDomainCalls);
} }
public function provideRoles(): iterable public function provideRoles(): iterable
{ {
$domain = Domain::withAuthority('example.com')->setId('1'); $domain = Domain::withAuthority('example.com')->setId('1');
$buildInput = function (array $definition): InputInterface { $buildInput = function (array $definition): InputInterface {
$input = $this->prophesize(InputInterface::class); $input = $this->createStub(InputInterface::class);
$input->method('getOption')->willReturnMap(
map($definition, static fn (mixed $returnValue, string $param) => [$param, $returnValue]),
);
foreach ($definition as $name => $value) { return $input;
$input->getOption($name)->willReturn($value);
}
return $input->reveal();
}; };
yield 'no roles' => [ yield 'no roles' => [
$buildInput([RoleResolver::DOMAIN_ONLY_PARAM => null, RoleResolver::AUTHOR_ONLY_PARAM => false]), $buildInput([Role::DOMAIN_SPECIFIC->paramName() => null, Role::AUTHORED_SHORT_URLS->paramName() => false]),
[], [],
0, 0,
]; ];
yield 'domain role only' => [ yield 'domain role only' => [
$buildInput([RoleResolver::DOMAIN_ONLY_PARAM => 'example.com', RoleResolver::AUTHOR_ONLY_PARAM => false]), $buildInput(
[Role::DOMAIN_SPECIFIC->paramName() => 'example.com', Role::AUTHORED_SHORT_URLS->paramName() => false],
),
[RoleDefinition::forDomain($domain)], [RoleDefinition::forDomain($domain)],
1, 1,
]; ];
yield 'false domain role' => [ yield 'false domain role' => [
$buildInput([RoleResolver::DOMAIN_ONLY_PARAM => false]), $buildInput([Role::DOMAIN_SPECIFIC->paramName() => false]),
[], [],
0, 0,
]; ];
yield 'true domain role' => [ yield 'true domain role' => [
$buildInput([RoleResolver::DOMAIN_ONLY_PARAM => true]), $buildInput([Role::DOMAIN_SPECIFIC->paramName() => true]),
[], [],
0, 0,
]; ];
yield 'string array domain role' => [ yield 'string array domain role' => [
$buildInput([RoleResolver::DOMAIN_ONLY_PARAM => ['foo', 'bar']]), $buildInput([Role::DOMAIN_SPECIFIC->paramName() => ['foo', 'bar']]),
[], [],
0, 0,
]; ];
yield 'author role only' => [ yield 'author role only' => [
$buildInput([RoleResolver::DOMAIN_ONLY_PARAM => null, RoleResolver::AUTHOR_ONLY_PARAM => true]), $buildInput([Role::DOMAIN_SPECIFIC->paramName() => null, Role::AUTHORED_SHORT_URLS->paramName() => true]),
[RoleDefinition::forAuthoredShortUrls()], [RoleDefinition::forAuthoredShortUrls()],
0, 0,
]; ];
yield 'both roles' => [ yield 'both roles' => [
$buildInput([RoleResolver::DOMAIN_ONLY_PARAM => 'example.com', RoleResolver::AUTHOR_ONLY_PARAM => true]), $buildInput(
[Role::DOMAIN_SPECIFIC->paramName() => 'example.com', Role::AUTHORED_SHORT_URLS->paramName() => true],
),
[RoleDefinition::forAuthoredShortUrls(), RoleDefinition::forDomain($domain)], [RoleDefinition::forAuthoredShortUrls(), RoleDefinition::forDomain($domain)],
1, 1,
]; ];
@ -99,12 +103,16 @@ class RoleResolverTest extends TestCase
/** @test */ /** @test */
public function exceptionIsThrownWhenTryingToAddDomainOnlyLinkedToDefaultDomain(): void public function exceptionIsThrownWhenTryingToAddDomainOnlyLinkedToDefaultDomain(): void
{ {
$input = $this->prophesize(InputInterface::class); $input = $this->createStub(InputInterface::class);
$input->getOption(RoleResolver::DOMAIN_ONLY_PARAM)->willReturn('default.com'); $input
$input->getOption(RoleResolver::AUTHOR_ONLY_PARAM)->willReturn(null); ->method('getOption')
->willReturnMap([
[Role::DOMAIN_SPECIFIC->paramName(), 'default.com'],
[Role::AUTHORED_SHORT_URLS->paramName(), null],
]);
$this->expectException(InvalidRoleConfigException::class); $this->expectException(InvalidRoleConfigException::class);
$this->resolver->determineRoles($input->reveal()); $this->resolver->determineRoles($input);
} }
} }

View File

@ -17,6 +17,22 @@ enum Role: string
case AUTHORED_SHORT_URLS = 'AUTHORED_SHORT_URLS'; case AUTHORED_SHORT_URLS = 'AUTHORED_SHORT_URLS';
case DOMAIN_SPECIFIC = 'DOMAIN_SPECIFIC'; case DOMAIN_SPECIFIC = 'DOMAIN_SPECIFIC';
public function toFriendlyName(): string
{
return match ($this) {
self::AUTHORED_SHORT_URLS => 'Author only',
self::DOMAIN_SPECIFIC => 'Domain only',
};
}
public function paramName(): string
{
return match ($this) {
self::AUTHORED_SHORT_URLS => 'author-only',
self::DOMAIN_SPECIFIC => 'domain-only',
};
}
public static function toSpec(ApiKeyRole $role, ?string $context = null): Specification public static function toSpec(ApiKeyRole $role, ?string $context = null): Specification
{ {
return match ($role->role()) { return match ($role->role()) {
@ -42,12 +58,4 @@ enum Role: string
{ {
return $meta['authority'] ?? ''; return $meta['authority'] ?? '';
} }
public static function toFriendlyName(Role $role): string
{
return match ($role) {
self::AUTHORED_SHORT_URLS => 'Author only',
self::DOMAIN_SPECIFIC => 'Domain only',
};
}
} }

View File

@ -99,9 +99,9 @@ class RoleTest extends TestCase
* @test * @test
* @dataProvider provideRoleNames * @dataProvider provideRoleNames
*/ */
public function getsExpectedRoleFriendlyName(Role $roleName, string $expectedFriendlyName): void public function getsExpectedRoleFriendlyName(Role $role, string $expectedFriendlyName): void
{ {
self::assertEquals($expectedFriendlyName, Role::toFriendlyName($roleName)); self::assertEquals($expectedFriendlyName, $role->toFriendlyName());
} }
public function provideRoleNames(): iterable public function provideRoleNames(): iterable