Files
shlink/module/Core/test/Service/UrlShortenerTest.php
T

165 lines
6.5 KiB
PHP
Raw Normal View History

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
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;
use Psr\EventDispatcher\EventDispatcherInterface;
2016-07-19 18:01:39 +02:00
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
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;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlTitleResolutionHelperInterface;
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;
private UrlShortener $urlShortener;
private ObjectProphecy $em;
private ObjectProphecy $titleResolutionHelper;
private ObjectProphecy $shortCodeHelper;
private ObjectProphecy $eventDispatcher;
2016-04-17 13:42:52 +02:00
public function setUp(): void
2016-04-17 13:42:52 +02: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 */
[$shortUrl] = $arguments;
$shortUrl->setId('10');
2016-04-17 13:42:52 +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();
});
$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);
$this->shortCodeHelper->ensureShortCodeUniqueness(Argument::cetera())->willReturn(true);
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$this->urlShortener = new UrlShortener(
$this->titleResolutionHelper->reveal(),
$this->em->reveal(),
new SimpleShortUrlRelationResolver(),
$this->shortCodeHelper->reveal(),
$this->eventDispatcher->reveal(),
);
2016-04-17 13:42:52 +02: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';
$meta = ShortUrlMeta::fromRawData(['longUrl' => $longUrl]);
$shortUrl = $this->urlShortener->shorten($meta);
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 */
public function exceptionIsThrownWhenNonUniqueSlugIsProvided(): void
2019-10-11 11:28:53 +02:00
{
$ensureUniqueness = $this->shortCodeHelper->ensureShortCodeUniqueness(Argument::cetera())->willReturn(false);
2019-10-11 11:28:53 +02:00
$ensureUniqueness->shouldBeCalledOnce();
$this->expectException(NonUniqueSlugException::class);
2021-01-30 19:17:12 +01:00
$this->urlShortener->shorten(ShortUrlMeta::fromRawData(
['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
{
$repo = $this->prophesize(ShortUrlRepository::class);
$findExisting = $repo->findOneMatching(Argument::cetera())->willReturn($expected);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
2021-01-30 19:17:12 +01:00
$result = $this->urlShortener->shorten($meta);
$findExisting->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledOnce();
$this->em->persist(Argument::cetera())->shouldNotHaveBeenCalled();
$this->titleResolutionHelper->processTitleAndValidateUrl(Argument::cetera())->shouldNotHaveBeenCalled();
2020-10-04 00:35:14 +02:00
self::assertSame($expected, $result);
}
2019-02-17 20:28:34 +01:00
public function provideExistingShortUrls(): iterable
{
$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 19:17:12 +01:00
yield [ShortUrlMeta::fromRawData(
['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']]),
ShortUrl::fromMeta(ShortUrlMeta::fromRawData(['longUrl' => $url, 'tags' => ['foo', 'bar']])),
2019-02-17 20:28:34 +01:00
];
yield [
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 [
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 [
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 [
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 [
ShortUrlMeta::fromRawData([
2019-02-17 20:28:34 +01:00
'findIfExists' => true,
'validUntil' => Chronos::parse('2017-01-01'),
'maxVisits' => 4,
'longUrl' => $url,
2021-01-30 19:17:12 +01:00
'tags' => ['baz', 'foo', 'bar'],
2019-02-17 20:28:34 +01:00
]),
ShortUrl::fromMeta(ShortUrlMeta::fromRawData([
2019-02-17 20:28:34 +01:00
'validUntil' => Chronos::parse('2017-01-01'),
'maxVisits' => 4,
'longUrl' => $url,
'tags' => ['foo', 'bar', 'baz'],
])),
];
}
2016-04-17 13:42:52 +02:00
}