1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPKitchen\Platform\Specs\Unit\Struct; |
4
|
|
|
|
5
|
|
|
use PHPKitchen\Platform\Specs\Base; |
6
|
|
|
use PHPKitchen\Platform\Struct\Collection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Specification for {@link Collection} |
10
|
|
|
* |
11
|
|
|
* @author Dmitry Kolodko <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class CollectionSpec extends Base\Spec { |
14
|
|
|
/** |
15
|
|
|
* @test |
16
|
|
|
*/ |
17
|
|
|
public function constructorBehavior() { |
18
|
|
|
$collection = Collection::of([]); |
19
|
|
|
$this->tester->seeBool($collection->isNull()) |
20
|
|
|
->isFalse(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @test |
25
|
|
|
*/ |
26
|
|
|
public function isEmptyBehavior() { |
27
|
|
|
$I = $this->tester; |
|
|
|
|
28
|
|
|
|
29
|
|
|
$I->describe('behavior of empty assert method'); |
|
|
|
|
30
|
|
|
$collection = Collection::of([1, 2, 3]); |
31
|
|
|
|
32
|
|
|
$I->expectThat('collection with data considered as not empty'); |
|
|
|
|
33
|
|
|
$I->seeBool($collection->isEmpty()) |
|
|
|
|
34
|
|
|
->isFalse(); |
35
|
|
|
|
36
|
|
|
$collection = Collection::of([]); |
37
|
|
|
|
38
|
|
|
$I->expectThat('collection without data considered as empty'); |
|
|
|
|
39
|
|
|
$I->seeBool($collection->isEmpty()) |
|
|
|
|
40
|
|
|
->isTrue(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @test |
45
|
|
|
*/ |
46
|
|
|
public function clearBehavior() { |
47
|
|
|
$I = $this->tester; |
|
|
|
|
48
|
|
|
|
49
|
|
|
$I->describe('behavior of collection cleaning method'); |
|
|
|
|
50
|
|
|
$collection = Collection::of([1, 2, 3]); |
51
|
|
|
$collection->clear(); |
52
|
|
|
|
53
|
|
|
$I->expectThat('after cleaning collection become empty'); |
|
|
|
|
54
|
|
|
$I->seeBool($collection->isEmpty()) |
|
|
|
|
55
|
|
|
->isTrue(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @test |
60
|
|
|
*/ |
61
|
|
|
public function hasMethodsGroupBehavior() { |
62
|
|
|
$I = $this->tester; |
|
|
|
|
63
|
|
|
|
64
|
|
|
$I->describe('behavior of "has" methods group'); |
|
|
|
|
65
|
|
|
$collection = Collection::of([1, 2, 3, null]); |
66
|
|
|
|
67
|
|
|
$I->expectThat('`hasKey` returns `true` for existing and `false` for not existing keys'); |
|
|
|
|
68
|
|
|
$I->seeBool($collection->hasKey($firstElementKey = 0)) |
|
|
|
|
69
|
|
|
->isTrue(); |
70
|
|
|
$I->seeBool($collection->hasKey($notExistingKey = 5)) |
|
|
|
|
71
|
|
|
->isFalse(); |
72
|
|
|
|
73
|
|
|
$I->expectThat('`has` returns `true` for existing and `false` for not existing element'); |
|
|
|
|
74
|
|
|
$I->seeBool($collection->has($secondElement = 2)) |
|
|
|
|
75
|
|
|
->isTrue(); |
76
|
|
|
$I->seeBool($collection->has($notExistingElement = 56)) |
|
|
|
|
77
|
|
|
->isFalse(); |
78
|
|
|
|
79
|
|
|
$I->expectThat('`hasSet` returns `true` for set and `false` for not set and null elements at specified key'); |
|
|
|
|
80
|
|
|
$I->seeBool($collection->hasSet($firstElementKey = 0)) |
|
|
|
|
81
|
|
|
->isTrue(); |
82
|
|
|
$I->seeBool($collection->hasSet($nullElementKey = 3)) |
|
|
|
|
83
|
|
|
->isFalse(); |
84
|
|
|
$I->seeBool($collection->hasSet($notExistingElementKey = 5)) |
|
|
|
|
85
|
|
|
->isFalse(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @test |
90
|
|
|
*/ |
91
|
|
|
public function removeMethodsGroupBehavior() { |
92
|
|
|
$I = $this->tester; |
|
|
|
|
93
|
|
|
|
94
|
|
|
$I->describe('behavior of "remove" methods group'); |
|
|
|
|
95
|
|
|
$collection = Collection::of([ |
96
|
|
|
1, |
97
|
|
|
3, |
98
|
|
|
3, |
99
|
|
|
3, |
100
|
|
|
4, |
101
|
|
|
4, |
102
|
|
|
4, |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
$I->expectThat('`removeAt` unset element with specified key'); |
|
|
|
|
106
|
|
|
|
107
|
|
|
$collection->removeAt($firstElementKey = 0); |
108
|
|
|
$I->seeBool($collection->hasKey($firstElementKey = 0)) |
|
|
|
|
109
|
|
|
->isFalse(); |
110
|
|
|
|
111
|
|
|
$I->expectThat('`remove` unset first occurrences of specified element'); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$collection->remove($elementWithDuplicates = 3); |
114
|
|
|
$I->seeBool($collection->hasKey($firstDuplicateKey = 1)) |
|
|
|
|
115
|
|
|
->isFalse(); |
116
|
|
|
$I->seeBool($collection->hasKey($secondDuplicateKey = 2)) |
|
|
|
|
117
|
|
|
->isTrue(); |
118
|
|
|
$I->seeBool($collection->hasKey($thirdDuplicateKey = 3)) |
|
|
|
|
119
|
|
|
->isTrue(); |
120
|
|
|
|
121
|
|
|
$I->expectThat('`removeAll` unset all occurrences of specified element'); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$collection->removeAll($secondDuplicate = 4); |
124
|
|
|
$I->seeBool($collection->has($secondDuplicate = 4)) |
|
|
|
|
125
|
|
|
->isFalse(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @test |
130
|
|
|
*/ |
131
|
|
|
public function onEachBehavior() { |
132
|
|
|
$I = $this->tester; |
|
|
|
|
133
|
|
|
|
134
|
|
|
$I->describe('`onEach` behavior'); |
|
|
|
|
135
|
|
|
|
136
|
|
|
$collection = Collection::of([1, 2, 3]); |
137
|
|
|
|
138
|
|
|
$I->expectThat('callback does not modify elements passed by value'); |
|
|
|
|
139
|
|
|
|
140
|
|
|
$collection->onEach(function ($element) { |
141
|
|
|
$element++; |
142
|
|
|
}); |
143
|
|
|
$I->see($collection) |
|
|
|
|
144
|
|
|
->isEqualTo(Collection::of([1, 2, 3])); |
145
|
|
|
|
146
|
|
|
$I->expectThat('callback can modify elements passed by link'); |
|
|
|
|
147
|
|
|
|
148
|
|
|
$collection->onEach(function (&$element) { |
149
|
|
|
$element++; |
150
|
|
|
}); |
151
|
|
|
$I->see($collection) |
|
|
|
|
152
|
|
|
->isEqualTo(Collection::of([2, 3, 4])); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @test |
157
|
|
|
*/ |
158
|
|
|
public function onEachRecursiveBehavior() { |
159
|
|
|
$I = $this->tester; |
|
|
|
|
160
|
|
|
|
161
|
|
|
$I->describe('`onEachRecursive` behavior'); |
|
|
|
|
162
|
|
|
|
163
|
|
|
$collection = Collection::of([1, 2, 3, [1, 2, 3]]); |
164
|
|
|
|
165
|
|
|
$I->expectThat('callback does not modify elements passed by value'); |
|
|
|
|
166
|
|
|
|
167
|
|
|
$collection->onEachRecursive(function ($element) { |
168
|
|
|
$element++; |
169
|
|
|
}); |
170
|
|
|
$I->see($collection) |
|
|
|
|
171
|
|
|
->isEqualTo(Collection::of([1, 2, 3, [1, 2, 3]])); |
172
|
|
|
|
173
|
|
|
$I->expectThat('callback can modify elements passed by link'); |
|
|
|
|
174
|
|
|
|
175
|
|
|
$collection->onEachRecursive(function (&$element) { |
176
|
|
|
$element++; |
177
|
|
|
}); |
178
|
|
|
$I->see($collection) |
|
|
|
|
179
|
|
|
->isEqualTo(Collection::of([2, 3, 4, [2, 3, 4]])); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @test |
184
|
|
|
*/ |
185
|
|
|
public function filterBehavior() { |
186
|
|
|
$I = $this->tester; |
|
|
|
|
187
|
|
|
|
188
|
|
|
$I->describe('`filter` behavior'); |
|
|
|
|
189
|
|
|
|
190
|
|
|
$collection = Collection::of([1, 2, 3, 3]); |
191
|
|
|
|
192
|
|
|
$I->expectThat('filter callable is able to change the collection '); |
|
|
|
|
193
|
|
|
|
194
|
|
|
$filteredCollection = $collection->filter(function ($element) { |
195
|
|
|
if ($element == 3) { |
|
|
|
|
196
|
|
|
return false; |
197
|
|
|
}; |
198
|
|
|
|
199
|
|
|
return true; |
200
|
|
|
}); |
201
|
|
|
$I->lookAt('collection filtered from `3` elements'); |
|
|
|
|
202
|
|
|
$I->see($filteredCollection) |
|
|
|
|
203
|
|
|
->isEqualTo(Collection::of([1, 2])); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @test |
208
|
|
|
*/ |
209
|
|
|
public function mapBehavior() { |
210
|
|
|
$I = $this->tester; |
|
|
|
|
211
|
|
|
|
212
|
|
|
$I->describe('`map` behavior'); |
|
|
|
|
213
|
|
|
|
214
|
|
|
$collection = Collection::of([1, 2, 3]); |
215
|
|
|
|
216
|
|
|
$I->expectThat('map callable is able to change the collection '); |
|
|
|
|
217
|
|
|
|
218
|
|
|
$mappedCollection = $collection->map(function ($element) { |
219
|
|
|
return $element * $element; |
220
|
|
|
}); |
221
|
|
|
$I->lookAt('collection mapped by `square` function'); |
|
|
|
|
222
|
|
|
$I->see($mappedCollection) |
|
|
|
|
223
|
|
|
->isEqualTo(Collection::of([1, 4, 9])); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @test |
228
|
|
|
*/ |
229
|
|
|
public function reduceBehavior() { |
230
|
|
|
$I = $this->tester; |
|
|
|
|
231
|
|
|
|
232
|
|
|
$I->describe('`reduce` behavior'); |
|
|
|
|
233
|
|
|
|
234
|
|
|
$collection = Collection::of([1, 2, 3]); |
235
|
|
|
|
236
|
|
|
$I->expectThat('reduce callable is able to traverse through collection'); |
|
|
|
|
237
|
|
|
|
238
|
|
|
$reduceValue = $collection->reduce(function ($result, $element) { |
239
|
|
|
$result += $element; |
240
|
|
|
|
241
|
|
|
return $result; |
242
|
|
|
}); |
243
|
|
|
$I->lookAt('reduce callable is able to traverse through collection with initial value set'); |
|
|
|
|
244
|
|
|
$I->seeNumber($reduceValue) |
|
|
|
|
245
|
|
|
->isEqualTo($sumOfCollection = 6); |
246
|
|
|
$I->expectThat('reduce callable is able to change the collection '); |
|
|
|
|
247
|
|
|
|
248
|
|
|
$reduceValue = $collection->reduce(function ($result, $element) { |
249
|
|
|
$result += $element; |
250
|
|
|
|
251
|
|
|
return $result; |
252
|
|
|
}, 4); |
253
|
|
|
$I->lookAt('collection value reduced by `sum` function with initial value'); |
|
|
|
|
254
|
|
|
$I->seeNumber($reduceValue) |
|
|
|
|
255
|
|
|
->isEqualTo($sumOfCollectionWithInitialValue = 10); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @test |
260
|
|
|
*/ |
261
|
|
|
public function keyMethodsGroupBehavior() { |
262
|
|
|
$I = $this->tester; |
|
|
|
|
263
|
|
|
|
264
|
|
|
$I->describe('`key` methods group behavior'); |
|
|
|
|
265
|
|
|
|
266
|
|
|
$collection = Collection::of([1, 2, 3, 3, 3]); |
267
|
|
|
|
268
|
|
|
$I->expectThat('`keyOf` return key of a first occurrence of given element'); |
|
|
|
|
269
|
|
|
|
270
|
|
|
$firstKeyOfDuplicate = $collection->keyOf(3); |
271
|
|
|
$I->lookAt('key of duplicated element'); |
|
|
|
|
272
|
|
|
$I->seeNumber($firstKeyOfDuplicate) |
|
|
|
|
273
|
|
|
->isEqualTo(2); |
274
|
|
|
|
275
|
|
|
$I->expectThat('`lastKeyOf` return key of a last occurrence of given element'); |
|
|
|
|
276
|
|
|
|
277
|
|
|
$lastKeyOfDuplicate = $collection->lastKeyOf(3); |
278
|
|
|
$I->lookAt('last key of duplicated element'); |
|
|
|
|
279
|
|
|
$I->seeNumber($lastKeyOfDuplicate) |
|
|
|
|
280
|
|
|
->isEqualTo(4); |
281
|
|
|
|
282
|
|
|
$I->expectThat('`allKeysOf` return collection of keys of all occurrences of given element'); |
|
|
|
|
283
|
|
|
|
284
|
|
|
$allKeysOfDuplicate = $collection->allKeysOf(3); |
285
|
|
|
$I->lookAt('collection duplicated element keys'); |
|
|
|
|
286
|
|
|
$I->seeArray($allKeysOfDuplicate) |
|
|
|
|
287
|
|
|
->isEqualTo(Collection::of([2, 3, 4])); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @test |
292
|
|
|
*/ |
293
|
|
|
public function countValuesFrequencyBehavior() { |
294
|
|
|
$I = $this->tester; |
|
|
|
|
295
|
|
|
|
296
|
|
|
$I->describe('`countValuesFrequency` methods group behavior'); |
|
|
|
|
297
|
|
|
|
298
|
|
|
$collection = Collection::of([3, 3, 3, 1, 'Sam', 'sam']); |
299
|
|
|
|
300
|
|
|
$I->expectThat('`countValuesFrequency` honor string values case'); |
|
|
|
|
301
|
|
|
|
302
|
|
|
$caseSensitiveFrequency = $collection->countValuesFrequency(); |
|
|
|
|
303
|
|
|
$I->lookAt('case sensitive values frequency'); |
|
|
|
|
304
|
|
|
$I->see($caseSensitiveFrequency) |
|
|
|
|
305
|
|
|
->isEqualTo(Collection::of([ |
306
|
|
|
3 => 3, |
307
|
|
|
1 => 1, |
308
|
|
|
'Sam' => 1, |
309
|
|
|
'sam' => 1, |
310
|
|
|
])); |
311
|
|
|
|
312
|
|
|
$I->expectThat('`countValuesFrequency` honor string values case'); |
|
|
|
|
313
|
|
|
|
314
|
|
|
$caseSensitiveFrequency = $collection->countValuesFrequencyIgnoringCase(); |
315
|
|
|
$I->lookAt('case sensitive values frequency'); |
|
|
|
|
316
|
|
|
$I->see($caseSensitiveFrequency) |
|
|
|
|
317
|
|
|
->isEqualTo(Collection::of([ |
318
|
|
|
3 => 3, |
319
|
|
|
1 => 1, |
320
|
|
|
'sam' => 2, |
321
|
|
|
])); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @test |
326
|
|
|
*/ |
327
|
|
View Code Duplication |
public function calculateProductBehavior() { |
|
|
|
|
328
|
|
|
$I = $this->tester; |
|
|
|
|
329
|
|
|
|
330
|
|
|
$I->describe('`calculateProduct` behavior'); |
|
|
|
|
331
|
|
|
|
332
|
|
|
$collection = Collection::of([2, 4, 3]); |
333
|
|
|
|
334
|
|
|
$I->expectThat('numeric collection product can be calculated'); |
|
|
|
|
335
|
|
|
|
336
|
|
|
$collectionProduct = $collection->calculateProduct(); |
337
|
|
|
|
338
|
|
|
$I->seeNumber($collectionProduct) |
|
|
|
|
339
|
|
|
->isEqualTo(24); |
340
|
|
|
|
341
|
|
|
$collection = Collection::of([2, 'hi', 'there']); |
342
|
|
|
|
343
|
|
|
$I->expectThat('mixed collection product can be calculated'); |
|
|
|
|
344
|
|
|
|
345
|
|
|
$collectionProduct = $collection->calculateProduct(); |
346
|
|
|
|
347
|
|
|
$I->seeNumber($collectionProduct) |
|
|
|
|
348
|
|
|
->isEqualTo(0); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @test |
353
|
|
|
*/ |
354
|
|
View Code Duplication |
public function popBehavior() { |
|
|
|
|
355
|
|
|
$I = $this->tester; |
|
|
|
|
356
|
|
|
|
357
|
|
|
$I->describe('`pop` behavior'); |
|
|
|
|
358
|
|
|
|
359
|
|
|
$collection = Collection::of([2, 4, 3]); |
360
|
|
|
|
361
|
|
|
$I->expectThat('`pop` ejects last element fro the collection'); |
|
|
|
|
362
|
|
|
|
363
|
|
|
$poppedElement = $collection->pop(); |
364
|
|
|
|
365
|
|
|
$I->lookAt('popped element'); |
|
|
|
|
366
|
|
|
$I->seeNumber($poppedElement) |
|
|
|
|
367
|
|
|
->isEqualTo($lastCollectionElement = 3); |
368
|
|
|
$I->lookAt('collection without last(popped) element'); |
|
|
|
|
369
|
|
|
$I->see($collection) |
|
|
|
|
370
|
|
|
->isEqualTo(Collection::of([2, 4])); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @test |
375
|
|
|
*/ |
376
|
|
View Code Duplication |
public function shiftBehavior() { |
|
|
|
|
377
|
|
|
$I = $this->tester; |
|
|
|
|
378
|
|
|
|
379
|
|
|
$I->describe('`shift` behavior'); |
|
|
|
|
380
|
|
|
|
381
|
|
|
$collection = Collection::of([2, 4, 3]); |
382
|
|
|
|
383
|
|
|
$I->expectThat('`shift` ejects first element fro the collection'); |
|
|
|
|
384
|
|
|
|
385
|
|
|
$poppedElement = $collection->shift(); |
386
|
|
|
|
387
|
|
|
$I->lookAt('shifted element'); |
|
|
|
|
388
|
|
|
$I->seeNumber($poppedElement) |
|
|
|
|
389
|
|
|
->isEqualTo($firstCollectionElement = 2); |
390
|
|
|
$I->lookAt('collection without first(shifted) element'); |
|
|
|
|
391
|
|
|
$I->see($collection) |
|
|
|
|
392
|
|
|
->isEqualTo(Collection::of([4, 3])); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* @test |
397
|
|
|
*/ |
398
|
|
|
public function addBehavior() { |
399
|
|
|
$I = $this->tester; |
|
|
|
|
400
|
|
|
|
401
|
|
|
$I->describe('`add` method behavior'); |
|
|
|
|
402
|
|
|
$collection = Collection::of([1, 2]); |
403
|
|
|
|
404
|
|
|
$I->expectThat('`add` method push element to the end of the collection'); |
|
|
|
|
405
|
|
|
|
406
|
|
|
$collection->add(3); |
407
|
|
|
$I->see($collection) |
|
|
|
|
408
|
|
|
->isEqualTo(Collection::of([1, 2, 3])); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @test |
413
|
|
|
*/ |
414
|
|
|
public function pushBehavior() { |
415
|
|
|
$I = $this->tester; |
|
|
|
|
416
|
|
|
|
417
|
|
|
$I->describe('`push` method behavior'); |
|
|
|
|
418
|
|
|
$collection = Collection::of([1, 2]); |
419
|
|
|
|
420
|
|
|
$I->expectThat('`push` with one argument push passed value to the end of the collection'); |
|
|
|
|
421
|
|
|
|
422
|
|
|
$collection->push(3); |
423
|
|
|
$I->see($collection) |
|
|
|
|
424
|
|
|
->isEqualTo(Collection::of([1, 2, 3])); |
425
|
|
|
$I->expectThat('`push` with several arguments push all of the elements to the end of the collection keeping order'); |
|
|
|
|
426
|
|
|
|
427
|
|
|
$collection->push(4, 5); |
428
|
|
|
$I->see($collection) |
|
|
|
|
429
|
|
|
->isEqualTo(Collection::of([1, 2, 3, 4, 5])); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* @test |
434
|
|
|
*/ |
435
|
|
|
public function padBehavior() { |
436
|
|
|
$I = $this->tester; |
|
|
|
|
437
|
|
|
|
438
|
|
|
$I->describe('`pad` method behavior'); |
|
|
|
|
439
|
|
|
|
440
|
|
|
$I->expectThat('`pad` with positive size add given value to the right side of the collection till collection rich required size'); |
|
|
|
|
441
|
|
|
|
442
|
|
|
$collection = Collection::of([1, 2]); |
443
|
|
|
$collection->pad(4, 9); |
444
|
|
|
$I->see($collection) |
|
|
|
|
445
|
|
|
->isEqualTo(Collection::of([1, 2, 9, 9])); |
446
|
|
|
|
447
|
|
|
$I->expectThat('`pad` with negative size add given value to the left side of the collection till collection rich required size'); |
|
|
|
|
448
|
|
|
|
449
|
|
|
$collection = Collection::of([1, 2, 3]); |
450
|
|
|
$collection->pad(-4, 9); |
451
|
|
|
$I->see($collection) |
|
|
|
|
452
|
|
|
->isEqualTo(Collection::of([9, 1, 2, 3])); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* @test |
457
|
|
|
*/ |
458
|
|
|
public function chunkByBehavior() { |
459
|
|
|
$I = $this->tester; |
|
|
|
|
460
|
|
|
|
461
|
|
|
$I->describe('`chunkBy` methods group behavior'); |
|
|
|
|
462
|
|
|
|
463
|
|
|
$collection = Collection::of([1, 2, 3, 4]); |
464
|
|
|
|
465
|
|
|
$I->expectThat('`chunkBy` split collection on chunks with specified size not keeping original keys.'); |
|
|
|
|
466
|
|
|
|
467
|
|
|
$chunks = $collection->chunkBy(2); |
468
|
|
|
$I->see($chunks) |
|
|
|
|
469
|
|
|
->isEqualTo(Collection::of([ |
470
|
|
|
Collection::of([1, 2]), |
471
|
|
|
Collection::of([3, 4]), |
472
|
|
|
])); |
473
|
|
|
|
474
|
|
|
$I->expectThat('`chunkKeepingKeysBy` split collection on chunks with specified size keeping original keys.'); |
|
|
|
|
475
|
|
|
|
476
|
|
|
$chunks = $collection->chunkKeepingKeysBy(2); |
477
|
|
|
$I->see($chunks) |
|
|
|
|
478
|
|
|
->isEqualTo(Collection::of([ |
479
|
|
|
Collection::of([0 => 1, 1 => 2]), |
480
|
|
|
Collection::of([2 => 3, 3 => 4]), |
481
|
|
|
])); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* @test |
486
|
|
|
*/ |
487
|
|
|
public function arrayAccessBehavior() { |
488
|
|
|
$I = $this->tester; |
|
|
|
|
489
|
|
|
|
490
|
|
|
$I->describe('array access behavior'); |
|
|
|
|
491
|
|
|
|
492
|
|
|
$collection = Collection::of([]); |
493
|
|
|
|
494
|
|
|
$I->expectThat('collection allows to set and read values through array access'); |
|
|
|
|
495
|
|
|
|
496
|
|
|
$collection[0] = 1; |
497
|
|
|
$collection['first'] = 1; |
498
|
|
|
|
499
|
|
|
$I->see($collection[0]) |
|
|
|
|
500
|
|
|
->isEqualTo(1); |
501
|
|
|
$I->see($collection['first']) |
|
|
|
|
502
|
|
|
->isEqualTo(1); |
503
|
|
|
|
504
|
|
|
$I->expectThat('not set keys return `null` and considered as not set'); |
|
|
|
|
505
|
|
|
|
506
|
|
|
$I->seeBool(isset($collection['not-existing'])) |
|
|
|
|
507
|
|
|
->isFalse(); |
508
|
|
|
$I->see($collection['not-existing']) |
|
|
|
|
509
|
|
|
->isNull(); |
510
|
|
|
} |
511
|
|
|
} |
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.