1
|
|
|
<?php |
2
|
|
|
use Wambo\Catalog\Model\Slug; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class SlugTest tests the Wambo\Catalog\Model\Slug class. |
6
|
|
|
*/ |
7
|
|
|
class SlugTest extends PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* If the given Slug is valid validateSlug should not throw an exception |
11
|
|
|
* |
12
|
|
|
* @test |
13
|
|
|
* @dataProvider getValidSlugs |
14
|
|
|
* |
15
|
|
|
* @param string $sku A Slug |
16
|
|
|
*/ |
17
|
|
|
public function validateSlug_validSlugs_NoExceptionIsThrown($sku) |
18
|
|
|
{ |
19
|
|
|
// act |
20
|
|
|
try { |
21
|
|
|
|
22
|
|
|
new Slug($sku); |
23
|
|
|
$exceptionThrown = false; |
24
|
|
|
|
25
|
|
|
} catch (Exception $validationException) { |
26
|
|
|
$exceptionThrown = true; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// assert |
30
|
|
|
$this->assertFalse($exceptionThrown, "validateSlug('$sku') should not have thrown an exception"); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* If the given Slug is invalid validateSlug should throw an exception |
35
|
|
|
* |
36
|
|
|
* @test |
37
|
|
|
* @dataProvider getInvalidSlugs |
38
|
|
|
* |
39
|
|
|
* @param string $sku A Slug |
40
|
|
|
* @param string $expectedExceptionMessage The expected exception message |
41
|
|
|
*/ |
42
|
|
|
public function validateSlug_invalidSlugs_ExceptionIsThrown($sku, $expectedExceptionMessage) |
43
|
|
|
{ |
44
|
|
|
// act |
45
|
|
|
$exceptionThrown = false; |
46
|
|
|
try { |
47
|
|
|
|
48
|
|
|
new Slug($sku); |
49
|
|
|
$exceptionThrown = false; |
50
|
|
|
|
51
|
|
|
} catch (Exception $validationException) { |
52
|
|
|
|
53
|
|
|
// assert |
54
|
|
|
$exceptionThrown = true; |
55
|
|
|
$this->assertContains($expectedExceptionMessage, $validationException->getMessage(), |
56
|
|
|
"validateSlug('$sku') should have thrown an exception with the message: $expectedExceptionMessage"); |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// assert |
61
|
|
|
$this->assertTrue($exceptionThrown, "validateSlug('$sku') should have thrown an exception"); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get a list of valid Slugs |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public static function getValidSlugs() |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
|
|
["abcdefghijklmnopqrstuvwxyz012345abcdefghijklmnopqrstuvwxyz012345"], // max length <= 32 |
73
|
|
|
["abcdefghijklmnopqrstuvwxyz"], // the whole alphabet |
74
|
|
|
["123456789"], // only digits |
75
|
|
|
["00000111111111"], // leading zeros |
76
|
|
|
["a-product"], // dashes |
77
|
|
|
["product1"], // characters and digits |
78
|
|
|
["product112345678"], // characters and digits |
79
|
|
|
["product-1"], |
80
|
|
|
["product-112345678"], |
81
|
|
|
["ab"], // min length >= 2 |
82
|
|
|
["12"], // min length >= 2 |
83
|
|
|
["abc-dfg"], |
84
|
|
|
["abc_dfg"], |
85
|
|
|
["abc:dfg"], |
86
|
|
|
["abc.dfg"], |
87
|
|
|
["abc,dfg"], |
88
|
|
|
["abc+dfg"], |
89
|
|
|
["ABC_xyz"], |
90
|
|
|
['Åre'], // Swedish umlaut |
91
|
|
|
['Öresund'], // German umlaut |
92
|
|
|
['наушник'], // Russian |
93
|
|
|
['이어폰'], // Korean |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get a list of invalid Slugs |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public static function getInvalidSlugs() |
103
|
|
|
{ |
104
|
|
|
// format: [ "sku", "expected exception messages" ] |
105
|
|
|
return [ |
106
|
|
|
// empty |
107
|
|
|
["", "empty"], |
108
|
|
|
|
109
|
|
|
// white space |
110
|
|
|
[" product", "white space"], |
111
|
|
|
["product ", "white space"], |
112
|
|
|
[" product ", "white space"], |
113
|
|
|
["pro duct", "white space"], |
114
|
|
|
|
115
|
|
|
// invalid characters |
116
|
|
|
['7$tshirt', "invalid characters"], // Dollar sign |
117
|
|
|
['product(1)', "invalid characters"], |
118
|
|
|
['product§1', "invalid characters"], |
119
|
|
|
['👃-spray', "invalid characters"], // nose emoji |
120
|
|
|
|
121
|
|
|
// minimum length |
122
|
|
|
["a", "too short"], |
123
|
|
|
["1", "too short"], |
124
|
|
|
["0", "too short"], |
125
|
|
|
|
126
|
|
|
// maximum length |
127
|
|
|
["abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789", "too long"], |
128
|
|
|
|
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|