Allow filtering by date in VisitIterationRepository

This commit is contained in:
Alejandro Celaya
2024-04-12 18:29:55 +02:00
parent 13ee71f351
commit ce0f61b66d
6 changed files with 54 additions and 16 deletions

View File

@@ -4,27 +4,29 @@ declare(strict_types=1);
namespace ShlinkioDbTest\Shlink\Core\Visit\Repository;
use Cake\Chronos\Chronos;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\Repository\VisitLocationRepository;
use Shlinkio\Shlink\Core\Visit\Repository\VisitIterationRepository;
use Shlinkio\Shlink\IpGeolocation\Model\Location;
use Shlinkio\Shlink\TestUtils\DbTest\DatabaseTestCase;
use function array_map;
use function range;
class VisitLocationRepositoryTest extends DatabaseTestCase
class VisitIterationRepositoryTest extends DatabaseTestCase
{
private VisitLocationRepository $repo;
private VisitIterationRepository $repo;
protected function setUp(): void
{
$em = $this->getEntityManager();
$this->repo = new VisitLocationRepository($em, $em->getClassMetadata(Visit::class));
$this->repo = new VisitIterationRepository($em, $em->getClassMetadata(Visit::class));
}
#[Test, DataProvider('provideBlockSize')]
@@ -33,7 +35,9 @@ class VisitLocationRepositoryTest extends DatabaseTestCase
$shortUrl = ShortUrl::createFake();
$this->getEntityManager()->persist($shortUrl);
$unmodifiedDate = Chronos::now();
for ($i = 0; $i < 6; $i++) {
Chronos::setTestNow($unmodifiedDate->subDays($i)); // Enforce a different day for every visit
$visit = Visit::forValidShortUrl($shortUrl, Visitor::emptyInstance());
if ($i >= 2) {
@@ -44,15 +48,34 @@ class VisitLocationRepositoryTest extends DatabaseTestCase
$this->getEntityManager()->persist($visit);
}
Chronos::setTestNow();
$this->getEntityManager()->flush();
$withEmptyLocation = $this->repo->findVisitsWithEmptyLocation($blockSize);
$unlocated = $this->repo->findUnlocatedVisits($blockSize);
$all = $this->repo->findAllVisits($blockSize);
$all = $this->repo->findAllVisits(blockSize: $blockSize);
$lastThreeDays = $this->repo->findAllVisits(
dateRange: DateRange::since(Chronos::now()->subDays(2)),
blockSize: $blockSize,
);
$firstTwoDays = $this->repo->findAllVisits(
dateRange: DateRange::until(Chronos::now()->subDays(4)),
blockSize: $blockSize,
);
$daysInBetween = $this->repo->findAllVisits(
dateRange: DateRange::between(
startDate: Chronos::now()->subDays(5),
endDate: Chronos::now()->subDays(2),
),
blockSize: $blockSize,
);
self::assertCount(2, [...$unlocated]);
self::assertCount(4, [...$withEmptyLocation]);
self::assertCount(6, [...$all]);
self::assertCount(3, [...$lastThreeDays]);
self::assertCount(2, [...$firstTwoDays]);
self::assertCount(4, [...$daysInBetween]);
}
public static function provideBlockSize(): iterable