mirror of
https://github.com/shlinkio/shlink.git
synced 2025-01-03 12:46:59 -06:00
Improved VisitRepository tests
This commit is contained in:
parent
15061d3e0d
commit
55e7f7ccb0
@ -7,13 +7,13 @@ namespace Shlinkio\Shlink\Core\Repository;
|
|||||||
use Doctrine\ORM\Query\ResultSetMappingBuilder;
|
use Doctrine\ORM\Query\ResultSetMappingBuilder;
|
||||||
use Doctrine\ORM\QueryBuilder;
|
use Doctrine\ORM\QueryBuilder;
|
||||||
use Happyr\DoctrineSpecification\EntitySpecificationRepository;
|
use Happyr\DoctrineSpecification\EntitySpecificationRepository;
|
||||||
use Happyr\DoctrineSpecification\Spec;
|
|
||||||
use Happyr\DoctrineSpecification\Specification\Specification;
|
use Happyr\DoctrineSpecification\Specification\Specification;
|
||||||
use Shlinkio\Shlink\Common\Util\DateRange;
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
||||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\Entity\Visit;
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
||||||
use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
||||||
use Shlinkio\Shlink\Rest\ApiKey\Spec\WithApiKeySpecsEnsuringJoin;
|
use Shlinkio\Shlink\Core\Visit\Spec\CountOfOrphanVisits;
|
||||||
|
use Shlinkio\Shlink\Core\Visit\Spec\CountOfShortUrlVisits;
|
||||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||||
|
|
||||||
use const PHP_INT_MAX;
|
use const PHP_INT_MAX;
|
||||||
@ -211,9 +211,11 @@ class VisitRepository extends EntitySpecificationRepository implements VisitRepo
|
|||||||
|
|
||||||
public function countVisits(?ApiKey $apiKey = null): int
|
public function countVisits(?ApiKey $apiKey = null): int
|
||||||
{
|
{
|
||||||
return (int) $this->matchSingleScalarResult(Spec::countOf(Spec::andX(
|
return (int) $this->matchSingleScalarResult(new CountOfShortUrlVisits($apiKey));
|
||||||
Spec::isNotNull('shortUrl'),
|
}
|
||||||
new WithApiKeySpecsEnsuringJoin($apiKey, 'shortUrl'),
|
|
||||||
)));
|
public function countOrphanVisits(): int
|
||||||
|
{
|
||||||
|
return (int) $this->matchSingleScalarResult(new CountOfOrphanVisits());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,4 +63,6 @@ interface VisitRepositoryInterface extends ObjectRepository, EntitySpecification
|
|||||||
public function countVisitsByTag(string $tag, ?DateRange $dateRange = null, ?Specification $spec = null): int;
|
public function countVisitsByTag(string $tag, ?DateRange $dateRange = null, ?Specification $spec = null): int;
|
||||||
|
|
||||||
public function countVisits(?ApiKey $apiKey = null): int;
|
public function countVisits(?ApiKey $apiKey = null): int;
|
||||||
|
|
||||||
|
public function countOrphanVisits(): int;
|
||||||
}
|
}
|
||||||
|
17
module/Core/src/Visit/Spec/CountOfOrphanVisits.php
Normal file
17
module/Core/src/Visit/Spec/CountOfOrphanVisits.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Core\Visit\Spec;
|
||||||
|
|
||||||
|
use Happyr\DoctrineSpecification\BaseSpecification;
|
||||||
|
use Happyr\DoctrineSpecification\Spec;
|
||||||
|
use Happyr\DoctrineSpecification\Specification\Specification;
|
||||||
|
|
||||||
|
class CountOfOrphanVisits extends BaseSpecification
|
||||||
|
{
|
||||||
|
protected function getSpec(): Specification
|
||||||
|
{
|
||||||
|
return Spec::countOf(Spec::isNull('shortUrl'));
|
||||||
|
}
|
||||||
|
}
|
30
module/Core/src/Visit/Spec/CountOfShortUrlVisits.php
Normal file
30
module/Core/src/Visit/Spec/CountOfShortUrlVisits.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Core\Visit\Spec;
|
||||||
|
|
||||||
|
use Happyr\DoctrineSpecification\BaseSpecification;
|
||||||
|
use Happyr\DoctrineSpecification\Spec;
|
||||||
|
use Happyr\DoctrineSpecification\Specification\Specification;
|
||||||
|
use Shlinkio\Shlink\Rest\ApiKey\Spec\WithApiKeySpecsEnsuringJoin;
|
||||||
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||||
|
|
||||||
|
class CountOfShortUrlVisits extends BaseSpecification
|
||||||
|
{
|
||||||
|
private ?ApiKey $apiKey;
|
||||||
|
|
||||||
|
public function __construct(?ApiKey $apiKey)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
$this->apiKey = $apiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getSpec(): Specification
|
||||||
|
{
|
||||||
|
return Spec::countOf(Spec::andX(
|
||||||
|
Spec::isNotNull('shortUrl'),
|
||||||
|
new WithApiKeySpecsEnsuringJoin($this->apiKey, 'shortUrl'),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
@ -168,7 +168,7 @@ class VisitRepositoryTest extends DatabaseTestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function countReturnsExpectedResultBasedOnApiKey(): void
|
public function countVisitsReturnsExpectedResultBasedOnApiKey(): void
|
||||||
{
|
{
|
||||||
$domain = new Domain('foo.com');
|
$domain = new Domain('foo.com');
|
||||||
$this->getEntityManager()->persist($domain);
|
$this->getEntityManager()->persist($domain);
|
||||||
@ -200,12 +200,18 @@ class VisitRepositoryTest extends DatabaseTestCase
|
|||||||
$domainApiKey = ApiKey::withRoles(RoleDefinition::forDomain($domain));
|
$domainApiKey = ApiKey::withRoles(RoleDefinition::forDomain($domain));
|
||||||
$this->getEntityManager()->persist($domainApiKey);
|
$this->getEntityManager()->persist($domainApiKey);
|
||||||
|
|
||||||
|
// Visits not linked to any short URL
|
||||||
|
$this->getEntityManager()->persist(Visit::forBasePath(Visitor::emptyInstance()));
|
||||||
|
$this->getEntityManager()->persist(Visit::forInvalidShortUrl(Visitor::emptyInstance()));
|
||||||
|
$this->getEntityManager()->persist(Visit::forRegularNotFound(Visitor::emptyInstance()));
|
||||||
|
|
||||||
$this->getEntityManager()->flush();
|
$this->getEntityManager()->flush();
|
||||||
|
|
||||||
self::assertEquals(4 + 5 + 7, $this->repo->countVisits());
|
self::assertEquals(4 + 5 + 7, $this->repo->countVisits());
|
||||||
self::assertEquals(4, $this->repo->countVisits($apiKey1));
|
self::assertEquals(4, $this->repo->countVisits($apiKey1));
|
||||||
self::assertEquals(5 + 7, $this->repo->countVisits($apiKey2));
|
self::assertEquals(5 + 7, $this->repo->countVisits($apiKey2));
|
||||||
self::assertEquals(4 + 7, $this->repo->countVisits($domainApiKey));
|
self::assertEquals(4 + 7, $this->repo->countVisits($domainApiKey));
|
||||||
|
self::assertEquals(3, $this->repo->countOrphanVisits());
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createShortUrlsAndVisits(bool $withDomain = true, array $tags = []): array
|
private function createShortUrlsAndVisits(bool $withDomain = true, array $tags = []): array
|
||||||
|
Loading…
Reference in New Issue
Block a user