mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-12 01:54:41 +08:00
Allow filtering by date in VisitIterationRepository
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user