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