|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Core\Entity; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortCodeCannotBeRegeneratedException; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta; |
|
11
|
|
|
|
|
12
|
|
|
class ShortUrlTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @test |
|
16
|
|
|
* @dataProvider provideInvalidShortUrls |
|
17
|
|
|
*/ |
|
18
|
|
|
public function regenerateShortCodeThrowsExceptionIfStateIsInvalid( |
|
19
|
|
|
ShortUrl $shortUrl, |
|
20
|
|
|
string $expectedMessage |
|
21
|
|
|
): void { |
|
22
|
|
|
$this->expectException(ShortCodeCannotBeRegeneratedException::class); |
|
23
|
|
|
$this->expectExceptionMessage($expectedMessage); |
|
24
|
|
|
|
|
25
|
|
|
$shortUrl->regenerateShortCode(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function provideInvalidShortUrls(): iterable |
|
29
|
|
|
{ |
|
30
|
|
|
yield 'with custom slug' => [ |
|
31
|
|
|
new ShortUrl('', ShortUrlMeta::createFromRawData(['customSlug' => 'custom-slug'])), |
|
32
|
|
|
'The short code cannot be regenerated on ShortUrls where a custom slug was provided.', |
|
33
|
|
|
]; |
|
34
|
|
|
yield 'already persisted' => [ |
|
35
|
|
|
(new ShortUrl(''))->setId('1'), |
|
36
|
|
|
'The short code can be regenerated only on new ShortUrls which have not been persisted yet.', |
|
37
|
|
|
]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** @test */ |
|
41
|
|
|
public function regenerateShortCodeProperlyChangesTheValueOnValidShortUrls(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$shortUrl = new ShortUrl(''); |
|
44
|
|
|
$firstShortCode = $shortUrl->getShortCode(); |
|
45
|
|
|
|
|
46
|
|
|
$shortUrl->regenerateShortCode(); |
|
47
|
|
|
$secondShortCode = $shortUrl->getShortCode(); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertNotEquals($firstShortCode, $secondShortCode); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|