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->assertEquals(1, count($list)); |
135
|
|
|
$this->assertInstanceOf(TestBasicObject::class, $list[0]); |
136
|
|
|
$this->assertTrue(isset($list[0])); |
137
|
|
|
$this->assertFalse(isset($list[1])); |
138
|
|
|
unset($list[0]); |
139
|
|
|
$this->assertEquals(0, $list->count()); |
140
|
|
|
$this->assertEquals(0, count($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
|
|
|
|