|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Core\Entity; |
|
6
|
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortCodeCannotBeRegeneratedException; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta; |
|
12
|
|
|
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter; |
|
13
|
|
|
|
|
14
|
|
|
use function Functional\map; |
|
15
|
|
|
use function range; |
|
16
|
|
|
use function strlen; |
|
17
|
|
|
|
|
18
|
|
|
use const Shlinkio\Shlink\Core\DEFAULT_SHORT_CODES_LENGTH; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
class ShortUrlTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @test |
|
24
|
|
|
* @dataProvider provideInvalidShortUrls |
|
25
|
|
|
*/ |
|
26
|
|
|
public function regenerateShortCodeThrowsExceptionIfStateIsInvalid( |
|
27
|
|
|
ShortUrl $shortUrl, |
|
28
|
|
|
string $expectedMessage |
|
29
|
|
|
): void { |
|
30
|
|
|
$this->expectException(ShortCodeCannotBeRegeneratedException::class); |
|
31
|
|
|
$this->expectExceptionMessage($expectedMessage); |
|
32
|
|
|
|
|
33
|
|
|
$shortUrl->regenerateShortCode(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function provideInvalidShortUrls(): iterable |
|
37
|
|
|
{ |
|
38
|
|
|
yield 'with custom slug' => [ |
|
39
|
|
|
new ShortUrl('', ShortUrlMeta::fromRawData(['customSlug' => 'custom-slug'])), |
|
40
|
|
|
'The short code cannot be regenerated on ShortUrls where a custom slug was provided.', |
|
41
|
|
|
]; |
|
42
|
|
|
yield 'already persisted' => [ |
|
43
|
|
|
(new ShortUrl(''))->setId('1'), |
|
44
|
|
|
'The short code can be regenerated only on new ShortUrls which have not been persisted yet.', |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** @test */ |
|
49
|
|
|
public function regenerateShortCodeProperlyChangesTheValueOnValidShortUrls(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$shortUrl = new ShortUrl(''); |
|
52
|
|
|
$firstShortCode = $shortUrl->getShortCode(); |
|
53
|
|
|
|
|
54
|
|
|
$shortUrl->regenerateShortCode(); |
|
55
|
|
|
$secondShortCode = $shortUrl->getShortCode(); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertNotEquals($firstShortCode, $secondShortCode); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @test |
|
62
|
|
|
* @dataProvider provideLengths |
|
63
|
|
|
*/ |
|
64
|
|
|
public function shortCodesHaveExpectedLength(?int $length, int $expectedLength): void |
|
65
|
|
|
{ |
|
66
|
|
|
$shortUrl = new ShortUrl('', ShortUrlMeta::fromRawData( |
|
67
|
|
|
[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => $length], |
|
68
|
|
|
)); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertEquals($expectedLength, strlen($shortUrl->getShortCode())); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function provideLengths(): iterable |
|
74
|
|
|
{ |
|
75
|
|
|
yield [null, DEFAULT_SHORT_CODES_LENGTH]; |
|
76
|
|
|
yield from map(range(4, 10), fn (int $value) => [$value, $value]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @test |
|
81
|
|
|
* @dataProvider provideCriteriaToMatch |
|
82
|
|
|
*/ |
|
83
|
|
|
public function criteriaIsMatchedWhenDatesMatch(ShortUrl $shortUrl, ShortUrlMeta $meta, bool $expected): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->assertEquals($expected, $shortUrl->matchesCriteria($meta, [])); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function provideCriteriaToMatch(): iterable |
|
89
|
|
|
{ |
|
90
|
|
|
$start = Chronos::parse('2020-03-05 20:18:30'); |
|
91
|
|
|
$end = Chronos::parse('2021-03-05 20:18:30'); |
|
92
|
|
|
|
|
93
|
|
|
yield [new ShortUrl('foo'), ShortUrlMeta::fromRawData(['validSince' => $start]), false]; |
|
94
|
|
|
yield [new ShortUrl('foo'), ShortUrlMeta::fromRawData(['validUntil' => $end]), false]; |
|
95
|
|
|
yield [new ShortUrl('foo'), ShortUrlMeta::fromRawData(['validSince' => $start, 'validUntil' => $end]), false]; |
|
96
|
|
|
yield [ |
|
97
|
|
|
new ShortUrl('foo', ShortUrlMeta::fromRawData(['validSince' => $start])), |
|
98
|
|
|
ShortUrlMeta::fromRawData(['validSince' => $start]), |
|
99
|
|
|
true, |
|
100
|
|
|
]; |
|
101
|
|
|
yield [ |
|
102
|
|
|
new ShortUrl('foo', ShortUrlMeta::fromRawData(['validUntil' => $end])), |
|
103
|
|
|
ShortUrlMeta::fromRawData(['validUntil' => $end]), |
|
104
|
|
|
true, |
|
105
|
|
|
]; |
|
106
|
|
|
yield [ |
|
107
|
|
|
new ShortUrl('foo', ShortUrlMeta::fromRawData(['validUntil' => $end, 'validSince' => $start])), |
|
108
|
|
|
ShortUrlMeta::fromRawData(['validUntil' => $end, 'validSince' => $start]), |
|
109
|
|
|
true, |
|
110
|
|
|
]; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|