2016-04-17 06:42:52 -05:00
|
|
|
<?php
|
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\InvalidShortCodeException;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
2017-10-21 10:18:57 -05:00
|
|
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
2019-02-16 03:53:45 -06:00
|
|
|
use Shlinkio\Shlink\Core\Exception\RuntimeException;
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
$repo->count(Argument::any())->willReturn(0);
|
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-02-03 06:02:12 -06:00
|
|
|
// 10 -> 0Q1Y
|
2019-01-29 06:55:47 -06:00
|
|
|
$shortUrl = $this->urlShortener->urlToShortCode(
|
|
|
|
new Uri('http://foobar.com/12345/hello?foo=bar'),
|
|
|
|
[],
|
|
|
|
ShortUrlMeta::createEmpty()
|
|
|
|
);
|
2019-02-03 06:02:12 -06:00
|
|
|
$this->assertEquals('0Q1Y', $shortUrl->getShortCode());
|
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 exceptionIsThrownWhenOrmThrowsException(): 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(RuntimeException::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);
|
|
|
|
$countBySlug = $repo->count(['shortCode' => 'custom-slug'])->willReturn(1);
|
2017-10-21 10:18:57 -05:00
|
|
|
$repo->findOneBy(Argument::cetera())->willReturn(null);
|
|
|
|
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
2019-01-29 06:55:47 -06:00
|
|
|
$countBySlug->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,
|
|
|
|
?ShortUrl $expected
|
|
|
|
): void {
|
|
|
|
$repo = $this->prophesize(ShortUrlRepository::class);
|
|
|
|
$findExisting = $repo->findOneBy(Argument::any())->willReturn($expected);
|
|
|
|
$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
|
|
|
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-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
|
|
|
{
|
2016-08-08 03:02:52 -05:00
|
|
|
$shortCode = '12C1c';
|
2018-10-28 10:00:54 -05:00
|
|
|
$shortUrl = new ShortUrl('expected_url');
|
|
|
|
$shortUrl->setShortCode($shortCode);
|
2016-04-17 06:42:52 -05:00
|
|
|
|
2017-10-21 04:58:20 -05:00
|
|
|
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
|
|
|
|
$repo->findOneByShortCode($shortCode)->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
|
|
|
}
|
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
/** @test */
|
2019-02-03 02:40:32 -06:00
|
|
|
public function invalidCharSetThrowsException(): void
|
2016-04-17 06:42:52 -05:00
|
|
|
{
|
2019-02-16 03:53:45 -06:00
|
|
|
$this->expectException(InvalidShortCodeException::class);
|
2016-04-17 06:42:52 -05:00
|
|
|
$this->urlShortener->shortCodeToUrl('&/(');
|
|
|
|
}
|
|
|
|
}
|