Added more database cases covering different combinations of finding short URL by short code and domain

This commit is contained in:
Alejandro Celaya 2019-10-04 17:34:34 +02:00
parent 49c3c9bec1
commit eced1af21d

View file

@ -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 */