2016-04-17 13:42:52 +02:00
|
|
|
<?php
|
2019-10-05 17:26:10 +02:00
|
|
|
|
2017-10-12 10:13:20 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2016-07-19 18:01:39 +02:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Service;
|
2016-04-17 13:42:52 +02:00
|
|
|
|
2019-02-03 09:40:32 +01:00
|
|
|
use Cake\Chronos\Chronos;
|
2016-04-17 13:42:52 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2017-03-24 20:34:18 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-04-17 13:42:52 +02:00
|
|
|
use Prophecy\Argument;
|
2020-11-02 11:50:19 +01:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2016-04-17 13:42:52 +02:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2022-07-24 11:07:20 +02:00
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface;
|
2016-07-19 18:01:39 +02:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2017-10-21 17:18:57 +02:00
|
|
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
2019-01-29 13:55:47 +01:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
|
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
|
2022-01-16 15:41:20 +01:00
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortCodeUniquenessHelperInterface;
|
2016-07-19 18:01:39 +02:00
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
2021-02-05 17:59:34 +01:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlTitleResolutionHelperInterface;
|
2020-11-07 09:31:46 +01:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver;
|
2016-04-17 13:42:52 +02:00
|
|
|
|
|
|
|
|
class UrlShortenerTest extends TestCase
|
|
|
|
|
{
|
2020-11-02 11:50:19 +01:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
|
2019-12-29 22:48:40 +01:00
|
|
|
private UrlShortener $urlShortener;
|
|
|
|
|
private ObjectProphecy $em;
|
2021-02-05 17:59:34 +01:00
|
|
|
private ObjectProphecy $titleResolutionHelper;
|
2020-10-25 11:16:42 +01:00
|
|
|
private ObjectProphecy $shortCodeHelper;
|
2022-07-24 11:07:20 +02:00
|
|
|
private ObjectProphecy $eventDispatcher;
|
2016-04-17 13:42:52 +02:00
|
|
|
|
2019-02-03 09:40:32 +01:00
|
|
|
public function setUp(): void
|
2016-04-17 13:42:52 +02:00
|
|
|
{
|
2021-02-05 17:59:34 +01:00
|
|
|
$this->titleResolutionHelper = $this->prophesize(ShortUrlTitleResolutionHelperInterface::class);
|
|
|
|
|
$this->titleResolutionHelper->processTitleAndValidateUrl(Argument::cetera())->willReturnArgument();
|
2016-04-17 13:42:52 +02:00
|
|
|
|
|
|
|
|
$this->em = $this->prophesize(EntityManagerInterface::class);
|
2020-01-01 20:48:31 +01:00
|
|
|
$this->em->persist(Argument::any())->will(function ($arguments): void {
|
2016-04-17 13:42:52 +02:00
|
|
|
/** @var ShortUrl $shortUrl */
|
2019-12-29 23:16:55 +01:00
|
|
|
[$shortUrl] = $arguments;
|
2018-09-15 10:03:42 +02:00
|
|
|
$shortUrl->setId('10');
|
2016-04-17 13:42:52 +02:00
|
|
|
});
|
2022-07-24 11:07:20 +02:00
|
|
|
$this->em->wrapInTransaction(Argument::type('callable'))->will(function (array $args) {
|
2020-11-06 20:05:57 +01:00
|
|
|
/** @var callable $callback */
|
|
|
|
|
[$callback] = $args;
|
|
|
|
|
|
|
|
|
|
return $callback();
|
|
|
|
|
});
|
2019-01-29 13:55:47 +01:00
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class);
|
2016-04-17 13:42:52 +02:00
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
|
2022-01-16 15:41:20 +01:00
|
|
|
$this->shortCodeHelper = $this->prophesize(ShortCodeUniquenessHelperInterface::class);
|
2020-10-25 11:16:42 +01:00
|
|
|
$this->shortCodeHelper->ensureShortCodeUniqueness(Argument::cetera())->willReturn(true);
|
|
|
|
|
|
2022-07-24 11:07:20 +02:00
|
|
|
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
|
|
|
|
|
|
2020-03-22 17:05:59 +01:00
|
|
|
$this->urlShortener = new UrlShortener(
|
2021-02-05 17:59:34 +01:00
|
|
|
$this->titleResolutionHelper->reveal(),
|
2020-03-22 17:05:59 +01:00
|
|
|
$this->em->reveal(),
|
2020-11-07 09:31:46 +01:00
|
|
|
new SimpleShortUrlRelationResolver(),
|
2020-10-25 11:16:42 +01:00
|
|
|
$this->shortCodeHelper->reveal(),
|
2022-07-24 11:07:20 +02:00
|
|
|
$this->eventDispatcher->reveal(),
|
2020-03-22 17:05:59 +01:00
|
|
|
);
|
2016-04-17 13:42:52 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-05 17:59:34 +01:00
|
|
|
/** @test */
|
|
|
|
|
public function urlIsProperlyShortened(): void
|
2016-04-17 13:42:52 +02:00
|
|
|
{
|
2021-02-03 13:28:51 +01:00
|
|
|
$longUrl = 'http://foobar.com/12345/hello?foo=bar';
|
2021-02-05 17:59:34 +01:00
|
|
|
$meta = ShortUrlMeta::fromRawData(['longUrl' => $longUrl]);
|
|
|
|
|
$shortUrl = $this->urlShortener->shorten($meta);
|
2021-02-03 19:22:47 +01:00
|
|
|
|
2021-02-05 17:59:34 +01:00
|
|
|
self::assertEquals($longUrl, $shortUrl->getLongUrl());
|
|
|
|
|
$this->titleResolutionHelper->processTitleAndValidateUrl($meta)->shouldHaveBeenCalledOnce();
|
2016-04-17 13:42:52 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-11 11:28:53 +02:00
|
|
|
/** @test */
|
2020-10-25 11:16:42 +01:00
|
|
|
public function exceptionIsThrownWhenNonUniqueSlugIsProvided(): void
|
2019-10-11 11:28:53 +02:00
|
|
|
{
|
2020-10-25 11:16:42 +01:00
|
|
|
$ensureUniqueness = $this->shortCodeHelper->ensureShortCodeUniqueness(Argument::cetera())->willReturn(false);
|
2019-10-11 11:28:53 +02:00
|
|
|
|
2020-10-25 11:16:42 +01:00
|
|
|
$ensureUniqueness->shouldBeCalledOnce();
|
|
|
|
|
$this->expectException(NonUniqueSlugException::class);
|
|
|
|
|
|
2021-01-30 19:17:12 +01:00
|
|
|
$this->urlShortener->shorten(ShortUrlMeta::fromRawData(
|
2021-01-30 14:18:44 +01:00
|
|
|
['customSlug' => 'custom-slug', 'longUrl' => 'http://foobar.com/12345/hello?foo=bar'],
|
|
|
|
|
));
|
2019-10-11 11:28:53 +02:00
|
|
|
}
|
|
|
|
|
|
2016-04-17 13:42:52 +02:00
|
|
|
/**
|
|
|
|
|
* @test
|
2019-02-17 20:28:34 +01:00
|
|
|
* @dataProvider provideExistingShortUrls
|
2016-04-17 13:42:52 +02:00
|
|
|
*/
|
2021-01-30 19:17:12 +01:00
|
|
|
public function existingShortUrlIsReturnedWhenRequested(ShortUrlMeta $meta, ShortUrl $expected): void
|
2021-01-30 14:18:44 +01:00
|
|
|
{
|
2019-02-03 09:40:32 +01:00
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class);
|
2020-09-23 00:22:29 +02:00
|
|
|
$findExisting = $repo->findOneMatching(Argument::cetera())->willReturn($expected);
|
2019-02-03 09:40:32 +01:00
|
|
|
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
|
2021-01-30 19:17:12 +01:00
|
|
|
$result = $this->urlShortener->shorten($meta);
|
2019-02-03 09:40:32 +01:00
|
|
|
|
|
|
|
|
$findExisting->shouldHaveBeenCalledOnce();
|
|
|
|
|
$getRepo->shouldHaveBeenCalledOnce();
|
2019-10-11 11:09:33 +02:00
|
|
|
$this->em->persist(Argument::cetera())->shouldNotHaveBeenCalled();
|
2021-02-05 17:59:34 +01:00
|
|
|
$this->titleResolutionHelper->processTitleAndValidateUrl(Argument::cetera())->shouldNotHaveBeenCalled();
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertSame($expected, $result);
|
2019-02-03 09:40:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-17 20:28:34 +01:00
|
|
|
public function provideExistingShortUrls(): iterable
|
2019-02-03 09:40:32 +01:00
|
|
|
{
|
|
|
|
|
$url = 'http://foo.com';
|
|
|
|
|
|
2021-01-30 19:17:12 +01:00
|
|
|
yield [ShortUrlMeta::fromRawData(['findIfExists' => true, 'longUrl' => $url]), ShortUrl::withLongUrl(
|
2019-02-17 20:28:34 +01:00
|
|
|
$url,
|
2021-01-30 14:18:44 +01:00
|
|
|
)];
|
2021-01-30 19:17:12 +01:00
|
|
|
yield [ShortUrlMeta::fromRawData(
|
2021-01-30 14:18:44 +01:00
|
|
|
['findIfExists' => true, 'customSlug' => 'foo', 'longUrl' => $url],
|
|
|
|
|
), ShortUrl::withLongUrl($url)];
|
|
|
|
|
yield [
|
2021-01-30 19:17:12 +01:00
|
|
|
ShortUrlMeta::fromRawData(['findIfExists' => true, 'longUrl' => $url, 'tags' => ['foo', 'bar']]),
|
2021-01-31 11:09:00 +01:00
|
|
|
ShortUrl::fromMeta(ShortUrlMeta::fromRawData(['longUrl' => $url, 'tags' => ['foo', 'bar']])),
|
2019-02-17 20:28:34 +01:00
|
|
|
];
|
|
|
|
|
yield [
|
2021-01-30 14:18:44 +01:00
|
|
|
ShortUrlMeta::fromRawData(['findIfExists' => true, 'maxVisits' => 3, 'longUrl' => $url]),
|
|
|
|
|
ShortUrl::fromMeta(ShortUrlMeta::fromRawData(['maxVisits' => 3, 'longUrl' => $url])),
|
2019-02-17 20:28:34 +01:00
|
|
|
];
|
|
|
|
|
yield [
|
2021-01-30 14:18:44 +01:00
|
|
|
ShortUrlMeta::fromRawData(
|
|
|
|
|
['findIfExists' => true, 'validSince' => Chronos::parse('2017-01-01'), 'longUrl' => $url],
|
|
|
|
|
),
|
|
|
|
|
ShortUrl::fromMeta(
|
|
|
|
|
ShortUrlMeta::fromRawData(['validSince' => Chronos::parse('2017-01-01'), 'longUrl' => $url]),
|
|
|
|
|
),
|
2019-02-17 20:28:34 +01:00
|
|
|
];
|
|
|
|
|
yield [
|
2021-01-30 14:18:44 +01:00
|
|
|
ShortUrlMeta::fromRawData(
|
|
|
|
|
['findIfExists' => true, 'validUntil' => Chronos::parse('2017-01-01'), 'longUrl' => $url],
|
|
|
|
|
),
|
|
|
|
|
ShortUrl::fromMeta(
|
|
|
|
|
ShortUrlMeta::fromRawData(['validUntil' => Chronos::parse('2017-01-01'), 'longUrl' => $url]),
|
|
|
|
|
),
|
2019-02-17 20:28:34 +01:00
|
|
|
];
|
2019-10-11 11:28:53 +02:00
|
|
|
yield [
|
2021-01-30 14:18:44 +01:00
|
|
|
ShortUrlMeta::fromRawData(['findIfExists' => true, 'domain' => 'example.com', 'longUrl' => $url]),
|
|
|
|
|
ShortUrl::fromMeta(ShortUrlMeta::fromRawData(['domain' => 'example.com', 'longUrl' => $url])),
|
2019-10-11 11:28:53 +02:00
|
|
|
];
|
2019-02-17 20:28:34 +01:00
|
|
|
yield [
|
2020-01-26 08:42:51 +01:00
|
|
|
ShortUrlMeta::fromRawData([
|
2019-02-17 20:28:34 +01:00
|
|
|
'findIfExists' => true,
|
|
|
|
|
'validUntil' => Chronos::parse('2017-01-01'),
|
|
|
|
|
'maxVisits' => 4,
|
2021-01-30 14:18:44 +01:00
|
|
|
'longUrl' => $url,
|
2021-01-30 19:17:12 +01:00
|
|
|
'tags' => ['baz', 'foo', 'bar'],
|
2019-02-17 20:28:34 +01:00
|
|
|
]),
|
2021-01-30 14:18:44 +01:00
|
|
|
ShortUrl::fromMeta(ShortUrlMeta::fromRawData([
|
2019-02-17 20:28:34 +01:00
|
|
|
'validUntil' => Chronos::parse('2017-01-01'),
|
|
|
|
|
'maxVisits' => 4,
|
2021-01-30 14:18:44 +01:00
|
|
|
'longUrl' => $url,
|
2021-01-31 11:09:00 +01:00
|
|
|
'tags' => ['foo', 'bar', 'baz'],
|
|
|
|
|
])),
|
2019-02-03 09:40:32 +01:00
|
|
|
];
|
|
|
|
|
}
|
2016-04-17 13:42:52 +02:00
|
|
|
}
|