From 14eeb91c5876360e684724065bc3e1ce5dbd5394 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 9 Jan 2021 17:54:04 +0100 Subject: [PATCH] Added db test for VisitRepository::countVisits --- .../Repository/DomainRepositoryTest.php | 8 +- .../Repository/ShortUrlRepositoryTest.php | 8 +- .../test-db/Repository/TagRepositoryTest.php | 4 +- .../Repository/VisitRepositoryTest.php | 87 ++++++++++++++----- module/Core/test/Domain/DomainServiceTest.php | 2 +- .../Core/test/Service/Tag/TagServiceTest.php | 4 +- module/Rest/src/Entity/ApiKey.php | 12 ++- 7 files changed, 87 insertions(+), 38 deletions(-) diff --git a/module/Core/test-db/Domain/Repository/DomainRepositoryTest.php b/module/Core/test-db/Domain/Repository/DomainRepositoryTest.php index d377e326..74d5297e 100644 --- a/module/Core/test-db/Domain/Repository/DomainRepositoryTest.php +++ b/module/Core/test-db/Domain/Repository/DomainRepositoryTest.php @@ -53,9 +53,9 @@ class DomainRepositoryTest extends DatabaseTestCase /** @test */ public function findDomainsReturnsJustThoseMatchingProvidedApiKey(): void { - $authorApiKey = new ApiKey(null, [RoleDefinition::forAuthoredShortUrls()]); + $authorApiKey = ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls()); $this->getEntityManager()->persist($authorApiKey); - $authorAndDomainApiKey = new ApiKey(null, [RoleDefinition::forAuthoredShortUrls()]); + $authorAndDomainApiKey = ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls()); $this->getEntityManager()->persist($authorAndDomainApiKey); $fooDomain = new Domain('foo.com'); @@ -74,10 +74,10 @@ class DomainRepositoryTest extends DatabaseTestCase $authorAndDomainApiKey->registerRole(RoleDefinition::forDomain($fooDomain->getId())); - $fooDomainApiKey = new ApiKey(null, [RoleDefinition::forDomain($fooDomain->getId())]); + $fooDomainApiKey = ApiKey::withRoles(RoleDefinition::forDomain($fooDomain->getId())); $this->getEntityManager()->persist($fooDomainApiKey); - $barDomainApiKey = new ApiKey(null, [RoleDefinition::forDomain($barDomain->getId())]); + $barDomainApiKey = ApiKey::withRoles(RoleDefinition::forDomain($barDomain->getId())); $this->getEntityManager()->persist($fooDomainApiKey); $this->getEntityManager()->flush(); diff --git a/module/Core/test-db/Repository/ShortUrlRepositoryTest.php b/module/Core/test-db/Repository/ShortUrlRepositoryTest.php index e0fa225a..a95308ff 100644 --- a/module/Core/test-db/Repository/ShortUrlRepositoryTest.php +++ b/module/Core/test-db/Repository/ShortUrlRepositoryTest.php @@ -331,13 +331,13 @@ class ShortUrlRepositoryTest extends DatabaseTestCase $this->getEntityManager()->flush(); - $apiKey = new ApiKey(null, [RoleDefinition::forAuthoredShortUrls()]); + $apiKey = ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls()); $this->getEntityManager()->persist($apiKey); - $otherApiKey = new ApiKey(null, [RoleDefinition::forAuthoredShortUrls()]); + $otherApiKey = ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls()); $this->getEntityManager()->persist($otherApiKey); - $wrongDomainApiKey = new ApiKey(null, [RoleDefinition::forDomain($wrongDomain->getId())]); + $wrongDomainApiKey = ApiKey::withRoles(RoleDefinition::forDomain($wrongDomain->getId())); $this->getEntityManager()->persist($wrongDomainApiKey); - $rightDomainApiKey = new ApiKey(null, [RoleDefinition::forDomain($rightDomain->getId())]); + $rightDomainApiKey = ApiKey::withRoles(RoleDefinition::forDomain($rightDomain->getId())); $this->getEntityManager()->persist($rightDomainApiKey); $shortUrl = new ShortUrl('foo', ShortUrlMeta::fromRawData( diff --git a/module/Core/test-db/Repository/TagRepositoryTest.php b/module/Core/test-db/Repository/TagRepositoryTest.php index d066d8a8..e28b38fb 100644 --- a/module/Core/test-db/Repository/TagRepositoryTest.php +++ b/module/Core/test-db/Repository/TagRepositoryTest.php @@ -112,9 +112,9 @@ class TagRepositoryTest extends DatabaseTestCase $this->getEntityManager()->persist($domain); $this->getEntityManager()->flush(); - $authorApiKey = new ApiKey(null, [RoleDefinition::forAuthoredShortUrls()]); + $authorApiKey = ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls()); $this->getEntityManager()->persist($authorApiKey); - $domainApiKey = new ApiKey(null, [RoleDefinition::forDomain($domain->getId())]); + $domainApiKey = ApiKey::withRoles(RoleDefinition::forDomain($domain->getId())); $this->getEntityManager()->persist($domainApiKey); $names = ['foo', 'bar', 'baz', 'another']; diff --git a/module/Core/test-db/Repository/VisitRepositoryTest.php b/module/Core/test-db/Repository/VisitRepositoryTest.php index f6df4b9b..516b1dd3 100644 --- a/module/Core/test-db/Repository/VisitRepositoryTest.php +++ b/module/Core/test-db/Repository/VisitRepositoryTest.php @@ -15,7 +15,10 @@ use Shlinkio\Shlink\Core\Entity\VisitLocation; use Shlinkio\Shlink\Core\Model\ShortUrlMeta; use Shlinkio\Shlink\Core\Model\Visitor; use Shlinkio\Shlink\Core\Repository\VisitRepository; +use Shlinkio\Shlink\Core\ShortUrl\Resolver\PersistenceShortUrlRelationResolver; use Shlinkio\Shlink\IpGeolocation\Model\Location; +use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition; +use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\TestUtils\DbTest\DatabaseTestCase; use function Functional\map; @@ -30,6 +33,7 @@ class VisitRepositoryTest extends DatabaseTestCase ShortUrl::class, Domain::class, Tag::class, + ApiKey::class, ]; private VisitRepository $repo; @@ -185,6 +189,49 @@ class VisitRepositoryTest extends DatabaseTestCase ))); } + /** @test */ + public function countReturnsExpectedResultBasedOnApiKey(): void + { + $domain = new Domain('foo.com'); + $this->getEntityManager()->persist($domain); + + $this->getEntityManager()->flush(); + + $apiKey1 = ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls()); + $this->getEntityManager()->persist($apiKey1); + $shortUrl = new ShortUrl( + '', + ShortUrlMeta::fromRawData(['apiKey' => $apiKey1, 'domain' => $domain->getAuthority()]), + new PersistenceShortUrlRelationResolver($this->getEntityManager()), + ); + $this->getEntityManager()->persist($shortUrl); + $this->createVisitsForShortUrl($shortUrl, 4); + + $apiKey2 = ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls()); + $this->getEntityManager()->persist($apiKey2); + $shortUrl2 = new ShortUrl('', ShortUrlMeta::fromRawData(['apiKey' => $apiKey2])); + $this->getEntityManager()->persist($shortUrl2); + $this->createVisitsForShortUrl($shortUrl2, 5); + + $shortUrl3 = new ShortUrl( + '', + ShortUrlMeta::fromRawData(['apiKey' => $apiKey2, 'domain' => $domain->getAuthority()]), + new PersistenceShortUrlRelationResolver($this->getEntityManager()), + ); + $this->getEntityManager()->persist($shortUrl3); + $this->createVisitsForShortUrl($shortUrl3, 7); + + $domainApiKey = ApiKey::withRoles(RoleDefinition::forDomain($domain->getId())); + $this->getEntityManager()->persist($domainApiKey); + + $this->getEntityManager()->flush(); + + self::assertEquals(4 + 5 + 7, $this->repo->countVisits()); + self::assertEquals(4, $this->repo->countVisits($apiKey1)); + self::assertEquals(5 + 7, $this->repo->countVisits($apiKey2)); + self::assertEquals(4 + 7, $this->repo->countVisits($domainApiKey)); + } + private function createShortUrlsAndVisits(bool $withDomain = true): array { $shortUrl = new ShortUrl(''); @@ -192,7 +239,24 @@ class VisitRepositoryTest extends DatabaseTestCase $shortCode = $shortUrl->getShortCode(); $this->getEntityManager()->persist($shortUrl); - for ($i = 0; $i < 6; $i++) { + $this->createVisitsForShortUrl($shortUrl); + + if ($withDomain) { + $shortUrlWithDomain = new ShortUrl('', ShortUrlMeta::fromRawData([ + 'customSlug' => $shortCode, + 'domain' => $domain, + ])); + $this->getEntityManager()->persist($shortUrlWithDomain); + $this->createVisitsForShortUrl($shortUrlWithDomain, 3); + $this->getEntityManager()->flush(); + } + + return [$shortCode, $domain, $shortUrl]; + } + + private function createVisitsForShortUrl(ShortUrl $shortUrl, int $amount = 6): void + { + for ($i = 0; $i < $amount; $i++) { $visit = new Visit( $shortUrl, Visitor::emptyInstance(), @@ -201,26 +265,5 @@ class VisitRepositoryTest extends DatabaseTestCase ); $this->getEntityManager()->persist($visit); } - - if ($withDomain) { - $shortUrlWithDomain = new ShortUrl('', ShortUrlMeta::fromRawData([ - 'customSlug' => $shortCode, - 'domain' => $domain, - ])); - $this->getEntityManager()->persist($shortUrlWithDomain); - - for ($i = 0; $i < 3; $i++) { - $visit = new Visit( - $shortUrlWithDomain, - Visitor::emptyInstance(), - true, - Chronos::parse(sprintf('2016-01-0%s', $i + 1)), - ); - $this->getEntityManager()->persist($visit); - } - $this->getEntityManager()->flush(); - } - - return [$shortCode, $domain, $shortUrl]; } } diff --git a/module/Core/test/Domain/DomainServiceTest.php b/module/Core/test/Domain/DomainServiceTest.php index 5fc4aaaa..7c21014c 100644 --- a/module/Core/test/Domain/DomainServiceTest.php +++ b/module/Core/test/Domain/DomainServiceTest.php @@ -50,7 +50,7 @@ class DomainServiceTest extends TestCase { $default = new DomainItem('default.com', true); $adminApiKey = new ApiKey(); - $domainSpecificApiKey = new ApiKey(null, [RoleDefinition::forDomain('123')]); + $domainSpecificApiKey = ApiKey::withRoles(RoleDefinition::forDomain('123')); yield 'empty list without API key' => [[], [$default], null]; yield 'one item without API key' => [ diff --git a/module/Core/test/Service/Tag/TagServiceTest.php b/module/Core/test/Service/Tag/TagServiceTest.php index c0ef8760..f1965439 100644 --- a/module/Core/test/Service/Tag/TagServiceTest.php +++ b/module/Core/test/Service/Tag/TagServiceTest.php @@ -88,7 +88,7 @@ class TagServiceTest extends TestCase $this->expectExceptionMessage('You are not allowed to delete tags'); $delete->shouldNotBeCalled(); - $this->service->deleteTags(['foo', 'bar'], new ApiKey(null, [RoleDefinition::forAuthoredShortUrls()])); + $this->service->deleteTags(['foo', 'bar'], ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls())); } /** @test */ @@ -182,7 +182,7 @@ class TagServiceTest extends TestCase $this->service->renameTag( TagRenaming::fromNames('foo', 'bar'), - new ApiKey(null, [RoleDefinition::forAuthoredShortUrls()]), + ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls()), ); } } diff --git a/module/Rest/src/Entity/ApiKey.php b/module/Rest/src/Entity/ApiKey.php index 937e42f0..f91a9732 100644 --- a/module/Rest/src/Entity/ApiKey.php +++ b/module/Rest/src/Entity/ApiKey.php @@ -24,19 +24,25 @@ class ApiKey extends AbstractEntity private Collection $roles; /** - * @param RoleDefinition[] $roleDefinitions * @throws Exception */ - public function __construct(?Chronos $expirationDate = null, array $roleDefinitions = []) + public function __construct(?Chronos $expirationDate = null) { $this->key = Uuid::uuid4()->toString(); $this->expirationDate = $expirationDate; $this->enabled = true; $this->roles = new ArrayCollection(); + } + + public static function withRoles(RoleDefinition ...$roleDefinitions): self + { + $apiKey = new self(); foreach ($roleDefinitions as $roleDefinition) { - $this->registerRole($roleDefinition); + $apiKey->registerRole($roleDefinition); } + + return $apiKey; } public function getExpirationDate(): ?Chronos