2016-06-18 02:43:29 -05:00
|
|
|
<?php
|
2016-07-19 11:01:39 -05:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Service;
|
2016-06-18 02:43:29 -05:00
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2016-08-21 11:15:25 -05:00
|
|
|
use Doctrine\ORM\EntityRepository;
|
2016-06-18 02:43:29 -05:00
|
|
|
use PHPUnit_Framework_TestCase as TestCase;
|
2016-07-04 02:18:10 -05:00
|
|
|
use Prophecy\Argument;
|
2016-06-18 02:43:29 -05:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2016-07-19 11:01:39 -05:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2016-08-21 11:15:25 -05:00
|
|
|
use Shlinkio\Shlink\Core\Entity\Tag;
|
2016-07-19 11:01:39 -05:00
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
|
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlService;
|
2016-06-18 02:43:29 -05:00
|
|
|
|
|
|
|
class ShortUrlServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ShortUrlService
|
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
/**
|
|
|
|
* @var ObjectProphecy|EntityManagerInterface
|
|
|
|
*/
|
|
|
|
protected $em;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->em = $this->prophesize(EntityManagerInterface::class);
|
2016-08-21 11:15:25 -05:00
|
|
|
$this->em->persist(Argument::any())->willReturn(null);
|
|
|
|
$this->em->flush()->willReturn(null);
|
2016-06-18 02:43:29 -05:00
|
|
|
$this->service = new ShortUrlService($this->em->reveal());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function listedUrlsAreReturnedFromEntityManager()
|
|
|
|
{
|
2016-07-04 02:18:10 -05:00
|
|
|
$list = [
|
2016-06-18 02:43:29 -05:00
|
|
|
new ShortUrl(),
|
|
|
|
new ShortUrl(),
|
|
|
|
new ShortUrl(),
|
|
|
|
new ShortUrl(),
|
2016-07-04 02:18:10 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class);
|
|
|
|
$repo->findList(Argument::cetera())->willReturn($list)->shouldBeCalledTimes(1);
|
|
|
|
$repo->countList(Argument::cetera())->willReturn(count($list))->shouldBeCalledTimes(1);
|
2016-06-18 02:43:29 -05:00
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
$list = $this->service->listShortUrls();
|
2016-07-04 02:18:10 -05:00
|
|
|
$this->assertEquals(4, $list->getCurrentItemCount());
|
2016-06-18 02:43:29 -05:00
|
|
|
}
|
2016-08-21 11:15:25 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @expectedException \Shlinkio\Shlink\Core\Exception\InvalidShortCodeException
|
|
|
|
*/
|
|
|
|
public function exceptionIsThrownWhenSettingTagsOnInvalidShortcode()
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class);
|
|
|
|
$repo->findOneBy(['shortCode' => $shortCode])->willReturn(null)
|
|
|
|
->shouldBeCalledTimes(1);
|
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
$this->service->setTagsByShortCode($shortCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function providedTagsAreGetFromRepoAndSetToTheShortUrl()
|
|
|
|
{
|
|
|
|
$shortUrl = $this->prophesize(ShortUrl::class);
|
|
|
|
$shortUrl->setTags(Argument::any())->shouldBeCalledTimes(1);
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class);
|
|
|
|
$repo->findOneBy(['shortCode' => $shortCode])->willReturn($shortUrl->reveal())
|
|
|
|
->shouldBeCalledTimes(1);
|
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
$tagRepo = $this->prophesize(EntityRepository::class);
|
|
|
|
$tagRepo->findOneBy(['name' => 'foo'])->willReturn(new Tag())->shouldbeCalledTimes(1);
|
|
|
|
$tagRepo->findOneBy(['name' => 'bar'])->willReturn(null)->shouldbeCalledTimes(1);
|
|
|
|
$this->em->getRepository(Tag::class)->willReturn($tagRepo->reveal());
|
|
|
|
|
|
|
|
$this->service->setTagsByShortCode($shortCode, ['foo', 'bar']);
|
|
|
|
}
|
2016-06-18 02:43:29 -05:00
|
|
|
}
|