1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Filter; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @coversDefaultClass \TraderInteractive\Filter\Arrays |
9
|
|
|
*/ |
10
|
|
|
final class ArraysTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @test |
14
|
|
|
* @covers ::filter |
15
|
|
|
*/ |
16
|
|
|
public function filterBasicPass() |
17
|
|
|
{ |
18
|
|
|
$this->assertSame(['boo'], Arrays::filter(['boo'])); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @test |
23
|
|
|
* @covers ::filter |
24
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
25
|
|
|
* @expectedExceptionMessage Value '1' is not an array |
26
|
|
|
*/ |
27
|
|
|
public function filterFailNotArray() |
28
|
|
|
{ |
29
|
|
|
Arrays::filter(1); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @test |
34
|
|
|
* @covers ::filter |
35
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
36
|
|
|
* @expectedExceptionMessage $value count of 0 is less than 1 |
37
|
|
|
*/ |
38
|
|
|
public function filterFailEmpty() |
39
|
|
|
{ |
40
|
|
|
Arrays::filter([]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @test |
45
|
|
|
* @covers ::filter |
46
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
47
|
|
|
* @expectedExceptionMessage $value count of 1 is less than 2 |
48
|
|
|
*/ |
49
|
|
|
public function filterCountLessThanMin() |
50
|
|
|
{ |
51
|
|
|
Arrays::filter([0], 2); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @test |
56
|
|
|
* @covers ::filter |
57
|
|
|
* @expectedException \TraderInteractive\Filter\Exception |
58
|
|
|
* @expectedExceptionMessage $value count of 2 is greater than 1 |
59
|
|
|
*/ |
60
|
|
|
public function filterCountGreaterThanMax() |
61
|
|
|
{ |
62
|
|
|
Arrays::filter([0, 1], 1, 1); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
* @covers ::filter |
68
|
|
|
* @expectedException \InvalidArgumentException |
69
|
|
|
* @expectedExceptionMessage $minCount was not an int |
70
|
|
|
*/ |
71
|
|
|
public function filterMinCountNotInt() |
72
|
|
|
{ |
73
|
|
|
Arrays::filter([], true); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @test |
78
|
|
|
* @covers ::filter |
79
|
|
|
* @expectedException \InvalidArgumentException |
80
|
|
|
* @expectedExceptionMessage $maxCount was not an int |
81
|
|
|
*/ |
82
|
|
|
public function filterMaxCountNotInt() |
83
|
|
|
{ |
84
|
|
|
Arrays::filter([], 0, true); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @test |
89
|
|
|
* @covers ::in |
90
|
|
|
*/ |
91
|
|
|
public function inPassStrict() |
92
|
|
|
{ |
93
|
|
|
$this->assertSame('boo', Arrays::in('boo', ['boo'])); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @test |
98
|
|
|
* @covers ::in |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function inFailStrict() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
try { |
103
|
|
|
Arrays::in('0', [0]); |
104
|
|
|
$this->fail(); |
105
|
|
|
} catch (Exception $e) { |
106
|
|
|
$this->assertSame("Value '0' is not in array array (\n 0 => 0,\n)", $e->getMessage()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @test |
112
|
|
|
* @covers ::in |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function inFailNotStrict() |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
try { |
117
|
|
|
Arrays::in('boo', ['foo'], false); |
118
|
|
|
$this->fail(); |
119
|
|
|
} catch (Exception $e) { |
120
|
|
|
$this->assertSame("Value 'boo' is not in array array (\n 0 => 'foo',\n)", $e->getMessage()); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @test |
126
|
|
|
* @covers ::in |
127
|
|
|
*/ |
128
|
|
|
public function inPassNotStrict() |
129
|
|
|
{ |
130
|
|
|
$this->assertSame('0', Arrays::in('0', [0], false)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @test |
135
|
|
|
* @covers ::in |
136
|
|
|
* @expectedException \InvalidArgumentException |
137
|
|
|
* @expectedExceptionMessage $strict was not a bool |
138
|
|
|
*/ |
139
|
|
|
public function inStrictNotBool() |
140
|
|
|
{ |
141
|
|
|
Arrays::in('boo', [], 1); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @test |
146
|
|
|
* @covers ::ofScalars |
147
|
|
|
*/ |
148
|
|
|
public function ofScalars() |
149
|
|
|
{ |
150
|
|
|
$this->assertSame([1, 2], Arrays::ofScalars(['1', '2'], [['uint']])); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @test |
155
|
|
|
* @covers ::ofScalars |
156
|
|
|
*/ |
157
|
|
|
public function ofScalarsChained() |
158
|
|
|
{ |
159
|
|
|
$this->assertSame([3.3, 5.5], Arrays::ofScalars(['a3.3', 'a5.5'], [['trim', 'a'], ['floatval']])); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @test |
164
|
|
|
* @covers ::ofScalars |
165
|
|
|
*/ |
166
|
|
|
public function ofScalarsWithMeaninglessKeys() |
167
|
|
|
{ |
168
|
|
|
$this->assertSame(['key1' => 1, 'key2' => 2], Arrays::ofScalars(['key1' => '1', 'key2' => '2'], [['uint']])); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @test |
173
|
|
|
* @covers ::ofScalars |
174
|
|
|
*/ |
175
|
|
View Code Duplication |
public function ofScalarsFail() |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
try { |
178
|
|
|
Arrays::ofScalars(['1', [], new \StdClass], [['string']]); |
179
|
|
|
$this->fail(); |
180
|
|
|
} catch (Exception $e) { |
181
|
|
|
$expected = <<<TXT |
182
|
|
|
Field '1' with value 'array ( |
183
|
|
|
)' failed filtering, message 'Value 'array ( |
184
|
|
|
)' is not a string' |
185
|
|
|
Field '2' with value 'stdClass::__set_state(array( |
186
|
|
|
))' failed filtering, message 'Value 'stdClass::__set_state(array( |
187
|
|
|
))' is not a string' |
188
|
|
|
TXT; |
189
|
|
|
$this->assertSame($expected, $e->getMessage()); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @test |
195
|
|
|
* @covers ::ofArrays |
196
|
|
|
*/ |
197
|
|
|
public function ofArrays() |
198
|
|
|
{ |
199
|
|
|
$expected = [['key' => 1], ['key' => 2]]; |
200
|
|
|
$this->assertSame($expected, Arrays::ofArrays([['key' => '1'], ['key' => '2']], ['key' => [['uint']]])); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @test |
205
|
|
|
* @covers ::ofArrays |
206
|
|
|
*/ |
207
|
|
|
public function ofArraysChained() |
208
|
|
|
{ |
209
|
|
|
$expected = [['key' => 3.3], ['key' => 5.5]]; |
210
|
|
|
$spec = ['key' => [['trim', 'a'], ['floatval']]]; |
211
|
|
|
$this->assertSame($expected, Arrays::ofArrays([['key' => 'a3.3'], ['key' => 'a5.5']], $spec)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @test |
216
|
|
|
* @covers ::ofArrays |
217
|
|
|
*/ |
218
|
|
View Code Duplication |
public function ofArraysRequiredAndUnknown() |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
try { |
221
|
|
|
Arrays::ofArrays([['key' => '1'], ['key2' => '2']], ['key' => ['required' => true, ['uint']]]); |
222
|
|
|
$this->fail(); |
223
|
|
|
} catch (Exception $e) { |
224
|
|
|
$expected = "Field 'key' was required and not present\nField 'key2' with value '2' is unknown"; |
225
|
|
|
$this->assertSame($expected, $e->getMessage()); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @test |
231
|
|
|
* @covers ::ofArrays |
232
|
|
|
*/ |
233
|
|
|
public function ofArraysFail() |
234
|
|
|
{ |
235
|
|
|
try { |
236
|
|
|
Arrays::ofArrays( |
237
|
|
|
[['key' => new \StdClass], ['key' => []], ['key' => null], 'key'], |
238
|
|
|
['key' => [['string']]] |
239
|
|
|
); |
240
|
|
|
$this->fail(); |
241
|
|
|
} catch (Exception $e) { |
242
|
|
|
$expected = <<<TXT |
243
|
|
|
Field 'key' with value 'stdClass::__set_state(array( |
244
|
|
|
))' failed filtering, message 'Value 'stdClass::__set_state(array( |
245
|
|
|
))' is not a string' |
246
|
|
|
Field 'key' with value 'array ( |
247
|
|
|
)' failed filtering, message 'Value 'array ( |
248
|
|
|
)' is not a string' |
249
|
|
|
Field 'key' with value 'NULL' failed filtering, message 'Value 'NULL' is not a string' |
250
|
|
|
Value at position '3' was not an array |
251
|
|
|
TXT; |
252
|
|
|
$this->assertSame($expected, $e->getMessage()); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @test |
258
|
|
|
* @covers ::ofArray |
259
|
|
|
*/ |
260
|
|
|
public function ofArray() |
261
|
|
|
{ |
262
|
|
|
$expected = ['key1' => 1, 'key2' => 2]; |
263
|
|
|
$spec = ['key1' => [['uint']], 'key2' => [['uint']]]; |
264
|
|
|
$this->assertSame($expected, Arrays::ofArray(['key1' => '1', 'key2' => '2'], $spec)); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @test |
269
|
|
|
* @covers ::ofArray |
270
|
|
|
*/ |
271
|
|
|
public function ofArrayChained() |
272
|
|
|
{ |
273
|
|
|
$expected = ['key' => 3.3]; |
274
|
|
|
$spec = ['key' => [['trim', 'a'], ['floatval']]]; |
275
|
|
|
$this->assertSame($expected, Arrays::ofArray(['key' => 'a3.3'], $spec)); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @test |
280
|
|
|
* @covers ::ofArray |
281
|
|
|
*/ |
282
|
|
|
public function ofArrayRequiredSuccess() |
283
|
|
|
{ |
284
|
|
|
$expected = ['key2' => 2]; |
285
|
|
|
$spec = ['key1' => [['uint']], 'key2' => ['required' => true, ['uint']]]; |
286
|
|
|
$this->assertSame($expected, Arrays::ofArray(['key2' => '2'], $spec)); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @test |
291
|
|
|
* @covers ::ofArray |
292
|
|
|
*/ |
293
|
|
View Code Duplication |
public function ofArrayRequiredFail() |
|
|
|
|
294
|
|
|
{ |
295
|
|
|
try { |
296
|
|
|
Arrays::ofArray(['key1' => '1'], ['key1' => [['uint']], 'key2' => ['required' => true, ['uint']]]); |
297
|
|
|
$this->fail(); |
298
|
|
|
} catch (Exception $e) { |
299
|
|
|
$expected = "Field 'key2' was required and not present"; |
300
|
|
|
$this->assertSame($expected, $e->getMessage()); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @test |
306
|
|
|
* @covers ::ofArray |
307
|
|
|
*/ |
308
|
|
View Code Duplication |
public function ofArrayUnknown() |
|
|
|
|
309
|
|
|
{ |
310
|
|
|
try { |
311
|
|
|
Arrays::ofArray(['key' => '1'], ['key2' => [['uint']]]); |
312
|
|
|
$this->fail(); |
313
|
|
|
} catch (Exception $e) { |
314
|
|
|
$expected = "Field 'key' with value '1' is unknown"; |
315
|
|
|
$this->assertSame($expected, $e->getMessage()); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Verifies the basic behavior of the flatten filter. |
321
|
|
|
* |
322
|
|
|
* @test |
323
|
|
|
* @covers ::flatten |
324
|
|
|
*/ |
325
|
|
|
public function flatten() |
326
|
|
|
{ |
327
|
|
|
$this->assertSame([1, 2, 3, 4, 5], Arrays::flatten([[1, 2], [[3, [4, 5]]]])); |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: