mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-27 20:11:34 +03:00
Created ShortUrlRepositoryTest
This commit is contained in:
parent
633f3b728f
commit
501a933d2e
2 changed files with 76 additions and 0 deletions
|
@ -204,6 +204,17 @@ class ShortUrl extends AbstractEntity implements \JsonSerializable
|
||||||
return count($this->visits);
|
return count($this->visits);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection $visits
|
||||||
|
* @return ShortUrl
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
public function setVisits(Collection $visits): self
|
||||||
|
{
|
||||||
|
$this->visits = $visits;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int|null
|
* @return int|null
|
||||||
*/
|
*/
|
||||||
|
|
65
module/Core/test-func/Repository/ShortUrlRepositoryTest.php
Normal file
65
module/Core/test-func/Repository/ShortUrlRepositoryTest.php
Normal 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()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue