Fixed API tests

This commit is contained in:
Alejandro Celaya 2021-02-26 20:24:57 +01:00
parent 1498b72966
commit e093480a5b
9 changed files with 17 additions and 14 deletions

View File

@ -6,7 +6,6 @@ namespace Shlinkio\Shlink\Core\Repository;
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
use Happyr\DoctrineSpecification\Spec;
use Happyr\DoctrineSpecification\Specification\Specification;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Tag\Model\TagInfo;
use Shlinkio\Shlink\Core\Tag\Spec\CountTagsWithName;
@ -33,7 +32,7 @@ class TagRepository extends EntitySpecificationRepository implements TagReposito
/**
* @return TagInfo[]
*/
public function findTagsWithInfo(?Specification $spec = null): array
public function findTagsWithInfo(?ApiKey $apiKey = null): array
{
$qb = $this->createQueryBuilder('t');
$qb->select('t AS tag', 'COUNT(DISTINCT s.id) AS shortUrlsCount', 'COUNT(DISTINCT v.id) AS visitsCount')
@ -42,7 +41,9 @@ class TagRepository extends EntitySpecificationRepository implements TagReposito
->groupBy('t')
->orderBy('t.name', 'ASC');
$this->applySpecification($qb, $spec, 't');
if ($apiKey !== null) {
$this->applySpecification($qb, $apiKey->spec(false, 'shortUrls'), 't');
}
$query = $qb->getQuery();

View File

@ -17,7 +17,7 @@ interface TagRepositoryInterface extends ObjectRepository, EntitySpecificationRe
/**
* @return TagInfo[]
*/
public function findTagsWithInfo(?Specification $spec = null): array;
public function findTagsWithInfo(?ApiKey $apiKey = null): array;
public function tagExists(string $tag, ?ApiKey $apiKey = null): bool;
}

View File

@ -18,7 +18,7 @@ class BelongsToApiKey extends BaseSpecification
{
$this->apiKey = $apiKey;
$this->dqlAlias = $dqlAlias;
parent::__construct($this->dqlAlias);
parent::__construct();
}
protected function getSpec(): Filter

View File

@ -17,7 +17,7 @@ class BelongsToDomain extends BaseSpecification
{
$this->domainId = $domainId;
$this->dqlAlias = $dqlAlias;
parent::__construct($this->dqlAlias);
parent::__construct();
}
protected function getSpec(): Filter

View File

@ -52,7 +52,7 @@ class TagService implements TagServiceInterface
{
/** @var TagRepositoryInterface $repo */
$repo = $this->em->getRepository(Tag::class);
return $repo->findTagsWithInfo($apiKey !== null ? $apiKey->spec() : null);
return $repo->findTagsWithInfo($apiKey);
}
/**

View File

@ -60,7 +60,7 @@ class TagServiceTest extends TestCase
{
$expected = [new TagInfo(new Tag('foo'), 1, 1), new TagInfo(new Tag('bar'), 3, 10)];
$find = $this->repo->findTagsWithInfo($apiKey === null ? null : $apiKey->spec())->willReturn($expected);
$find = $this->repo->findTagsWithInfo($apiKey)->willReturn($expected);
$result = $this->service->tagsInfo($apiKey);

View File

@ -21,16 +21,18 @@ class Role
self::DOMAIN_SPECIFIC => 'Domain only',
];
public static function toSpec(ApiKeyRole $role, bool $inlined): Specification
public static function toSpec(ApiKeyRole $role, bool $inlined, ?string $context = null): Specification
{
if ($role->name() === self::AUTHORED_SHORT_URLS) {
$apiKey = $role->apiKey();
return $inlined ? Spec::andX(new BelongsToApiKeyInlined($apiKey)) : new BelongsToApiKey($apiKey);
return $inlined ? Spec::andX(new BelongsToApiKeyInlined($apiKey)) : new BelongsToApiKey($apiKey, $context);
}
if ($role->name() === self::DOMAIN_SPECIFIC) {
$domainId = self::domainIdFromMeta($role->meta());
return $inlined ? Spec::andX(new BelongsToDomainInlined($domainId)) : new BelongsToDomain($domainId);
return $inlined
? Spec::andX(new BelongsToDomainInlined($domainId))
: new BelongsToDomain($domainId, $context);
}
return Spec::andX();

View File

@ -25,7 +25,7 @@ class WithApiKeySpecsEnsuringJoin extends BaseSpecification
{
return $this->apiKey === null || $this->apiKey->isAdmin() ? Spec::andX() : Spec::andX(
Spec::join($this->fieldToJoin, 's'),
$this->apiKey->spec(),
$this->apiKey->spec(false, $this->fieldToJoin),
);
}
}

View File

@ -92,9 +92,9 @@ class ApiKey extends AbstractEntity
return $this->key;
}
public function spec(bool $inlined = false): Specification
public function spec(bool $inlined = false, ?string $context = null): Specification
{
$specs = $this->roles->map(fn (ApiKeyRole $role) => Role::toSpec($role, $inlined))->getValues();
$specs = $this->roles->map(fn (ApiKeyRole $role) => Role::toSpec($role, $inlined, $context))->getValues();
return Spec::andX(...$specs);
}