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

253 lines
9.0 KiB
PHP
Raw Normal View History

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
use Cake\Chronos\Chronos;
use Doctrine\Common\Collections\ArrayCollection;
2016-04-17 06:42:52 -05:00
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMException;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Request;
2017-03-24 14:34:18 -05:00
use PHPUnit\Framework\TestCase;
2016-04-17 06:42:52 -05:00
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
2016-07-19 11:01:39 -05:00
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Tag;
2019-02-16 03:53:45 -06:00
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
2016-07-19 11:01:39 -05:00
use Shlinkio\Shlink\Core\Service\UrlShortener;
2016-04-17 06:42:52 -05:00
use Zend\Diactoros\Uri;
use function array_map;
2016-04-17 06:42:52 -05:00
class UrlShortenerTest extends TestCase
{
/** @var UrlShortener */
private $urlShortener;
/** @var ObjectProphecy */
private $em;
/** @var ObjectProphecy */
private $httpClient;
2016-04-17 06:42:52 -05:00
public function setUp(): void
2016-04-17 06:42:52 -05:00
{
$this->httpClient = $this->prophesize(ClientInterface::class);
$this->em = $this->prophesize(EntityManagerInterface::class);
$conn = $this->prophesize(Connection::class);
$conn->isTransactionActive()->willReturn(false);
$this->em->getConnection()->willReturn($conn->reveal());
$this->em->flush()->willReturn(null);
$this->em->commit()->willReturn(null);
$this->em->beginTransaction()->willReturn(null);
$this->em->persist(Argument::any())->will(function ($arguments) {
/** @var ShortUrl $shortUrl */
$shortUrl = $arguments[0];
$shortUrl->setId('10');
2016-04-17 06:42:52 -05:00
});
$repo = $this->prophesize(ShortUrlRepository::class);
$repo->shortCodeIsInUse(Argument::cetera())->willReturn(false);
2016-04-17 06:42:52 -05:00
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$this->setUrlShortener(false);
}
2019-02-17 13:28:34 -06:00
private function setUrlShortener(bool $urlValidationEnabled): void
{
$this->urlShortener = new UrlShortener(
$this->httpClient->reveal(),
$this->em->reveal(),
new UrlShortenerOptions(['validate_url' => $urlValidationEnabled])
);
2016-04-17 06:42:52 -05:00
}
2019-02-17 13:28:34 -06:00
/** @test */
public function urlIsProperlyShortened(): void
2016-04-17 06:42:52 -05:00
{
$shortUrl = $this->urlShortener->urlToShortCode(
new Uri('http://foobar.com/12345/hello?foo=bar'),
[],
ShortUrlMeta::createEmpty()
);
$this->assertEquals('http://foobar.com/12345/hello?foo=bar', $shortUrl->getLongUrl());
2016-04-17 06:42:52 -05:00
}
2019-02-16 03:53:45 -06:00
/** @test */
public function transactionIsRolledBackAndExceptionRethrownWhenExceptionIsThrown(): void
2016-04-17 06:42:52 -05:00
{
$conn = $this->prophesize(Connection::class);
$conn->isTransactionActive()->willReturn(true);
$this->em->getConnection()->willReturn($conn->reveal());
2018-11-11 06:18:21 -06:00
$this->em->rollback()->shouldBeCalledOnce();
$this->em->close()->shouldBeCalledOnce();
2016-04-17 06:42:52 -05:00
$this->em->flush()->willThrow(new ORMException());
2019-02-16 03:53:45 -06:00
$this->expectException(ORMException::class);
$this->urlShortener->urlToShortCode(
new Uri('http://foobar.com/12345/hello?foo=bar'),
[],
ShortUrlMeta::createEmpty()
);
2016-04-17 06:42:52 -05:00
}
2019-02-16 03:53:45 -06:00
/** @test */
public function exceptionIsThrownWhenUrlDoesNotExist(): void
2016-04-17 06:42:52 -05:00
{
$this->setUrlShortener(true);
2016-04-17 06:42:52 -05:00
$this->httpClient->request(Argument::cetera())->willThrow(
new ClientException('', $this->prophesize(Request::class)->reveal())
);
2019-02-16 03:53:45 -06:00
$this->expectException(InvalidUrlException::class);
$this->urlShortener->urlToShortCode(
new Uri('http://foobar.com/12345/hello?foo=bar'),
[],
ShortUrlMeta::createEmpty()
);
2016-04-17 06:42:52 -05:00
}
2019-02-17 13:28:34 -06:00
/** @test */
public function exceptionIsThrownWhenNonUniqueSlugIsProvided(): void
{
$repo = $this->prophesize(ShortUrlRepository::class);
$shortCodeIsInUse = $repo->shortCodeIsInUse('custom-slug', null)->willReturn(true);
$repo->findBy(Argument::cetera())->willReturn([]);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$shortCodeIsInUse->shouldBeCalledOnce();
$getRepo->shouldBeCalled();
$this->expectException(NonUniqueSlugException::class);
$this->urlShortener->urlToShortCode(
new Uri('http://foobar.com/12345/hello?foo=bar'),
[],
ShortUrlMeta::createFromRawData(['customSlug' => 'custom-slug'])
);
}
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
*/
public function existingShortUrlIsReturnedWhenRequested(
string $url,
array $tags,
ShortUrlMeta $meta,
ShortUrl $expected
): void {
$repo = $this->prophesize(ShortUrlRepository::class);
$findExisting = $repo->findBy(Argument::any())->willReturn([$expected]);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$result = $this->urlShortener->urlToShortCode(new Uri($url), $tags, $meta);
$findExisting->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledOnce();
$this->em->persist(Argument::cetera())->shouldNotHaveBeenCalled();
$this->assertSame($expected, $result);
}
2019-02-17 13:28:34 -06:00
public function provideExistingShortUrls(): iterable
{
$url = 'http://foo.com';
2019-02-17 13:28:34 -06:00
yield [$url, [], ShortUrlMeta::createFromRawData(['findIfExists' => true]), new ShortUrl($url)];
yield [$url, [], ShortUrlMeta::createFromRawData(
['findIfExists' => true, 'customSlug' => 'foo']
), new ShortUrl($url)];
yield [
$url,
['foo', 'bar'],
ShortUrlMeta::createFromRawData(['findIfExists' => true]),
(new ShortUrl($url))->setTags(new ArrayCollection([new Tag('bar'), new Tag('foo')])),
];
yield [
$url,
[],
ShortUrlMeta::createFromRawData(['findIfExists' => true, 'maxVisits' => 3]),
new ShortUrl($url, ShortUrlMeta::createFromRawData(['maxVisits' => 3])),
];
yield [
$url,
[],
ShortUrlMeta::createFromRawData(['findIfExists' => true, 'validSince' => Chronos::parse('2017-01-01')]),
new ShortUrl($url, ShortUrlMeta::createFromRawData(['validSince' => Chronos::parse('2017-01-01')])),
];
yield [
$url,
[],
ShortUrlMeta::createFromRawData(['findIfExists' => true, 'validUntil' => Chronos::parse('2017-01-01')]),
new ShortUrl($url, ShortUrlMeta::createFromRawData(['validUntil' => Chronos::parse('2017-01-01')])),
];
yield [
$url,
['baz', 'foo', 'bar'],
ShortUrlMeta::createFromRawData([
'findIfExists' => true,
'validUntil' => Chronos::parse('2017-01-01'),
'maxVisits' => 4,
]),
(new ShortUrl($url, ShortUrlMeta::createFromRawData([
'validUntil' => Chronos::parse('2017-01-01'),
'maxVisits' => 4,
])))->setTags(new ArrayCollection([new Tag('foo'), new Tag('bar'), new Tag('baz')])),
];
}
/** @test */
public function properExistingShortUrlIsReturnedWhenMultipleMatch(): void
{
$url = 'http://foo.com';
$tags = ['baz', 'foo', 'bar'];
$meta = ShortUrlMeta::createFromRawData([
'findIfExists' => true,
'validUntil' => Chronos::parse('2017-01-01'),
'maxVisits' => 4,
]);
$tagsCollection = new ArrayCollection(array_map(function (string $tag) {
return new Tag($tag);
}, $tags));
$expected = (new ShortUrl($url, $meta))->setTags($tagsCollection);
$repo = $this->prophesize(ShortUrlRepository::class);
$findExisting = $repo->findBy(Argument::any())->willReturn([
new ShortUrl($url),
new ShortUrl($url, $meta),
$expected,
(new ShortUrl($url))->setTags($tagsCollection),
]);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$result = $this->urlShortener->urlToShortCode(new Uri($url), $tags, $meta);
$this->assertSame($expected, $result);
$findExisting->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledOnce();
}
2019-02-17 13:28:34 -06:00
/** @test */
public function shortCodeIsProperlyParsed(): void
2016-04-17 06:42:52 -05:00
{
$shortUrl = new ShortUrl('expected_url');
$shortCode = $shortUrl->getShortCode();
2016-04-17 06:42:52 -05:00
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
$repo->findOneByShortCode($shortCode, null)->willReturn($shortUrl);
2016-04-17 06:42:52 -05:00
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$url = $this->urlShortener->shortCodeToUrl($shortCode);
$this->assertSame($shortUrl, $url);
2016-04-17 06:42:52 -05:00
}
}