1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPKitchen\Platform\Specs\Unit\Struct; |
4
|
|
|
|
5
|
|
|
use PHPKitchen\Platform\Specs\Base\Spec; |
6
|
|
|
use PHPKitchen\Platform\Struct\Collection; |
7
|
|
|
use PHPKitchen\Platform\Struct\Map; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Specification for {@link Map} |
11
|
|
|
* |
12
|
|
|
* @author Dmitry Kolodko <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class MapSpec extends Spec { |
15
|
|
|
protected const INVALID_KEY_ERROR_MESSAGE = 'Map support only `string` keys.'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @test |
19
|
|
|
*/ |
20
|
|
|
public function constructorBehavior() { |
21
|
|
|
|
22
|
|
|
$I = $this->tester; |
|
|
|
|
23
|
|
|
$I->describe('constructor `behavior`'); |
|
|
|
|
24
|
|
|
|
25
|
|
|
try { |
26
|
|
|
Map::of([1, 2, 3]); |
27
|
|
|
$errorMessage = 'Constructor validation failed'; |
28
|
|
|
} catch (\Throwable $error) { |
29
|
|
|
$errorMessage = $error->getMessage(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$I->expectThat('Map reject arrays with non-string keys'); |
|
|
|
|
33
|
|
|
$I->seeString($errorMessage) |
|
|
|
|
34
|
|
|
->isEqualTo(self::INVALID_KEY_ERROR_MESSAGE); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @test |
39
|
|
|
*/ |
40
|
|
|
public function isEmptyBehavior() { |
41
|
|
|
$I = $this->tester; |
|
|
|
|
42
|
|
|
|
43
|
|
|
$I->describe('behavior of empty assert method'); |
|
|
|
|
44
|
|
|
$map = Map::of([ |
45
|
|
|
'first' => 1, |
46
|
|
|
'second' => 2, |
47
|
|
|
'third' => 3, |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
$I->expectThat('collection with data considered as not empty'); |
|
|
|
|
51
|
|
|
$I->seeBool($map->isEmpty()) |
|
|
|
|
52
|
|
|
->isFalse(); |
53
|
|
|
|
54
|
|
|
$map = Map::of([]); |
55
|
|
|
|
56
|
|
|
$I->expectThat('collection without data considered as empty'); |
|
|
|
|
57
|
|
|
$I->seeBool($map->isEmpty()) |
|
|
|
|
58
|
|
|
->isTrue(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @test |
63
|
|
|
*/ |
64
|
|
|
public function clearBehavior() { |
65
|
|
|
$I = $this->tester; |
|
|
|
|
66
|
|
|
|
67
|
|
|
$I->describe('behavior of collection cleaning method'); |
|
|
|
|
68
|
|
|
$map = Map::of([ |
69
|
|
|
'first' => 1, |
70
|
|
|
'second' => 2, |
71
|
|
|
'third' => 3, |
72
|
|
|
]); |
73
|
|
|
$map->clear(); |
74
|
|
|
|
75
|
|
|
$I->expectThat('after cleaning collection become empty'); |
|
|
|
|
76
|
|
|
$I->seeBool($map->isEmpty()) |
|
|
|
|
77
|
|
|
->isTrue(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @test |
82
|
|
|
*/ |
83
|
|
|
public function hasMethodsGroupBehavior() { |
84
|
|
|
$I = $this->tester; |
|
|
|
|
85
|
|
|
|
86
|
|
|
$I->describe('behavior of "has" methods group'); |
|
|
|
|
87
|
|
|
$map = Map::of([ |
88
|
|
|
'first' => 1, |
89
|
|
|
'second' => 2, |
90
|
|
|
'third' => 3, |
91
|
|
|
'null-element' => null, |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
$I->expectThat('`hasKey` returns `true` for existing and `false` for not existing keys'); |
|
|
|
|
95
|
|
|
$I->seeBool($map->hasKey($firstElementKey = 'first')) |
|
|
|
|
96
|
|
|
->isTrue(); |
97
|
|
|
$I->seeBool($map->hasKey($notExistingKey = 'not-exists')) |
|
|
|
|
98
|
|
|
->isFalse(); |
99
|
|
|
|
100
|
|
|
$I->expectThat('`has` returns `true` for existing and `false` for not existing element'); |
|
|
|
|
101
|
|
|
$I->seeBool($map->has($secondElement = 2)) |
|
|
|
|
102
|
|
|
->isTrue(); |
103
|
|
|
$I->seeBool($map->has($notExistingElement = 56)) |
|
|
|
|
104
|
|
|
->isFalse(); |
105
|
|
|
|
106
|
|
|
$I->expectThat('`hasSet` returns `true` for set and `false` for not set and null elements at specified key'); |
|
|
|
|
107
|
|
|
$I->seeBool($map->hasSet($firstElementKey = 'first')) |
|
|
|
|
108
|
|
|
->isTrue(); |
109
|
|
|
$I->seeBool($map->hasSet($nullElementKey = 'null-element')) |
|
|
|
|
110
|
|
|
->isFalse(); |
111
|
|
|
$I->seeBool($map->hasSet($notExistingElementKey = 'not-exists')) |
|
|
|
|
112
|
|
|
->isFalse(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @test |
117
|
|
|
*/ |
118
|
|
|
public function removeMethodsGroupBehavior() { |
119
|
|
|
$I = $this->tester; |
|
|
|
|
120
|
|
|
|
121
|
|
|
$I->describe('behavior of "remove" methods group'); |
|
|
|
|
122
|
|
|
$map = Map::of([ |
123
|
|
|
'first' => 1, |
124
|
|
|
'first-duplicate-1' => 3, |
125
|
|
|
'first-duplicate-2' => 3, |
126
|
|
|
'first-duplicate-3' => 3, |
127
|
|
|
'second-duplicate-1' => 4, |
128
|
|
|
'second-duplicate-2' => 4, |
129
|
|
|
'second-duplicate-3' => 4, |
130
|
|
|
]); |
131
|
|
|
|
132
|
|
|
$I->expectThat('`removeAt` unset element with specified key'); |
|
|
|
|
133
|
|
|
|
134
|
|
|
$map->removeAt($firstElementKey = 'first'); |
135
|
|
|
$I->seeBool($map->hasKey($firstElementKey = 'first')) |
|
|
|
|
136
|
|
|
->isFalse(); |
137
|
|
|
|
138
|
|
|
$I->expectThat('`remove` unset first occurrences of specified element'); |
|
|
|
|
139
|
|
|
|
140
|
|
|
$map->remove($elementWithDuplicates = 3); |
141
|
|
|
$I->seeBool($map->hasKey('first-duplicate-1')) |
|
|
|
|
142
|
|
|
->isFalse(); |
143
|
|
|
$I->seeBool($map->hasKey('first-duplicate-2')) |
|
|
|
|
144
|
|
|
->isTrue(); |
145
|
|
|
$I->seeBool($map->hasKey('first-duplicate-3')) |
|
|
|
|
146
|
|
|
->isTrue(); |
147
|
|
|
|
148
|
|
|
$I->expectThat('`removeAll` unset all occurrences of specified element'); |
|
|
|
|
149
|
|
|
|
150
|
|
|
$map->removeAll($secondDuplicate = 4); |
151
|
|
|
$I->seeBool($map->has($secondDuplicate = 4)) |
|
|
|
|
152
|
|
|
->isFalse(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @test |
157
|
|
|
*/ |
158
|
|
|
public function onEachBehavior() { |
159
|
|
|
$I = $this->tester; |
|
|
|
|
160
|
|
|
|
161
|
|
|
$I->describe('`onEach` behavior'); |
|
|
|
|
162
|
|
|
|
163
|
|
|
$map = Map::of([ |
164
|
|
|
'first' => 1, |
165
|
|
|
'second' => 2, |
166
|
|
|
'third' => 3, |
167
|
|
|
]); |
168
|
|
|
|
169
|
|
|
$I->expectThat('callback does not modify elements passed by value'); |
|
|
|
|
170
|
|
|
|
171
|
|
|
$map->onEach(function ($element) { |
172
|
|
|
$element++; |
173
|
|
|
}); |
174
|
|
|
$I->see($map) |
|
|
|
|
175
|
|
|
->isEqualTo(Map::of([ |
176
|
|
|
'first' => 1, |
177
|
|
|
'second' => 2, |
178
|
|
|
'third' => 3, |
179
|
|
|
])); |
180
|
|
|
|
181
|
|
|
$I->expectThat('callback can modify elements passed by link'); |
|
|
|
|
182
|
|
|
|
183
|
|
|
$map->onEach(function (&$element) { |
184
|
|
|
$element++; |
185
|
|
|
}); |
186
|
|
|
$I->see($map) |
|
|
|
|
187
|
|
|
->isEqualTo(Map::of([ |
188
|
|
|
'first' => 2, |
189
|
|
|
'second' => 3, |
190
|
|
|
'third' => 4, |
191
|
|
|
])); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @test |
196
|
|
|
*/ |
197
|
|
|
public function onEachRecursiveBehavior() { |
198
|
|
|
$I = $this->tester; |
|
|
|
|
199
|
|
|
|
200
|
|
|
$I->describe('`onEachRecursive` behavior'); |
|
|
|
|
201
|
|
|
|
202
|
|
|
$map = Map::of([ |
203
|
|
|
'first' => 1, |
204
|
|
|
'second' => 2, |
205
|
|
|
'third' => 3, |
206
|
|
|
'sub-collection' => [ |
207
|
|
|
'first' => 1, |
208
|
|
|
'second' => 2, |
209
|
|
|
'third' => 3, |
210
|
|
|
], |
211
|
|
|
]); |
212
|
|
|
|
213
|
|
|
$I->expectThat('callback does not modify elements passed by value'); |
|
|
|
|
214
|
|
|
|
215
|
|
|
$map->onEachRecursive(function ($element) { |
216
|
|
|
$element++; |
217
|
|
|
}); |
218
|
|
|
$I->see($map) |
|
|
|
|
219
|
|
|
->isEqualTo(Map::of([ |
220
|
|
|
'first' => 1, |
221
|
|
|
'second' => 2, |
222
|
|
|
'third' => 3, |
223
|
|
|
'sub-collection' => [ |
224
|
|
|
'first' => 1, |
225
|
|
|
'second' => 2, |
226
|
|
|
'third' => 3, |
227
|
|
|
], |
228
|
|
|
])); |
229
|
|
|
|
230
|
|
|
$I->expectThat('callback can modify elements passed by link'); |
|
|
|
|
231
|
|
|
|
232
|
|
|
$map->onEachRecursive(function (&$element) { |
233
|
|
|
$element++; |
234
|
|
|
}); |
235
|
|
|
$I->see($map) |
|
|
|
|
236
|
|
|
->isEqualTo(Map::of([ |
237
|
|
|
'first' => 2, |
238
|
|
|
'second' => 3, |
239
|
|
|
'third' => 4, |
240
|
|
|
'sub-collection' => [ |
241
|
|
|
'first' => 2, |
242
|
|
|
'second' => 3, |
243
|
|
|
'third' => 4, |
244
|
|
|
], |
245
|
|
|
])); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @test |
250
|
|
|
*/ |
251
|
|
|
public function filterBehavior() { |
252
|
|
|
$I = $this->tester; |
|
|
|
|
253
|
|
|
|
254
|
|
|
$I->describe('`filter` behavior'); |
|
|
|
|
255
|
|
|
|
256
|
|
|
$map = Map::of([ |
257
|
|
|
'first' => 1, |
258
|
|
|
'second' => 2, |
259
|
|
|
'third' => 3, |
260
|
|
|
'third-duplicate' => 3, |
261
|
|
|
]); |
262
|
|
|
|
263
|
|
|
$I->expectThat('filter callable is able to change the collection '); |
|
|
|
|
264
|
|
|
|
265
|
|
|
$filteredCollection = $map->filter(function ($element) { |
266
|
|
|
if ($element == 3) { |
|
|
|
|
267
|
|
|
return false; |
268
|
|
|
}; |
269
|
|
|
|
270
|
|
|
return true; |
271
|
|
|
}); |
272
|
|
|
$I->lookAt('collection filtered from `3` elements'); |
|
|
|
|
273
|
|
|
$I->see($filteredCollection) |
|
|
|
|
274
|
|
|
->isEqualTo(Map::of([ |
275
|
|
|
'first' => 1, |
276
|
|
|
'second' => 2, |
277
|
|
|
])); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @test |
282
|
|
|
*/ |
283
|
|
|
public function mapBehavior() { |
284
|
|
|
$I = $this->tester; |
|
|
|
|
285
|
|
|
|
286
|
|
|
$I->describe('`map` behavior'); |
|
|
|
|
287
|
|
|
|
288
|
|
|
$map = Map::of([ |
289
|
|
|
'first' => 1, |
290
|
|
|
'second' => 2, |
291
|
|
|
'third' => 3, |
292
|
|
|
]); |
293
|
|
|
|
294
|
|
|
$I->expectThat('map callable is able to change the collection '); |
|
|
|
|
295
|
|
|
|
296
|
|
|
$mappedCollection = $map->map(function ($element) { |
297
|
|
|
return $element * $element; |
298
|
|
|
}); |
299
|
|
|
$I->lookAt('collection mapped by `square` function'); |
|
|
|
|
300
|
|
|
$I->see($mappedCollection) |
|
|
|
|
301
|
|
|
->isEqualTo(Map::of([ |
302
|
|
|
'first' => 1, |
303
|
|
|
'second' => 4, |
304
|
|
|
'third' => 9, |
305
|
|
|
])); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @test |
310
|
|
|
*/ |
311
|
|
|
public function reduceBehavior() { |
312
|
|
|
$I = $this->tester; |
|
|
|
|
313
|
|
|
|
314
|
|
|
$I->describe('`reduce` behavior'); |
|
|
|
|
315
|
|
|
|
316
|
|
|
$map = Map::of([ |
317
|
|
|
'first' => 1, |
318
|
|
|
'second' => 2, |
319
|
|
|
'third' => 3, |
320
|
|
|
]); |
321
|
|
|
|
322
|
|
|
$I->expectThat('reduce callable is able to traverse through collection'); |
|
|
|
|
323
|
|
|
|
324
|
|
|
$reduceValue = $map->reduce(function ($result, $element) { |
325
|
|
|
$result += $element; |
326
|
|
|
|
327
|
|
|
return $result; |
328
|
|
|
}); |
329
|
|
|
$I->lookAt('reduce callable is able to traverse through collection with initial value set'); |
|
|
|
|
330
|
|
|
$I->seeNumber($reduceValue) |
|
|
|
|
331
|
|
|
->isEqualTo($sumOfCollection = 6); |
332
|
|
|
$I->expectThat('reduce callable is able to change the collection '); |
|
|
|
|
333
|
|
|
|
334
|
|
|
$reduceValue = $map->reduce(function ($result, $element) { |
335
|
|
|
$result += $element; |
336
|
|
|
|
337
|
|
|
return $result; |
338
|
|
|
}, 4); |
339
|
|
|
$I->lookAt('collection value reduced by `sum` function with initial value'); |
|
|
|
|
340
|
|
|
$I->seeNumber($reduceValue) |
|
|
|
|
341
|
|
|
->isEqualTo($sumOfCollectionWithInitialValue = 10); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @test |
346
|
|
|
*/ |
347
|
|
|
public function keyMethodsGroupBehavior() { |
348
|
|
|
$I = $this->tester; |
|
|
|
|
349
|
|
|
|
350
|
|
|
$I->describe('`key` methods group behavior'); |
|
|
|
|
351
|
|
|
|
352
|
|
|
$map = Map::of([ |
353
|
|
|
'first' => 1, |
354
|
|
|
'second' => 2, |
355
|
|
|
'third' => 3, |
356
|
|
|
'first-third-duplicate' => 3, |
357
|
|
|
'second-third-duplicate' => 3, |
358
|
|
|
]); |
359
|
|
|
|
360
|
|
|
$I->expectThat('`keyOf` return key of a first occurrence of given element'); |
|
|
|
|
361
|
|
|
|
362
|
|
|
$firstKeyOfDuplicate = $map->keyOf(3); |
363
|
|
|
$I->lookAt('key of duplicated element'); |
|
|
|
|
364
|
|
|
$I->seeNumber($firstKeyOfDuplicate) |
|
|
|
|
365
|
|
|
->isEqualTo('third'); |
366
|
|
|
|
367
|
|
|
$I->expectThat('`lastKeyOf` return key of a last occurrence of given element'); |
|
|
|
|
368
|
|
|
|
369
|
|
|
$lastKeyOfDuplicate = $map->lastKeyOf(3); |
370
|
|
|
$I->lookAt('last key of duplicated element'); |
|
|
|
|
371
|
|
|
$I->seeNumber($lastKeyOfDuplicate) |
|
|
|
|
372
|
|
|
->isEqualTo('second-third-duplicate'); |
373
|
|
|
|
374
|
|
|
$I->expectThat('`allKeysOf` return collection of keys of all occurrences of given element'); |
|
|
|
|
375
|
|
|
|
376
|
|
|
$allKeysOfDuplicate = $map->allKeysOf(3); |
377
|
|
|
$I->lookAt('collection duplicated element keys'); |
|
|
|
|
378
|
|
|
$I->seeArray($allKeysOfDuplicate) |
|
|
|
|
379
|
|
|
->isEqualTo(Collection::of([ |
380
|
|
|
'third', |
381
|
|
|
'first-third-duplicate', |
382
|
|
|
'second-third-duplicate', |
383
|
|
|
])); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* @test |
388
|
|
|
*/ |
389
|
|
|
public function countValuesFrequencyBehavior() { |
390
|
|
|
$I = $this->tester; |
|
|
|
|
391
|
|
|
|
392
|
|
|
$I->describe('`countValuesFrequency` methods group behavior'); |
|
|
|
|
393
|
|
|
|
394
|
|
|
$map = Map::of([ |
395
|
|
|
'first' => 1, |
396
|
|
|
'second' => 3, |
397
|
|
|
'first-second-duplicate' => 3, |
398
|
|
|
'second-second-duplicate' => 3, |
399
|
|
|
'Name' => 'Sam', |
400
|
|
|
'name' => 'sam', |
401
|
|
|
]); |
402
|
|
|
|
403
|
|
|
$I->expectThat('`countValuesFrequency` honor string values case'); |
|
|
|
|
404
|
|
|
|
405
|
|
|
$caseSensitiveFrequency = $map->countValuesFrequency(); |
406
|
|
|
$I->lookAt('case sensitive values frequency'); |
|
|
|
|
407
|
|
|
$I->see($caseSensitiveFrequency) |
|
|
|
|
408
|
|
|
->isEqualTo(Collection::of([ |
409
|
|
|
3 => 3, |
410
|
|
|
1 => 1, |
411
|
|
|
'Sam' => 1, |
412
|
|
|
'sam' => 1, |
413
|
|
|
])); |
414
|
|
|
|
415
|
|
|
$I->expectThat('`countValuesFrequency` honor string values case'); |
|
|
|
|
416
|
|
|
|
417
|
|
|
$caseSensitiveFrequency = $map->countValuesFrequencyIgnoringCase(); |
418
|
|
|
$I->lookAt('case sensitive values frequency'); |
|
|
|
|
419
|
|
|
$I->see($caseSensitiveFrequency) |
|
|
|
|
420
|
|
|
->isEqualTo(Collection::of([ |
421
|
|
|
3 => 3, |
422
|
|
|
1 => 1, |
423
|
|
|
'sam' => 2, |
424
|
|
|
])); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* @test |
429
|
|
|
*/ |
430
|
|
|
public function calculateProductBehavior() { |
431
|
|
|
$I = $this->tester; |
|
|
|
|
432
|
|
|
|
433
|
|
|
$I->describe('`calculateProduct` behavior'); |
|
|
|
|
434
|
|
|
|
435
|
|
|
$map = Map::of([ |
436
|
|
|
'first' => 2, |
437
|
|
|
'second' => 4, |
438
|
|
|
'third' => 3, |
439
|
|
|
]); |
440
|
|
|
|
441
|
|
|
$I->expectThat('numeric collection product can be calculated'); |
|
|
|
|
442
|
|
|
|
443
|
|
|
$mapProduct = $map->calculateProduct(); |
444
|
|
|
|
445
|
|
|
$I->seeNumber($mapProduct) |
|
|
|
|
446
|
|
|
->isEqualTo(24); |
447
|
|
|
|
448
|
|
|
$map = Map::of([ |
449
|
|
|
'first' => 2, |
450
|
|
|
'second' => 'hi', |
451
|
|
|
'third' => 'there', |
452
|
|
|
]); |
453
|
|
|
|
454
|
|
|
$I->expectThat('mixed collection product can be calculated'); |
|
|
|
|
455
|
|
|
|
456
|
|
|
$mapProduct = $map->calculateProduct(); |
457
|
|
|
|
458
|
|
|
$I->seeNumber($mapProduct) |
|
|
|
|
459
|
|
|
->isEqualTo(0); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* @test |
464
|
|
|
*/ |
465
|
|
|
public function popBehavior() { |
466
|
|
|
$I = $this->tester; |
|
|
|
|
467
|
|
|
|
468
|
|
|
$I->describe('`pop` behavior'); |
|
|
|
|
469
|
|
|
|
470
|
|
|
$map = Map::of([ |
471
|
|
|
'first' => 2, |
472
|
|
|
'second' => 4, |
473
|
|
|
'third' => 3, |
474
|
|
|
]); |
475
|
|
|
|
476
|
|
|
$I->expectThat('`pop` ejects last element fro the collection'); |
|
|
|
|
477
|
|
|
|
478
|
|
|
$poppedElement = $map->pop(); |
479
|
|
|
|
480
|
|
|
$I->lookAt('popped element'); |
|
|
|
|
481
|
|
|
$I->seeNumber($poppedElement) |
|
|
|
|
482
|
|
|
->isEqualTo($lastCollectionElement = 3); |
483
|
|
|
$I->lookAt('collection without last(popped) element'); |
|
|
|
|
484
|
|
|
$I->see($map) |
|
|
|
|
485
|
|
|
->isEqualTo(Map::of([ |
486
|
|
|
'first' => 2, |
487
|
|
|
'second' => 4, |
488
|
|
|
])); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* @test |
493
|
|
|
*/ |
494
|
|
|
public function shiftBehavior() { |
495
|
|
|
$I = $this->tester; |
|
|
|
|
496
|
|
|
|
497
|
|
|
$I->describe('`shift` behavior'); |
|
|
|
|
498
|
|
|
|
499
|
|
|
$map = Map::of([ |
500
|
|
|
'first' => 2, |
501
|
|
|
'second' => 4, |
502
|
|
|
'third' => 3, |
503
|
|
|
]); |
504
|
|
|
|
505
|
|
|
$I->expectThat('`shift` ejects first element fro the collection'); |
|
|
|
|
506
|
|
|
|
507
|
|
|
$poppedElement = $map->shift(); |
508
|
|
|
|
509
|
|
|
$I->lookAt('shifted element'); |
|
|
|
|
510
|
|
|
$I->seeNumber($poppedElement) |
|
|
|
|
511
|
|
|
->isEqualTo($firstCollectionElement = 2); |
512
|
|
|
$I->lookAt('collection without first(shifted) element'); |
|
|
|
|
513
|
|
|
$I->see($map) |
|
|
|
|
514
|
|
|
->isEqualTo(Map::of([ |
515
|
|
|
'second' => 4, |
516
|
|
|
'third' => 3, |
517
|
|
|
])); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* @test |
522
|
|
|
*/ |
523
|
|
|
public function keysToLoweCaseBehavior() { |
524
|
|
|
$I = $this->tester; |
|
|
|
|
525
|
|
|
|
526
|
|
|
$I->describe('`keysToLowerCase` behavior'); |
|
|
|
|
527
|
|
|
|
528
|
|
|
$map = Map::of([ |
529
|
|
|
'FIRST' => 2, |
530
|
|
|
'SecoND' => 4, |
531
|
|
|
'thirD' => 3, |
532
|
|
|
]); |
533
|
|
|
|
534
|
|
|
$I->expectThat('`keysToLowerCase` convert all of the collection keys with any notation to lower case'); |
|
|
|
|
535
|
|
|
|
536
|
|
|
$map->keysToLowerCase(); |
537
|
|
|
|
538
|
|
|
$I->see($map) |
|
|
|
|
539
|
|
|
->isEqualTo(Map::of([ |
540
|
|
|
'first' => 2, |
541
|
|
|
'second' => 4, |
542
|
|
|
'third' => 3, |
543
|
|
|
])); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* @test |
548
|
|
|
*/ |
549
|
|
|
public function keysToUpperCaseBehavior() { |
550
|
|
|
$I = $this->tester; |
|
|
|
|
551
|
|
|
|
552
|
|
|
$I->describe('`keysToLowerCase` behavior'); |
|
|
|
|
553
|
|
|
|
554
|
|
|
$map = Map::of([ |
555
|
|
|
'FIRST' => 2, |
556
|
|
|
'SecoND' => 4, |
557
|
|
|
'thirD' => 3, |
558
|
|
|
'forth' => 3, |
559
|
|
|
]); |
560
|
|
|
|
561
|
|
|
$I->expectThat('`keysToLowerCase` convert all of the collection keys with any notation to lower case'); |
|
|
|
|
562
|
|
|
|
563
|
|
|
$map->keysToUpperCase(); |
564
|
|
|
|
565
|
|
|
$I->see($map) |
|
|
|
|
566
|
|
|
->isEqualTo(Map::of([ |
567
|
|
|
'FIRST' => 2, |
568
|
|
|
'SECOND' => 4, |
569
|
|
|
'THIRD' => 3, |
570
|
|
|
'FORTH' => 3, |
571
|
|
|
])); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* @test |
576
|
|
|
*/ |
577
|
|
|
public function chunkByBehavior() { |
578
|
|
|
$I = $this->tester; |
|
|
|
|
579
|
|
|
|
580
|
|
|
$I->describe('`chunkBy` behavior'); |
|
|
|
|
581
|
|
|
|
582
|
|
|
$map = Map::of([ |
583
|
|
|
'FIRST' => 2, |
584
|
|
|
'SECOND' => 4, |
585
|
|
|
'THIRD' => 3, |
586
|
|
|
'FORTH' => 3, |
587
|
|
|
]); |
588
|
|
|
|
589
|
|
|
$I->expectThat('`chunkBy` split collection on chunks with specified size.'); |
|
|
|
|
590
|
|
|
|
591
|
|
|
$chunks = $map->chunkBy(2); |
592
|
|
|
$I->see($chunks) |
|
|
|
|
593
|
|
|
->isEqualTo(Collection::of([ |
594
|
|
|
Map::of([ |
595
|
|
|
'FIRST' => 2, |
596
|
|
|
'SECOND' => 4, |
597
|
|
|
]), |
598
|
|
|
Map::of([ |
599
|
|
|
'THIRD' => 3, |
600
|
|
|
'FORTH' => 3, |
601
|
|
|
]), |
602
|
|
|
])); |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
/** |
606
|
|
|
* @test |
607
|
|
|
*/ |
608
|
|
|
public function arrayAccessBehavior() { |
609
|
|
|
$I = $this->tester; |
|
|
|
|
610
|
|
|
|
611
|
|
|
$I->describe('array access behavior'); |
|
|
|
|
612
|
|
|
|
613
|
|
|
$map = Map::of([]); |
614
|
|
|
|
615
|
|
|
$I->expectThat('map allows to set and read values through array access'); |
|
|
|
|
616
|
|
|
|
617
|
|
|
$map['first'] = 1; |
618
|
|
|
|
619
|
|
|
$I->see($map['first']) |
|
|
|
|
620
|
|
|
->isEqualTo(1); |
621
|
|
|
|
622
|
|
|
$I->expectThat('not set keys return `null` and considered as not set'); |
|
|
|
|
623
|
|
|
|
624
|
|
|
$I->seeBool(isset($map['not-existing'])) |
|
|
|
|
625
|
|
|
->isFalse(); |
626
|
|
|
$I->see($map['not-existing']) |
|
|
|
|
627
|
|
|
->isNull(); |
628
|
|
|
|
629
|
|
|
$I->expectThat('Map reject non-string keys'); |
|
|
|
|
630
|
|
|
|
631
|
|
|
try { |
632
|
|
|
$map[] = 4; |
633
|
|
|
$errorMessage = 'Key validation failed'; |
634
|
|
|
} catch (\Throwable $error) { |
635
|
|
|
$errorMessage = $error->getMessage(); |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
$I->seeString($errorMessage) |
|
|
|
|
639
|
|
|
->isEqualTo(self::INVALID_KEY_ERROR_MESSAGE); |
640
|
|
|
} |
641
|
|
|
} |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.