|
1
|
|
|
<?php |
|
2
|
|
|
use Wambo\Catalog\Model\SKU; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Class SKUTest contains tests for the Wambo\Catalog\Model\SKU class. |
|
6
|
|
|
*/ |
|
7
|
|
|
class SKUTest extends PHPUnit_Framework_TestCase |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* If the given SKU is valid validateSKU should not throw an exception |
|
11
|
|
|
* |
|
12
|
|
|
* @test |
|
13
|
|
|
* @dataProvider getValidSKUs |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $sku A SKU |
|
16
|
|
|
*/ |
|
17
|
|
View Code Duplication |
public function validateSKU_validSKUs_NoExceptionIsThrown($sku) |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
// act |
|
20
|
|
|
$exceptionThrown = false; |
|
|
|
|
|
|
21
|
|
|
try { |
|
22
|
|
|
|
|
23
|
|
|
new SKU($sku); |
|
24
|
|
|
$exceptionThrown = false; |
|
25
|
|
|
|
|
26
|
|
|
} catch (Exception $validationException) { |
|
27
|
|
|
$exceptionThrown = true; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
// assert |
|
31
|
|
|
$this->assertFalse($exceptionThrown, "validateSKU('$sku') should not have thrown an exception"); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* If the given SKU is invalid validateSKU should throw an exception |
|
36
|
|
|
* |
|
37
|
|
|
* @test |
|
38
|
|
|
* @dataProvider getInvalidSKUs |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $sku A SKU |
|
41
|
|
|
* @param string $expectedExceptionMessage The expected exception message |
|
42
|
|
|
*/ |
|
43
|
|
View Code Duplication |
public function validateSKU_invalidSKUs_ExceptionIsThrown($sku, $expectedExceptionMessage) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
// act |
|
46
|
|
|
$exceptionThrown = false; |
|
|
|
|
|
|
47
|
|
|
try { |
|
48
|
|
|
|
|
49
|
|
|
new SKU($sku); |
|
50
|
|
|
$exceptionThrown = false; |
|
51
|
|
|
|
|
52
|
|
|
} catch (Exception $validationException) { |
|
53
|
|
|
|
|
54
|
|
|
// assert |
|
55
|
|
|
$exceptionThrown = true; |
|
56
|
|
|
$this->assertContains($expectedExceptionMessage, $validationException->getMessage(), |
|
57
|
|
|
"validateSKU('$sku') should have thrown an exception with the message: $expectedExceptionMessage"); |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// assert |
|
62
|
|
|
$this->assertTrue($exceptionThrown, "validateSKU('$sku') should have thrown an exception"); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get a list of valid SKUs |
|
67
|
|
|
* |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function getValidSKUs() |
|
71
|
|
|
{ |
|
72
|
|
|
return [ |
|
73
|
|
|
["abcdefghijklmnopqrstuvwxyz012345"], // 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
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get a list of invalid SKUs |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function getInvalidSKUs() |
|
93
|
|
|
{ |
|
94
|
|
|
// format: [ "sku", "expected exception messages" ] |
|
|
|
|
|
|
95
|
|
|
return [ |
|
96
|
|
|
// empty |
|
97
|
|
|
["", "empty"], |
|
98
|
|
|
|
|
99
|
|
|
// white space |
|
100
|
|
|
[" product", "white space"], |
|
101
|
|
|
["product ", "white space"], |
|
102
|
|
|
[" product ", "white space"], |
|
103
|
|
|
["pro duct", "white space"], |
|
104
|
|
|
|
|
105
|
|
|
// invalid characters |
|
106
|
|
|
['7$tshirt', "invalid characters"], // Dollar sign |
|
107
|
|
|
['gâteau', "invalid characters"], // French umlaut |
|
108
|
|
|
['käse', "invalid characters"], // German umlaut |
|
109
|
|
|
['product/1', "invalid characters"], |
|
110
|
|
|
['product:1', "invalid characters"], |
|
111
|
|
|
['product.1', "invalid characters"], |
|
112
|
|
|
['product(1)', "invalid characters"], |
|
113
|
|
|
['product§1', "invalid characters"], |
|
114
|
|
|
['👃-spray', "invalid characters"], // nose emoji |
|
115
|
|
|
['Åre', "invalid characters"], // Swedish umlaut |
|
116
|
|
|
['Öresund', "invalid characters"], // German umlaut |
|
117
|
|
|
['наушник', "invalid characters"], // Russian |
|
118
|
|
|
['이어폰', "invalid characters"], // Korean |
|
119
|
|
|
|
|
120
|
|
|
// invalid prefix |
|
121
|
|
|
['-product', "cannot start"], |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
// invalid postfix |
|
125
|
|
|
['product-', "cannot end"], |
|
126
|
|
|
|
|
127
|
|
|
// uppercase characters |
|
128
|
|
|
["Product-123", "uppercase"], |
|
129
|
|
|
["pro-Duct-123", "uppercase"], |
|
130
|
|
|
["AAA", "uppercase"], |
|
131
|
|
|
["aBc", "uppercase"], |
|
132
|
|
|
["abC", "uppercase"], |
|
133
|
|
|
["abC", "uppercase"], |
|
134
|
|
|
|
|
135
|
|
|
// minimum length |
|
136
|
|
|
["a", "too short"], |
|
137
|
|
|
["1", "too short"], |
|
138
|
|
|
["0", "too short"], |
|
139
|
|
|
|
|
140
|
|
|
// maximum length |
|
141
|
|
|
["abcdefghijklmnopqrstuvwxyz0123456789", "too long"], |
|
142
|
|
|
|
|
143
|
|
|
]; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
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.