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 ::in |
68
|
|
|
*/ |
69
|
|
|
public function inPassStrict() |
70
|
|
|
{ |
71
|
|
|
$this->assertSame('boo', Arrays::in('boo', ['boo'])); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
* @covers ::in |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function inFailStrict() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
try { |
81
|
|
|
Arrays::in('0', [0]); |
82
|
|
|
$this->fail(); |
83
|
|
|
} catch (Exception $e) { |
84
|
|
|
$this->assertSame("Value '0' is not in array array (\n 0 => 0,\n)", $e->getMessage()); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @test |
90
|
|
|
* @covers ::in |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
public function inFailNotStrict() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
try { |
95
|
|
|
Arrays::in('boo', ['foo'], false); |
96
|
|
|
$this->fail(); |
97
|
|
|
} catch (Exception $e) { |
98
|
|
|
$this->assertSame("Value 'boo' is not in array array (\n 0 => 'foo',\n)", $e->getMessage()); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @test |
104
|
|
|
* @covers ::in |
105
|
|
|
*/ |
106
|
|
|
public function inPassNotStrict() |
107
|
|
|
{ |
108
|
|
|
$this->assertSame('0', Arrays::in('0', [0], false)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @test |
113
|
|
|
* @covers ::ofScalars |
114
|
|
|
*/ |
115
|
|
|
public function ofScalars() |
116
|
|
|
{ |
117
|
|
|
$this->assertSame([1, 2], Arrays::ofScalars(['1', '2'], [['uint']])); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @test |
122
|
|
|
* @covers ::ofScalars |
123
|
|
|
*/ |
124
|
|
|
public function ofScalarsChained() |
125
|
|
|
{ |
126
|
|
|
$this->assertSame([3.3, 5.5], Arrays::ofScalars(['a3.3', 'a5.5'], [['trim', 'a'], ['floatval']])); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @test |
131
|
|
|
* @covers ::ofScalars |
132
|
|
|
*/ |
133
|
|
|
public function ofScalarsWithMeaninglessKeys() |
134
|
|
|
{ |
135
|
|
|
$this->assertSame(['key1' => 1, 'key2' => 2], Arrays::ofScalars(['key1' => '1', 'key2' => '2'], [['uint']])); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @test |
140
|
|
|
* @covers ::ofScalars |
141
|
|
|
*/ |
142
|
|
View Code Duplication |
public function ofScalarsFail() |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
try { |
145
|
|
|
Arrays::ofScalars(['1', [], new \StdClass], [['string']]); |
146
|
|
|
$this->fail(); |
147
|
|
|
} catch (Exception $e) { |
148
|
|
|
$expected = <<<TXT |
149
|
|
|
Field '1' with value 'array ( |
150
|
|
|
)' failed filtering, message 'Value 'array ( |
151
|
|
|
)' is not a string' |
152
|
|
|
Field '2' with value 'stdClass::__set_state(array( |
153
|
|
|
))' failed filtering, message 'Value 'stdClass::__set_state(array( |
154
|
|
|
))' is not a string' |
155
|
|
|
TXT; |
156
|
|
|
$this->assertSame($expected, $e->getMessage()); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @test |
162
|
|
|
* @covers ::ofArrays |
163
|
|
|
*/ |
164
|
|
|
public function ofArrays() |
165
|
|
|
{ |
166
|
|
|
$expected = [['key' => 1], ['key' => 2]]; |
167
|
|
|
$this->assertSame($expected, Arrays::ofArrays([['key' => '1'], ['key' => '2']], ['key' => [['uint']]])); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @test |
172
|
|
|
* @covers ::ofArrays |
173
|
|
|
*/ |
174
|
|
|
public function ofArraysChained() |
175
|
|
|
{ |
176
|
|
|
$expected = [['key' => 3.3], ['key' => 5.5]]; |
177
|
|
|
$spec = ['key' => [['trim', 'a'], ['floatval']]]; |
178
|
|
|
$this->assertSame($expected, Arrays::ofArrays([['key' => 'a3.3'], ['key' => 'a5.5']], $spec)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @test |
183
|
|
|
* @covers ::ofArrays |
184
|
|
|
*/ |
185
|
|
View Code Duplication |
public function ofArraysRequiredAndUnknown() |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
try { |
188
|
|
|
Arrays::ofArrays([['key' => '1'], ['key2' => '2']], ['key' => ['required' => true, ['uint']]]); |
189
|
|
|
$this->fail(); |
190
|
|
|
} catch (Exception $e) { |
191
|
|
|
$expected = "Field 'key' was required and not present\nField 'key2' with value '2' is unknown"; |
192
|
|
|
$this->assertSame($expected, $e->getMessage()); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @test |
198
|
|
|
* @covers ::ofArrays |
199
|
|
|
*/ |
200
|
|
|
public function ofArraysFail() |
201
|
|
|
{ |
202
|
|
|
try { |
203
|
|
|
Arrays::ofArrays( |
204
|
|
|
[['key' => new \StdClass], ['key' => []], ['key' => null], 'key'], |
205
|
|
|
['key' => [['string']]] |
206
|
|
|
); |
207
|
|
|
$this->fail(); |
208
|
|
|
} catch (Exception $e) { |
209
|
|
|
$expected = <<<TXT |
210
|
|
|
Field 'key' with value 'stdClass::__set_state(array( |
211
|
|
|
))' failed filtering, message 'Value 'stdClass::__set_state(array( |
212
|
|
|
))' is not a string' |
213
|
|
|
Field 'key' with value 'array ( |
214
|
|
|
)' failed filtering, message 'Value 'array ( |
215
|
|
|
)' is not a string' |
216
|
|
|
Field 'key' with value 'NULL' failed filtering, message 'Value 'NULL' is not a string' |
217
|
|
|
Value at position '3' was not an array |
218
|
|
|
TXT; |
219
|
|
|
$this->assertSame($expected, $e->getMessage()); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @test |
225
|
|
|
* @covers ::ofArray |
226
|
|
|
*/ |
227
|
|
|
public function ofArray() |
228
|
|
|
{ |
229
|
|
|
$expected = ['key1' => 1, 'key2' => 2]; |
230
|
|
|
$spec = ['key1' => [['uint']], 'key2' => [['uint']]]; |
231
|
|
|
$this->assertSame($expected, Arrays::ofArray(['key1' => '1', 'key2' => '2'], $spec)); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @test |
236
|
|
|
* @covers ::ofArray |
237
|
|
|
*/ |
238
|
|
|
public function ofArrayChained() |
239
|
|
|
{ |
240
|
|
|
$expected = ['key' => 3.3]; |
241
|
|
|
$spec = ['key' => [['trim', 'a'], ['floatval']]]; |
242
|
|
|
$this->assertSame($expected, Arrays::ofArray(['key' => 'a3.3'], $spec)); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @test |
247
|
|
|
* @covers ::ofArray |
248
|
|
|
*/ |
249
|
|
|
public function ofArrayRequiredSuccess() |
250
|
|
|
{ |
251
|
|
|
$expected = ['key2' => 2]; |
252
|
|
|
$spec = ['key1' => [['uint']], 'key2' => ['required' => true, ['uint']]]; |
253
|
|
|
$this->assertSame($expected, Arrays::ofArray(['key2' => '2'], $spec)); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @test |
258
|
|
|
* @covers ::ofArray |
259
|
|
|
*/ |
260
|
|
View Code Duplication |
public function ofArrayRequiredFail() |
|
|
|
|
261
|
|
|
{ |
262
|
|
|
try { |
263
|
|
|
Arrays::ofArray(['key1' => '1'], ['key1' => [['uint']], 'key2' => ['required' => true, ['uint']]]); |
264
|
|
|
$this->fail(); |
265
|
|
|
} catch (Exception $e) { |
266
|
|
|
$expected = "Field 'key2' was required and not present"; |
267
|
|
|
$this->assertSame($expected, $e->getMessage()); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @test |
273
|
|
|
* @covers ::ofArray |
274
|
|
|
*/ |
275
|
|
View Code Duplication |
public function ofArrayUnknown() |
|
|
|
|
276
|
|
|
{ |
277
|
|
|
try { |
278
|
|
|
Arrays::ofArray(['key' => '1'], ['key2' => [['uint']]]); |
279
|
|
|
$this->fail(); |
280
|
|
|
} catch (Exception $e) { |
281
|
|
|
$expected = "Field 'key' with value '1' is unknown"; |
282
|
|
|
$this->assertSame($expected, $e->getMessage()); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Verifies the basic behavior of the flatten filter. |
288
|
|
|
* |
289
|
|
|
* @test |
290
|
|
|
* @covers ::flatten |
291
|
|
|
*/ |
292
|
|
|
public function flatten() |
293
|
|
|
{ |
294
|
|
|
$this->assertSame([1, 2, 3, 4, 5], Arrays::flatten([[1, 2], [[3, [4, 5]]]])); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
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.