1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license MIT, http://opensource.org/licenses/MIT |
5
|
|
|
* @author Taylor Otwell, Aimeos.org developers |
6
|
|
|
* @package MW |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
namespace Aimeos\MW; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class MapTest extends \PHPUnit\Framework\TestCase |
14
|
|
|
{ |
15
|
|
|
public function testFirstReturnsFirstItemInMap() |
16
|
|
|
{ |
17
|
|
|
$c = new Map(['foo', 'bar']); |
18
|
|
|
$this->assertEquals('foo', $c->first()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testFirstWithCallback() |
22
|
|
|
{ |
23
|
|
|
$data = new Map(['foo', 'bar', 'baz']); |
24
|
|
|
$result = $data->first( function( $value ) { |
25
|
|
|
return $value === 'bar'; |
26
|
|
|
} ); |
27
|
|
|
$this->assertEquals('bar', $result); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testFirstWithCallbackAndDefault() |
31
|
|
|
{ |
32
|
|
|
$data = new Map(['foo', 'bar']); |
33
|
|
|
$result = $data->first( function( $value ) { |
34
|
|
|
return $value === 'baz'; |
35
|
|
|
}, 'default' ); |
36
|
|
|
$this->assertEquals('default', $result); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testFirstWithDefaultAndWithoutCallback() |
40
|
|
|
{ |
41
|
|
|
$data = new Map; |
42
|
|
|
$result = $data->first(null, 'default'); |
43
|
|
|
$this->assertEquals('default', $result); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testLastReturnsLastItemInMap() |
47
|
|
|
{ |
48
|
|
|
$c = new Map(['foo', 'bar']); |
49
|
|
|
$this->assertEquals('bar', $c->last()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testLastWithCallback() |
53
|
|
|
{ |
54
|
|
|
$data = new Map([100, 200, 300]); |
55
|
|
|
$result = $data->last( function( $value ) { |
56
|
|
|
return $value < 250; |
57
|
|
|
} ); |
58
|
|
|
$this->assertEquals(200, $result); |
59
|
|
|
$result = $data->last( function( $value, $key ) { |
60
|
|
|
return $key < 2; |
61
|
|
|
} ); |
62
|
|
|
$this->assertEquals(200, $result); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testLastWithCallbackAndDefault() |
66
|
|
|
{ |
67
|
|
|
$data = new Map(['foo', 'bar']); |
68
|
|
|
$result = $data->last( function( $value ) { |
69
|
|
|
return $value === 'baz'; |
70
|
|
|
}, 'default' ); |
71
|
|
|
$this->assertEquals('default', $result); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testLastWithDefaultAndWithoutCallback() |
75
|
|
|
{ |
76
|
|
|
$data = new Map; |
77
|
|
|
$result = $data->last(null, 'default'); |
78
|
|
|
$this->assertEquals('default', $result); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testPopReturnsAndRemovesLastItemInMap() |
82
|
|
|
{ |
83
|
|
|
$c = new Map(['foo', 'bar']); |
84
|
|
|
|
85
|
|
|
$this->assertEquals('bar', $c->pop()); |
86
|
|
|
$this->assertEquals('foo', $c->first()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testShiftReturnsAndRemovesFirstItemInMap() |
90
|
|
|
{ |
91
|
|
|
$c = new Map(['foo', 'bar']); |
92
|
|
|
|
93
|
|
|
$this->assertEquals('foo', $c->shift()); |
94
|
|
|
$this->assertEquals('bar', $c->first()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testEmptyMapIsEmpty() |
98
|
|
|
{ |
99
|
|
|
$c = new Map; |
100
|
|
|
|
101
|
|
|
$this->assertTrue($c->isEmpty()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testMapIsConstructed() |
105
|
|
|
{ |
106
|
|
|
$map = new Map; |
107
|
|
|
$this->assertEmpty($map->toArray()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testMapShuffle() |
111
|
|
|
{ |
112
|
|
|
$map = new Map(range(0, 100, 10)); |
113
|
|
|
|
114
|
|
|
$firstRandom = $map->copy()->shuffle(); |
115
|
|
|
$secondRandom = $map->copy()->shuffle(); |
116
|
|
|
|
117
|
|
|
$this->assertNotEquals($firstRandom, $secondRandom); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testGetArray() |
121
|
|
|
{ |
122
|
|
|
$map = new Map; |
123
|
|
|
|
124
|
|
|
$class = new \ReflectionClass($map); |
125
|
|
|
$method = $class->getMethod('getArray'); |
126
|
|
|
$method->setAccessible(true); |
127
|
|
|
|
128
|
|
|
$items = new \ArrayIterator( ['foo' => 'bar']); |
129
|
|
|
$array = $method->invokeArgs($map, [$items]); |
130
|
|
|
$this->assertSame(['foo' => 'bar'], $array); |
131
|
|
|
|
132
|
|
|
$items = new Map(['foo' => 'bar']); |
133
|
|
|
$array = $method->invokeArgs($map, [$items]); |
134
|
|
|
$this->assertSame(['foo' => 'bar'], $array); |
135
|
|
|
|
136
|
|
|
$items = ['foo' => 'bar']; |
137
|
|
|
$array = $method->invokeArgs($map, [$items]); |
138
|
|
|
$this->assertSame(['foo' => 'bar'], $array); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testOffsetAccess() |
142
|
|
|
{ |
143
|
|
|
$c = new Map(['name' => 'test']); |
144
|
|
|
$this->assertEquals('test', $c['name']); |
145
|
|
|
$c['name'] = 'me'; |
146
|
|
|
$this->assertEquals('me', $c['name']); |
147
|
|
|
$this->assertTrue(isset($c['name'])); |
148
|
|
|
unset($c['name']); |
149
|
|
|
$this->assertFalse(isset($c['name'])); |
150
|
|
|
$c[] = 'jason'; |
151
|
|
|
$this->assertEquals('jason', $c[0]); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testArrayAccessOffsetExists() |
155
|
|
|
{ |
156
|
|
|
$c = new Map(['foo', 'bar']); |
157
|
|
|
$this->assertTrue($c->offsetExists(0)); |
158
|
|
|
$this->assertTrue($c->offsetExists(1)); |
159
|
|
|
$this->assertFalse($c->offsetExists(1000)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testArrayAccessOffsetGet() |
163
|
|
|
{ |
164
|
|
|
$c = new Map(['foo', 'bar']); |
165
|
|
|
$this->assertEquals('foo', $c->offsetGet(0)); |
166
|
|
|
$this->assertEquals('bar', $c->offsetGet(1)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testArrayAccessOffsetSet() |
170
|
|
|
{ |
171
|
|
|
$c = new Map(['foo', 'foo']); |
172
|
|
|
|
173
|
|
|
$c->offsetSet(1, 'bar'); |
174
|
|
|
$this->assertEquals('bar', $c[1]); |
175
|
|
|
|
176
|
|
|
$c->offsetSet(null, 'qux'); |
177
|
|
|
$this->assertEquals('qux', $c[2]); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testArrayAccessOffsetUnset() |
181
|
|
|
{ |
182
|
|
|
$c = new Map(['foo', 'bar']); |
183
|
|
|
|
184
|
|
|
$c->offsetUnset(1); |
185
|
|
|
$this->assertFalse(isset($c[1])); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testRemoveSingleKey() |
189
|
|
|
{ |
190
|
|
|
$c = new Map(['foo', 'bar']); |
191
|
|
|
$c->remove(0); |
192
|
|
|
$this->assertFalse(isset($c['foo'])); |
193
|
|
|
|
194
|
|
|
$c = new Map(['foo' => 'bar', 'baz' => 'qux']); |
195
|
|
|
$c->remove('foo'); |
196
|
|
|
$this->assertFalse(isset($c['foo'])); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testRemoveArrayOfKeys() |
200
|
|
|
{ |
201
|
|
|
$c = new Map(['foo', 'bar', 'baz']); |
202
|
|
|
$c->remove([0, 2]); |
203
|
|
|
$this->assertFalse(isset($c[0])); |
204
|
|
|
$this->assertFalse(isset($c[2])); |
205
|
|
|
$this->assertTrue(isset($c[1])); |
206
|
|
|
|
207
|
|
|
$c = new Map(['name' => 'test', 'foo' => 'bar', 'baz' => 'qux']); |
208
|
|
|
$c->remove(['foo', 'baz']); |
209
|
|
|
$this->assertFalse(isset($c['foo'])); |
210
|
|
|
$this->assertFalse(isset($c['baz'])); |
211
|
|
|
$this->assertTrue(isset($c['name'])); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function testCountable() |
215
|
|
|
{ |
216
|
|
|
$c = new Map(['foo', 'bar']); |
217
|
|
|
$this->assertCount(2, $c); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function testIterable() |
221
|
|
|
{ |
222
|
|
|
$c = new Map(['foo']); |
223
|
|
|
$this->assertInstanceOf(\ArrayIterator::class, $c->getIterator()); |
224
|
|
|
$this->assertEquals(['foo'], $c->getIterator()->getArrayCopy()); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function testFilter() |
228
|
|
|
{ |
229
|
|
|
$c = new Map([['id' => 1, 'name' => 'Hello'], ['id' => 2, 'name' => 'World']]); |
230
|
|
|
$this->assertEquals([1 => ['id' => 2, 'name' => 'World']], $c->filter( function( $item ) { |
231
|
|
|
return $item['id'] == 2; |
232
|
|
|
} )->toArray()); |
233
|
|
|
|
234
|
|
|
$c = new Map(['', 'Hello', '', 'World']); |
235
|
|
|
$this->assertEquals(['Hello', 'World'], $c->filter()->values()->toArray()); |
236
|
|
|
|
237
|
|
|
$c = new Map(['id' => 1, 'first' => 'Hello', 'second' => 'World']); |
238
|
|
|
$this->assertEquals(['first' => 'Hello', 'second' => 'World'], $c->filter( function( $item, $key ) { |
239
|
|
|
return $key != 'id'; |
240
|
|
|
} )->toArray()); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function testValues() |
244
|
|
|
{ |
245
|
|
|
$c = new Map([['id' => 1, 'name' => 'Hello'], ['id' => 2, 'name' => 'World']]); |
246
|
|
|
$this->assertEquals([['id' => 2, 'name' => 'World']], $c->filter( function( $item ) { |
247
|
|
|
return $item['id'] == 2; |
248
|
|
|
} )->values()->toArray()); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function testMergeArray() |
252
|
|
|
{ |
253
|
|
|
$c = new Map(['name' => 'Hello']); |
254
|
|
|
$this->assertEquals(['name' => 'Hello', 'id' => 1], $c->merge(['id' => 1])->toArray()); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function testMergeMap() |
258
|
|
|
{ |
259
|
|
|
$c = new Map(['name' => 'Hello']); |
260
|
|
|
$this->assertEquals(['name' => 'World', 'id' => 1], $c->merge(new Map(['name' => 'World', 'id' => 1]))->toArray()); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function testReplaceArray() |
264
|
|
|
{ |
265
|
|
|
$c = new Map(['a', 'b', 'c']); |
266
|
|
|
$this->assertEquals(['a', 'd', 'e'], $c->replace([1 => 'd', 2 => 'e'])->toArray()); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function testReplaceMap() |
270
|
|
|
{ |
271
|
|
|
$c = new Map(['a', 'b', 'c']); |
272
|
|
|
$this->assertEquals( |
273
|
|
|
['a', 'd', 'e'], |
274
|
|
|
$c->replace(new Map([1 => 'd', 2 => 'e']))->toArray() |
275
|
|
|
); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function testReplaceRecursiveArray() |
279
|
|
|
{ |
280
|
|
|
$c = new Map(['a', 'b', ['c', 'd']]); |
281
|
|
|
$this->assertEquals(['z', 'b', ['c', 'e']], $c->replace(['z', 2 => [1 => 'e']])->toArray()); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
public function testReplaceRecursiveMap() |
285
|
|
|
{ |
286
|
|
|
$c = new Map(['a', 'b', ['c', 'd']]); |
287
|
|
|
$this->assertEquals( |
288
|
|
|
['z', 'b', ['c', 'e']], |
289
|
|
|
$c->replace(new Map(['z', 2 => [1 => 'e']]))->toArray() |
290
|
|
|
); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
public function testUnionArray() |
294
|
|
|
{ |
295
|
|
|
$c = new Map(['name' => 'Hello']); |
296
|
|
|
$this->assertEquals(['name' => 'Hello', 'id' => 1], $c->union(['id' => 1])->toArray()); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function testUnionMap() |
300
|
|
|
{ |
301
|
|
|
$c = new Map(['name' => 'Hello']); |
302
|
|
|
$this->assertEquals(['name' => 'Hello', 'id' => 1], $c->union(new Map(['name' => 'World', 'id' => 1]))->toArray()); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
public function testDiffMap() |
306
|
|
|
{ |
307
|
|
|
$c = new Map(['id' => 1, 'first_word' => 'Hello']); |
308
|
|
|
$this->assertEquals(['id' => 1], $c->diff(new Map(['first_word' => 'Hello', 'last_word' => 'World']))->toArray()); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function testDiffUsingWithMap() |
312
|
|
|
{ |
313
|
|
|
$c = new Map(['en_GB', 'fr', 'HR']); |
314
|
|
|
// demonstrate that diffKeys wont support case insensitivity |
315
|
|
|
$this->assertEquals(['en_GB', 'fr', 'HR'], $c->diff(new Map(['en_gb', 'hr']))->values()->toArray()); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
public function testDiffKeys() |
319
|
|
|
{ |
320
|
|
|
$c1 = new Map(['id' => 1, 'first_word' => 'Hello']); |
321
|
|
|
$c2 = new Map(['id' => 123, 'foo_bar' => 'Hello']); |
322
|
|
|
$this->assertEquals(['first_word' => 'Hello'], $c1->diffKeys($c2)->toArray()); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function testDiffKeysFunction() |
326
|
|
|
{ |
327
|
|
|
$c1 = new Map(['id' => 1, 'first_word' => 'Hello']); |
328
|
|
|
$c2 = new Map(['ID' => 123, 'foo_bar' => 'Hello']); |
329
|
|
|
// demonstrate that diffKeys wont support case insensitivity |
330
|
|
|
$this->assertEquals(['id'=>1, 'first_word'=> 'Hello'], $c1->diffKeys($c2)->toArray()); |
331
|
|
|
// allow for case insensitive difference |
332
|
|
|
$this->assertEquals(['first_word' => 'Hello'], $c1->diffKeys($c2, 'strcasecmp')->toArray()); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function testDiffAssoc() |
336
|
|
|
{ |
337
|
|
|
$c1 = new Map(['id' => 1, 'first_word' => 'Hello', 'not_affected' => 'value']); |
338
|
|
|
$c2 = new Map(['id' => 123, 'foo_bar' => 'Hello', 'not_affected' => 'value']); |
339
|
|
|
$this->assertEquals(['id' => 1, 'first_word' => 'Hello'], $c1->diffAssoc($c2)->toArray()); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
public function testDiffAssocFunction() |
343
|
|
|
{ |
344
|
|
|
$c1 = new Map(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red']); |
345
|
|
|
$c2 = new Map(['A' => 'green', 'yellow', 'red']); |
346
|
|
|
// demonstrate that the case of the keys will affect the output when diffAssoc is used |
347
|
|
|
$this->assertEquals(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red'], $c1->diffAssoc($c2)->toArray()); |
348
|
|
|
// allow for case insensitive difference |
349
|
|
|
$this->assertEquals(['b' => 'brown', 'c' => 'blue', 'red'], $c1->diffAssoc($c2, 'strcasecmp')->toArray()); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
public function testEach() |
353
|
|
|
{ |
354
|
|
|
$c = new Map($original = [1, 2, 'foo' => 'bar', 'bam' => 'baz']); |
355
|
|
|
|
356
|
|
|
$result = []; |
357
|
|
|
$c->each( function( $item, $key ) use ( &$result ) { |
358
|
|
|
$result[$key] = $item; |
359
|
|
|
} ); |
360
|
|
|
$this->assertEquals($original, $result); |
361
|
|
|
|
362
|
|
|
$result = []; |
363
|
|
|
$c->each( function( $item, $key ) use ( &$result ) { |
364
|
|
|
$result[$key] = $item; |
365
|
|
|
if( is_string( $key ) ) { |
366
|
|
|
return false; |
367
|
|
|
} |
368
|
|
|
} ); |
369
|
|
|
$this->assertEquals([1, 2, 'foo' => 'bar'], $result); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
public function testIntersectMap() |
373
|
|
|
{ |
374
|
|
|
$c = new Map(['id' => 1, 'first_word' => 'Hello']); |
375
|
|
|
$this->assertEquals(['first_word' => 'Hello'], $c->intersect(new Map(['first_world' => 'Hello', 'last_word' => 'World']))->toArray()); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
public function testIntersectKeys() |
379
|
|
|
{ |
380
|
|
|
$c = new Map(['name' => 'Mateus', 'age' => 18]); |
381
|
|
|
$this->assertEquals(['name' => 'Mateus'], $c->intersectKeys(new Map(['name' => 'Mateus', 'surname' => 'Guimaraes']))->toArray()); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
public function testUnique() |
385
|
|
|
{ |
386
|
|
|
$c = new Map(['Hello', 'World', 'World']); |
387
|
|
|
$this->assertEquals(['Hello', 'World'], $c->unique()->toArray()); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
public function testSort() |
391
|
|
|
{ |
392
|
|
|
$data = (new Map([5, 3, 1, 2, 4]))->sort(); |
393
|
|
|
$this->assertEquals([1, 2, 3, 4, 5], $data->values()->toArray()); |
394
|
|
|
|
395
|
|
|
$data = (new Map([-1, -3, -2, -4, -5, 0, 5, 3, 1, 2, 4]))->sort(); |
396
|
|
|
$this->assertEquals([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], $data->values()->toArray()); |
397
|
|
|
|
398
|
|
|
$data = (new Map(['foo', 'bar-10', 'bar-1']))->sort(); |
399
|
|
|
$this->assertEquals(['bar-1', 'bar-10', 'foo'], $data->values()->toArray()); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
public function testKsort() |
403
|
|
|
{ |
404
|
|
|
$data = new Map(['b' => 'me', 'a' => 'test']); |
405
|
|
|
|
406
|
|
|
$this->assertSame(['a' => 'test', 'b' => 'me'], $data->ksort()->toArray()); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
public function testReverse() |
410
|
|
|
{ |
411
|
|
|
$data = new Map(['hello', 'world']); |
412
|
|
|
$reversed = $data->reverse(); |
413
|
|
|
|
414
|
|
|
$this->assertSame([1 => 'world', 0 => 'hello'], $reversed->toArray()); |
415
|
|
|
|
416
|
|
|
$data = new Map(['name' => 'test', 'last' => 'user']); |
417
|
|
|
$reversed = $data->reverse(); |
418
|
|
|
|
419
|
|
|
$this->assertSame(['last' => 'user', 'name' => 'test'], $reversed->toArray()); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
public function testHas() |
423
|
|
|
{ |
424
|
|
|
$data = new Map(['id' => 1, 'first' => 'Hello', 'second' => 'World']); |
425
|
|
|
$this->assertTrue($data->has('first')); |
426
|
|
|
$this->assertFalse($data->has('third')); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
public function testMethod() |
430
|
|
|
{ |
431
|
|
|
Map::method('foo', function() { |
432
|
|
|
return $this->filter( function( $item ) { |
|
|
|
|
433
|
|
|
return strpos($item, 'a') === 0; |
434
|
|
|
}) |
435
|
|
|
->unique() |
436
|
|
|
->values(); |
437
|
|
|
} ); |
438
|
|
|
|
439
|
|
|
$c = new Map(['a', 'a', 'aa', 'aaa', 'bar']); |
440
|
|
|
|
441
|
|
|
$this->assertSame(['a', 'aa', 'aaa'], $c->foo()->toArray()); |
|
|
|
|
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
public function testMakeMethodFromMap() |
445
|
|
|
{ |
446
|
|
|
$firstMap = Map::from(['foo' => 'bar']); |
447
|
|
|
$secondMap = Map::from($firstMap); |
448
|
|
|
$this->assertEquals(['foo' => 'bar'], $secondMap->toArray()); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
public function testMakeMethodFromArray() |
452
|
|
|
{ |
453
|
|
|
$map = Map::from(['foo' => 'bar']); |
454
|
|
|
$this->assertEquals(['foo' => 'bar'], $map->toArray()); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
public function testConstructMethodFromMap() |
458
|
|
|
{ |
459
|
|
|
$firstMap = new Map(['foo' => 'bar']); |
460
|
|
|
$secondMap = new Map($firstMap); |
461
|
|
|
$this->assertEquals(['foo' => 'bar'], $secondMap->toArray()); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
public function testConstructMethodFromArray() |
465
|
|
|
{ |
466
|
|
|
$map = new Map(['foo' => 'bar']); |
467
|
|
|
$this->assertEquals(['foo' => 'bar'], $map->toArray()); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
public function testSplice() |
471
|
|
|
{ |
472
|
|
|
$data = new Map(['foo', 'baz']); |
473
|
|
|
$data->splice(1); |
474
|
|
|
$this->assertEquals(['foo'], $data->toArray()); |
475
|
|
|
|
476
|
|
|
$data = new Map(['foo', 'baz']); |
477
|
|
|
$data->splice(1, 0, 'bar'); |
478
|
|
|
$this->assertEquals(['foo', 'bar', 'baz'], $data->toArray()); |
479
|
|
|
|
480
|
|
|
$data = new Map(['foo', 'baz']); |
481
|
|
|
$data->splice(1, 1); |
482
|
|
|
$this->assertEquals(['foo'], $data->toArray()); |
483
|
|
|
|
484
|
|
|
$data = new Map(['foo', 'baz']); |
485
|
|
|
$cut = $data->splice(1, 1, 'bar'); |
486
|
|
|
$this->assertEquals(['foo', 'bar'], $data->toArray()); |
487
|
|
|
$this->assertEquals(['baz'], $cut->toArray()); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
public function testMap() |
491
|
|
|
{ |
492
|
|
|
$data = new Map(['first' => 'test', 'last' => 'user']); |
493
|
|
|
$data = $data->map( function( $item, $key ) { |
494
|
|
|
return $key.'-'.strrev($item); |
495
|
|
|
} ); |
496
|
|
|
$this->assertEquals(['first' => 'first-tset', 'last' => 'last-resu'], $data->toArray()); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
public function testPullRetrievesItemFromMap() |
500
|
|
|
{ |
501
|
|
|
$c = new Map(['foo', 'bar']); |
502
|
|
|
|
503
|
|
|
$this->assertEquals('foo', $c->pull(0)); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
public function testPullRemovesItemFromMap() |
507
|
|
|
{ |
508
|
|
|
$c = new Map(['foo', 'bar']); |
509
|
|
|
$c->pull(0); |
510
|
|
|
$this->assertEquals([1 => 'bar'], $c->toArray()); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
public function testPullReturnsDefault() |
514
|
|
|
{ |
515
|
|
|
$c = new Map([]); |
516
|
|
|
$value = $c->pull(0, 'foo'); |
517
|
|
|
$this->assertEquals('foo', $value); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
public function testSearch() |
521
|
|
|
{ |
522
|
|
|
$c = new Map([false, 0, 1, [], '']); |
523
|
|
|
$this->assertNull($c->search('false')); |
524
|
|
|
$this->assertNull($c->search('1')); |
525
|
|
|
$this->assertEquals(0, $c->search(false)); |
526
|
|
|
$this->assertEquals(1, $c->search(0)); |
527
|
|
|
$this->assertEquals(2, $c->search(1)); |
528
|
|
|
$this->assertEquals(3, $c->search([])); |
529
|
|
|
$this->assertEquals(4, $c->search('')); |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
public function testSearchReturnsNullWhenItemIsNotFound() |
533
|
|
|
{ |
534
|
|
|
$c = new Map([1, 2, 3, 4, 5, 'foo' => 'bar']); |
535
|
|
|
|
536
|
|
|
$this->assertNull($c->search(6)); |
537
|
|
|
$this->assertNull($c->search('foo')); |
538
|
|
|
$this->assertNull($c->search( function( $value ) { |
539
|
|
|
return $value < 1 && is_numeric($value); |
540
|
|
|
} )); |
541
|
|
|
$this->assertNull($c->search( function( $value ) { |
542
|
|
|
return $value == 'nope'; |
543
|
|
|
} )); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
public function testKeys() |
547
|
|
|
{ |
548
|
|
|
$c = new Map(['name' => 'test', 'last' => 'user']); |
549
|
|
|
$this->assertEquals(['name', 'last'], $c->keys()->toArray()); |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
public function testUnshift() |
553
|
|
|
{ |
554
|
|
|
$c = new Map(['one', 'two', 'three', 'four']); |
555
|
|
|
$this->assertEquals(['zero', 'one', 'two', 'three', 'four'], $c->unshift('zero')->toArray()); |
556
|
|
|
|
557
|
|
|
$c = new Map(['one' => 1, 'two' => 2]); |
558
|
|
|
$this->assertEquals(['zero' => 0, 'one' => 1, 'two' => 2], $c->unshift(0, 'zero')->toArray()); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
public function testConcatWithArray() |
562
|
|
|
{ |
563
|
|
|
$expected = [ |
564
|
|
|
0 => 4, |
565
|
|
|
1 => 5, |
566
|
|
|
2 => 6, |
567
|
|
|
3 => 'a', |
568
|
|
|
4 => 'b', |
569
|
|
|
5 => 'c', |
570
|
|
|
6 => 'Jonny', |
571
|
|
|
7 => 'from', |
572
|
|
|
8 => 'Laroe', |
573
|
|
|
9 => 'Jonny', |
574
|
|
|
10 => 'from', |
575
|
|
|
11 => 'Laroe', |
576
|
|
|
]; |
577
|
|
|
|
578
|
|
|
$map = new Map([4, 5, 6]); |
579
|
|
|
$map = $map->concat(['a', 'b', 'c']); |
580
|
|
|
$map = $map->concat(['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe']); |
581
|
|
|
$actual = $map->concat(['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe'])->toArray(); |
582
|
|
|
|
583
|
|
|
$this->assertSame($expected, $actual); |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
public function testConcatWithMap() |
587
|
|
|
{ |
588
|
|
|
$expected = [ |
589
|
|
|
0 => 4, |
590
|
|
|
1 => 5, |
591
|
|
|
2 => 6, |
592
|
|
|
3 => 'a', |
593
|
|
|
4 => 'b', |
594
|
|
|
5 => 'c', |
595
|
|
|
6 => 'Jonny', |
596
|
|
|
7 => 'from', |
597
|
|
|
8 => 'Laroe', |
598
|
|
|
9 => 'Jonny', |
599
|
|
|
10 => 'from', |
600
|
|
|
11 => 'Laroe', |
601
|
|
|
]; |
602
|
|
|
|
603
|
|
|
$firstMap = new Map([4, 5, 6]); |
604
|
|
|
$secondMap = new Map(['a', 'b', 'c']); |
605
|
|
|
$thirdMap = new Map(['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe']); |
606
|
|
|
$firstMap = $firstMap->concat($secondMap); |
607
|
|
|
$firstMap = $firstMap->concat($thirdMap); |
608
|
|
|
$actual = $firstMap->concat($thirdMap)->toArray(); |
609
|
|
|
|
610
|
|
|
$this->assertSame($expected, $actual); |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
public function testReduce() |
614
|
|
|
{ |
615
|
|
|
$data = new Map([1, 2, 3]); |
616
|
|
|
$this->assertEquals(6, $data->reduce( function( $carry, $element ) { |
617
|
|
|
return $carry += $element; |
618
|
|
|
} )); |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
public function testPipe() |
622
|
|
|
{ |
623
|
|
|
$map = new Map([1, 2, 3]); |
624
|
|
|
|
625
|
|
|
$this->assertEquals( 3, $map->pipe( function( $map ) { |
626
|
|
|
return $map->last(); |
627
|
|
|
} ) ); |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
public function testSliceOffset() |
631
|
|
|
{ |
632
|
|
|
$map = new Map([1, 2, 3, 4, 5, 6, 7, 8]); |
633
|
|
|
$this->assertEquals([4, 5, 6, 7, 8], $map->slice(3)->values()->toArray()); |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
public function testSliceNegativeOffset() |
637
|
|
|
{ |
638
|
|
|
$map = new Map([1, 2, 3, 4, 5, 6, 7, 8]); |
639
|
|
|
$this->assertEquals([6, 7, 8], $map->slice(-3)->values()->toArray()); |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
public function testSliceOffsetAndLength() |
643
|
|
|
{ |
644
|
|
|
$map = new Map([1, 2, 3, 4, 5, 6, 7, 8]); |
645
|
|
|
$this->assertEquals([4, 5, 6], $map->slice(3, 3)->values()->toArray()); |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
public function testSliceOffsetAndNegativeLength() |
649
|
|
|
{ |
650
|
|
|
$map = new Map([1, 2, 3, 4, 5, 6, 7, 8]); |
651
|
|
|
$this->assertEquals([4, 5, 6, 7], $map->slice(3, -1)->values()->toArray()); |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
public function testSliceNegativeOffsetAndLength() |
655
|
|
|
{ |
656
|
|
|
$map = new Map([1, 2, 3, 4, 5, 6, 7, 8]); |
657
|
|
|
$this->assertEquals([4, 5, 6], $map->slice(-5, 3)->values()->toArray()); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
public function testSliceNegativeOffsetAndNegativeLength() |
661
|
|
|
{ |
662
|
|
|
$map = new Map([1, 2, 3, 4, 5, 6, 7, 8]); |
663
|
|
|
$this->assertEquals([3, 4, 5, 6], $map->slice(-6, -2)->values()->toArray()); |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
public function testMapFromTraversable() |
667
|
|
|
{ |
668
|
|
|
$map = new Map(new \ArrayObject([1, 2, 3])); |
669
|
|
|
$this->assertEquals([1, 2, 3], $map->toArray()); |
670
|
|
|
} |
671
|
|
|
|
672
|
|
|
public function testMapFromTraversableWithKeys() |
673
|
|
|
{ |
674
|
|
|
$map = new Map(new \ArrayObject(['foo' => 1, 'bar' => 2, 'baz' => 3])); |
675
|
|
|
$this->assertEquals(['foo' => 1, 'bar' => 2, 'baz' => 3], $map->toArray()); |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
public function testHasReturnsValidResults() |
679
|
|
|
{ |
680
|
|
|
$map = new Map(['foo' => 'one', 'bar' => 'two', 1 => 'three']); |
681
|
|
|
$this->assertTrue($map->has('foo')); |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
public function testSetAddsItemToMap() |
685
|
|
|
{ |
686
|
|
|
$map = new Map; |
687
|
|
|
$this->assertSame([], $map->toArray()); |
688
|
|
|
$map->set('foo', 1); |
689
|
|
|
$this->assertSame(['foo' => 1], $map->toArray()); |
690
|
|
|
$map->set('bar', ['nested' => 'two']); |
691
|
|
|
$this->assertSame(['foo' => 1, 'bar' => ['nested' => 'two']], $map->toArray()); |
692
|
|
|
$map->set('foo', 3); |
693
|
|
|
$this->assertSame(['foo' => 3, 'bar' => ['nested' => 'two']], $map->toArray()); |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
public function testGetWithNullReturnsNull() |
697
|
|
|
{ |
698
|
|
|
$map = new Map([1, 2, 3]); |
699
|
|
|
$this->assertNull($map->get(null)); |
700
|
|
|
} |
701
|
|
|
} |
702
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.