mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-11 17:44:44 +08:00
Improved VisitsRepositoryTest to cover fetching visits for URL with domain
This commit is contained in:
@@ -6,9 +6,11 @@ namespace ShlinkioTest\Shlink\Core\Repository;
|
|||||||
|
|
||||||
use Cake\Chronos\Chronos;
|
use Cake\Chronos\Chronos;
|
||||||
use Shlinkio\Shlink\Common\Util\DateRange;
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
||||||
|
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||||
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\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\IpGeolocation\Model\Location;
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
||||||
@@ -24,6 +26,7 @@ class VisitRepositoryTest extends DatabaseTestCase
|
|||||||
VisitLocation::class,
|
VisitLocation::class,
|
||||||
Visit::class,
|
Visit::class,
|
||||||
ShortUrl::class,
|
ShortUrl::class,
|
||||||
|
Domain::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
private VisitRepository $repo;
|
private VisitRepository $repo;
|
||||||
@@ -72,48 +75,73 @@ class VisitRepositoryTest extends DatabaseTestCase
|
|||||||
/** @test */
|
/** @test */
|
||||||
public function findVisitsByShortCodeReturnsProperData(): void
|
public function findVisitsByShortCodeReturnsProperData(): void
|
||||||
{
|
{
|
||||||
$shortUrl = new ShortUrl('');
|
[$shortCode, $domain] = $this->createShortUrlsAndVisits();
|
||||||
$this->getEntityManager()->persist($shortUrl);
|
|
||||||
|
|
||||||
for ($i = 0; $i < 6; $i++) {
|
|
||||||
$visit = new Visit($shortUrl, Visitor::emptyInstance(), Chronos::parse(sprintf('2016-01-0%s', $i + 1)));
|
|
||||||
$this->getEntityManager()->persist($visit);
|
|
||||||
}
|
|
||||||
$this->getEntityManager()->flush();
|
|
||||||
|
|
||||||
$this->assertCount(0, $this->repo->findVisitsByShortCode('invalid'));
|
$this->assertCount(0, $this->repo->findVisitsByShortCode('invalid'));
|
||||||
$this->assertCount(6, $this->repo->findVisitsByShortCode($shortUrl->getShortCode()));
|
$this->assertCount(6, $this->repo->findVisitsByShortCode($shortCode));
|
||||||
$this->assertCount(2, $this->repo->findVisitsByShortCode($shortUrl->getShortCode(), null, new DateRange(
|
$this->assertCount(3, $this->repo->findVisitsByShortCode($shortCode, $domain));
|
||||||
|
$this->assertCount(2, $this->repo->findVisitsByShortCode($shortCode, null, new DateRange(
|
||||||
Chronos::parse('2016-01-02'),
|
Chronos::parse('2016-01-02'),
|
||||||
Chronos::parse('2016-01-03'),
|
Chronos::parse('2016-01-03'),
|
||||||
)));
|
)));
|
||||||
$this->assertCount(4, $this->repo->findVisitsByShortCode($shortUrl->getShortCode(), null, new DateRange(
|
$this->assertCount(4, $this->repo->findVisitsByShortCode($shortCode, null, new DateRange(
|
||||||
Chronos::parse('2016-01-03'),
|
Chronos::parse('2016-01-03'),
|
||||||
)));
|
)));
|
||||||
$this->assertCount(3, $this->repo->findVisitsByShortCode($shortUrl->getShortCode(), null, null, 3, 2));
|
$this->assertCount(1, $this->repo->findVisitsByShortCode($shortCode, $domain, new DateRange(
|
||||||
$this->assertCount(2, $this->repo->findVisitsByShortCode($shortUrl->getShortCode(), null, null, 5, 4));
|
Chronos::parse('2016-01-03'),
|
||||||
|
)));
|
||||||
|
$this->assertCount(3, $this->repo->findVisitsByShortCode($shortCode, null, null, 3, 2));
|
||||||
|
$this->assertCount(2, $this->repo->findVisitsByShortCode($shortCode, null, null, 5, 4));
|
||||||
|
$this->assertCount(1, $this->repo->findVisitsByShortCode($shortCode, $domain, null, 3, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function countVisitsByShortCodeReturnsProperData(): void
|
public function countVisitsByShortCodeReturnsProperData(): void
|
||||||
|
{
|
||||||
|
[$shortCode, $domain] = $this->createShortUrlsAndVisits();
|
||||||
|
|
||||||
|
$this->assertEquals(0, $this->repo->countVisitsByShortCode('invalid'));
|
||||||
|
$this->assertEquals(6, $this->repo->countVisitsByShortCode($shortCode));
|
||||||
|
$this->assertEquals(3, $this->repo->countVisitsByShortCode($shortCode, $domain));
|
||||||
|
$this->assertEquals(2, $this->repo->countVisitsByShortCode($shortCode, null, new DateRange(
|
||||||
|
Chronos::parse('2016-01-02'),
|
||||||
|
Chronos::parse('2016-01-03'),
|
||||||
|
)));
|
||||||
|
$this->assertEquals(4, $this->repo->countVisitsByShortCode($shortCode, null, new DateRange(
|
||||||
|
Chronos::parse('2016-01-03'),
|
||||||
|
)));
|
||||||
|
$this->assertEquals(1, $this->repo->countVisitsByShortCode($shortCode, $domain, new DateRange(
|
||||||
|
Chronos::parse('2016-01-03'),
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createShortUrlsAndVisits(): array
|
||||||
{
|
{
|
||||||
$shortUrl = new ShortUrl('');
|
$shortUrl = new ShortUrl('');
|
||||||
|
$domain = 'example.com';
|
||||||
|
$shortCode = $shortUrl->getShortCode();
|
||||||
|
$shortUrlWithDomain = new ShortUrl('', ShortUrlMeta::fromRawData([
|
||||||
|
'customSlug' => $shortCode,
|
||||||
|
'domain' => $domain,
|
||||||
|
]));
|
||||||
|
|
||||||
$this->getEntityManager()->persist($shortUrl);
|
$this->getEntityManager()->persist($shortUrl);
|
||||||
|
$this->getEntityManager()->persist($shortUrlWithDomain);
|
||||||
|
|
||||||
for ($i = 0; $i < 6; $i++) {
|
for ($i = 0; $i < 6; $i++) {
|
||||||
$visit = new Visit($shortUrl, Visitor::emptyInstance(), Chronos::parse(sprintf('2016-01-0%s', $i + 1)));
|
$visit = new Visit($shortUrl, Visitor::emptyInstance(), Chronos::parse(sprintf('2016-01-0%s', $i + 1)));
|
||||||
$this->getEntityManager()->persist($visit);
|
$this->getEntityManager()->persist($visit);
|
||||||
}
|
}
|
||||||
|
for ($i = 0; $i < 3; $i++) {
|
||||||
|
$visit = new Visit(
|
||||||
|
$shortUrlWithDomain,
|
||||||
|
Visitor::emptyInstance(),
|
||||||
|
Chronos::parse(sprintf('2016-01-0%s', $i + 1)),
|
||||||
|
);
|
||||||
|
$this->getEntityManager()->persist($visit);
|
||||||
|
}
|
||||||
$this->getEntityManager()->flush();
|
$this->getEntityManager()->flush();
|
||||||
|
|
||||||
$this->assertEquals(0, $this->repo->countVisitsByShortCode('invalid'));
|
return [$shortCode, $domain];
|
||||||
$this->assertEquals(6, $this->repo->countVisitsByShortCode($shortUrl->getShortCode()));
|
|
||||||
$this->assertEquals(2, $this->repo->countVisitsByShortCode($shortUrl->getShortCode(), null, new DateRange(
|
|
||||||
Chronos::parse('2016-01-02'),
|
|
||||||
Chronos::parse('2016-01-03'),
|
|
||||||
)));
|
|
||||||
$this->assertEquals(4, $this->repo->countVisitsByShortCode($shortUrl->getShortCode(), null, new DateRange(
|
|
||||||
Chronos::parse('2016-01-03'),
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user