1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Filter; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use TraderInteractive\Exceptions\FilterException; |
8
|
|
|
use TraderInteractive\Filter\Exceptions\DuplicateValuesException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @coversDefaultClass \TraderInteractive\Filter\Arrays |
12
|
|
|
* @covers ::<private> |
13
|
|
|
*/ |
14
|
|
|
final class ArraysTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @test |
18
|
|
|
* @covers ::filter |
19
|
|
|
*/ |
20
|
|
|
public function filterBasicPass() |
21
|
|
|
{ |
22
|
|
|
$this->assertSame(['boo'], Arrays::filter(['boo'])); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @test |
27
|
|
|
* @covers ::filter |
28
|
|
|
* @expectedException \TraderInteractive\Exceptions\FilterException |
29
|
|
|
* @expectedExceptionMessage Value '1' is not an array |
30
|
|
|
*/ |
31
|
|
|
public function filterFailNotArray() |
32
|
|
|
{ |
33
|
|
|
Arrays::filter(1); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @test |
38
|
|
|
* @covers ::filter |
39
|
|
|
* @expectedException \TraderInteractive\Exceptions\FilterException |
40
|
|
|
* @expectedExceptionMessage $value count of 0 is less than 1 |
41
|
|
|
*/ |
42
|
|
|
public function filterFailEmpty() |
43
|
|
|
{ |
44
|
|
|
Arrays::filter([]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @test |
49
|
|
|
* @covers ::filter |
50
|
|
|
* @expectedException \TraderInteractive\Exceptions\FilterException |
51
|
|
|
* @expectedExceptionMessage $value count of 1 is less than 2 |
52
|
|
|
*/ |
53
|
|
|
public function filterCountLessThanMin() |
54
|
|
|
{ |
55
|
|
|
Arrays::filter([0], 2); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @test |
60
|
|
|
* @covers ::filter |
61
|
|
|
* @expectedException \TraderInteractive\Exceptions\FilterException |
62
|
|
|
* @expectedExceptionMessage $value count of 2 is greater than 1 |
63
|
|
|
*/ |
64
|
|
|
public function filterCountGreaterThanMax() |
65
|
|
|
{ |
66
|
|
|
Arrays::filter([0, 1], 1, 1); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @test |
71
|
|
|
* @covers ::in |
72
|
|
|
*/ |
73
|
|
|
public function inPassStrict() |
74
|
|
|
{ |
75
|
|
|
$this->assertSame('boo', Arrays::in('boo', ['boo'])); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @test |
80
|
|
|
* @covers ::in |
81
|
|
|
*/ |
82
|
|
|
public function inFailStrict() |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
|
|
Arrays::in('0', [0]); |
86
|
|
|
$this->fail(); |
87
|
|
|
} catch (FilterException $e) { |
88
|
|
|
$this->assertSame("Value '0' is not in array array (\n 0 => 0,\n)", $e->getMessage()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @test |
94
|
|
|
* @covers ::in |
95
|
|
|
*/ |
96
|
|
|
public function inFailNotStrict() |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
|
|
Arrays::in('boo', ['foo'], false); |
100
|
|
|
$this->fail(); |
101
|
|
|
} catch (FilterException $e) { |
102
|
|
|
$this->assertSame("Value 'boo' is not in array array (\n 0 => 'foo',\n)", $e->getMessage()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @test |
108
|
|
|
* @covers ::in |
109
|
|
|
*/ |
110
|
|
|
public function inPassNotStrict() |
111
|
|
|
{ |
112
|
|
|
$this->assertSame('0', Arrays::in('0', [0], false)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Verifies the basic behavior of the flatten filter. |
117
|
|
|
* |
118
|
|
|
* @test |
119
|
|
|
* @covers ::flatten |
120
|
|
|
*/ |
121
|
|
|
public function flatten() |
122
|
|
|
{ |
123
|
|
|
$this->assertSame([1, 2, 3, 4, 5], Arrays::flatten([[1, 2], [[3, [4, 5]]]])); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @test |
128
|
|
|
* @covers ::arrayize |
129
|
|
|
*/ |
130
|
|
|
public function arrayizeReturnsInputIfItIsAnArray() |
131
|
|
|
{ |
132
|
|
|
$this->assertSame([1, 2, 3, 4, 5], Arrays::arrayize([1, 2, 3, 4, 5])); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @test |
137
|
|
|
* @covers ::arrayize |
138
|
|
|
*/ |
139
|
|
|
public function arrayizeWrapsNonArrayValue() |
140
|
|
|
{ |
141
|
|
|
$value = new \StdClass(); |
142
|
|
|
$this->assertSame([$value], Arrays::arrayize($value)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @test |
147
|
|
|
* @covers ::arrayize |
148
|
|
|
*/ |
149
|
|
|
public function arrayizeConvertsNullToEmptyArray() |
150
|
|
|
{ |
151
|
|
|
$this->assertSame([], Arrays::arrayize(null)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @test |
156
|
|
|
* @covers ::copy |
157
|
|
|
*/ |
158
|
|
|
public function copy() |
159
|
|
|
{ |
160
|
|
|
$source = ['foo' => 1, 'bar' => 2, 'extra' => 3]; |
161
|
|
|
$keyMap = [ |
162
|
|
|
'far' => 'foo', |
163
|
|
|
'bar', |
164
|
|
|
]; |
165
|
|
|
$result = Arrays::copy($source, $keyMap); |
166
|
|
|
$this->assertSame( |
167
|
|
|
[ |
168
|
|
|
'far' => $source['foo'], |
169
|
|
|
'bar' => $source['bar'], |
170
|
|
|
], |
171
|
|
|
$result |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @test |
177
|
|
|
* @covers ::copyEach |
178
|
|
|
*/ |
179
|
|
|
public function copyEach() |
180
|
|
|
{ |
181
|
|
|
$input = [ |
182
|
|
|
['foo' => 1, 'bar' => 2], |
183
|
|
|
['foo' => 3, 'bar' => 4], |
184
|
|
|
]; |
185
|
|
|
$keyMap = [ |
186
|
|
|
'far' => 'foo', |
187
|
|
|
'bar', |
188
|
|
|
]; |
189
|
|
|
$result = Arrays::copyEach($input, $keyMap); |
190
|
|
|
$this->assertSame( |
191
|
|
|
[ |
192
|
|
|
[ |
193
|
|
|
'far' => 1, |
194
|
|
|
'bar' => 2, |
195
|
|
|
], |
196
|
|
|
[ |
197
|
|
|
'far' => 3, |
198
|
|
|
'bar' => 4, |
199
|
|
|
], |
200
|
|
|
], |
201
|
|
|
$result |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @test |
207
|
|
|
* @covers ::pad |
208
|
|
|
*/ |
209
|
|
|
public function pad() |
210
|
|
|
{ |
211
|
|
|
$result = Arrays::pad([12, 10, 9], 5, 0); |
212
|
|
|
$this->assertSame([12, 10, 9, 0, 0], $result); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @test |
217
|
|
|
* @covers ::pad |
218
|
|
|
*/ |
219
|
|
|
public function padArrayLengthGreaterThanSize() |
220
|
|
|
{ |
221
|
|
|
$result = Arrays::pad(['a', 'b', 'c'], 2, 0); |
222
|
|
|
$this->assertSame(['a', 'b', 'c'], $result); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @test |
227
|
|
|
* @covers ::pad |
228
|
|
|
*/ |
229
|
|
|
public function padFront() |
230
|
|
|
{ |
231
|
|
|
$result = Arrays::pad(['a', 'b', 'c'], 5, null, Arrays::ARRAY_PAD_FRONT); |
232
|
|
|
$this->assertSame([null, null, 'a', 'b', 'c'], $result); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @test |
237
|
|
|
* @covers ::pad |
238
|
|
|
*/ |
239
|
|
|
public function padInvalidPadType() |
240
|
|
|
{ |
241
|
|
|
$this->expectException(InvalidArgumentException::class); |
242
|
|
|
$this->expectExceptionMessage('Invalid $padType value provided'); |
243
|
|
|
Arrays::pad(['a', 'b', 'c'], 5, null, 0); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @test |
248
|
|
|
* @covers ::unique |
249
|
|
|
*/ |
250
|
|
|
public function unique() |
251
|
|
|
{ |
252
|
|
|
$input = ['foo', 'bar', 'foo']; |
253
|
|
|
$filteredValue = Arrays::unique($input); |
254
|
|
|
$this->assertSame(['foo', 'bar'], $filteredValue); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @test |
259
|
|
|
* @covers ::unique |
260
|
|
|
*/ |
261
|
|
|
public function uniqueStrict() |
262
|
|
|
{ |
263
|
|
|
$input = ['foo', 'bar', 'foo']; |
264
|
|
|
$expectedDuplicates = ['2' => 'foo']; |
265
|
|
|
$expectedException = new DuplicateValuesException($expectedDuplicates); |
266
|
|
|
$this->expectException(DuplicateValuesException::class); |
267
|
|
|
$this->expectExceptionMessage($expectedException->getMessage()); |
268
|
|
|
Arrays::unique($input, Arrays::ARRAY_UNIQUE_SORT_STRING, true); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|