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