From a3ff545d4354db0747d5cdf998ea4c1dcaee043b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 1 Feb 2020 17:44:37 +0100 Subject: [PATCH] Improved VisitsRepositoryTest to cover fetching visits for URL with domain --- .../Repository/VisitRepositoryTest.php | 72 +++++++++++++------ 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/module/Core/test-db/Repository/VisitRepositoryTest.php b/module/Core/test-db/Repository/VisitRepositoryTest.php index d4a757e1..bd1a0f2d 100644 --- a/module/Core/test-db/Repository/VisitRepositoryTest.php +++ b/module/Core/test-db/Repository/VisitRepositoryTest.php @@ -6,9 +6,11 @@ namespace ShlinkioTest\Shlink\Core\Repository; use Cake\Chronos\Chronos; use Shlinkio\Shlink\Common\Util\DateRange; +use Shlinkio\Shlink\Core\Entity\Domain; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Visit; 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\IpGeolocation\Model\Location; @@ -24,6 +26,7 @@ class VisitRepositoryTest extends DatabaseTestCase VisitLocation::class, Visit::class, ShortUrl::class, + Domain::class, ]; private VisitRepository $repo; @@ -72,48 +75,73 @@ class VisitRepositoryTest extends DatabaseTestCase /** @test */ public function findVisitsByShortCodeReturnsProperData(): void { - $shortUrl = new ShortUrl(''); - $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(); + [$shortCode, $domain] = $this->createShortUrlsAndVisits(); $this->assertCount(0, $this->repo->findVisitsByShortCode('invalid')); - $this->assertCount(6, $this->repo->findVisitsByShortCode($shortUrl->getShortCode())); - $this->assertCount(2, $this->repo->findVisitsByShortCode($shortUrl->getShortCode(), null, new DateRange( + $this->assertCount(6, $this->repo->findVisitsByShortCode($shortCode)); + $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-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'), ))); - $this->assertCount(3, $this->repo->findVisitsByShortCode($shortUrl->getShortCode(), null, null, 3, 2)); - $this->assertCount(2, $this->repo->findVisitsByShortCode($shortUrl->getShortCode(), null, null, 5, 4)); + $this->assertCount(1, $this->repo->findVisitsByShortCode($shortCode, $domain, new DateRange( + 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 */ 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(''); + $domain = 'example.com'; + $shortCode = $shortUrl->getShortCode(); + $shortUrlWithDomain = new ShortUrl('', ShortUrlMeta::fromRawData([ + 'customSlug' => $shortCode, + 'domain' => $domain, + ])); + $this->getEntityManager()->persist($shortUrl); + $this->getEntityManager()->persist($shortUrlWithDomain); 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); } + 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->assertEquals(0, $this->repo->countVisitsByShortCode('invalid')); - $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'), - ))); + return [$shortCode, $domain]; } }