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\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;
|
2019-02-03 02:40:32 -06:00
|
|
|
use Shlinkio\Shlink\Core\Entity\Tag;
|
2019-02-16 03:53:45 -06:00
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
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;
|
2018-12-01 14:38:29 -06:00
|
|
|
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
|
2019-01-29 06:55:47 -06:00
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
|
2017-10-21 04:58:20 -05:00
|
|
|
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;
|
|
|
|
|
2019-03-30 01:45:57 -05:00
|
|
|
use function array_map;
|
|
|
|
|
2016-04-17 06:42:52 -05:00
|
|
|
class UrlShortenerTest extends TestCase
|
|
|
|
{
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var UrlShortener */
|
2018-11-20 12:37:22 -06:00
|
|
|
private $urlShortener;
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 12:37:22 -06:00
|
|
|
private $em;
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 12:37:22 -06:00
|
|
|
private $httpClient;
|
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
|
|
|
{
|
|
|
|
$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];
|
2018-09-15 03:03:42 -05:00
|
|
|
$shortUrl->setId('10');
|
2016-04-17 06:42:52 -05:00
|
|
|
});
|
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());
|
|
|
|
|
2017-10-17 04:03:12 -05:00
|
|
|
$this->setUrlShortener(false);
|
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
private function setUrlShortener(bool $urlValidationEnabled): void
|
2017-10-17 04:03:12 -05:00
|
|
|
{
|
2017-10-21 10:18:57 -05:00
|
|
|
$this->urlShortener = new UrlShortener(
|
|
|
|
$this->httpClient->reveal(),
|
|
|
|
$this->em->reveal(),
|
2019-02-03 01:17:22 -06:00
|
|
|
new UrlShortenerOptions(['validate_url' => $urlValidationEnabled])
|
2017-10-21 10:18:57 -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
|
|
|
{
|
2019-01-29 06:55:47 -06:00
|
|
|
$shortUrl = $this->urlShortener->urlToShortCode(
|
|
|
|
new Uri('http://foobar.com/12345/hello?foo=bar'),
|
|
|
|
[],
|
|
|
|
ShortUrlMeta::createEmpty()
|
|
|
|
);
|
2019-10-11 02:14:25 -05:00
|
|
|
|
|
|
|
$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 */
|
2019-10-11 04:09:33 -05:00
|
|
|
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
|
|
|
|
2019-10-11 04:09:33 -05:00
|
|
|
$this->expectException(ORMException::class);
|
2019-01-29 06:55:47 -06:00
|
|
|
$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 */
|
2019-02-03 02:40:32 -06:00
|
|
|
public function exceptionIsThrownWhenUrlDoesNotExist(): void
|
2016-04-17 06:42:52 -05:00
|
|
|
{
|
2017-10-17 04:03:12 -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);
|
2019-01-29 06:55:47 -06:00
|
|
|
$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 */
|
2019-02-03 02:40:32 -06:00
|
|
|
public function exceptionIsThrownWhenNonUniqueSlugIsProvided(): void
|
2017-10-21 10:18:57 -05:00
|
|
|
{
|
2019-01-29 06:55:47 -06:00
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class);
|
2019-10-11 04:09:33 -05:00
|
|
|
$shortCodeIsInUse = $repo->shortCodeIsInUse('custom-slug', null)->willReturn(true);
|
2019-03-30 01:36:57 -05:00
|
|
|
$repo->findBy(Argument::cetera())->willReturn([]);
|
2017-10-21 10:18:57 -05:00
|
|
|
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
2019-10-11 04:09:33 -05:00
|
|
|
$shortCodeIsInUse->shouldBeCalledOnce();
|
2017-10-21 10:18:57 -05:00
|
|
|
$getRepo->shouldBeCalled();
|
|
|
|
$this->expectException(NonUniqueSlugException::class);
|
|
|
|
|
|
|
|
$this->urlShortener->urlToShortCode(
|
|
|
|
new Uri('http://foobar.com/12345/hello?foo=bar'),
|
|
|
|
[],
|
2019-01-29 06:55:47 -06:00
|
|
|
ShortUrlMeta::createFromRawData(['customSlug' => 'custom-slug'])
|
2017-10-21 10:18:57 -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);
|
2019-10-11 04:09:33 -05:00
|
|
|
$findExisting = $repo->findBy(Argument::any())->willReturn([$expected]);
|
2019-02-03 02:40:32 -06:00
|
|
|
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
$result = $this->urlShortener->urlToShortCode(new Uri($url), $tags, $meta);
|
|
|
|
|
|
|
|
$findExisting->shouldHaveBeenCalledOnce();
|
|
|
|
$getRepo->shouldHaveBeenCalledOnce();
|
2019-10-11 04:09:33 -05:00
|
|
|
$this->em->persist(Argument::cetera())->shouldNotHaveBeenCalled();
|
|
|
|
$this->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';
|
|
|
|
|
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')])),
|
2019-02-03 02:40:32 -06:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-03-30 01:45:57 -05:00
|
|
|
/** @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 */
|
2019-02-03 02:40:32 -06:00
|
|
|
public function shortCodeIsProperlyParsed(): void
|
2016-04-17 06:42:52 -05:00
|
|
|
{
|
2018-10-28 10:00:54 -05:00
|
|
|
$shortUrl = new ShortUrl('expected_url');
|
2019-10-11 02:14:25 -05:00
|
|
|
$shortCode = $shortUrl->getShortCode();
|
2016-04-17 06:42:52 -05:00
|
|
|
|
2017-10-21 04:58:20 -05:00
|
|
|
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
|
2019-10-04 10:21:22 -05:00
|
|
|
$repo->findOneByShortCode($shortCode, null)->willReturn($shortUrl);
|
2016-04-17 06:42:52 -05:00
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
2016-08-08 03:02:52 -05:00
|
|
|
$url = $this->urlShortener->shortCodeToUrl($shortCode);
|
2018-08-11 03:40:44 -05:00
|
|
|
$this->assertSame($shortUrl, $url);
|
2016-04-17 06:42:52 -05:00
|
|
|
}
|
|
|
|
}
|