diff --git a/module/Core/test-db/Repository/ShortUrlRepositoryTest.php b/module/Core/test-db/Repository/ShortUrlRepositoryTest.php index 35d647e7..2f7e27b6 100644 --- a/module/Core/test-db/Repository/ShortUrlRepositoryTest.php +++ b/module/Core/test-db/Repository/ShortUrlRepositoryTest.php @@ -36,31 +36,58 @@ class ShortUrlRepositoryTest extends DatabaseTestCase /** @test */ public function findOneByShortCodeReturnsProperData(): void { - $foo = new ShortUrl('foo'); - $foo->setShortCode('foo'); - $this->getEntityManager()->persist($foo); + $regularOne = new ShortUrl('foo'); + $regularOne->setShortCode('foo'); + $this->getEntityManager()->persist($regularOne); - $bar = new ShortUrl('bar', ShortUrlMeta::createFromParams(Chronos::now()->addMonth())); - $bar->setShortCode('bar_very_long_text'); - $this->getEntityManager()->persist($bar); + $notYetValid = new ShortUrl('bar', ShortUrlMeta::createFromParams(Chronos::now()->addMonth())); + $notYetValid->setShortCode('bar_very_long_text'); + $this->getEntityManager()->persist($notYetValid); - $baz = new ShortUrl('baz', ShortUrlMeta::createFromRawData(['maxVisits' => 3])); + $expired = new ShortUrl('expired', ShortUrlMeta::createFromParams(null, Chronos::now()->subMonth())); + $expired->setShortCode('expired'); + $this->getEntityManager()->persist($expired); + + $allVisitsComplete = new ShortUrl('baz', ShortUrlMeta::createFromRawData(['maxVisits' => 3])); $visits = []; for ($i = 0; $i < 3; $i++) { - $visit = new Visit($baz, Visitor::emptyInstance()); + $visit = new Visit($allVisitsComplete, Visitor::emptyInstance()); $this->getEntityManager()->persist($visit); $visits[] = $visit; } - $baz->setShortCode('baz') - ->setVisits(new ArrayCollection($visits)); - $this->getEntityManager()->persist($baz); + $allVisitsComplete->setShortCode('baz') + ->setVisits(new ArrayCollection($visits)); + $this->getEntityManager()->persist($allVisitsComplete); + + $withDomain = new ShortUrl('foo', ShortUrlMeta::createFromRawData(['domain' => 'example.com'])); + $withDomain->setShortCode('domain-short-code'); + $this->getEntityManager()->persist($withDomain); + + $withDomainDuplicatingRegular = new ShortUrl('foo', ShortUrlMeta::createFromRawData([ + 'domain' => 'doma.in', + ])); + $withDomainDuplicatingRegular->setShortCode('foo'); + $this->getEntityManager()->persist($withDomainDuplicatingRegular); $this->getEntityManager()->flush(); - $this->assertSame($foo, $this->repo->findOneByShortCode($foo->getShortCode())); + $this->assertSame($regularOne, $this->repo->findOneByShortCode($regularOne->getShortCode())); + $this->assertSame($regularOne, $this->repo->findOneByShortCode($withDomainDuplicatingRegular->getShortCode())); + $this->assertSame($withDomain, $this->repo->findOneByShortCode($withDomain->getShortCode(), 'example.com')); + $this->assertSame( + $withDomainDuplicatingRegular, + $this->repo->findOneByShortCode($withDomainDuplicatingRegular->getShortCode(), 'doma.in') + ); + $this->assertSame( + $regularOne, + $this->repo->findOneByShortCode($withDomainDuplicatingRegular->getShortCode(), 'other-domain.com') + ); $this->assertNull($this->repo->findOneByShortCode('invalid')); - $this->assertNull($this->repo->findOneByShortCode($bar->getShortCode())); - $this->assertNull($this->repo->findOneByShortCode($baz->getShortCode())); + $this->assertNull($this->repo->findOneByShortCode($withDomain->getShortCode())); + $this->assertNull($this->repo->findOneByShortCode($withDomain->getShortCode(), 'other-domain.com')); + $this->assertNull($this->repo->findOneByShortCode($notYetValid->getShortCode())); + $this->assertNull($this->repo->findOneByShortCode($expired->getShortCode())); + $this->assertNull($this->repo->findOneByShortCode($allVisitsComplete->getShortCode())); } /** @test */