Created ShortUrlRepositoryTest

This commit is contained in:
Alejandro Celaya 2017-10-23 13:03:23 +02:00
parent 633f3b728f
commit 501a933d2e
2 changed files with 76 additions and 0 deletions

View file

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

View file

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Repository;
use Doctrine\Common\Collections\ArrayCollection;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
use ShlinkioTest\Shlink\Common\DbUnit\DatabaseTestCase;
class ShortUrlRepositoryTest extends DatabaseTestCase
{
const ENTITIES_TO_EMPTY = [
ShortUrl::class,
Visit::class,
];
/**
* @var ShortUrlRepository
*/
private $repo;
public function setUp()
{
$this->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()));
}
}