2016-04-17 06:42:52 -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-04-17 06:42:52 -05:00
|
|
|
|
2019-02-03 02:40:32 -06:00
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2016-04-17 06:42:52 -05:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-04-17 06:42:52 -05:00
|
|
|
use Prophecy\Argument;
|
2020-11-02 04:50:19 -06:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2016-04-17 06:42:52 -05:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2016-07-19 11:01:39 -05:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2019-02-03 02:40:32 -06:00
|
|
|
use Shlinkio\Shlink\Core\Entity\Tag;
|
2017-10-21 10:18:57 -05:00
|
|
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
2019-01-29 06:55:47 -06:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
|
2020-10-25 05:16:42 -05:00
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortCodeHelperInterface;
|
2016-07-19 11:01:39 -05:00
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
2020-11-07 02:31:46 -06:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver;
|
2019-11-16 03:19:25 -06:00
|
|
|
use Shlinkio\Shlink\Core\Util\UrlValidatorInterface;
|
2016-04-17 06:42:52 -05:00
|
|
|
|
|
|
|
class UrlShortenerTest extends TestCase
|
|
|
|
{
|
2020-11-02 04:50:19 -06:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2019-12-29 15:48:40 -06:00
|
|
|
private UrlShortener $urlShortener;
|
|
|
|
private ObjectProphecy $em;
|
|
|
|
private ObjectProphecy $urlValidator;
|
2020-10-25 05:16:42 -05:00
|
|
|
private ObjectProphecy $shortCodeHelper;
|
2016-04-17 06:42:52 -05:00
|
|
|
|
2019-02-03 02:40:32 -06:00
|
|
|
public function setUp(): void
|
2016-04-17 06:42:52 -05:00
|
|
|
{
|
2019-11-16 03:19:25 -06:00
|
|
|
$this->urlValidator = $this->prophesize(UrlValidatorInterface::class);
|
2020-09-23 12:19:17 -05:00
|
|
|
$this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar', null)->will(
|
2020-03-22 10:58:28 -05:00
|
|
|
function (): void {
|
|
|
|
},
|
|
|
|
);
|
2016-04-17 06:42:52 -05:00
|
|
|
|
|
|
|
$this->em = $this->prophesize(EntityManagerInterface::class);
|
2020-01-01 13:48:31 -06:00
|
|
|
$this->em->persist(Argument::any())->will(function ($arguments): void {
|
2016-04-17 06:42:52 -05:00
|
|
|
/** @var ShortUrl $shortUrl */
|
2019-12-29 16:16:55 -06:00
|
|
|
[$shortUrl] = $arguments;
|
2018-09-15 03:03:42 -05:00
|
|
|
$shortUrl->setId('10');
|
2016-04-17 06:42:52 -05:00
|
|
|
});
|
2020-11-06 13:05:57 -06:00
|
|
|
$this->em->transactional(Argument::type('callable'))->will(function (array $args) {
|
|
|
|
/** @var callable $callback */
|
|
|
|
[$callback] = $args;
|
|
|
|
|
|
|
|
return $callback();
|
|
|
|
});
|
2019-01-29 06:55:47 -06:00
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class);
|
2019-10-11 04:09:33 -05:00
|
|
|
$repo->shortCodeIsInUse(Argument::cetera())->willReturn(false);
|
2016-04-17 06:42:52 -05:00
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
2020-10-25 05:16:42 -05:00
|
|
|
$this->shortCodeHelper = $this->prophesize(ShortCodeHelperInterface::class);
|
|
|
|
$this->shortCodeHelper->ensureShortCodeUniqueness(Argument::cetera())->willReturn(true);
|
|
|
|
|
2020-03-22 11:05:59 -05:00
|
|
|
$this->urlShortener = new UrlShortener(
|
|
|
|
$this->urlValidator->reveal(),
|
|
|
|
$this->em->reveal(),
|
2020-11-07 02:31:46 -06:00
|
|
|
new SimpleShortUrlRelationResolver(),
|
2020-10-25 05:16:42 -05:00
|
|
|
$this->shortCodeHelper->reveal(),
|
2020-03-22 11:05:59 -05:00
|
|
|
);
|
2016-04-17 06:42:52 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-02-03 02:40:32 -06:00
|
|
|
public function urlIsProperlyShortened(): void
|
2016-04-17 06:42:52 -05:00
|
|
|
{
|
2020-11-06 13:05:57 -06:00
|
|
|
$shortUrl = $this->urlShortener->shorten(
|
2020-06-27 04:09:56 -05:00
|
|
|
'http://foobar.com/12345/hello?foo=bar',
|
2019-01-29 06:55:47 -06:00
|
|
|
[],
|
2020-01-01 13:48:31 -06:00
|
|
|
ShortUrlMeta::createEmpty(),
|
2019-01-29 06:55:47 -06:00
|
|
|
);
|
2019-10-11 02:14:25 -05:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals('http://foobar.com/12345/hello?foo=bar', $shortUrl->getLongUrl());
|
2016-04-17 06:42:52 -05:00
|
|
|
}
|
|
|
|
|
2019-10-11 04:28:53 -05:00
|
|
|
/** @test */
|
2020-10-25 05:16:42 -05:00
|
|
|
public function exceptionIsThrownWhenNonUniqueSlugIsProvided(): void
|
2019-10-11 04:28:53 -05:00
|
|
|
{
|
2020-10-25 05:16:42 -05:00
|
|
|
$ensureUniqueness = $this->shortCodeHelper->ensureShortCodeUniqueness(Argument::cetera())->willReturn(false);
|
2019-10-11 04:28:53 -05:00
|
|
|
|
2020-10-25 05:16:42 -05:00
|
|
|
$ensureUniqueness->shouldBeCalledOnce();
|
|
|
|
$this->expectException(NonUniqueSlugException::class);
|
|
|
|
|
2020-11-06 13:05:57 -06:00
|
|
|
$this->urlShortener->shorten(
|
2020-06-27 04:09:56 -05:00
|
|
|
'http://foobar.com/12345/hello?foo=bar',
|
2019-10-11 04:28:53 -05:00
|
|
|
[],
|
2020-10-25 05:16:42 -05:00
|
|
|
ShortUrlMeta::fromRawData(['customSlug' => 'custom-slug']),
|
2019-10-11 04:28:53 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-17 06:42:52 -05:00
|
|
|
/**
|
|
|
|
* @test
|
2019-02-17 13:28:34 -06:00
|
|
|
* @dataProvider provideExistingShortUrls
|
2016-04-17 06:42:52 -05:00
|
|
|
*/
|
2019-02-03 02:40:32 -06:00
|
|
|
public function existingShortUrlIsReturnedWhenRequested(
|
|
|
|
string $url,
|
|
|
|
array $tags,
|
|
|
|
ShortUrlMeta $meta,
|
2019-10-11 04:09:33 -05:00
|
|
|
ShortUrl $expected
|
2019-02-03 02:40:32 -06:00
|
|
|
): void {
|
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class);
|
2020-09-22 17:22:29 -05:00
|
|
|
$findExisting = $repo->findOneMatching(Argument::cetera())->willReturn($expected);
|
2019-02-03 02:40:32 -06:00
|
|
|
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
2020-11-06 13:05:57 -06:00
|
|
|
$result = $this->urlShortener->shorten($url, $tags, $meta);
|
2019-02-03 02:40:32 -06:00
|
|
|
|
|
|
|
$findExisting->shouldHaveBeenCalledOnce();
|
|
|
|
$getRepo->shouldHaveBeenCalledOnce();
|
2019-10-11 04:09:33 -05:00
|
|
|
$this->em->persist(Argument::cetera())->shouldNotHaveBeenCalled();
|
2020-03-22 10:58:28 -05:00
|
|
|
$this->urlValidator->validateUrl(Argument::cetera())->shouldNotHaveBeenCalled();
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertSame($expected, $result);
|
2019-02-03 02:40:32 -06:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
public function provideExistingShortUrls(): iterable
|
2019-02-03 02:40:32 -06:00
|
|
|
{
|
|
|
|
$url = 'http://foo.com';
|
|
|
|
|
2020-01-26 01:42:51 -06:00
|
|
|
yield [$url, [], ShortUrlMeta::fromRawData(['findIfExists' => true]), new ShortUrl($url)];
|
|
|
|
yield [$url, [], ShortUrlMeta::fromRawData(
|
2020-01-01 13:48:31 -06:00
|
|
|
['findIfExists' => true, 'customSlug' => 'foo'],
|
2019-02-17 13:28:34 -06:00
|
|
|
), new ShortUrl($url)];
|
|
|
|
yield [
|
|
|
|
$url,
|
|
|
|
['foo', 'bar'],
|
2020-01-26 01:42:51 -06:00
|
|
|
ShortUrlMeta::fromRawData(['findIfExists' => true]),
|
2019-02-17 13:28:34 -06:00
|
|
|
(new ShortUrl($url))->setTags(new ArrayCollection([new Tag('bar'), new Tag('foo')])),
|
|
|
|
];
|
|
|
|
yield [
|
|
|
|
$url,
|
|
|
|
[],
|
2020-01-26 01:42:51 -06:00
|
|
|
ShortUrlMeta::fromRawData(['findIfExists' => true, 'maxVisits' => 3]),
|
|
|
|
new ShortUrl($url, ShortUrlMeta::fromRawData(['maxVisits' => 3])),
|
2019-02-17 13:28:34 -06:00
|
|
|
];
|
|
|
|
yield [
|
|
|
|
$url,
|
|
|
|
[],
|
2020-01-26 01:42:51 -06:00
|
|
|
ShortUrlMeta::fromRawData(['findIfExists' => true, 'validSince' => Chronos::parse('2017-01-01')]),
|
|
|
|
new ShortUrl($url, ShortUrlMeta::fromRawData(['validSince' => Chronos::parse('2017-01-01')])),
|
2019-02-17 13:28:34 -06:00
|
|
|
];
|
|
|
|
yield [
|
|
|
|
$url,
|
|
|
|
[],
|
2020-01-26 01:42:51 -06:00
|
|
|
ShortUrlMeta::fromRawData(['findIfExists' => true, 'validUntil' => Chronos::parse('2017-01-01')]),
|
|
|
|
new ShortUrl($url, ShortUrlMeta::fromRawData(['validUntil' => Chronos::parse('2017-01-01')])),
|
2019-02-17 13:28:34 -06:00
|
|
|
];
|
2019-10-11 04:28:53 -05:00
|
|
|
yield [
|
|
|
|
$url,
|
|
|
|
[],
|
2020-01-26 01:42:51 -06:00
|
|
|
ShortUrlMeta::fromRawData(['findIfExists' => true, 'domain' => 'example.com']),
|
|
|
|
new ShortUrl($url, ShortUrlMeta::fromRawData(['domain' => 'example.com'])),
|
2019-10-11 04:28:53 -05:00
|
|
|
];
|
2019-02-17 13:28:34 -06:00
|
|
|
yield [
|
|
|
|
$url,
|
|
|
|
['baz', 'foo', 'bar'],
|
2020-01-26 01:42:51 -06:00
|
|
|
ShortUrlMeta::fromRawData([
|
2019-02-17 13:28:34 -06:00
|
|
|
'findIfExists' => true,
|
|
|
|
'validUntil' => Chronos::parse('2017-01-01'),
|
|
|
|
'maxVisits' => 4,
|
|
|
|
]),
|
2020-01-26 01:42:51 -06:00
|
|
|
(new ShortUrl($url, ShortUrlMeta::fromRawData([
|
2019-02-17 13:28:34 -06:00
|
|
|
'validUntil' => Chronos::parse('2017-01-01'),
|
|
|
|
'maxVisits' => 4,
|
|
|
|
])))->setTags(new ArrayCollection([new Tag('foo'), new Tag('bar'), new Tag('baz')])),
|
2019-02-03 02:40:32 -06:00
|
|
|
];
|
|
|
|
}
|
2016-04-17 06:42:52 -05:00
|
|
|
}
|