2016-04-17 13:42:52 +02:00
|
|
|
<?php
|
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
|
|
|
|
2017-10-21 17:18:57 +02:00
|
|
|
use Cocur\Slugify\SlugifyInterface;
|
2016-04-17 13:42:52 +02:00
|
|
|
use Doctrine\Common\Persistence\ObjectRepository;
|
|
|
|
|
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 20:34:18 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-04-17 13:42:52 +02:00
|
|
|
use Prophecy\Argument;
|
2017-10-21 17:18:57 +02:00
|
|
|
use Prophecy\Prophecy\MethodProphecy;
|
2016-04-17 13:42:52 +02:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2016-07-19 18:01:39 +02:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2017-10-21 17:18:57 +02:00
|
|
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
2017-10-21 11:58:20 +02:00
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
|
2016-07-19 18:01:39 +02:00
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
2016-04-17 13:42:52 +02:00
|
|
|
use Zend\Diactoros\Uri;
|
|
|
|
|
|
|
|
|
|
class UrlShortenerTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @var UrlShortener
|
|
|
|
|
*/
|
|
|
|
|
protected $urlShortener;
|
|
|
|
|
/**
|
|
|
|
|
* @var ObjectProphecy
|
|
|
|
|
*/
|
|
|
|
|
protected $em;
|
|
|
|
|
/**
|
|
|
|
|
* @var ObjectProphecy
|
|
|
|
|
*/
|
|
|
|
|
protected $httpClient;
|
2017-10-21 17:18:57 +02:00
|
|
|
/**
|
|
|
|
|
* @var ObjectProphecy
|
|
|
|
|
*/
|
|
|
|
|
protected $slugger;
|
2016-04-17 13:42:52 +02:00
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
|
{
|
|
|
|
|
$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 10:03:42 +02:00
|
|
|
$shortUrl->setId('10');
|
2016-04-17 13:42:52 +02:00
|
|
|
});
|
|
|
|
|
$repo = $this->prophesize(ObjectRepository::class);
|
|
|
|
|
$repo->findOneBy(Argument::any())->willReturn(null);
|
|
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
|
2017-10-21 17:18:57 +02:00
|
|
|
$this->slugger = $this->prophesize(SlugifyInterface::class);
|
2016-08-08 10:02:52 +02:00
|
|
|
|
2017-10-17 11:03:12 +02:00
|
|
|
$this->setUrlShortener(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-17 11:44:30 +02:00
|
|
|
* @param bool $urlValidationEnabled
|
2017-10-17 11:03:12 +02:00
|
|
|
*/
|
2017-10-17 11:44:30 +02:00
|
|
|
public function setUrlShortener($urlValidationEnabled)
|
2017-10-17 11:03:12 +02:00
|
|
|
{
|
2017-10-21 17:18:57 +02:00
|
|
|
$this->urlShortener = new UrlShortener(
|
|
|
|
|
$this->httpClient->reveal(),
|
|
|
|
|
$this->em->reveal(),
|
2017-10-17 11:44:30 +02:00
|
|
|
$urlValidationEnabled,
|
2017-10-21 17:18:57 +02:00
|
|
|
UrlShortener::DEFAULT_CHARS,
|
|
|
|
|
$this->slugger->reveal()
|
|
|
|
|
);
|
2016-04-17 13:42:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @test
|
|
|
|
|
*/
|
|
|
|
|
public function urlIsProperlyShortened()
|
|
|
|
|
{
|
2016-05-01 11:24:00 +02:00
|
|
|
// 10 -> 12C1c
|
2018-09-12 20:32:58 +02:00
|
|
|
$shortUrl = $this->urlShortener->urlToShortCode(new Uri('http://foobar.com/12345/hello?foo=bar'));
|
|
|
|
|
$this->assertEquals('12C1c', $shortUrl->getShortCode());
|
2016-04-17 13:42:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @test
|
2017-12-30 21:35:26 +01:00
|
|
|
* @expectedException \Shlinkio\Shlink\Core\Exception\RuntimeException
|
2016-04-17 13:42:52 +02:00
|
|
|
*/
|
|
|
|
|
public function exceptionIsThrownWhenOrmThrowsException()
|
|
|
|
|
{
|
|
|
|
|
$conn = $this->prophesize(Connection::class);
|
|
|
|
|
$conn->isTransactionActive()->willReturn(true);
|
|
|
|
|
$this->em->getConnection()->willReturn($conn->reveal());
|
|
|
|
|
$this->em->rollback()->shouldBeCalledTimes(1);
|
|
|
|
|
$this->em->close()->shouldBeCalledTimes(1);
|
|
|
|
|
|
|
|
|
|
$this->em->flush()->willThrow(new ORMException());
|
|
|
|
|
$this->urlShortener->urlToShortCode(new Uri('http://foobar.com/12345/hello?foo=bar'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @test
|
2016-07-19 18:05:06 +02:00
|
|
|
* @expectedException \Shlinkio\Shlink\Core\Exception\InvalidUrlException
|
2016-04-17 13:42:52 +02:00
|
|
|
*/
|
|
|
|
|
public function exceptionIsThrownWhenUrlDoesNotExist()
|
|
|
|
|
{
|
2017-10-17 11:03:12 +02:00
|
|
|
$this->setUrlShortener(true);
|
|
|
|
|
|
2016-04-17 13:42:52 +02:00
|
|
|
$this->httpClient->request(Argument::cetera())->willThrow(
|
|
|
|
|
new ClientException('', $this->prophesize(Request::class)->reveal())
|
|
|
|
|
);
|
|
|
|
|
$this->urlShortener->urlToShortCode(new Uri('http://foobar.com/12345/hello?foo=bar'));
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 17:18:57 +02:00
|
|
|
/**
|
|
|
|
|
* @test
|
|
|
|
|
*/
|
|
|
|
|
public function whenCustomSlugIsProvidedItIsUsed()
|
|
|
|
|
{
|
|
|
|
|
/** @var MethodProphecy $slugify */
|
|
|
|
|
$slugify = $this->slugger->slugify('custom-slug')->willReturnArgument();
|
|
|
|
|
|
|
|
|
|
$this->urlShortener->urlToShortCode(
|
|
|
|
|
new Uri('http://foobar.com/12345/hello?foo=bar'),
|
|
|
|
|
[],
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
'custom-slug'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$slugify->shouldHaveBeenCalledTimes(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @test
|
|
|
|
|
*/
|
|
|
|
|
public function exceptionIsThrownWhenNonUniqueSlugIsProvided()
|
|
|
|
|
{
|
|
|
|
|
/** @var MethodProphecy $slugify */
|
|
|
|
|
$slugify = $this->slugger->slugify('custom-slug')->willReturnArgument();
|
|
|
|
|
|
|
|
|
|
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
|
|
|
|
|
/** @var MethodProphecy $findBySlug */
|
2018-10-28 16:00:54 +01:00
|
|
|
$findBySlug = $repo->findOneBy(['shortCode' => 'custom-slug'])->willReturn(new ShortUrl(''));
|
2017-10-21 17:18:57 +02:00
|
|
|
$repo->findOneBy(Argument::cetera())->willReturn(null);
|
|
|
|
|
/** @var MethodProphecy $getRepo */
|
|
|
|
|
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
|
|
|
|
|
$slugify->shouldBeCalledTimes(1);
|
|
|
|
|
$findBySlug->shouldBeCalledTimes(1);
|
|
|
|
|
$getRepo->shouldBeCalled();
|
|
|
|
|
$this->expectException(NonUniqueSlugException::class);
|
|
|
|
|
|
|
|
|
|
$this->urlShortener->urlToShortCode(
|
|
|
|
|
new Uri('http://foobar.com/12345/hello?foo=bar'),
|
|
|
|
|
[],
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
'custom-slug'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-17 13:42:52 +02:00
|
|
|
/**
|
|
|
|
|
* @test
|
|
|
|
|
*/
|
|
|
|
|
public function shortCodeIsProperlyParsed()
|
|
|
|
|
{
|
2016-05-01 11:24:00 +02:00
|
|
|
// 12C1c -> 10
|
2016-08-08 10:02:52 +02:00
|
|
|
$shortCode = '12C1c';
|
2018-10-28 16:00:54 +01:00
|
|
|
$shortUrl = new ShortUrl('expected_url');
|
|
|
|
|
$shortUrl->setShortCode($shortCode);
|
2016-04-17 13:42:52 +02:00
|
|
|
|
2017-10-21 11:58:20 +02:00
|
|
|
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
|
|
|
|
|
$repo->findOneByShortCode($shortCode)->willReturn($shortUrl);
|
2016-04-17 13:42:52 +02:00
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
|
|
|
|
|
2016-08-08 10:02:52 +02:00
|
|
|
$url = $this->urlShortener->shortCodeToUrl($shortCode);
|
2018-08-11 10:40:44 +02:00
|
|
|
$this->assertSame($shortUrl, $url);
|
2016-04-17 13:42:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @test
|
2016-07-19 18:05:06 +02:00
|
|
|
* @expectedException \Shlinkio\Shlink\Core\Exception\InvalidShortCodeException
|
2016-04-17 13:42:52 +02:00
|
|
|
*/
|
|
|
|
|
public function invalidCharSetThrowsException()
|
|
|
|
|
{
|
|
|
|
|
$this->urlShortener->shortCodeToUrl('&/(');
|
|
|
|
|
}
|
|
|
|
|
}
|