shlink/module/Core/test/Service/ShortUrlServiceTest.php

112 lines
4.3 KiB
PHP
Raw Normal View History

<?php
2019-10-05 10:26:10 -05:00
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;
use Cake\Chronos\Chronos;
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;
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;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Model\ShortUrlsParams;
2016-07-19 11:01:39 -05:00
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
use Shlinkio\Shlink\Core\Service\ShortUrlService;
use function count;
class ShortUrlServiceTest extends TestCase
{
private ShortUrlService $service;
private ObjectProphecy $em;
2019-02-16 03:53:45 -06:00
public function setUp(): void
{
$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);
$this->service = new ShortUrlService($this->em->reveal());
}
2019-02-17 13:28:34 -06:00
/** @test */
public function listedUrlsAreReturnedFromEntityManager(): void
{
2016-07-04 02:18:10 -05:00
$list = [
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();
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$list = $this->service->listShortUrls(ShortUrlsParams::emptyInstance());
2016-07-04 02:18:10 -05:00
$this->assertEquals(4, $list->getCurrentItemCount());
}
2016-08-21 11:15:25 -05:00
2019-02-17 13:28:34 -06:00
/** @test */
public function exceptionIsThrownWhenSettingTagsOnInvalidShortcode(): void
2016-08-21 11:15:25 -05:00
{
$shortCode = 'abc123';
$repo = $this->prophesize(ShortUrlRepository::class);
$repo->findOneByShortCode($shortCode, null)->willReturn(null)
->shouldBeCalledOnce();
2016-08-21 11:15:25 -05:00
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$this->expectException(ShortUrlNotFoundException::class);
2016-08-21 11:15:25 -05:00
$this->service->setTagsByShortCode($shortCode);
}
2019-02-17 13:28:34 -06:00
/** @test */
public function providedTagsAreGetFromRepoAndSetToTheShortUrl(): void
2016-08-21 11:15:25 -05:00
{
$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->findOneByShortCode($shortCode, null)->willReturn($shortUrl->reveal())
->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']);
}
2019-02-17 13:28:34 -06:00
/** @test */
public function updateMetadataByShortCodeUpdatesProvidedData(): void
{
$shortUrl = new ShortUrl('');
$repo = $this->prophesize(ShortUrlRepository::class);
$findShortUrl = $repo->findOneByShortCode('abc123', null)->willReturn($shortUrl);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$flush = $this->em->flush()->willReturn(null);
$result = $this->service->updateMetadataByShortCode('abc123', ShortUrlMeta::fromRawData([
'validSince' => Chronos::parse('2017-01-01 00:00:00')->toAtomString(),
'validUntil' => Chronos::parse('2017-01-05 00:00:00')->toAtomString(),
'maxVisits' => 5,
]));
$this->assertSame($shortUrl, $result);
$this->assertEquals(Chronos::parse('2017-01-01 00:00:00'), $shortUrl->getValidSince());
$this->assertEquals(Chronos::parse('2017-01-05 00:00:00'), $shortUrl->getValidUntil());
$this->assertEquals(5, $shortUrl->getMaxVisits());
$findShortUrl->shouldHaveBeenCalled();
$getRepo->shouldHaveBeenCalled();
$flush->shouldHaveBeenCalled();
}
}