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