Added logic to check if a short code is in use and regenerate it otherwise

This commit is contained in:
Alejandro Celaya
2019-10-11 11:09:33 +02:00
parent 8f2e78c946
commit 9538f474de
7 changed files with 90 additions and 42 deletions

View File

@@ -19,7 +19,6 @@ use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Exception\RuntimeException;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
@@ -55,7 +54,7 @@ class UrlShortenerTest extends TestCase
$shortUrl->setId('10');
});
$repo = $this->prophesize(ShortUrlRepository::class);
$repo->slugIsInUse(Argument::cetera())->willReturn(false);
$repo->shortCodeIsInUse(Argument::cetera())->willReturn(false);
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$this->setUrlShortener(false);
@@ -83,7 +82,7 @@ class UrlShortenerTest extends TestCase
}
/** @test */
public function exceptionIsThrownWhenOrmThrowsException(): void
public function transactionIsRolledBackAndExceptionRethrownWhenExceptionIsThrown(): void
{
$conn = $this->prophesize(Connection::class);
$conn->isTransactionActive()->willReturn(true);
@@ -93,7 +92,7 @@ class UrlShortenerTest extends TestCase
$this->em->flush()->willThrow(new ORMException());
$this->expectException(RuntimeException::class);
$this->expectException(ORMException::class);
$this->urlShortener->urlToShortCode(
new Uri('http://foobar.com/12345/hello?foo=bar'),
[],
@@ -122,11 +121,11 @@ class UrlShortenerTest extends TestCase
public function exceptionIsThrownWhenNonUniqueSlugIsProvided(): void
{
$repo = $this->prophesize(ShortUrlRepository::class);
$slugIsInUse = $repo->slugIsInUse('custom-slug', null)->willReturn(true);
$shortCodeIsInUse = $repo->shortCodeIsInUse('custom-slug', null)->willReturn(true);
$repo->findBy(Argument::cetera())->willReturn([]);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$slugIsInUse->shouldBeCalledOnce();
$shortCodeIsInUse->shouldBeCalledOnce();
$getRepo->shouldBeCalled();
$this->expectException(NonUniqueSlugException::class);
@@ -145,26 +144,24 @@ class UrlShortenerTest extends TestCase
string $url,
array $tags,
ShortUrlMeta $meta,
?ShortUrl $expected
ShortUrl $expected
): void {
$repo = $this->prophesize(ShortUrlRepository::class);
$findExisting = $repo->findBy(Argument::any())->willReturn($expected !== null ? [$expected] : []);
$findExisting = $repo->findBy(Argument::any())->willReturn([$expected]);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$result = $this->urlShortener->urlToShortCode(new Uri($url), $tags, $meta);
$findExisting->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledOnce();
if ($expected) {
$this->assertSame($expected, $result);
}
$this->em->persist(Argument::cetera())->shouldNotHaveBeenCalled();
$this->assertSame($expected, $result);
}
public function provideExistingShortUrls(): iterable
{
$url = 'http://foo.com';
yield [$url, [], ShortUrlMeta::createFromRawData(['findIfExists' => true]), null];
yield [$url, [], ShortUrlMeta::createFromRawData(['findIfExists' => true]), new ShortUrl($url)];
yield [$url, [], ShortUrlMeta::createFromRawData(
['findIfExists' => true, 'customSlug' => 'foo']