2016-06-18 02:43:29 -05:00
|
|
|
<?php
|
2017-10-12 03:13:20 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-07-19 11:01:39 -05:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Service;
|
2016-06-18 02:43:29 -05:00
|
|
|
|
2018-09-29 05:52:32 -05:00
|
|
|
use Cake\Chronos\Chronos;
|
2016-06-18 02:43:29 -05:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2016-08-21 11:15:25 -05:00
|
|
|
use Doctrine\ORM\EntityRepository;
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\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;
|
2018-01-07 13:00:21 -06:00
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
2016-07-19 11:01:39 -05:00
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
|
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlService;
|
2018-10-28 02:34:02 -05:00
|
|
|
use function count;
|
2016-06-18 02:43:29 -05:00
|
|
|
|
|
|
|
class ShortUrlServiceTest extends TestCase
|
|
|
|
{
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ShortUrlService */
|
2018-11-20 12:37:22 -06:00
|
|
|
private $service;
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ObjectProphecy|EntityManagerInterface */
|
2018-11-20 12:37:22 -06:00
|
|
|
private $em;
|
2016-06-18 02:43:29 -05:00
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
public function setUp(): void
|
2016-06-18 02:43:29 -05:00
|
|
|
{
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2016-06-18 02:43:29 -05:00
|
|
|
public function listedUrlsAreReturnedFromEntityManager()
|
|
|
|
{
|
2016-07-04 02:18:10 -05:00
|
|
|
$list = [
|
2018-10-28 10:00:54 -05:00
|
|
|
new ShortUrl(''),
|
|
|
|
new ShortUrl(''),
|
|
|
|
new ShortUrl(''),
|
|
|
|
new ShortUrl(''),
|
2016-07-04 02:18:10 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class);
|
2018-11-11 06:18:21 -06:00
|
|
|
$repo->findList(Argument::cetera())->willReturn($list)->shouldBeCalledOnce();
|
|
|
|
$repo->countList(Argument::cetera())->willReturn(count($list))->shouldBeCalledOnce();
|
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
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2016-08-21 11:15:25 -05:00
|
|
|
public function exceptionIsThrownWhenSettingTagsOnInvalidShortcode()
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class);
|
|
|
|
$repo->findOneBy(['shortCode' => $shortCode])->willReturn(null)
|
2018-11-11 06:18:21 -06:00
|
|
|
->shouldBeCalledOnce();
|
2016-08-21 11:15:25 -05:00
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
2018-01-07 13:00:21 -06:00
|
|
|
$this->expectException(InvalidShortCodeException::class);
|
2016-08-21 11:15:25 -05:00
|
|
|
$this->service->setTagsByShortCode($shortCode);
|
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2016-08-21 11:15:25 -05:00
|
|
|
public function providedTagsAreGetFromRepoAndSetToTheShortUrl()
|
|
|
|
{
|
|
|
|
$shortUrl = $this->prophesize(ShortUrl::class);
|
2018-11-11 06:18:21 -06:00
|
|
|
$shortUrl->setTags(Argument::any())->shouldBeCalledOnce();
|
2016-08-21 11:15:25 -05:00
|
|
|
$shortCode = 'abc123';
|
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class);
|
|
|
|
$repo->findOneBy(['shortCode' => $shortCode])->willReturn($shortUrl->reveal())
|
2018-11-11 06:18:21 -06:00
|
|
|
->shouldBeCalledOnce();
|
2016-08-21 11:15:25 -05:00
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
$tagRepo = $this->prophesize(EntityRepository::class);
|
2018-11-11 06:18:21 -06:00
|
|
|
$tagRepo->findOneBy(['name' => 'foo'])->willReturn(new Tag('foo'))->shouldBeCalledOnce();
|
|
|
|
$tagRepo->findOneBy(['name' => 'bar'])->willReturn(null)->shouldBeCalledOnce();
|
2016-08-21 11:15:25 -05:00
|
|
|
$this->em->getRepository(Tag::class)->willReturn($tagRepo->reveal());
|
|
|
|
|
|
|
|
$this->service->setTagsByShortCode($shortCode, ['foo', 'bar']);
|
|
|
|
}
|
2018-01-07 13:00:21 -06:00
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2018-01-07 13:00:21 -06:00
|
|
|
public function updateMetadataByShortCodeUpdatesProvidedData()
|
|
|
|
{
|
2018-10-28 10:00:54 -05:00
|
|
|
$shortUrl = new ShortUrl('');
|
2018-01-07 13:00:21 -06:00
|
|
|
|
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class);
|
|
|
|
$findShortUrl = $repo->findOneBy(['shortCode' => 'abc123'])->willReturn($shortUrl);
|
|
|
|
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
$flush = $this->em->flush($shortUrl)->willReturn(null);
|
|
|
|
|
|
|
|
$result = $this->service->updateMetadataByShortCode('abc123', ShortUrlMeta::createFromParams(
|
2018-09-29 05:52:32 -05:00
|
|
|
Chronos::parse('2017-01-01 00:00:00')->toAtomString(),
|
|
|
|
Chronos::parse('2017-01-05 00:00:00')->toAtomString(),
|
2018-01-07 13:00:21 -06:00
|
|
|
null,
|
|
|
|
5
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertSame($shortUrl, $result);
|
2018-09-29 05:52:32 -05:00
|
|
|
$this->assertEquals(Chronos::parse('2017-01-01 00:00:00'), $shortUrl->getValidSince());
|
|
|
|
$this->assertEquals(Chronos::parse('2017-01-05 00:00:00'), $shortUrl->getValidUntil());
|
2018-01-07 13:00:21 -06:00
|
|
|
$this->assertEquals(5, $shortUrl->getMaxVisits());
|
|
|
|
$findShortUrl->shouldHaveBeenCalled();
|
|
|
|
$getRepo->shouldHaveBeenCalled();
|
|
|
|
$flush->shouldHaveBeenCalled();
|
|
|
|
}
|
2016-06-18 02:43:29 -05:00
|
|
|
}
|