From 501a933d2e58cbb6d3631f872e4814c91944434d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 23 Oct 2017 13:03:23 +0200 Subject: [PATCH] Created ShortUrlRepositoryTest --- module/Core/src/Entity/ShortUrl.php | 11 ++++ .../Repository/ShortUrlRepositoryTest.php | 65 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 module/Core/test-func/Repository/ShortUrlRepositoryTest.php diff --git a/module/Core/src/Entity/ShortUrl.php b/module/Core/src/Entity/ShortUrl.php index 3039bd19..b1ffe60f 100644 --- a/module/Core/src/Entity/ShortUrl.php +++ b/module/Core/src/Entity/ShortUrl.php @@ -204,6 +204,17 @@ class ShortUrl extends AbstractEntity implements \JsonSerializable return count($this->visits); } + /** + * @param Collection $visits + * @return ShortUrl + * @internal + */ + public function setVisits(Collection $visits): self + { + $this->visits = $visits; + return $this; + } + /** * @return int|null */ diff --git a/module/Core/test-func/Repository/ShortUrlRepositoryTest.php b/module/Core/test-func/Repository/ShortUrlRepositoryTest.php new file mode 100644 index 00000000..3c8688bd --- /dev/null +++ b/module/Core/test-func/Repository/ShortUrlRepositoryTest.php @@ -0,0 +1,65 @@ +repo = $this->getEntityManager()->getRepository(ShortUrl::class); + } + + /** + * @test + */ + public function findOneByShortCodeReturnsProperData() + { + $foo = new ShortUrl(); + $foo->setOriginalUrl('foo') + ->setShortCode('foo'); + $this->getEntityManager()->persist($foo); + + $bar = new ShortUrl(); + $bar->setOriginalUrl('bar') + ->setShortCode('bar') + ->setValidSince((new \DateTime())->add(new \DateInterval('P1M'))); + $this->getEntityManager()->persist($bar); + + $visits = []; + for ($i = 0; $i < 3; $i++) { + $visit = new Visit(); + $this->getEntityManager()->persist($visit); + $visits[] = $visit; + } + $baz = new ShortUrl(); + $baz->setOriginalUrl('baz') + ->setShortCode('baz') + ->setVisits(new ArrayCollection($visits)) + ->setMaxVisits(3); + $this->getEntityManager()->persist($baz); + + $this->getEntityManager()->flush(); + + $this->assertSame($foo, $this->repo->findOneByShortCode($foo->getShortCode())); + $this->assertNull($this->repo->findOneByShortCode('invalid')); + $this->assertNull($this->repo->findOneByShortCode($bar->getShortCode())); + $this->assertNull($this->repo->findOneByShortCode($baz->getShortCode())); + } +}