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
|
|
|
$this->assertTrue($meta->hasValidSince()); |
72
|
|
|
$this->assertEquals(Chronos::parse('2015-01-01'), $meta->getValidSince()); |
73
|
|
|
|
74
|
|
|
$this->assertFalse($meta->hasValidUntil()); |
75
|
|
|
$this->assertNull($meta->getValidUntil()); |
76
|
|
|
|
77
|
|
|
$this->assertTrue($meta->hasCustomSlug()); |
78
|
|
|
$this->assertEquals($expectedSlug, $meta->getCustomSlug()); |
79
|
|
|
|
80
|
|
|
$this->assertFalse($meta->hasMaxVisits()); |
81
|
|
|
$this->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
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|