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