|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Core\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter; |
|
12
|
|
|
use stdClass; |
|
13
|
|
|
|
|
14
|
|
|
class ShortUrlMetaTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param array $data |
|
18
|
|
|
* @test |
|
19
|
|
|
* @dataProvider provideInvalidData |
|
20
|
|
|
*/ |
|
21
|
|
|
public function exceptionIsThrownIfProvidedDataIsInvalid(array $data): void |
|
22
|
|
|
{ |
|
23
|
|
|
$this->expectException(ValidationException::class); |
|
24
|
|
|
ShortUrlMeta::fromRawData($data); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function provideInvalidData(): iterable |
|
28
|
|
|
{ |
|
29
|
|
|
yield [[ |
|
30
|
|
|
ShortUrlMetaInputFilter::VALID_SINCE => '', |
|
31
|
|
|
ShortUrlMetaInputFilter::VALID_UNTIL => '', |
|
32
|
|
|
ShortUrlMetaInputFilter::CUSTOM_SLUG => 'foobar', |
|
33
|
|
|
ShortUrlMetaInputFilter::MAX_VISITS => 'invalid', |
|
34
|
|
|
]]; |
|
35
|
|
|
yield [[ |
|
36
|
|
|
ShortUrlMetaInputFilter::VALID_SINCE => '2017', |
|
37
|
|
|
ShortUrlMetaInputFilter::MAX_VISITS => 5, |
|
38
|
|
|
]]; |
|
39
|
|
|
yield [[ |
|
40
|
|
|
ShortUrlMetaInputFilter::VALID_SINCE => new stdClass(), |
|
41
|
|
|
ShortUrlMetaInputFilter::VALID_UNTIL => 'foo', |
|
42
|
|
|
]]; |
|
43
|
|
|
yield [[ |
|
44
|
|
|
ShortUrlMetaInputFilter::VALID_UNTIL => 500, |
|
45
|
|
|
ShortUrlMetaInputFilter::DOMAIN => 4, |
|
46
|
|
|
]]; |
|
47
|
|
|
yield [[ |
|
48
|
|
|
ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => 3, |
|
49
|
|
|
]]; |
|
50
|
|
|
yield [[ |
|
51
|
|
|
ShortUrlMetaInputFilter::CUSTOM_SLUG => '/', |
|
52
|
|
|
]]; |
|
53
|
|
|
yield [[ |
|
54
|
|
|
ShortUrlMetaInputFilter::CUSTOM_SLUG => '', |
|
55
|
|
|
]]; |
|
56
|
|
|
yield [[ |
|
57
|
|
|
ShortUrlMetaInputFilter::CUSTOM_SLUG => ' ', |
|
58
|
|
|
]]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @test |
|
63
|
|
|
* @dataProvider provideCustomSlugs |
|
64
|
|
|
*/ |
|
65
|
|
|
public function properlyCreatedInstanceReturnsValues(string $customSlug, string $expectedSlug): void |
|
66
|
|
|
{ |
|
67
|
|
|
$meta = ShortUrlMeta::fromRawData( |
|
68
|
|
|
['validSince' => Chronos::parse('2015-01-01')->toAtomString(), 'customSlug' => $customSlug], |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
self::assertTrue($meta->hasValidSince()); |
|
72
|
|
|
self::assertEquals(Chronos::parse('2015-01-01'), $meta->getValidSince()); |
|
73
|
|
|
|
|
74
|
|
|
self::assertFalse($meta->hasValidUntil()); |
|
75
|
|
|
self::assertNull($meta->getValidUntil()); |
|
76
|
|
|
|
|
77
|
|
|
self::assertTrue($meta->hasCustomSlug()); |
|
78
|
|
|
self::assertEquals($expectedSlug, $meta->getCustomSlug()); |
|
79
|
|
|
|
|
80
|
|
|
self::assertFalse($meta->hasMaxVisits()); |
|
81
|
|
|
self::assertNull($meta->getMaxVisits()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function provideCustomSlugs(): iterable |
|
85
|
|
|
{ |
|
86
|
|
|
yield ['foobar', 'foobar']; |
|
87
|
|
|
yield ['foo bar', 'foo-bar']; |
|
88
|
|
|
yield ['wp-admin.php', 'wp-admin.php']; |
|
89
|
|
|
yield ['UPPER_lower', 'UPPER_lower']; |
|
90
|
|
|
yield ['more~url_special.chars', 'more~url_special.chars']; |
|
91
|
|
|
yield ['äéñ', 'äen']; |
|
92
|
|
|
yield ['구글', '구글']; |
|
93
|
|
|
yield ['グーグル', 'グーグル']; |
|
94
|
|
|
yield ['谷歌', '谷歌']; |
|
95
|
|
|
yield ['гугл', 'гугл']; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|