mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-23 07:33:58 -06:00
Added more tests for new logics
This commit is contained in:
parent
9538f474de
commit
5bd7b53e0a
@ -6,24 +6,13 @@ namespace Shlinkio\Shlink\Core\Exception;
|
|||||||
|
|
||||||
class ShortCodeCannotBeRegeneratedException extends RuntimeException
|
class ShortCodeCannotBeRegeneratedException extends RuntimeException
|
||||||
{
|
{
|
||||||
/** @var @bool */
|
|
||||||
private $reasonIsSlug = false;
|
|
||||||
|
|
||||||
public static function forShortUrlWithCustomSlug(): self
|
public static function forShortUrlWithCustomSlug(): self
|
||||||
{
|
{
|
||||||
$e = new self('The short code cannot be regenerated on ShortUrls where a custom slug was provided.');
|
return new self('The short code cannot be regenerated on ShortUrls where a custom slug was provided.');
|
||||||
$e->reasonIsSlug = true;
|
|
||||||
|
|
||||||
return $e;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function forShortUrlAlreadyPersisted(): self
|
public static function forShortUrlAlreadyPersisted(): self
|
||||||
{
|
{
|
||||||
return new self('The short code can be regenerated only on new ShortUrls which have not been persisted yet.');
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
51
module/Core/test/Entity/ShortUrlTest.php
Normal file
51
module/Core/test/Entity/ShortUrlTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -81,6 +81,32 @@ class UrlShortenerTest extends TestCase
|
|||||||
$this->assertEquals('http://foobar.com/12345/hello?foo=bar', $shortUrl->getLongUrl());
|
$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 */
|
/** @test */
|
||||||
public function transactionIsRolledBackAndExceptionRethrownWhenExceptionIsThrown(): void
|
public function transactionIsRolledBackAndExceptionRethrownWhenExceptionIsThrown(): void
|
||||||
{
|
{
|
||||||
@ -190,6 +216,12 @@ class UrlShortenerTest extends TestCase
|
|||||||
ShortUrlMeta::createFromRawData(['findIfExists' => true, 'validUntil' => Chronos::parse('2017-01-01')]),
|
ShortUrlMeta::createFromRawData(['findIfExists' => true, 'validUntil' => Chronos::parse('2017-01-01')]),
|
||||||
new ShortUrl($url, ShortUrlMeta::createFromRawData(['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 [
|
yield [
|
||||||
$url,
|
$url,
|
||||||
['baz', 'foo', 'bar'],
|
['baz', 'foo', 'bar'],
|
||||||
|
Loading…
Reference in New Issue
Block a user