2016-06-18 02:43:29 -05:00
|
|
|
<?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;
|
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;
|
2020-02-01 15:54:02 -06:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
2018-01-07 13:00:21 -06:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
2020-01-28 03:49:55 -06:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlsParams;
|
2016-07-19 11:01:39 -05:00
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
|
2020-02-01 15:54:02 -06:00
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
2016-07-19 11:01:39 -05:00
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlService;
|
2019-02-26 15:56:43 -06:00
|
|
|
|
2018-10-28 02:34:02 -05:00
|
|
|
use function count;
|
2016-06-18 02:43:29 -05:00
|
|
|
|
|
|
|
class ShortUrlServiceTest extends TestCase
|
|
|
|
{
|
2019-12-29 15:48:40 -06:00
|
|
|
private ShortUrlService $service;
|
|
|
|
private ObjectProphecy $em;
|
2020-02-01 15:54:02 -06:00
|
|
|
private ObjectProphecy $urlResolver;
|
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);
|
2020-02-01 15:54:02 -06:00
|
|
|
|
|
|
|
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
|
|
|
|
|
|
|
$this->service = new ShortUrlService($this->em->reveal(), $this->urlResolver->reveal());
|
2016-06-18 02:43:29 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-11-20 13:03:06 -06:00
|
|
|
public function listedUrlsAreReturnedFromEntityManager(): void
|
2016-06-18 02:43:29 -05:00
|
|
|
{
|
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());
|
|
|
|
|
2020-01-28 03:49:55 -06:00
|
|
|
$list = $this->service->listShortUrls(ShortUrlsParams::emptyInstance());
|
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 */
|
2019-11-20 13:03:06 -06:00
|
|
|
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';
|
2020-02-01 15:54:02 -06:00
|
|
|
$this->urlResolver->resolveShortUrl(new ShortUrlIdentifier($shortCode))->willReturn($shortUrl->reveal())
|
|
|
|
->shouldBeCalledOnce();
|
2016-08-21 11:15:25 -05:00
|
|
|
|
|
|
|
$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 */
|
2019-11-20 13:03:06 -06:00
|
|
|
public function updateMetadataByShortCodeUpdatesProvidedData(): void
|
2018-01-07 13:00:21 -06:00
|
|
|
{
|
2018-10-28 10:00:54 -05:00
|
|
|
$shortUrl = new ShortUrl('');
|
2018-01-07 13:00:21 -06:00
|
|
|
|
2020-02-01 15:54:02 -06:00
|
|
|
$findShortUrl = $this->urlResolver->resolveShortUrl(new ShortUrlIdentifier('abc123'))->willReturn($shortUrl);
|
2019-11-20 13:03:06 -06:00
|
|
|
$flush = $this->em->flush()->willReturn(null);
|
2018-01-07 13:00:21 -06:00
|
|
|
|
2020-02-01 15:54:02 -06:00
|
|
|
$result = $this->service->updateMetadataByShortCode(new ShortUrlIdentifier('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,
|
|
|
|
],
|
|
|
|
));
|
2018-01-07 13:00:21 -06:00
|
|
|
|
|
|
|
$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();
|
|
|
|
$flush->shouldHaveBeenCalled();
|
|
|
|
}
|
2016-06-18 02:43:29 -05:00
|
|
|
}
|