1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ICanBoogie package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ICanBoogie; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @group error |
16
|
|
|
*/ |
17
|
|
|
class ErrorCollectionTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ErrorCollection |
21
|
|
|
*/ |
22
|
|
|
private $errors; |
23
|
|
|
|
24
|
|
|
public function setUp() |
25
|
|
|
{ |
26
|
|
|
$this->errors = new ErrorCollection; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function test_add_with_string() |
30
|
|
|
{ |
31
|
|
|
$attribute = uniqid(); |
32
|
|
|
$format = uniqid(); |
33
|
|
|
$args = [ uniqid() => uniqid() ]; |
34
|
|
|
$this->errors->add($attribute, $format, $args); |
35
|
|
|
$this->assertEquals(1, $this->errors->count()); |
36
|
|
|
|
37
|
|
|
$errors = $this->errors[$attribute]; |
38
|
|
|
$this->assertInternalType('array', $errors); |
39
|
|
|
$error = reset($errors); |
40
|
|
|
$this->assertInstanceOf(Error::class, $error); |
41
|
|
|
$this->assertSame($format, $error->format); |
42
|
|
|
$this->assertSame($args, $error->args); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function test_add_with_true() |
46
|
|
|
{ |
47
|
|
|
$attribute = uniqid(); |
48
|
|
|
$args = [ uniqid() => uniqid() ]; |
49
|
|
|
$this->errors->add($attribute, true, $args); |
50
|
|
|
$this->assertEquals(1, $this->errors->count()); |
51
|
|
|
|
52
|
|
|
$errors = $this->errors[$attribute]; |
53
|
|
|
$this->assertInternalType('array', $errors); |
54
|
|
|
$error = reset($errors); |
55
|
|
|
$this->assertInstanceOf(Error::class, $error); |
56
|
|
|
$this->assertSame("", $error->format); |
57
|
|
|
$this->assertSame($args, $error->args); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function test_add_with_error() |
61
|
|
|
{ |
62
|
|
|
$error = new Error(uniqid(), [ uniqid() => uniqid() ]); |
63
|
|
|
$attribute = uniqid(); |
64
|
|
|
$this->errors->add($attribute, $error, [ uniqid() => uniqid() ]); |
65
|
|
|
$this->assertEquals(1, $this->errors->count()); |
66
|
|
|
|
67
|
|
|
$errors = $this->errors[$attribute]; |
68
|
|
|
$this->assertInternalType('array', $errors); |
69
|
|
|
$this->assertSame($error, reset($errors)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @dataProvider provide_test_add_with_invalid_attribute |
74
|
|
|
* @expectedException \InvalidArgumentException |
75
|
|
|
* |
76
|
|
|
* @param mixed $attribute |
77
|
|
|
*/ |
78
|
|
|
public function test_add_with_invalid_attribute($attribute) |
79
|
|
|
{ |
80
|
|
|
$this->errors->add($attribute, "Error"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function provide_test_add_with_invalid_attribute() |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
|
|
|
87
|
|
|
[ null ], |
88
|
|
|
[ false ], |
89
|
|
|
[ true ], |
90
|
|
|
[ 123 ], |
91
|
|
|
[ [] ], |
92
|
|
|
[ new \stdClass ] |
93
|
|
|
|
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @dataProvider provide_test_add_with_invalid_type |
99
|
|
|
* @expectedException \InvalidArgumentException |
100
|
|
|
* |
101
|
|
|
* @param mixed $error |
102
|
|
|
*/ |
103
|
|
|
public function test_add_with_invalid_type($error) |
104
|
|
|
{ |
105
|
|
|
$this->errors->add(uniqid(), $error); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function provide_test_add_with_invalid_type() |
109
|
|
|
{ |
110
|
|
|
return [ |
111
|
|
|
|
112
|
|
|
[ false ], |
113
|
|
|
[ [ uniqid() => uniqid() ] ], |
114
|
|
|
[ 1234 ], |
115
|
|
|
[ new \stdClass ] |
116
|
|
|
|
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function test_add_generic() |
121
|
|
|
{ |
122
|
|
|
$error = new Error(uniqid(), [ uniqid() => uniqid() ]); |
123
|
|
|
$this->errors->add_generic($error); |
124
|
|
|
$errors = $this->errors[null]; |
125
|
|
|
$this->assertSame($error, reset($errors)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function test_array_access_interface() |
129
|
|
|
{ |
130
|
|
|
$errors = $this->errors; |
131
|
|
|
$err1 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
132
|
|
|
$err2 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
133
|
|
|
$err3 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
134
|
|
|
$err4 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
135
|
|
|
$attribute = uniqid(); |
136
|
|
|
$errors[] = $err1; |
137
|
|
|
$errors[] = $err2; |
138
|
|
|
$errors[$attribute] = $err3; |
139
|
|
|
$errors[$attribute] = $err4; |
140
|
|
|
|
141
|
|
|
$this->assertTrue(isset($errors[null])); |
142
|
|
|
$this->assertTrue(isset($errors[$attribute])); |
143
|
|
|
$this->assertSame([ $err1, $err2 ], $errors[null]); |
144
|
|
|
$this->assertSame([ $err3, $err4 ], $errors[$attribute]); |
145
|
|
|
|
146
|
|
|
unset($errors[null]); |
147
|
|
|
$this->assertSame([], $errors[null]); |
148
|
|
|
unset($errors[$attribute]); |
149
|
|
|
$this->assertSame([], $errors[$attribute]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function test_iterator() |
153
|
|
|
{ |
154
|
|
|
$errors = $this->errors; |
155
|
|
|
$err1 = new Error('err1-' . uniqid()); |
156
|
|
|
$err2 = new Error('err2-' . uniqid()); |
157
|
|
|
$err3 = new Error('err3-' . uniqid()); |
158
|
|
|
$err4 = new Error('err4-' . uniqid()); |
159
|
|
|
$attribute = uniqid(); |
160
|
|
|
$errors[$attribute] = $err3; |
161
|
|
|
$errors[] = $err1; |
162
|
|
|
$errors[$attribute] = $err4; |
163
|
|
|
$errors[] = $err2; |
164
|
|
|
|
165
|
|
|
$iterator_errors = []; |
166
|
|
|
|
167
|
|
|
foreach ($errors as $a => $e) |
168
|
|
|
{ |
169
|
|
|
$iterator_errors[] = [ $a => $e ]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$this->assertSame([ |
173
|
|
|
|
174
|
|
|
[ ErrorCollection::GENERIC => $err1 ], |
175
|
|
|
[ ErrorCollection::GENERIC => $err2 ], |
176
|
|
|
[ $attribute => $err3 ], |
177
|
|
|
[ $attribute => $err4 ], |
178
|
|
|
|
179
|
|
|
], $iterator_errors); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function test_each() |
183
|
|
|
{ |
184
|
|
|
$errors = $this->errors; |
185
|
|
|
$err1 = new Error('err1-' . uniqid()); |
186
|
|
|
$err2 = new Error('err2-' . uniqid()); |
187
|
|
|
$err3 = new Error('err3-' . uniqid()); |
188
|
|
|
$err4 = new Error('err4-' . uniqid()); |
189
|
|
|
$attribute = uniqid(); |
190
|
|
|
$errors[$attribute] = $err3; |
191
|
|
|
$errors[] = $err1; |
192
|
|
|
$errors[$attribute] = $err4; |
193
|
|
|
$errors[] = $err2; |
194
|
|
|
|
195
|
|
|
$iterator_errors = []; |
196
|
|
|
|
197
|
|
|
$errors->each(function ($error, $attribute, $collection) use (&$iterator_errors, $errors) { |
198
|
|
|
|
199
|
|
|
$this->assertSame($errors, $collection); |
200
|
|
|
$iterator_errors[] = [ $attribute => $error ]; |
201
|
|
|
|
202
|
|
|
}); |
203
|
|
|
|
204
|
|
|
$this->assertSame([ |
205
|
|
|
|
206
|
|
|
[ ErrorCollection::GENERIC => $err1 ], |
207
|
|
|
[ ErrorCollection::GENERIC => $err2 ], |
208
|
|
|
[ $attribute => $err3 ], |
209
|
|
|
[ $attribute => $err4 ], |
210
|
|
|
|
211
|
|
|
], $iterator_errors); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function test_clear() |
215
|
|
|
{ |
216
|
|
|
$errors = $this->errors->add(uniqid())->add(uniqid())->add(uniqid()); |
217
|
|
|
$this->assertEquals(3, $errors->count()); |
218
|
|
|
$this->assertEquals(0, $errors->clear()->count()); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function test_merge() |
222
|
|
|
{ |
223
|
|
|
$er1 = new Error(uniqid()); |
224
|
|
|
$er2 = new Error(uniqid()); |
225
|
|
|
|
226
|
|
|
$col1 = (new ErrorCollection) |
227
|
|
|
->add_generic($er1); |
228
|
|
|
$col2 = (new ErrorCollection) |
229
|
|
|
->add_generic($er2); |
230
|
|
|
|
231
|
|
|
$col1->merge($col2); |
232
|
|
|
|
233
|
|
|
$this->assertSame([ |
234
|
|
|
|
235
|
|
|
ErrorCollection::GENERIC => [ |
236
|
|
|
|
237
|
|
|
$er1, $er2 |
238
|
|
|
|
239
|
|
|
] |
240
|
|
|
|
241
|
|
|
], $col1->to_array()); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function test_json_serialize() |
245
|
|
|
{ |
246
|
|
|
$format = "error: {arg}"; |
247
|
|
|
$arg1 = uniqid(); |
248
|
|
|
$arg2 = uniqid(); |
249
|
|
|
$arg3 = uniqid(); |
250
|
|
|
$arg4 = uniqid(); |
251
|
|
|
$attribute = uniqid(); |
252
|
|
|
$errors = $this->errors; |
253
|
|
|
$errors->add($attribute, $format, [ 'arg' => $arg3 ]); |
254
|
|
|
$errors->add_generic($format, [ 'arg' => $arg1 ]); |
255
|
|
|
$errors->add($attribute, $format, [ 'arg' => $arg4 ]); |
256
|
|
|
$errors->add_generic($format, [ 'arg' => $arg2 ]); |
257
|
|
|
|
258
|
|
|
$this->assertSame(json_encode([ |
259
|
|
|
|
260
|
|
|
ErrorCollection::GENERIC => [ |
261
|
|
|
|
262
|
|
|
"error: {$arg1}", |
263
|
|
|
"error: {$arg2}", |
264
|
|
|
|
265
|
|
|
], |
266
|
|
|
|
267
|
|
|
$attribute => [ |
268
|
|
|
|
269
|
|
|
"error: {$arg3}", |
270
|
|
|
"error: {$arg4}", |
271
|
|
|
|
272
|
|
|
] |
273
|
|
|
|
274
|
|
|
]), json_encode($errors)); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|