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

237 lines
7.4 KiB
PHP
Raw Normal View History

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
use Cocur\Slugify\SlugifyInterface;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
2016-04-17 06:42:52 -05: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 14:34:18 -05:00
use PHPUnit\Framework\TestCase;
2016-04-17 06:42:52 -05:00
use Prophecy\Argument;
use Prophecy\Prophecy\MethodProphecy;
2016-04-17 06:42:52 -05:00
use Prophecy\Prophecy\ObjectProphecy;
2016-07-19 11:01:39 -05:00
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
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
{
/**
* @var UrlShortener
*/
protected $urlShortener;
/**
* @var ObjectProphecy
*/
protected $em;
/**
* @var ObjectProphecy
*/
protected $httpClient;
/**
* @var Cache
*/
protected $cache;
/**
* @var ObjectProphecy
*/
protected $slugger;
2016-04-17 06:42:52 -05: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];
$shortUrl->setId(10);
});
$repo = $this->prophesize(ObjectRepository::class);
$repo->findOneBy(Argument::any())->willReturn(null);
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$this->cache = new ArrayCache();
$this->slugger = $this->prophesize(SlugifyInterface::class);
$this->setUrlShortener(false);
}
/**
* @param bool $urlValidationEnabled
*/
public function setUrlShortener($urlValidationEnabled)
{
$this->urlShortener = new UrlShortener(
$this->httpClient->reveal(),
$this->em->reveal(),
$this->cache,
$urlValidationEnabled,
UrlShortener::DEFAULT_CHARS,
$this->slugger->reveal()
);
2016-04-17 06:42:52 -05:00
}
/**
* @test
*/
public function urlIsProperlyShortened()
{
// 10 -> 12C1c
2016-04-17 06:42:52 -05:00
$shortCode = $this->urlShortener->urlToShortCode(new Uri('http://foobar.com/12345/hello?foo=bar'));
$this->assertEquals('12C1c', $shortCode);
2016-04-17 06:42:52 -05:00
}
/**
* @test
* @expectedException \Shlinkio\Shlink\Core\Exception\RuntimeException
2016-04-17 06:42:52 -05: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
* @expectedException \Shlinkio\Shlink\Core\Exception\InvalidUrlException
2016-04-17 06:42:52 -05:00
*/
public function exceptionIsThrownWhenUrlDoesNotExist()
{
$this->setUrlShortener(true);
2016-04-17 06:42:52 -05: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'));
}
/**
* @test
*/
public function whenShortUrlExistsItsShortcodeIsReturned()
{
$shortUrl = new ShortUrl();
$shortUrl->setShortCode('expected_shortcode');
$repo = $this->prophesize(ObjectRepository::class);
$repo->findOneBy(Argument::any())->willReturn($shortUrl);
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$shortCode = $this->urlShortener->urlToShortCode(new Uri('http://foobar.com/12345/hello?foo=bar'));
$this->assertEquals($shortUrl->getShortCode(), $shortCode);
}
/**
* @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 */
$findBySlug = $repo->findOneBy(['shortCode' => 'custom-slug'])->willReturn(new ShortUrl());
$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 06:42:52 -05:00
/**
* @test
*/
public function shortCodeIsProperlyParsed()
{
// 12C1c -> 10
$shortCode = '12C1c';
2016-04-17 06:42:52 -05:00
$shortUrl = new ShortUrl();
$shortUrl->setShortCode($shortCode)
2016-04-17 06:42:52 -05:00
->setOriginalUrl('expected_url');
$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());
$this->assertFalse($this->cache->contains($shortCode . '_longUrl'));
$url = $this->urlShortener->shortCodeToUrl($shortCode);
2016-04-17 06:42:52 -05:00
$this->assertEquals($shortUrl->getOriginalUrl(), $url);
$this->assertTrue($this->cache->contains($shortCode . '_longUrl'));
2016-04-17 06:42:52 -05:00
}
/**
* @test
* @expectedException \Shlinkio\Shlink\Core\Exception\InvalidShortCodeException
2016-04-17 06:42:52 -05:00
*/
public function invalidCharSetThrowsException()
{
$this->urlShortener->shortCodeToUrl('&/(');
}
/**
* @test
*/
public function cachedShortCodeDoesNotHitDatabase()
{
$shortCode = '12C1c';
$expectedUrl = 'expected_url';
$this->cache->save($shortCode . '_longUrl', $expectedUrl);
$this->em->getRepository(ShortUrl::class)->willReturn(null)->shouldBeCalledTimes(0);
$url = $this->urlShortener->shortCodeToUrl($shortCode);
$this->assertEquals($expectedUrl, $url);
}
2016-04-17 06:42:52 -05:00
}