Moved short code uniqueness checks to external helper class that is used in UrlShortener and ImportedLinksProcessor

This commit is contained in:
Alejandro Celaya
2020-10-25 11:16:42 +01:00
parent b1a073b1ab
commit 786e4f642b
9 changed files with 180 additions and 82 deletions

View File

@@ -10,8 +10,8 @@ use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\ShortCodeCannotBeRegeneratedException;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
use function Functional\map;
use function range;
use function strlen;

View File

@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Service\ShortUrl;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortCodeHelper;
class ShortCodeHelperTest extends TestCase
{
private ShortCodeHelper $helper;
private ObjectProphecy $em;
private ObjectProphecy $shortUrl;
protected function setUp(): void
{
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->helper = new ShortCodeHelper($this->em->reveal());
$this->shortUrl = $this->prophesize(ShortUrl::class);
$this->shortUrl->getShortCode()->willReturn('abc123');
}
/**
* @test
* @dataProvider provideDomains
*/
public function shortCodeIsRegeneratedIfAlreadyInUse(?Domain $domain, ?string $expectedAuthority): void
{
$callIndex = 0;
$expectedCalls = 3;
$repo = $this->prophesize(ShortUrlRepository::class);
$shortCodeIsInUse = $repo->shortCodeIsInUse('abc123', $expectedAuthority)->will(
function () use (&$callIndex, $expectedCalls) {
$callIndex++;
return $callIndex < $expectedCalls;
},
);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$this->shortUrl->getDomain()->willReturn($domain);
$result = $this->helper->ensureShortCodeUniqueness($this->shortUrl->reveal(), false);
self::assertTrue($result);
$this->shortUrl->regenerateShortCode()->shouldHaveBeenCalledTimes($expectedCalls - 1);
$getRepo->shouldBeCalledTimes($expectedCalls);
$shortCodeIsInUse->shouldBeCalledTimes($expectedCalls);
}
public function provideDomains(): iterable
{
yield 'no domain' => [null, null];
yield 'domain' => [new Domain($authority = 'doma.in'), $authority];
}
/** @test */
public function inUseSlugReturnsError(): void
{
$repo = $this->prophesize(ShortUrlRepository::class);
$shortCodeIsInUse = $repo->shortCodeIsInUse('abc123', null)->willReturn(true);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$this->shortUrl->getDomain()->willReturn(null);
$result = $this->helper->ensureShortCodeUniqueness($this->shortUrl->reveal(), true);
self::assertFalse($result);
$this->shortUrl->regenerateShortCode()->shouldNotHaveBeenCalled();
$getRepo->shouldBeCalledOnce();
$shortCodeIsInUse->shouldBeCalledOnce();
}
}

View File

@@ -18,6 +18,7 @@ use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortCodeHelperInterface;
use Shlinkio\Shlink\Core\Service\UrlShortener;
use Shlinkio\Shlink\Core\Util\UrlValidatorInterface;
@@ -26,6 +27,7 @@ class UrlShortenerTest extends TestCase
private UrlShortener $urlShortener;
private ObjectProphecy $em;
private ObjectProphecy $urlValidator;
private ObjectProphecy $shortCodeHelper;
public function setUp(): void
{
@@ -51,10 +53,14 @@ class UrlShortenerTest extends TestCase
$repo->shortCodeIsInUse(Argument::cetera())->willReturn(false);
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$this->shortCodeHelper = $this->prophesize(ShortCodeHelperInterface::class);
$this->shortCodeHelper->ensureShortCodeUniqueness(Argument::cetera())->willReturn(true);
$this->urlShortener = new UrlShortener(
$this->urlValidator->reveal(),
$this->em->reveal(),
new SimpleDomainResolver(),
$this->shortCodeHelper->reveal(),
);
}
@@ -71,29 +77,18 @@ class UrlShortenerTest extends TestCase
}
/** @test */
public function shortCodeIsRegeneratedIfAlreadyInUse(): void
public function exceptionIsThrownWhenNonUniqueSlugIsProvided(): void
{
$callIndex = 0;
$expectedCalls = 3;
$repo = $this->prophesize(ShortUrlRepository::class);
$shortCodeIsInUse = $repo->shortCodeIsInUse(Argument::cetera())->will(
function () use (&$callIndex, $expectedCalls) {
$callIndex++;
return $callIndex < $expectedCalls;
},
);
$repo->findBy(Argument::cetera())->willReturn([]);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$ensureUniqueness = $this->shortCodeHelper->ensureShortCodeUniqueness(Argument::cetera())->willReturn(false);
$shortUrl = $this->urlShortener->urlToShortCode(
$ensureUniqueness->shouldBeCalledOnce();
$this->expectException(NonUniqueSlugException::class);
$this->urlShortener->urlToShortCode(
'http://foobar.com/12345/hello?foo=bar',
[],
ShortUrlMeta::createEmpty(),
ShortUrlMeta::fromRawData(['customSlug' => 'custom-slug']),
);
self::assertEquals('http://foobar.com/12345/hello?foo=bar', $shortUrl->getLongUrl());
$getRepo->shouldBeCalledTimes($expectedCalls);
$shortCodeIsInUse->shouldBeCalledTimes($expectedCalls);
}
/** @test */
@@ -115,25 +110,6 @@ class UrlShortenerTest extends TestCase
);
}
/** @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(
'http://foobar.com/12345/hello?foo=bar',
[],
ShortUrlMeta::fromRawData(['customSlug' => 'custom-slug']),
);
}
/**
* @test
* @dataProvider provideExistingShortUrls