mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-24 13:49:03 +03:00
Added db test for VisitRepository::countVisits
This commit is contained in:
parent
01dceca9ef
commit
14eeb91c58
7 changed files with 87 additions and 38 deletions
|
@ -53,9 +53,9 @@ class DomainRepositoryTest extends DatabaseTestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function findDomainsReturnsJustThoseMatchingProvidedApiKey(): void
|
public function findDomainsReturnsJustThoseMatchingProvidedApiKey(): void
|
||||||
{
|
{
|
||||||
$authorApiKey = new ApiKey(null, [RoleDefinition::forAuthoredShortUrls()]);
|
$authorApiKey = ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls());
|
||||||
$this->getEntityManager()->persist($authorApiKey);
|
$this->getEntityManager()->persist($authorApiKey);
|
||||||
$authorAndDomainApiKey = new ApiKey(null, [RoleDefinition::forAuthoredShortUrls()]);
|
$authorAndDomainApiKey = ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls());
|
||||||
$this->getEntityManager()->persist($authorAndDomainApiKey);
|
$this->getEntityManager()->persist($authorAndDomainApiKey);
|
||||||
|
|
||||||
$fooDomain = new Domain('foo.com');
|
$fooDomain = new Domain('foo.com');
|
||||||
|
@ -74,10 +74,10 @@ class DomainRepositoryTest extends DatabaseTestCase
|
||||||
|
|
||||||
$authorAndDomainApiKey->registerRole(RoleDefinition::forDomain($fooDomain->getId()));
|
$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);
|
$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()->persist($fooDomainApiKey);
|
||||||
|
|
||||||
$this->getEntityManager()->flush();
|
$this->getEntityManager()->flush();
|
||||||
|
|
|
@ -331,13 +331,13 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
|
||||||
|
|
||||||
$this->getEntityManager()->flush();
|
$this->getEntityManager()->flush();
|
||||||
|
|
||||||
$apiKey = new ApiKey(null, [RoleDefinition::forAuthoredShortUrls()]);
|
$apiKey = ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls());
|
||||||
$this->getEntityManager()->persist($apiKey);
|
$this->getEntityManager()->persist($apiKey);
|
||||||
$otherApiKey = new ApiKey(null, [RoleDefinition::forAuthoredShortUrls()]);
|
$otherApiKey = ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls());
|
||||||
$this->getEntityManager()->persist($otherApiKey);
|
$this->getEntityManager()->persist($otherApiKey);
|
||||||
$wrongDomainApiKey = new ApiKey(null, [RoleDefinition::forDomain($wrongDomain->getId())]);
|
$wrongDomainApiKey = ApiKey::withRoles(RoleDefinition::forDomain($wrongDomain->getId()));
|
||||||
$this->getEntityManager()->persist($wrongDomainApiKey);
|
$this->getEntityManager()->persist($wrongDomainApiKey);
|
||||||
$rightDomainApiKey = new ApiKey(null, [RoleDefinition::forDomain($rightDomain->getId())]);
|
$rightDomainApiKey = ApiKey::withRoles(RoleDefinition::forDomain($rightDomain->getId()));
|
||||||
$this->getEntityManager()->persist($rightDomainApiKey);
|
$this->getEntityManager()->persist($rightDomainApiKey);
|
||||||
|
|
||||||
$shortUrl = new ShortUrl('foo', ShortUrlMeta::fromRawData(
|
$shortUrl = new ShortUrl('foo', ShortUrlMeta::fromRawData(
|
||||||
|
|
|
@ -112,9 +112,9 @@ class TagRepositoryTest extends DatabaseTestCase
|
||||||
$this->getEntityManager()->persist($domain);
|
$this->getEntityManager()->persist($domain);
|
||||||
$this->getEntityManager()->flush();
|
$this->getEntityManager()->flush();
|
||||||
|
|
||||||
$authorApiKey = new ApiKey(null, [RoleDefinition::forAuthoredShortUrls()]);
|
$authorApiKey = ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls());
|
||||||
$this->getEntityManager()->persist($authorApiKey);
|
$this->getEntityManager()->persist($authorApiKey);
|
||||||
$domainApiKey = new ApiKey(null, [RoleDefinition::forDomain($domain->getId())]);
|
$domainApiKey = ApiKey::withRoles(RoleDefinition::forDomain($domain->getId()));
|
||||||
$this->getEntityManager()->persist($domainApiKey);
|
$this->getEntityManager()->persist($domainApiKey);
|
||||||
|
|
||||||
$names = ['foo', 'bar', 'baz', 'another'];
|
$names = ['foo', 'bar', 'baz', 'another'];
|
||||||
|
|
|
@ -15,7 +15,10 @@ use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
||||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||||
use Shlinkio\Shlink\Core\Model\Visitor;
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
||||||
use Shlinkio\Shlink\Core\Repository\VisitRepository;
|
use Shlinkio\Shlink\Core\Repository\VisitRepository;
|
||||||
|
use Shlinkio\Shlink\Core\ShortUrl\Resolver\PersistenceShortUrlRelationResolver;
|
||||||
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
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 Shlinkio\Shlink\TestUtils\DbTest\DatabaseTestCase;
|
||||||
|
|
||||||
use function Functional\map;
|
use function Functional\map;
|
||||||
|
@ -30,6 +33,7 @@ class VisitRepositoryTest extends DatabaseTestCase
|
||||||
ShortUrl::class,
|
ShortUrl::class,
|
||||||
Domain::class,
|
Domain::class,
|
||||||
Tag::class,
|
Tag::class,
|
||||||
|
ApiKey::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
private VisitRepository $repo;
|
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
|
private function createShortUrlsAndVisits(bool $withDomain = true): array
|
||||||
{
|
{
|
||||||
$shortUrl = new ShortUrl('');
|
$shortUrl = new ShortUrl('');
|
||||||
|
@ -192,7 +239,24 @@ class VisitRepositoryTest extends DatabaseTestCase
|
||||||
$shortCode = $shortUrl->getShortCode();
|
$shortCode = $shortUrl->getShortCode();
|
||||||
$this->getEntityManager()->persist($shortUrl);
|
$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(
|
$visit = new Visit(
|
||||||
$shortUrl,
|
$shortUrl,
|
||||||
Visitor::emptyInstance(),
|
Visitor::emptyInstance(),
|
||||||
|
@ -201,26 +265,5 @@ class VisitRepositoryTest extends DatabaseTestCase
|
||||||
);
|
);
|
||||||
$this->getEntityManager()->persist($visit);
|
$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];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ class DomainServiceTest extends TestCase
|
||||||
{
|
{
|
||||||
$default = new DomainItem('default.com', true);
|
$default = new DomainItem('default.com', true);
|
||||||
$adminApiKey = new ApiKey();
|
$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 'empty list without API key' => [[], [$default], null];
|
||||||
yield 'one item without API key' => [
|
yield 'one item without API key' => [
|
||||||
|
|
|
@ -88,7 +88,7 @@ class TagServiceTest extends TestCase
|
||||||
$this->expectExceptionMessage('You are not allowed to delete tags');
|
$this->expectExceptionMessage('You are not allowed to delete tags');
|
||||||
$delete->shouldNotBeCalled();
|
$delete->shouldNotBeCalled();
|
||||||
|
|
||||||
$this->service->deleteTags(['foo', 'bar'], new ApiKey(null, [RoleDefinition::forAuthoredShortUrls()]));
|
$this->service->deleteTags(['foo', 'bar'], ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
@ -182,7 +182,7 @@ class TagServiceTest extends TestCase
|
||||||
|
|
||||||
$this->service->renameTag(
|
$this->service->renameTag(
|
||||||
TagRenaming::fromNames('foo', 'bar'),
|
TagRenaming::fromNames('foo', 'bar'),
|
||||||
new ApiKey(null, [RoleDefinition::forAuthoredShortUrls()]),
|
ApiKey::withRoles(RoleDefinition::forAuthoredShortUrls()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,19 +24,25 @@ class ApiKey extends AbstractEntity
|
||||||
private Collection $roles;
|
private Collection $roles;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param RoleDefinition[] $roleDefinitions
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct(?Chronos $expirationDate = null, array $roleDefinitions = [])
|
public function __construct(?Chronos $expirationDate = null)
|
||||||
{
|
{
|
||||||
$this->key = Uuid::uuid4()->toString();
|
$this->key = Uuid::uuid4()->toString();
|
||||||
$this->expirationDate = $expirationDate;
|
$this->expirationDate = $expirationDate;
|
||||||
$this->enabled = true;
|
$this->enabled = true;
|
||||||
$this->roles = new ArrayCollection();
|
$this->roles = new ArrayCollection();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function withRoles(RoleDefinition ...$roleDefinitions): self
|
||||||
|
{
|
||||||
|
$apiKey = new self();
|
||||||
|
|
||||||
foreach ($roleDefinitions as $roleDefinition) {
|
foreach ($roleDefinitions as $roleDefinition) {
|
||||||
$this->registerRole($roleDefinition);
|
$apiKey->registerRole($roleDefinition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $apiKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getExpirationDate(): ?Chronos
|
public function getExpirationDate(): ?Chronos
|
||||||
|
|
Loading…
Reference in a new issue