Created method in VisitRepository to fetch all non-orphan visits

This commit is contained in:
Alejandro Celaya
2022-01-10 21:43:32 +01:00
parent db4ef328b1
commit 7f4ada9c4b
4 changed files with 86 additions and 25 deletions

View File

@@ -29,6 +29,9 @@ use function Functional\map;
use function is_string;
use function range;
use function sprintf;
use function str_pad;
use const STR_PAD_LEFT;
class VisitRepositoryTest extends DatabaseTestCase
{
@@ -388,6 +391,49 @@ class VisitRepositoryTest extends DatabaseTestCase
));
}
/** @test */
public function findNonOrphanVisitsReturnsExpectedResult(): void
{
$shortUrl = ShortUrl::fromMeta(ShortUrlMeta::fromRawData(['longUrl' => '1']));
$this->getEntityManager()->persist($shortUrl);
$this->createVisitsForShortUrl($shortUrl, 7);
$shortUrl2 = ShortUrl::fromMeta(ShortUrlMeta::fromRawData(['longUrl' => '2']));
$this->getEntityManager()->persist($shortUrl2);
$this->createVisitsForShortUrl($shortUrl2, 4);
$shortUrl3 = ShortUrl::fromMeta(ShortUrlMeta::fromRawData(['longUrl' => '3']));
$this->getEntityManager()->persist($shortUrl3);
$this->createVisitsForShortUrl($shortUrl3, 10);
$this->getEntityManager()->flush();
self::assertCount(21, $this->repo->findNonOrphanVisits(new VisitsListFiltering()));
self::assertCount(21, $this->repo->findNonOrphanVisits(new VisitsListFiltering(DateRange::emptyInstance())));
self::assertCount(7, $this->repo->findNonOrphanVisits(new VisitsListFiltering(DateRange::withStartDate(
Chronos::parse('2016-01-05')->endOfDay(),
))));
self::assertCount(12, $this->repo->findNonOrphanVisits(new VisitsListFiltering(DateRange::withEndDate(
Chronos::parse('2016-01-04')->endOfDay(),
))));
self::assertCount(6, $this->repo->findNonOrphanVisits(new VisitsListFiltering(DateRange::withStartAndEndDate(
Chronos::parse('2016-01-03')->startOfDay(),
Chronos::parse('2016-01-04')->endOfDay(),
))));
self::assertCount(13, $this->repo->findNonOrphanVisits(new VisitsListFiltering(DateRange::withStartAndEndDate(
Chronos::parse('2016-01-03')->startOfDay(),
Chronos::parse('2016-01-08')->endOfDay(),
))));
self::assertCount(3, $this->repo->findNonOrphanVisits(new VisitsListFiltering(DateRange::withStartAndEndDate(
Chronos::parse('2016-01-03')->startOfDay(),
Chronos::parse('2016-01-08')->endOfDay(),
), false, null, 10, 10)));
self::assertCount(15, $this->repo->findNonOrphanVisits(new VisitsListFiltering(null, true)));
self::assertCount(10, $this->repo->findNonOrphanVisits(new VisitsListFiltering(null, false, null, 10)));
self::assertCount(1, $this->repo->findNonOrphanVisits(new VisitsListFiltering(null, false, null, 10, 20)));
self::assertCount(5, $this->repo->findNonOrphanVisits(new VisitsListFiltering(null, false, null, 5, 5)));
}
/**
* @return array{string, string, ShortUrl}
*/
@@ -429,7 +475,7 @@ class VisitRepositoryTest extends DatabaseTestCase
$shortUrl,
$botsAmount < 1 ? Visitor::emptyInstance() : Visitor::botInstance(),
),
Chronos::parse(sprintf('2016-01-0%s', $i + 1)),
Chronos::parse(sprintf('2016-01-%s', str_pad((string) ($i + 1), 2, '0', STR_PAD_LEFT)))->startOfDay(),
);
$botsAmount--;