diff --git a/module/Core/test-db/Visit/Repository/VisitDeleterRepositoryTest.php b/module/Core/test-db/Visit/Repository/VisitDeleterRepositoryTest.php new file mode 100644 index 00000000..1598fc94 --- /dev/null +++ b/module/Core/test-db/Visit/Repository/VisitDeleterRepositoryTest.php @@ -0,0 +1,63 @@ +getEntityManager(); + $this->repo = new VisitDeleterRepository($em, $em->getClassMetadata(Visit::class)); + } + + #[Test] + public function deletesExpectedVisits(): void + { + $shortUrl1 = ShortUrl::withLongUrl('https://foo.com'); + $this->getEntityManager()->persist($shortUrl1); + $this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl1, Visitor::emptyInstance())); + $this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl1, Visitor::emptyInstance())); + + $shortUrl2 = ShortUrl::create(ShortUrlCreation::fromRawData([ + ShortUrlInputFilter::LONG_URL => 'https://foo.com', + ShortUrlInputFilter::DOMAIN => 's.test', + ShortUrlInputFilter::CUSTOM_SLUG => 'foo', + ]), new PersistenceShortUrlRelationResolver($this->getEntityManager())); + $this->getEntityManager()->persist($shortUrl2); + $this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl2, Visitor::emptyInstance())); + $this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl2, Visitor::emptyInstance())); + $this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl2, Visitor::emptyInstance())); + $this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl2, Visitor::emptyInstance())); + + $shortUrl3 = ShortUrl::create(ShortUrlCreation::fromRawData([ + ShortUrlInputFilter::LONG_URL => 'https://foo.com', + ShortUrlInputFilter::CUSTOM_SLUG => 'foo', + ]), new PersistenceShortUrlRelationResolver($this->getEntityManager())); + $this->getEntityManager()->persist($shortUrl3); + $this->getEntityManager()->persist(Visit::forValidShortUrl($shortUrl3, Visitor::emptyInstance())); + + $this->getEntityManager()->flush(); + + self::assertEquals(0, $this->repo->deleteShortUrlVisits(ShortUrl::withLongUrl('https://invalid')->setId('99'))); + self::assertEquals(2, $this->repo->deleteShortUrlVisits($shortUrl1)); + self::assertEquals(0, $this->repo->deleteShortUrlVisits($shortUrl1)); + self::assertEquals(4, $this->repo->deleteShortUrlVisits($shortUrl2)); + self::assertEquals(0, $this->repo->deleteShortUrlVisits($shortUrl2)); + self::assertEquals(1, $this->repo->deleteShortUrlVisits($shortUrl3)); + self::assertEquals(0, $this->repo->deleteShortUrlVisits($shortUrl3)); + } +}