1
|
|
|
<?php |
2
|
|
|
class CollectionTest extends \PHPUnit\Framework\TestCase |
3
|
|
|
{ |
4
|
|
|
public function testConstruct() |
5
|
|
|
{ |
6
|
|
|
$arr = array(1,2,3); |
7
|
|
|
$collection = new \Suricate\Collection($arr); |
8
|
|
|
$this->assertEquals($arr, $collection->getItems()); |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
public function testIsEmpty() |
12
|
|
|
{ |
13
|
|
|
$arr = array(); |
14
|
|
|
$collection = new \Suricate\Collection($arr); |
15
|
|
|
$this->assertEquals(true, $collection->isEmpty()); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testUnique() |
19
|
|
|
{ |
20
|
|
|
$arr = array(1, 2, 1, 3); |
21
|
|
|
$collection = new \Suricate\Collection($arr); |
22
|
|
|
$this->assertEquals([0 => 1, 1 => 2, 3 => 3], $collection->unique()->getItems()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testCount() |
26
|
|
|
{ |
27
|
|
|
$arr = array(1, 2, 3); |
28
|
|
|
$collection = new \Suricate\Collection($arr); |
29
|
|
|
$this->assertEquals(3, $collection->count()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testPaginate() |
33
|
|
|
{ |
34
|
|
|
$arr = array(1, 2, 3, 4, 5); |
35
|
|
|
$collection = new \Suricate\Collection($arr); |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
$collection->paginate(1, 3); |
39
|
|
|
$this->assertEquals(['page' => 3, 'nbItems' => 5, 'nbPages' => 5], $collection->pagination); |
40
|
|
|
$this->assertEquals([3], $collection->getItems()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testGetPossibleValuesFor() |
44
|
|
|
{ |
45
|
|
|
$arr = [ |
46
|
|
|
['id' => 40, 'name' => 'name 1'], |
47
|
|
|
['id' => 20, 'name' => 'name 2'], |
48
|
|
|
['id' => 35, 'name' => 'name 3'], |
49
|
|
|
]; |
50
|
|
|
$collection = new \Suricate\Collection($arr); |
51
|
|
|
|
52
|
|
|
$this->assertEquals([40 => 'name 1', 20 => 'name 2', 35 =>'name 3'], $collection->getPossibleValuesFor('name', 'id')); |
53
|
|
|
$this->assertEquals(['name 1', 'name 2', 'name 3'], $collection->getPossibleValuesFor('name')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testFirst() |
57
|
|
|
{ |
58
|
|
|
$arr = array(4, 5, 6); |
59
|
|
|
$collection = new \Suricate\Collection($arr); |
60
|
|
|
$this->assertEquals(4, $collection->first()); |
61
|
|
|
|
62
|
|
|
$collection = new \Suricate\Collection([]); |
63
|
|
|
$this->assertNull($collection->first()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testLast() |
67
|
|
|
{ |
68
|
|
|
$arr = array(1, 2, 3); |
69
|
|
|
$collection = new \Suricate\Collection($arr); |
70
|
|
|
$this->assertEquals(3, $collection->last()); |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
$collection = new \Suricate\Collection(); |
74
|
|
|
$this->assertEquals(null, $collection->last()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testSum() |
78
|
|
|
{ |
79
|
|
|
$arr = array(1, 2, 3); |
80
|
|
|
$collection = new \Suricate\Collection($arr); |
81
|
|
|
$this->assertEquals(6, $collection->sum()); |
82
|
|
|
|
83
|
|
|
$arr = [ |
84
|
|
|
['id' => 10, 'name' => 'azerty'], |
85
|
|
|
['id' => 2, 'name' => 'qsdfg'], |
86
|
|
|
['id' => 3, 'name' => 'wxcvbn'] |
87
|
|
|
]; |
88
|
|
|
$collection = new \Suricate\Collection($arr); |
89
|
|
|
$this->assertEquals(15, $collection->sum('id')); |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testHas() |
94
|
|
|
{ |
95
|
|
|
$arr = array('a' => 1, 'b' => 2, 'c' => 3); |
96
|
|
|
$collection = new \Suricate\Collection($arr); |
97
|
|
|
$this->assertEquals(true, $collection->has('b')); |
98
|
|
|
$this->assertEquals(false, $collection->has('d')); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testKeys() |
102
|
|
|
{ |
103
|
|
|
$arr = array('a' => 1, 'b' => 2, 'c' => 3); |
104
|
|
|
$collection = new \Suricate\Collection($arr); |
105
|
|
|
$this->assertEquals(['a', 'b', 'c'], $collection->keys()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testPrepend() |
109
|
|
|
{ |
110
|
|
|
$arr = [4, 5, 6]; |
111
|
|
|
$collection = new \Suricate\Collection($arr); |
112
|
|
|
$collection->prepend(99); |
113
|
|
|
$this->assertEquals([99, 4, 5, 6], $collection->getItems()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testPut() |
117
|
|
|
{ |
118
|
|
|
$arr = array('a' => 1, 'b' => 2, 'c' => 3); |
119
|
|
|
$collection = new \Suricate\Collection($arr); |
120
|
|
|
$collection->put('z', 99); |
121
|
|
|
$this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'z' => 99], $collection->getItems()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testShift() |
125
|
|
|
{ |
126
|
|
|
$arr = array('a' => 1, 'b' => 2, 'c' => 3); |
127
|
|
|
$collection = new \Suricate\Collection($arr); |
128
|
|
|
$shifted = $collection->shift(); |
129
|
|
|
$this->assertEquals(1, $shifted); |
130
|
|
|
$this->assertEquals(['b' => 2, 'c' => 3], $collection->getItems()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testPop() |
134
|
|
|
{ |
135
|
|
|
$arr = array('a' => 1, 'b' => 2, 'c' => 3); |
136
|
|
|
$collection = new \Suricate\Collection($arr); |
137
|
|
|
$popped = $collection->pop(); |
138
|
|
|
$this->assertEquals(3, $popped); |
139
|
|
|
$this->assertEquals(['a' => 1, 'b' => 2], $collection->getItems()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testReverse() |
143
|
|
|
{ |
144
|
|
|
$arr = array('a' => 1, 'b' => 2, 'c' => 3); |
145
|
|
|
$collection = new \Suricate\Collection($arr); |
146
|
|
|
$reversed = $collection->reverse(); |
147
|
|
|
$this->assertEquals(['c' => 3, 'b' => 2, 'a' => 1], $reversed->getItems()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testReduce() |
151
|
|
|
{ |
152
|
|
|
// Basic reduce |
153
|
|
|
$arr = ['a' => 1, 'b' => 2, 'c' => 3]; |
154
|
|
|
$collection = new \Suricate\Collection($arr); |
155
|
|
|
$callback = function ($carry, $item) { |
156
|
|
|
$carry += $item; |
157
|
|
|
|
158
|
|
|
return $carry; |
159
|
|
|
}; |
160
|
|
|
$reduced = $collection->reduce($callback); |
161
|
|
|
|
162
|
|
|
$this->assertEquals(6, $reduced); |
163
|
|
|
|
164
|
|
|
// reduce with initial value |
165
|
|
|
$arr = ['a' => 1, 'b' => 2, 'c' => 3]; |
166
|
|
|
$collection = new \Suricate\Collection($arr); |
167
|
|
|
$callback = function ($carry, $item) { |
168
|
|
|
$carry += $item; |
169
|
|
|
|
170
|
|
|
return $carry; |
171
|
|
|
}; |
172
|
|
|
$reduced = $collection->reduce($callback, 100); |
173
|
|
|
|
174
|
|
|
$this->assertEquals(106, $reduced); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function testSlice() |
178
|
|
|
{ |
179
|
|
|
// Basic slice |
180
|
|
|
$arr = ['a' => 1, 'b' => 2, 'c' => 3]; |
181
|
|
|
$collection = new \Suricate\Collection($arr); |
182
|
|
|
$sliced = $collection->slice(1, 2); |
183
|
|
|
|
184
|
|
|
$this->assertEquals(['b' => 2, 'c' => 3], $sliced->getItems()); |
185
|
|
|
|
186
|
|
|
// Slice with numeric keys |
187
|
|
|
$arr = [1, 2, 3]; |
188
|
|
|
$collection = new \Suricate\Collection($arr); |
189
|
|
|
$sliced = $collection->slice(1, 2); |
190
|
|
|
|
191
|
|
|
$this->assertEquals([2, 3], $sliced->getItems()); |
192
|
|
|
|
193
|
|
|
// Slice preserve keys |
194
|
|
|
$arr = [1, 2, 3]; |
195
|
|
|
$collection = new \Suricate\Collection($arr); |
196
|
|
|
$sliced = $collection->slice(1, 2, true); |
197
|
|
|
|
198
|
|
|
$this->assertEquals([1 => 2, 2 => 3], $sliced->getItems()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function testTake() |
202
|
|
|
{ |
203
|
|
|
$arr = array('a' => 1, 'b' => 2, 'c' => 3); |
204
|
|
|
$collection = new \Suricate\Collection($arr); |
205
|
|
|
$taken = $collection->take(2); |
206
|
|
|
$this->assertEquals(['a' => 1, 'b' => 2], $taken->getItems()); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function testFilter() |
210
|
|
|
{ |
211
|
|
|
$arr = array('a' => 1, 'b' => 2, 'c' => 3); |
212
|
|
|
$collection = new \Suricate\Collection($arr); |
213
|
|
|
$this->assertEquals( |
214
|
|
|
['b' => 2], |
215
|
|
|
$collection->filter(function ($value) { |
216
|
|
|
return ($value % 2) === 0; |
217
|
|
|
})->getItems() |
218
|
|
|
); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testPush() |
222
|
|
|
{ |
223
|
|
|
$arr = array(1, 2, 3); |
224
|
|
|
$collection = new \Suricate\Collection($arr); |
225
|
|
|
$this->assertEquals([1, 2, 3, 4], $collection->push(4)->getItems()); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function testGetValuesFor() |
229
|
|
|
{ |
230
|
|
|
$arr = [ |
231
|
|
|
['id' => 1, 'name' => 'azerty'], |
232
|
|
|
['id' => 2, 'name' => 'qsdfg'], |
233
|
|
|
['id' => 3, 'name' => 'wxcvbn'] |
234
|
|
|
]; |
235
|
|
|
$collection = new \Suricate\Collection($arr); |
236
|
|
|
$this->assertEquals([ |
237
|
|
|
'azerty', |
238
|
|
|
'qsdfg', |
239
|
|
|
'wxcvbn'], |
240
|
|
|
$collection->getValuesFor('name') |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function testSearch() |
245
|
|
|
{ |
246
|
|
|
$arr = array('a' => 1, 'b' => 2, 'c' => 3); |
247
|
|
|
$collection = new \Suricate\Collection($arr); |
248
|
|
|
$this->assertEquals('c', $collection->search('3')); |
249
|
|
|
$this->assertEquals('c', $collection->search(3, true)); |
250
|
|
|
$this->assertFalse($collection->search(22)); |
251
|
|
|
$this->assertFalse($collection->search('3', true)); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function testOffsetExists() |
255
|
|
|
{ |
256
|
|
|
$arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => null, 'z' => 99]; |
257
|
|
|
|
258
|
|
|
$collection = new \Suricate\Collection($arr); |
259
|
|
|
$this->assertTrue($collection->offsetExists('a')); |
260
|
|
|
$this->assertTrue($collection->offsetExists('d')); |
261
|
|
|
$this->assertFalse($collection->offsetExists('e')); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function testOffsetGet() |
265
|
|
|
{ |
266
|
|
|
$arr = [1, 2, 3]; |
267
|
|
|
|
268
|
|
|
$collection = new \Suricate\Collection($arr); |
269
|
|
|
|
270
|
|
|
$this->assertNull($collection->offsetGet(4)); |
271
|
|
|
$this->assertSame(2, $collection->offsetGet(1)); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
public function testOffsetSet() |
275
|
|
|
{ |
276
|
|
|
$arr = [1, 2, 3]; |
277
|
|
|
|
278
|
|
|
$collection = new \Suricate\Collection($arr); |
279
|
|
|
$collection->offsetSet(null, 4); |
280
|
|
|
$this->assertSame([1, 2, 3, 4], $collection->getItems()); |
281
|
|
|
|
282
|
|
|
$collection->offsetSet('a', 5); |
283
|
|
|
$this->assertSame([1, 2, 3, 4, 'a' => 5], $collection->getItems()); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
public function testOffsetUnset() |
287
|
|
|
{ |
288
|
|
|
$arr = [1, 2, 3]; |
289
|
|
|
|
290
|
|
|
$collection = new \Suricate\Collection($arr); |
291
|
|
|
$collection->offsetUnset(1); |
292
|
|
|
$this->assertSame([0 => 1, 2 => 3], $collection->getItems()); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|