1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HelePartnerSyncApi; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use PHPUnit_Framework_TestCase; |
7
|
|
|
use stdClass; |
8
|
|
|
|
9
|
|
|
class ValidatorTest extends PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
public function testValidExamples() |
13
|
|
|
{ |
14
|
|
|
$now = new DateTime(); |
15
|
|
|
Validator::checkNull(null); |
16
|
|
|
Validator::checkString('true'); |
17
|
|
|
Validator::checkString('0'); |
18
|
|
|
Validator::checkString('1.9'); |
19
|
|
|
Validator::checkString('array'); |
20
|
|
|
Validator::checkInt(121); |
21
|
|
|
Validator::checkInt(-78); |
22
|
|
|
Validator::checkInt(0); |
23
|
|
|
Validator::checkDateTime($now); |
24
|
|
|
Validator::checkDateTimeString($now->format(DATE_W3C)); |
25
|
|
|
$this->assertTrue(true); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return mixed[][] |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function provideInvalidNulls() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
return array( |
34
|
|
|
array(true), |
35
|
|
|
array(false), |
36
|
|
|
array(0), |
37
|
|
|
array(0.0), |
38
|
|
|
array(array()), |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param mixed $value |
44
|
|
|
* |
45
|
|
|
* @dataProvider provideInvalidNulls |
46
|
|
|
* @expectedException \HelePartnerSyncApi\ValidatorException |
47
|
|
|
* @expectedExceptionMessage Null expected |
48
|
|
|
*/ |
49
|
|
|
public function testCheckInvalidNulls($value) |
50
|
|
|
{ |
51
|
|
|
Validator::checkNull($value); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return mixed[][] |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function provideInvalidStrings() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
return array( |
60
|
|
|
array(true), |
61
|
|
|
array(0.5), |
62
|
|
|
array(1212), |
63
|
|
|
array(null), |
64
|
|
|
array(array()), |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param mixed $value |
70
|
|
|
* |
71
|
|
|
* @dataProvider provideInvalidStrings |
72
|
|
|
* @expectedException \HelePartnerSyncApi\ValidatorException |
73
|
|
|
* @expectedExceptionMessage String expected |
74
|
|
|
*/ |
75
|
|
|
public function testCheckInvalidStrings($value) |
76
|
|
|
{ |
77
|
|
|
Validator::checkString($value); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return mixed[][] |
82
|
|
|
*/ |
83
|
|
|
public function provideInvalidInts() |
84
|
|
|
{ |
85
|
|
|
return array( |
86
|
|
|
array('1212'), |
87
|
|
|
array(true), |
88
|
|
|
array(0.5), |
89
|
|
|
array(null), |
90
|
|
|
array(array()), |
91
|
|
|
array(new stdClass()), |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param mixed $value |
97
|
|
|
* |
98
|
|
|
* @dataProvider provideInvalidInts |
99
|
|
|
* @expectedException \HelePartnerSyncApi\ValidatorException |
100
|
|
|
* @expectedExceptionMessage Int expected |
101
|
|
|
*/ |
102
|
|
|
public function testCheckInvalidIntegers($value) |
103
|
|
|
{ |
104
|
|
|
Validator::checkInt($value); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testCheckValidStructures() |
108
|
|
|
{ |
109
|
|
|
Validator::checkStructure(array(-1, 0, 1), array(Validator::TYPE_INT)); |
110
|
|
|
|
111
|
|
|
Validator::checkStructure(array( |
112
|
|
|
'key' => 123, |
113
|
|
|
'data' => array( |
114
|
|
|
'text' => 'abc', |
115
|
|
|
), |
116
|
|
|
), array( |
117
|
|
|
'key' => Validator::TYPE_INT, |
118
|
|
|
'data' => array( |
119
|
|
|
'text' => Validator::TYPE_STRING, |
120
|
|
|
), |
121
|
|
|
)); |
122
|
|
|
|
123
|
|
|
Validator::checkStructure(array( |
124
|
|
|
array( |
125
|
|
|
'from' => 12, |
126
|
|
|
'to' => 16, |
127
|
|
|
), |
128
|
|
|
array( |
129
|
|
|
'from' => 17, |
130
|
|
|
'to' => 22, |
131
|
|
|
), |
132
|
|
|
), array( |
133
|
|
|
array( |
134
|
|
|
'from' => Validator::TYPE_INT, |
135
|
|
|
'to' => Validator::TYPE_INT, |
136
|
|
|
), |
137
|
|
|
)); |
138
|
|
|
|
139
|
|
|
$this->assertTrue(true); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @expectedException \HelePartnerSyncApi\ValidatorException |
144
|
|
|
* @expectedExceptionMessage Invalid type in key 'foo': Int expected, array given. |
145
|
|
|
*/ |
146
|
|
|
public function testCheckInvalidStructuresBasic() |
147
|
|
|
{ |
148
|
|
|
Validator::checkStructure(array( |
149
|
|
|
'moo' => 0, |
150
|
|
|
'foo' => array(), |
151
|
|
|
), array( |
152
|
|
|
'moo' => Validator::TYPE_INT, |
153
|
|
|
'foo' => Validator::TYPE_INT, |
154
|
|
|
)); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @expectedException \HelePartnerSyncApi\ValidatorException |
159
|
|
|
* @expectedExceptionMessage Expected list structure in root |
160
|
|
|
*/ |
161
|
|
|
public function testCheckInvalidStructureNotAList() |
162
|
|
|
{ |
163
|
|
|
Validator::checkStructure(array(1, 2, 'key' => 'not-a-list'), array(Validator::TYPE_INT)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @expectedException \HelePartnerSyncApi\ValidatorException |
168
|
|
|
* @expectedExceptionMessage Invalid type in key '2': Int expected, string (3) given. |
169
|
|
|
*/ |
170
|
|
|
public function testCheckInvalidStructureListElement() |
171
|
|
|
{ |
172
|
|
|
Validator::checkStructure(array(1, 2, '3'), array(Validator::TYPE_INT)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @expectedException \HelePartnerSyncApi\ValidatorException |
177
|
|
|
* @expectedExceptionMessage Invalid type in key '2': Array expected, string (what?) given. |
178
|
|
|
*/ |
179
|
|
View Code Duplication |
public function testCheckInvalidStructureNonArrayElement() |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
Validator::checkStructure(array( |
182
|
|
|
array( |
183
|
|
|
'moo' => 1, |
184
|
|
|
), |
185
|
|
|
array( |
186
|
|
|
'moo' => 2, |
187
|
|
|
), |
188
|
|
|
'what?' |
189
|
|
|
), array( |
190
|
|
|
array( |
191
|
|
|
'moo' => Validator::TYPE_INT, |
192
|
|
|
) |
193
|
|
|
)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @expectedException \HelePartnerSyncApi\ValidatorException |
198
|
|
|
* @expectedExceptionMessage Invalid type in path 'foo.bar': Int expected, string (moo) given. |
199
|
|
|
*/ |
200
|
|
View Code Duplication |
public function testCheckInvalidStructuresDeep() |
|
|
|
|
201
|
|
|
{ |
202
|
|
|
Validator::checkStructure(array( |
203
|
|
|
'boo' => null, |
204
|
|
|
'foo' => array( |
205
|
|
|
'bar' => 'moo', |
206
|
|
|
), |
207
|
|
|
), array( |
208
|
|
|
'boo' => Validator::TYPE_NULL, |
209
|
|
|
'foo' => array( |
210
|
|
|
'bar' => Validator::TYPE_INT, |
211
|
|
|
), |
212
|
|
|
)); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @expectedException \HelePartnerSyncApi\ValidatorException |
217
|
|
|
* @expectedExceptionMessage Missing keys (bar, foo) in root |
218
|
|
|
*/ |
219
|
|
|
public function testCheckInvalidStructuresMissingKeys() |
220
|
|
|
{ |
221
|
|
|
Validator::checkStructure(array(), array( |
222
|
|
|
'bar' => Validator::TYPE_INT, |
223
|
|
|
'foo' => Validator::TYPE_INT, |
224
|
|
|
)); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @expectedException \HelePartnerSyncApi\ValidatorException |
229
|
|
|
* @expectedExceptionMessage Unknown keys (foo, bar) in root |
230
|
|
|
*/ |
231
|
|
|
public function testCheckInvalidStructuresUnknownKeys() |
232
|
|
|
{ |
233
|
|
|
Validator::checkStructure(array( |
234
|
|
|
'foo' => 'foo', |
235
|
|
|
'bar' => 'bar', |
236
|
|
|
), array( |
237
|
|
|
'moo' => Validator::TYPE_INT, |
238
|
|
|
)); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.