|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DimshTests\Models\Collections; |
|
4
|
|
|
|
|
5
|
|
|
use Dimsh\Models\Collections\Collection; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
|
|
8
|
|
|
class CollectionTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
protected function getNewTestBasicObject() |
|
11
|
|
|
{ |
|
12
|
|
|
return new TestBasicObject; |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
protected function getArray() |
|
16
|
|
|
{ |
|
17
|
|
|
$arr = ['a']; |
|
18
|
|
|
return $arr; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Tell if two variables are PHP References to each other. |
|
23
|
|
|
* |
|
24
|
|
|
* @param $var1 |
|
25
|
|
|
* @param $var2 |
|
26
|
|
|
* |
|
27
|
|
|
* @return bool |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function isReference(&$var1, &$var2) |
|
30
|
|
|
{ |
|
31
|
|
|
//If a reference exists, the type IS the same |
|
32
|
|
|
if (gettype($var1) !== gettype($var2)) { |
|
33
|
|
|
return false; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$same = false; |
|
37
|
|
|
|
|
38
|
|
|
//We now only need to ask for var1 to be an array ;-) |
|
39
|
|
|
if (is_array($var1)) { |
|
40
|
|
|
//Look for an unused index in $var1 |
|
41
|
|
|
do { |
|
42
|
|
|
$key = uniqid("is_ref_", true); |
|
43
|
|
|
} while (array_key_exists($key, $var1)); |
|
44
|
|
|
|
|
45
|
|
|
//The two variables differ in content ... They can't be the same |
|
46
|
|
|
if (array_key_exists($key, $var2)) { |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
//The arrays point to the same data if changes are reflected in $var2 |
|
51
|
|
|
$data = uniqid("is_ref_data_", true); |
|
52
|
|
|
$var1[$key] =& $data; |
|
53
|
|
|
//There seems to be a modification ... |
|
54
|
|
|
if (array_key_exists($key, $var2)) { |
|
55
|
|
|
if ($var2[$key] === $data) { |
|
56
|
|
|
$same = true; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
//Undo our changes ... |
|
61
|
|
|
unset($var1[$key]); |
|
62
|
|
|
} elseif (is_object($var1)) { |
|
63
|
|
|
//The same objects are required to have equal class names ;-) |
|
64
|
|
|
if (get_class($var1) !== get_class($var2)) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$obj1 = array_keys(get_object_vars($var1)); |
|
69
|
|
|
$obj2 = array_keys(get_object_vars($var2)); |
|
70
|
|
|
|
|
71
|
|
|
//Look for an unused index in $var1 |
|
72
|
|
|
do { |
|
73
|
|
|
$key = uniqid("is_ref_", true); |
|
74
|
|
|
} while (in_array($key, $obj1)); |
|
75
|
|
|
|
|
76
|
|
|
//The two variables differ in content ... They can't be the same |
|
77
|
|
|
if (in_array($key, $obj2)) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
//The arrays point to the same data if changes are reflected in $var2 |
|
82
|
|
|
$data = uniqid("is_ref_data_", true); |
|
83
|
|
|
$var1->$key =& $data; |
|
84
|
|
|
//There seems to be a modification ... |
|
85
|
|
|
if (isset($var2->$key)) { |
|
86
|
|
|
if ($var2->$key === $data) { |
|
87
|
|
|
$same = true; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
//Undo our changes ... |
|
92
|
|
|
unset($var1->$key); |
|
93
|
|
|
} elseif (is_resource($var1)) { |
|
94
|
|
|
if (get_resource_type($var1) !== get_resource_type($var2)) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return ((string)$var1) === ((string)$var2); |
|
99
|
|
|
} else { |
|
100
|
|
|
//Simple variables ... |
|
101
|
|
|
if ($var1 !== $var2) { |
|
102
|
|
|
//Data mismatch ... They can't be the same ... |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
//To check for a reference of a variable with simple type |
|
107
|
|
|
//simply store its old value and check against modifications of the second variable ;-) |
|
108
|
|
|
|
|
109
|
|
|
do { |
|
110
|
|
|
$key = uniqid("is_ref_", true); |
|
111
|
|
|
} while ($key === $var1); |
|
112
|
|
|
|
|
113
|
|
|
$tmp = $var1; //WE NEED A COPY HERE!!! |
|
114
|
|
|
$var1 = $key; //Set var1 to the value of $key (copy) |
|
115
|
|
|
$same = $var1 === $var2; //Check if $var2 was modified too ... |
|
116
|
|
|
$var1 = $tmp; //Undo our changes ... |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $same; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testSetupStrict() |
|
123
|
|
|
{ |
|
124
|
|
|
$list = new TestBasicObjectsList; |
|
125
|
|
|
$this->expectException(\Exception::class); |
|
126
|
|
|
$list[] = 1; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function testSetup() |
|
130
|
|
|
{ |
|
131
|
|
|
$list = new TestBasicObjectsList; |
|
132
|
|
|
$this->assertEquals($list, $list->add(new TestBasicObject)); |
|
133
|
|
|
$this->assertEquals(1, $list->count()); |
|
134
|
|
|
$this->assertCount(1, $list); |
|
135
|
|
|
$this->assertInstanceOf(TestBasicObject::class, $list[0]); |
|
136
|
|
|
$this->assertNotEmpty($list[0]); |
|
137
|
|
|
$this->assertFalse(isset($list[1])); |
|
138
|
|
|
unset($list[0]); |
|
139
|
|
|
$this->assertEquals(0, $list->count()); |
|
140
|
|
|
$this->assertCount(0, $list); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
protected function getListSetPropertyValue5MethodAppend(TestBasicObject $myObject) |
|
144
|
|
|
{ |
|
145
|
|
|
$list = new Collection(); |
|
146
|
|
|
$myObject->public_property = 5; |
|
147
|
|
|
$list[] = $myObject; |
|
148
|
|
|
return $list; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
protected function getListSetPropertyValue5MethodAdd(TestBasicObject $myObject) |
|
152
|
|
|
{ |
|
153
|
|
|
$list = new Collection(); |
|
154
|
|
|
$myObject->public_property = 5; |
|
155
|
|
|
$list->add($myObject); |
|
156
|
|
|
return $list; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function testReferences() |
|
160
|
|
|
{ |
|
161
|
|
|
$list = new Collection(); |
|
162
|
|
|
$list[] = $this->getNewTestBasicObject(); |
|
163
|
|
|
$list->add($this->getNewTestBasicObject()); |
|
164
|
|
|
$this->assertFalse($this->isReference($list[0], $list[1])); |
|
165
|
|
|
|
|
166
|
|
|
$myObject = new TestBasicObject(); |
|
167
|
|
|
$myObject->public_property = 3; |
|
168
|
|
|
$list[] = $myObject; |
|
169
|
|
|
|
|
170
|
|
|
$myObject->public_property = 6; |
|
171
|
|
|
$this->assertTrue($this->isReference($list[2], $myObject)); |
|
172
|
|
|
$this->assertEquals($list[2]->getMyHash(), $myObject->getMyHash()); |
|
173
|
|
|
$this->assertEquals(6, $list[2]->public_property); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function testReferences2MethodAppend() |
|
177
|
|
|
{ |
|
178
|
|
|
$myObject = new TestBasicObject(); |
|
179
|
|
|
$list = $this->getListSetPropertyValue5MethodAppend($myObject); |
|
180
|
|
|
$this->assertEquals(5, $myObject->public_property); |
|
181
|
|
|
$this->assertEquals(5, $list[0]->public_property); |
|
182
|
|
|
|
|
183
|
|
|
$list->rewind(); |
|
184
|
|
|
$returnedObject = $list->current(); |
|
185
|
|
|
$this->assertEquals(5, $returnedObject->public_property); |
|
186
|
|
|
|
|
187
|
|
|
$this->assertTrue($this->isReference($myObject, $returnedObject)); |
|
188
|
|
|
|
|
189
|
|
|
$returnedObject->public_property = 9; |
|
190
|
|
|
unset($list[0]); |
|
191
|
|
|
unset($returnedObject); |
|
192
|
|
|
$this->assertEquals(9, $myObject->public_property); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function testReferences2MethodAdd() |
|
196
|
|
|
{ |
|
197
|
|
|
$myObject = new TestBasicObject(); |
|
198
|
|
|
$list = $this->getListSetPropertyValue5MethodAdd($myObject); |
|
199
|
|
|
$this->assertEquals(5, $myObject->public_property); |
|
200
|
|
|
$this->assertEquals(5, $list[0]->public_property); |
|
201
|
|
|
|
|
202
|
|
|
$list->rewind(); |
|
203
|
|
|
$returnedObject = $list->current(); |
|
204
|
|
|
$this->assertEquals(5, $returnedObject->public_property); |
|
205
|
|
|
|
|
206
|
|
|
$this->assertTrue($this->isReference($myObject, $returnedObject)); |
|
207
|
|
|
|
|
208
|
|
|
$returnedObject->public_property = 9; |
|
209
|
|
|
unset($list[0]); |
|
210
|
|
|
unset($returnedObject); |
|
211
|
|
|
$this->assertEquals(9, $myObject->public_property); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function testReferencesScalar() |
|
215
|
|
|
{ |
|
216
|
|
|
$list = new Collection(); |
|
217
|
|
|
$list->add("my string"); |
|
218
|
|
|
$this->assertEquals("my string", $list[0]); |
|
219
|
|
|
$list->add($this->getArray()); |
|
220
|
|
|
$array = $this->getArray(); |
|
221
|
|
|
$this->assertFalse($this->isReference($list[1], $array)); |
|
222
|
|
|
|
|
223
|
|
|
$list[1][0] = 'b'; |
|
224
|
|
|
$this->assertNotEquals($this->getArray(), $list[1]); |
|
225
|
|
|
$this->assertNotEquals($array, $list[1]); |
|
226
|
|
|
$array = $this->getArray(); |
|
227
|
|
|
$this->assertNotEquals($array, $list[1]); |
|
228
|
|
|
|
|
229
|
|
|
unset($list); |
|
230
|
|
|
|
|
231
|
|
|
$list = new Collection(); |
|
232
|
|
|
$array = $this->getArray(); |
|
233
|
|
|
|
|
234
|
|
|
$list->addByReference($array); |
|
235
|
|
|
$this->assertTrue($this->isReference($list[0], $array)); |
|
236
|
|
|
$list[0][0] = 'b'; |
|
237
|
|
|
$this->assertEquals($array, $list[0]); |
|
238
|
|
|
|
|
239
|
|
|
$list->rewind(); |
|
240
|
|
|
$zero = &$list[0]; |
|
241
|
|
|
$current = $list->current(); |
|
242
|
|
|
|
|
243
|
|
|
$this->assertTrue($this->isReference($zero, $array)); |
|
244
|
|
|
$this->assertFalse($this->isReference($zero, $current)); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
public function testSwapAndReferencesWithSwap() |
|
248
|
|
|
{ |
|
249
|
|
|
$list = new Collection(); |
|
250
|
|
|
$list[] = "my string"; |
|
251
|
|
|
$list[] = "another string"; |
|
252
|
|
|
$list[] = ["array item"]; |
|
253
|
|
|
$list[] = 5; |
|
254
|
|
|
$list[] = "last string"; |
|
255
|
|
|
$myObject = new TestBasicObject(); |
|
256
|
|
|
$list[] = $myObject; |
|
257
|
|
|
|
|
258
|
|
|
$list->swap(1, 3); |
|
259
|
|
|
$this->assertEquals("my string", $list[0]); |
|
260
|
|
|
$this->assertEquals(5, $list[1]); |
|
261
|
|
|
$this->assertEquals("5", $list[1]); |
|
262
|
|
|
$this->assertEquals(["array item"], $list[2]); |
|
263
|
|
|
$this->assertEquals("another string", $list[3]); |
|
264
|
|
|
|
|
265
|
|
|
$clone1 = clone $list; |
|
266
|
|
|
$clone2 = clone $list; |
|
267
|
|
|
|
|
268
|
|
|
$clone1->swap(0, 1); |
|
269
|
|
|
$clone2->swapReorder(0, 1); |
|
270
|
|
|
|
|
271
|
|
|
$this->assertEquals($clone1->toArray(), $clone2->toArray()); |
|
272
|
|
|
$this->assertFalse($this->isReference($clone1, $clone2)); |
|
273
|
|
|
|
|
274
|
|
|
$myObject->public_property = 9; |
|
275
|
|
|
|
|
276
|
|
|
$this->assertEquals(9, $clone1[5]->public_property); |
|
277
|
|
|
$this->assertTrue($this->isReference($clone1[5], $clone2[5])); |
|
278
|
|
|
|
|
279
|
|
|
$clone1->swap(0, 5); |
|
280
|
|
|
$clone2->swap(0, 5); |
|
281
|
|
|
$this->assertTrue($this->isReference($clone1[0], $clone2[0])); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
public function testArrayFunctions() |
|
285
|
|
|
{ |
|
286
|
|
|
$list_multi_2_flipped = new Collection(); |
|
287
|
|
|
$list_multi_3_flipped = new Collection(); |
|
288
|
|
|
for ($i = 1; $i < 6; $i++) { |
|
289
|
|
|
$list_multi_2_flipped[$i * 2] = $i; |
|
290
|
|
|
$list_multi_3_flipped[$i * 3] = $i; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
|
|
294
|
|
|
$list_diff = $list_multi_2_flipped->diffKey($list_multi_3_flipped); |
|
295
|
|
|
$this->assertEquals([ |
|
296
|
|
|
2 => 1, |
|
297
|
|
|
4 => 2, |
|
298
|
|
|
8 => 4, |
|
299
|
|
|
10 => 5, |
|
300
|
|
|
], $list_diff->toArray()); |
|
301
|
|
|
|
|
302
|
|
|
$list_diff = $list_multi_3_flipped->diffKey($list_multi_2_flipped); |
|
303
|
|
|
$this->assertEquals([ |
|
304
|
|
|
3 => 1, |
|
305
|
|
|
9 => 3, |
|
306
|
|
|
12 => 4, |
|
307
|
|
|
15 => 5, |
|
308
|
|
|
], $list_diff->toArray()); |
|
309
|
|
|
|
|
310
|
|
|
$list_intersect = $list_multi_2_flipped->intersectKey($list_multi_3_flipped); |
|
311
|
|
|
$this->assertEquals([ |
|
312
|
|
|
6 => 3, |
|
313
|
|
|
], $list_intersect->toArray()); |
|
314
|
|
|
|
|
315
|
|
|
$list_intersect = $list_multi_3_flipped->intersectKey($list_multi_2_flipped); |
|
316
|
|
|
$this->assertEquals([ |
|
317
|
|
|
6 => 2, |
|
318
|
|
|
], $list_intersect->toArray()); |
|
319
|
|
|
|
|
320
|
|
|
|
|
321
|
|
|
$list_multi_2 = new Collection(); |
|
322
|
|
|
$list_multi_4 = new Collection(); |
|
323
|
|
|
for ($i = 1; $i < 6; $i++) { |
|
324
|
|
|
$list_multi_2[$i] = $i * 2; |
|
325
|
|
|
$list_multi_4[$i] = $i * 4; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
$list_diff = $list_multi_2->diffRecursive($list_multi_4); |
|
329
|
|
|
$this->assertEquals([ |
|
330
|
|
|
1 => 2, |
|
331
|
|
|
3 => 6, |
|
332
|
|
|
5 => 10, |
|
333
|
|
|
], $list_diff->toArray()); |
|
334
|
|
|
|
|
335
|
|
|
$list_diff = $list_multi_4->diffRecursive($list_multi_2); |
|
336
|
|
|
$this->assertEquals([ |
|
337
|
|
|
3 => 12, |
|
338
|
|
|
4 => 16, |
|
339
|
|
|
5 => 20, |
|
340
|
|
|
], $list_diff->toArray()); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
public function testOffsetGetOnNonExistedKey() |
|
344
|
|
|
{ |
|
345
|
|
|
$collection = new Collection(); |
|
346
|
|
|
|
|
347
|
|
|
$this->assertNull($collection->offsetGet('non_existed_key')); |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
public function testPrev() |
|
351
|
|
|
{ |
|
352
|
|
|
$collection = new Collection(); |
|
353
|
|
|
$collection[0] = 0; |
|
354
|
|
|
$collection[1] = 1; |
|
355
|
|
|
$collection[2] = 2; |
|
356
|
|
|
|
|
357
|
|
|
$this->assertEquals(0, $collection->current()); |
|
358
|
|
|
|
|
359
|
|
|
$collection->next(); |
|
360
|
|
|
|
|
361
|
|
|
$this->assertEquals(1, $collection->current()); |
|
362
|
|
|
|
|
363
|
|
|
$collection->prev(); |
|
364
|
|
|
|
|
365
|
|
|
$this->assertEquals(0, $collection->current()); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
public function testValidOnEmptyCollection() |
|
369
|
|
|
{ |
|
370
|
|
|
$collection = new Collection(); |
|
371
|
|
|
|
|
372
|
|
|
$this->assertFalse($collection->valid()); |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* @expectedException \Exception |
|
377
|
|
|
* @expectedExceptionMessage Calling next or rewind is required after call to unset, current item can't be accessed otherwise because it is removed |
|
378
|
|
|
*/ |
|
379
|
|
|
public function testCurrentThrowAfterUnsetException() |
|
380
|
|
|
{ |
|
381
|
|
|
$collection = new Collection(); |
|
382
|
|
|
$collection[] = 0; |
|
383
|
|
|
$collection->offsetUnset(0); |
|
384
|
|
|
|
|
385
|
|
|
$collection->current(); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
public function testLast() |
|
389
|
|
|
{ |
|
390
|
|
|
$collection = new Collection(); |
|
391
|
|
|
$collection[0] = 0; |
|
392
|
|
|
$collection[1] = 1; |
|
393
|
|
|
$collection[2] = 2; |
|
394
|
|
|
|
|
395
|
|
|
$this->assertEquals(2, $collection->last()); |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
public function testLastOnEmptyCollection() |
|
399
|
|
|
{ |
|
400
|
|
|
$collection = new Collection(); |
|
401
|
|
|
|
|
402
|
|
|
$this->assertNull($collection->last()); |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
public function testFirst() |
|
406
|
|
|
{ |
|
407
|
|
|
$collection = new Collection(); |
|
408
|
|
|
$collection[0] = 0; |
|
409
|
|
|
$collection[1] = 1; |
|
410
|
|
|
$collection[2] = 2; |
|
411
|
|
|
|
|
412
|
|
|
$this->assertEquals(0, $collection->first()); |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
public function testFirstOnEmptyCollection() |
|
416
|
|
|
{ |
|
417
|
|
|
$collection = new Collection(); |
|
418
|
|
|
|
|
419
|
|
|
$this->assertNull($collection->first()); |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
public function testKeys() |
|
423
|
|
|
{ |
|
424
|
|
|
$collection = new Collection(); |
|
425
|
|
|
$collection[] = 0; |
|
426
|
|
|
$collection[] = 1; |
|
427
|
|
|
|
|
428
|
|
|
$this->assertEquals([0, 1], $collection->keys()); |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
public function testMerge() |
|
432
|
|
|
{ |
|
433
|
|
|
$collection = new Collection(); |
|
434
|
|
|
$collection[] = 0; |
|
435
|
|
|
$collection[] = 1; |
|
436
|
|
|
$collection[] = 2; |
|
437
|
|
|
|
|
438
|
|
|
$anotherCollection = new Collection(); |
|
439
|
|
|
$collection[] = 3; |
|
440
|
|
|
|
|
441
|
|
|
$resultMergedCollection = $collection->merge($anotherCollection); |
|
442
|
|
|
|
|
443
|
|
|
$this->assertEquals([ |
|
444
|
|
|
0, |
|
445
|
|
|
1, |
|
446
|
|
|
2, |
|
447
|
|
|
3, |
|
448
|
|
|
], $resultMergedCollection->toArray()); |
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
public function testSwapOnSameOffsetPosition() |
|
452
|
|
|
{ |
|
453
|
|
|
$collection = new Collection(); |
|
454
|
|
|
$collection[] = 0; |
|
455
|
|
|
$collection[] = 1; |
|
456
|
|
|
$collection[] = 2; |
|
457
|
|
|
|
|
458
|
|
|
$this->assertInstanceOf(Collection::class, $collection->swap(1, 1)); |
|
459
|
|
|
$this->assertEquals([ |
|
460
|
|
|
0, |
|
461
|
|
|
1, |
|
462
|
|
|
2, |
|
463
|
|
|
], $collection->toArray()); |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
public function testSwapThrowExceptionOnInvalidPosition() |
|
467
|
|
|
{ |
|
468
|
|
|
$collection = new Collection(); |
|
469
|
|
|
$collection[] = 0; |
|
470
|
|
|
$collection[] = 1; |
|
471
|
|
|
$collection[] = 2; |
|
472
|
|
|
|
|
473
|
|
|
$this->expectException(\Exception::class); |
|
474
|
|
|
|
|
475
|
|
|
$collection->swap(3, 2); |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
public function testSwapReorderOnSameOffsetPosition() |
|
479
|
|
|
{ |
|
480
|
|
|
$collection = new Collection(); |
|
481
|
|
|
$collection[] = 0; |
|
482
|
|
|
$collection[] = 1; |
|
483
|
|
|
$collection[] = 2; |
|
484
|
|
|
|
|
485
|
|
|
$this->assertInstanceOf(Collection::class, $collection->swapReorder(1, 1)); |
|
486
|
|
|
$this->assertEquals([ |
|
487
|
|
|
0, |
|
488
|
|
|
1, |
|
489
|
|
|
2, |
|
490
|
|
|
], $collection->toArray()); |
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
|
|
public function testSwapReorderThrowExceptionOnInvalidPosition() |
|
494
|
|
|
{ |
|
495
|
|
|
$collection = new Collection(); |
|
496
|
|
|
$collection[] = 0; |
|
497
|
|
|
$collection[] = 1; |
|
498
|
|
|
$collection[] = 2; |
|
499
|
|
|
|
|
500
|
|
|
$this->expectException(\Exception::class); |
|
501
|
|
|
|
|
502
|
|
|
$collection->swapReorder(3, 2); |
|
503
|
|
|
} |
|
504
|
|
|
|
|
505
|
|
|
public function testByPositionGet() |
|
506
|
|
|
{ |
|
507
|
|
|
$collection = new Collection(); |
|
508
|
|
|
$collection[] = 0; |
|
509
|
|
|
$collection[] = 1; |
|
510
|
|
|
$collection[] = 2; |
|
511
|
|
|
|
|
512
|
|
|
$this->assertEquals(2, $collection->byPositionGet(2)); |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
public function testByPositionGetOnOutOfRangePosition() |
|
516
|
|
|
{ |
|
517
|
|
|
$collection = new Collection(); |
|
518
|
|
|
$collection[] = 0; |
|
519
|
|
|
$collection[] = 1; |
|
520
|
|
|
$collection[] = 2; |
|
521
|
|
|
|
|
522
|
|
|
$this->assertNull($collection->byPositionGet(3)); |
|
523
|
|
|
} |
|
524
|
|
|
|
|
525
|
|
|
public function testIsEmptyOnEmptyCollection() |
|
526
|
|
|
{ |
|
527
|
|
|
$collection = new Collection(); |
|
528
|
|
|
|
|
529
|
|
|
$this->assertTrue($collection->isEmpty()); |
|
530
|
|
|
} |
|
531
|
|
|
|
|
532
|
|
|
public function testIsEmptyOnNotEmptyCollection() |
|
533
|
|
|
{ |
|
534
|
|
|
$collection = new Collection(); |
|
535
|
|
|
$collection[] = 0; |
|
536
|
|
|
$collection[] = 1; |
|
537
|
|
|
$collection[] = 2; |
|
538
|
|
|
|
|
539
|
|
|
$this->assertFalse($collection->isEmpty()); |
|
540
|
|
|
} |
|
541
|
|
|
|
|
542
|
|
|
public function testDiffRecursiveAssocOnValueItems() |
|
543
|
|
|
{ |
|
544
|
|
|
$anotherCollection = new Collection(); |
|
545
|
|
|
$anotherCollection['key3'] = 0; |
|
546
|
|
|
$anotherCollection['key4'] = 1; |
|
547
|
|
|
$anotherCollection['key5'] = 2; |
|
548
|
|
|
|
|
549
|
|
|
$collection = new Collection(); |
|
550
|
|
|
$collection['key0'] = 0; |
|
551
|
|
|
$collection['key1'] = 1; |
|
552
|
|
|
$collection['key2'] = 2; |
|
553
|
|
|
|
|
554
|
|
|
$this->assertEquals($collection->toArray(), $collection->diffRecursiveAssoc($anotherCollection)->toArray()); |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
public function testDiffRecursiveAssocOnCollections() |
|
558
|
|
|
{ |
|
559
|
|
|
$anotherCollection = new Collection(); |
|
560
|
|
|
$anotherCollection[] = 0; |
|
561
|
|
|
$anotherCollection[] = 1; |
|
562
|
|
|
$anotherCollection[] = 2; |
|
563
|
|
|
|
|
564
|
|
|
$collection = new Collection(); |
|
565
|
|
|
$collection['key0'] = $anotherCollection; |
|
566
|
|
|
$collection['key1'] = $anotherCollection; |
|
567
|
|
|
$collection['key2'] = $anotherCollection; |
|
568
|
|
|
|
|
569
|
|
|
$this->assertEquals([], $collection->diffRecursiveAssoc($collection)->toArray()); |
|
570
|
|
|
} |
|
571
|
|
|
|
|
572
|
|
|
public function testDiffRecursiveAssocOnArrayValues() |
|
573
|
|
|
{ |
|
574
|
|
|
$collection = new Collection(); |
|
575
|
|
|
$collection['key0'] = [0, 1, 2, 3]; |
|
576
|
|
|
$collection['key1'] = [0, 1, 2, 3]; |
|
577
|
|
|
$collection['key2'] = [0, 1, 2, 3]; |
|
578
|
|
|
|
|
579
|
|
|
$anotherCollection = new Collection(); |
|
580
|
|
|
$anotherCollection[] = 0; |
|
581
|
|
|
$anotherCollection[] = 1; |
|
582
|
|
|
$anotherCollection[] = 2; |
|
583
|
|
|
|
|
584
|
|
|
$this->assertEquals($collection->toArray(), $collection->diffRecursiveAssoc($anotherCollection)->toArray()); |
|
585
|
|
|
} |
|
586
|
|
|
} |
|
587
|
|
|
|