Added more tests for new logics

This commit is contained in:
Alejandro Celaya 2019-10-11 11:28:53 +02:00
parent 9538f474de
commit 5bd7b53e0a
3 changed files with 84 additions and 12 deletions

View File

@ -6,24 +6,13 @@ namespace Shlinkio\Shlink\Core\Exception;
class ShortCodeCannotBeRegeneratedException extends RuntimeException
{
/** @var @bool */
private $reasonIsSlug = false;
public static function forShortUrlWithCustomSlug(): self
{
$e = new self('The short code cannot be regenerated on ShortUrls where a custom slug was provided.');
$e->reasonIsSlug = true;
return $e;
return new self('The short code cannot be regenerated on ShortUrls where a custom slug was provided.');
}
public static function forShortUrlAlreadyPersisted(): self
{
return new self('The short code can be regenerated only on new ShortUrls which have not been persisted yet.');
}
public function reasonIsSlug(): bool
{
return $this->reasonIsSlug;
}
}

View File

@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Entity;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\ShortCodeCannotBeRegeneratedException;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
class ShortUrlTest extends TestCase
{
/**
* @test
* @dataProvider provideInvalidShortUrls
*/
public function regenerateShortCodeThrowsExceptionIfStateIsInvalid(
ShortUrl $shortUrl,
string $expectedMessage
): void {
$this->expectException(ShortCodeCannotBeRegeneratedException::class);
$this->expectExceptionMessage($expectedMessage);
$shortUrl->regenerateShortCode();
}
public function provideInvalidShortUrls(): iterable
{
yield 'with custom slug' => [
new ShortUrl('', ShortUrlMeta::createFromRawData(['customSlug' => 'custom-slug'])),
'The short code cannot be regenerated on ShortUrls where a custom slug was provided.',
];
yield 'already persisted' => [
(new ShortUrl(''))->setId('1'),
'The short code can be regenerated only on new ShortUrls which have not been persisted yet.',
];
}
/** @test */
public function regenerateShortCodeProperlyChangesTheValueOnValidShortUrls(): void
{
$shortUrl = new ShortUrl('');
$firstShortCode = $shortUrl->getShortCode();
$shortUrl->regenerateShortCode();
$secondShortCode = $shortUrl->getShortCode();
$this->assertNotEquals($firstShortCode, $secondShortCode);
}
}

View File

@ -81,6 +81,32 @@ class UrlShortenerTest extends TestCase
$this->assertEquals('http://foobar.com/12345/hello?foo=bar', $shortUrl->getLongUrl());
}
/** @test */
public function shortCodeIsRegeneratedIfAlreadyInUse(): 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());
$shortUrl = $this->urlShortener->urlToShortCode(
new Uri('http://foobar.com/12345/hello?foo=bar'),
[],
ShortUrlMeta::createEmpty()
);
$this->assertEquals('http://foobar.com/12345/hello?foo=bar', $shortUrl->getLongUrl());
$getRepo->shouldBeCalledTimes($expectedCalls);
$shortCodeIsInUse->shouldBeCalledTimes($expectedCalls);
}
/** @test */
public function transactionIsRolledBackAndExceptionRethrownWhenExceptionIsThrown(): void
{
@ -190,6 +216,12 @@ class UrlShortenerTest extends TestCase
ShortUrlMeta::createFromRawData(['findIfExists' => true, 'validUntil' => Chronos::parse('2017-01-01')]),
new ShortUrl($url, ShortUrlMeta::createFromRawData(['validUntil' => Chronos::parse('2017-01-01')])),
];
yield [
$url,
[],
ShortUrlMeta::createFromRawData(['findIfExists' => true, 'domain' => 'example.com']),
new ShortUrl($url, ShortUrlMeta::createFromRawData(['domain' => 'example.com'])),
];
yield [
$url,
['baz', 'foo', 'bar'],