mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-22 08:56:42 -06:00
Fixed API tests
This commit is contained in:
parent
1498b72966
commit
e093480a5b
@ -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();
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ class BelongsToApiKey extends BaseSpecification
|
||||
{
|
||||
$this->apiKey = $apiKey;
|
||||
$this->dqlAlias = $dqlAlias;
|
||||
parent::__construct($this->dqlAlias);
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function getSpec(): Filter
|
||||
|
@ -17,7 +17,7 @@ class BelongsToDomain extends BaseSpecification
|
||||
{
|
||||
$this->domainId = $domainId;
|
||||
$this->dqlAlias = $dqlAlias;
|
||||
parent::__construct($this->dqlAlias);
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function getSpec(): Filter
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user