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;
|
2020-11-02 04:50:19 -06:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
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-03-22 08:04:01 -05:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlEdit;
|
2020-02-01 15:54:02 -06:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
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;
|
2020-03-22 11:22:52 -05:00
|
|
|
use Shlinkio\Shlink\Core\Util\UrlValidatorInterface;
|
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
|
|
|
|
{
|
2020-11-02 04:50:19 -06:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
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;
|
2020-03-22 11:22:52 -05:00
|
|
|
private ObjectProphecy $urlValidator;
|
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);
|
2020-03-22 11:22:52 -05:00
|
|
|
$this->urlValidator = $this->prophesize(UrlValidatorInterface::class);
|
2020-02-01 15:54:02 -06:00
|
|
|
|
2020-03-22 11:22:52 -05:00
|
|
|
$this->service = new ShortUrlService(
|
|
|
|
$this->em->reveal(),
|
|
|
|
$this->urlResolver->reveal(),
|
|
|
|
$this->urlValidator->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());
|
2020-10-03 17:35:14 -05:00
|
|
|
self::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());
|
|
|
|
|
2020-02-01 15:59:21 -06:00
|
|
|
$this->service->setTagsByShortCode(new ShortUrlIdentifier($shortCode), ['foo', 'bar']);
|
2016-08-21 11:15:25 -05:00
|
|
|
}
|
2018-01-07 13:00:21 -06:00
|
|
|
|
2020-03-22 11:22:52 -05:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideShortUrlEdits
|
|
|
|
*/
|
|
|
|
public function updateMetadataByShortCodeUpdatesProvidedData(
|
|
|
|
int $expectedValidateCalls,
|
|
|
|
ShortUrlEdit $shortUrlEdit
|
|
|
|
): void {
|
|
|
|
$originalLongUrl = 'originalLongUrl';
|
|
|
|
$shortUrl = new ShortUrl($originalLongUrl);
|
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-03-22 11:22:52 -05:00
|
|
|
$result = $this->service->updateMetadataByShortCode(new ShortUrlIdentifier('abc123'), $shortUrlEdit);
|
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertSame($shortUrl, $result);
|
|
|
|
self::assertEquals($shortUrlEdit->validSince(), $shortUrl->getValidSince());
|
|
|
|
self::assertEquals($shortUrlEdit->validUntil(), $shortUrl->getValidUntil());
|
|
|
|
self::assertEquals($shortUrlEdit->maxVisits(), $shortUrl->getMaxVisits());
|
|
|
|
self::assertEquals($shortUrlEdit->longUrl() ?? $originalLongUrl, $shortUrl->getLongUrl());
|
2020-03-22 11:22:52 -05:00
|
|
|
$findShortUrl->shouldHaveBeenCalled();
|
|
|
|
$flush->shouldHaveBeenCalled();
|
2020-09-23 12:19:17 -05:00
|
|
|
$this->urlValidator->validateUrl(
|
|
|
|
$shortUrlEdit->longUrl(),
|
|
|
|
$shortUrlEdit->doValidateUrl(),
|
|
|
|
)->shouldHaveBeenCalledTimes($expectedValidateCalls);
|
2020-03-22 11:22:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideShortUrlEdits(): iterable
|
|
|
|
{
|
|
|
|
yield 'no long URL' => [0, ShortUrlEdit::fromRawData(
|
2020-02-01 15:54:02 -06:00
|
|
|
[
|
|
|
|
'validSince' => Chronos::parse('2017-01-01 00:00:00')->toAtomString(),
|
|
|
|
'validUntil' => Chronos::parse('2017-01-05 00:00:00')->toAtomString(),
|
|
|
|
'maxVisits' => 5,
|
|
|
|
],
|
2020-03-22 11:22:52 -05:00
|
|
|
)];
|
|
|
|
yield 'long URL' => [1, ShortUrlEdit::fromRawData(
|
|
|
|
[
|
|
|
|
'validSince' => Chronos::parse('2017-01-01 00:00:00')->toAtomString(),
|
|
|
|
'maxVisits' => 10,
|
|
|
|
'longUrl' => 'modifiedLongUrl',
|
|
|
|
],
|
|
|
|
)];
|
2020-09-23 12:19:17 -05:00
|
|
|
yield 'long URL with validation' => [1, ShortUrlEdit::fromRawData(
|
|
|
|
[
|
|
|
|
'longUrl' => 'modifiedLongUrl',
|
|
|
|
'validateUrl' => true,
|
|
|
|
],
|
|
|
|
)];
|
2018-01-07 13:00:21 -06:00
|
|
|
}
|
2016-06-18 02:43:29 -05:00
|
|
|
}
|