GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Failed Conditions
Push — master ( 0cbf54...b6a90e )
by Dušan
02:44
created

CollectionSpec::it_can_serialize_and_unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace spec\DusanKasan\Knapsack;
4
5
use ArrayIterator;
6
use DOMXPath;
7
use DusanKasan\Knapsack\Collection;
8
use DusanKasan\Knapsack\Exceptions\InvalidArgument;
9
use DusanKasan\Knapsack\Exceptions\ItemNotFound;
10
use DusanKasan\Knapsack\Exceptions\NoMoreItems;
11
use DusanKasan\Knapsack\Exceptions\NonEqualCollectionLength;
12
use DusanKasan\Knapsack\Tests\Helpers\PlusOneAdder;
13
use Iterator;
14
use IteratorAggregate;
15
use PhpSpec\ObjectBehavior;
16
use function DusanKasan\Knapsack\reverse;
17
18
/**
19
 * @mixin Collection
20
 */
21
class CollectionSpec extends ObjectBehavior
22
{
23
    function let()
24
    {
25
        $this->beConstructedWith([1, 3, 3, 2,]);
26
    }
27
28
    function it_is_initializable()
29
    {
30
        $this->shouldHaveType(Collection::class);
31
        $this->shouldHaveType(Iterator::class);
32
    }
33
34
    function it_can_be_instantiated_from_iterator()
35
    {
36
        $iterator = new ArrayIterator([1, 2]);
37
        $this->beConstructedWith($iterator);
38
        $this->toArray()->shouldReturn([1, 2]);
39
    }
40
41
    function it_can_be_instantiated_from_iterator_aggregate(IteratorAggregate $iteratorAggregate)
42
    {
43
        $iterator = new ArrayIterator([1, 2]);
44
        $iteratorAggregate->getIterator()->willReturn($iterator);
45
        $this->beConstructedWith($iteratorAggregate);
46
        $this->toArray()->shouldReturn([1, 2]);
47
    }
48
49
    function it_can_be_instantiated_from_array()
50
    {
51
        $this->beConstructedWith([1, 2, 3]);
52
        $this->toArray()->shouldReturn([1, 2, 3]);
53
    }
54
55
    function it_will_throw_when_passed_something_other_than_array_or_traversable()
56
    {
57
        $this->shouldThrow(InvalidArgument::class)->during('__construct', [1]);
58
    }
59
60
    function it_can_be_created_statically()
61
    {
62
        $this->beConstructedThrough('from', [[1, 2]]);
63
        $this->toArray()->shouldReturn([1, 2]);
64
    }
65
66
    function it_can_be_created_to_iterate_over_function_infinitely()
67
    {
68
        $this->beConstructedThrough('iterate', [1, function($i) {return $i+1;}]);
69
        $this->take(2)->toArray()->shouldReturn([1, 2]);
70
    }
71
72
    function it_can_be_created_to_iterate_over_function_non_infinitely()
73
    {
74
        $this->beConstructedThrough(
75
            'iterate',
76
            [
77
                1,
78
                function($i) {
79
                    if ($i > 3) {
80
                        throw new NoMoreItems;
81
                    }
82
83
                    return $i+1;
84
                }
85
            ]
86
        );
87
        $this->toArray()->shouldReturn([1, 2, 3, 4]);
88
    }
89
90
    function it_can_be_created_to_repeat_a_value_infinite_times()
91
    {
92
        $this->beConstructedThrough('repeat', [1]);
93
        $this->take(2)->toArray()->shouldReturn([1, 1]);
94
    }
95
96
    function it_can_filter()
97
    {
98
        $this
99
            ->filter(function ($item) {
100
                return $item > 2;
101
            })
102
            ->toArray()
103
            ->shouldReturn([1 => 3, 2 => 3]);
104
105
        $this
106
            ->filter(function ($item, $key) {
107
                return $key > 2 && $item < 3;
108
            })
109
            ->toArray()
110
            ->shouldReturn([3 => 2]);
111
    }
112
113
    function it_can_distinct()
114
    {
115
        $this
116
            ->distinct()
117
            ->toArray()
118
            ->shouldReturn([1, 3, 3 => 2]);
119
    }
120
121
    function it_can_concat()
122
    {
123
        $c1 = $this->concat([4, 5]);
124
        $c1->toArray()->shouldReturn([4, 5, 3, 2]);
125
        $c1->size()->shouldReturn(6);
126
    }
127
128
    function it_can_map()
129
    {
130
        $this
131
            ->map(function ($item) {
132
                return $item + 1;
133
            })
134
            ->toArray()
135
            ->shouldReturn([2, 4, 4, 3]);
136
    }
137
138 View Code Duplication
    function it_can_reduce()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $this
141
            ->reduce(
142
                function ($temp, $item) {
143
                    return $temp + $item;
144
                },
145
                0
146
            )
147
            ->shouldReturn(9);
148
149
        $this
150
            ->reduce(
151
                function ($temp, $item, $key) {
152
                    return $temp + $key + $item;
153
                },
154
                0
155
            )
156
            ->shouldReturn(15);
157
158
        $this
159
            ->reduce(
160
                function (Collection $temp, $item) {
161
                    return $temp->append($item);
162
                },
163
                new Collection([])
164
            )
165
            ->toArray()
166
            ->shouldReturn([1, 3, 3, 2]);
167
    }
168
169
    function it_can_flatten()
170
    {
171
        $this->beConstructedWith([1, [2, [3]]]);
172
        $this->flatten()->values()->toArray()->shouldReturn([1, 2, 3]);
173
        $this->flatten(1)->values()->toArray()->shouldReturn([1, 2, [3]]);
174
    }
175
176
    function it_can_sort()
177
    {
178
        $this->beConstructedWith([3, 1, 2]);
179
180
        $this
181
            ->sort(function ($a, $b) {
182
                return $a > $b;
183
            })
184
            ->toArray()
185
            ->shouldReturn([1 => 1, 2 => 2, 0 => 3]);
186
187
        $this
188
            ->sort(function ($v1, $v2, $k1, $k2) {
189
                return $k1 < $k2 || $v1 == $v2;
190
            })
191
            ->toArray()
192
            ->shouldReturn([2 => 2, 1 => 1, 0 => 3]);
193
    }
194
195
    function it_can_slice()
196
    {
197
        $this->beConstructedWith([1, 2, 3, 4, 5]);
198
199
        $this
200
            ->slice(2, 4)
201
            ->toArray()
202
            ->shouldReturn([2 => 3, 3 => 4]);
203
204
        $this
205
            ->slice(4)
206
            ->toArray()
207
            ->shouldReturn([4 => 5]);
208
    }
209
210
    function it_can_group_by()
211
    {
212
        $this->beConstructedWith([1, 2, 3, 4, 5]);
213
214
        $collection = $this->groupBy(function ($i) {
215
            return $i % 2;
216
        });
217
218
        $collection->get(0)->toArray()->shouldReturn([2, 4]);
219
        $collection->get(1)->toArray()->shouldReturn([1, 3, 5]);
220
221
        $collection = $this->groupBy(function ($k, $i) {
222
            return ($k + $i) % 3;
223
        });
224
        $collection->get(0)->toArray()->shouldReturn([2, 5]);
225
        $collection->get(1)->toArray()->shouldReturn([1, 4]);
226
        $collection->get(2)->toArray()->shouldReturn([3]);
227
    }
228
229
    function it_can_group_by_key()
230
    {
231
        $this->beConstructedWith([
232
            'some' => 'thing',
233
            ['letter' => 'A', 'type' => 'caps'],
234
            ['letter' => 'a', 'type' => 'small'],
235
            ['letter' => 'B', 'type' => 'caps'],
236
            ['letter' => 'Z']
237
        ]);
238
239
        $collection = $this->groupByKey('type');
240
        $collection->get('small')->toArray()->shouldReturn([
241
            ['letter' => 'a', 'type' => 'small']
242
        ]);
243
        $collection->get('caps')->toArray()->shouldReturn([
244
            ['letter' => 'A', 'type' => 'caps'],
245
            ['letter' => 'B', 'type' => 'caps']
246
        ]);
247
248
        $collection = $this->groupByKey('types');
249
        $collection->shouldThrow(new ItemNotFound)->during('get', ['caps']);
250
    }
251
252
    function it_can_execute_callback_for_each_item(DOMXPath $a)
253
    {
254
        $a->query('asd')->shouldBeCalled();
255
        $this->beConstructedWith([$a]);
256
257
        $this
258
            ->each(function (DOMXPath $i) {
259
                $i->query('asd');
260
            })
261
            ->toArray()
262
            ->shouldReturn([$a]);
263
    }
264
265
    function it_can_get_size()
266
    {
267
        $this->size()->shouldReturn(4);
268
    }
269
270
    function it_can_get_item_by_key()
271
    {
272
        $this->beConstructedWith([1, [2], 3]);
273
        $this->get(0)->shouldReturn(1);
274
        $this->get(1, true)->first()->shouldReturn(2);
275
        $this->get(1)->shouldReturn([2]);
276
        $this->shouldThrow(new ItemNotFound)->during('get', [5]);
277
    }
278
279
    function it_can_get_item_by_key_or_return_default()
280
    {
281
        $this->beConstructedWith([1, [2], 3]);
282
        $this->getOrDefault(0)->shouldReturn(1);
283
        $this->getOrDefault(1, null, true)->first()->shouldReturn(2);
284
        $this->getOrDefault(1, null)->shouldReturn([2]);
285
        $this->getOrDefault(5)->shouldReturn(null);
286
        $this->getOrDefault(5, 'not found')->shouldReturn('not found');
287
    }
288
289 View Code Duplication
    function it_can_get_nth_item()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
290
    {
291
        $this->beConstructedWith([1, [2], 3]);
292
        $this->getNth(0)->shouldReturn(1);
293
        $this->getNth(1, true)->first()->shouldReturn(2);
294
        $this->getNth(1)->shouldReturn([2]);
295
    }
296
297
    function it_can_find()
298
    {
299
        $this->beConstructedWith([1, 3, 3, 2, [5]]);
300
301
        $this
302
            ->find(function ($v) {
303
                return $v < 3;
304
            })
305
            ->shouldReturn(1);
306
307
        $this
308
            ->find(function ($v, $k) {
309
                return $v < 3 && $k > 1;
310
            })
311
            ->shouldReturn(2);
312
313
        $this
314
            ->find(function ($v) {
315
                return $v < 0;
316
            })
317
            ->shouldReturn(null);
318
319
        $this
320
            ->find(
321
                function ($v) {
322
                    return $v < 0;
323
                },
324
                'not found'
325
            )
326
            ->shouldReturn('not found');
327
328
        $this->find('\DusanKasan\Knapsack\isCollection', null, true)->first()->shouldReturn(5);
329
        $this->find('\DusanKasan\Knapsack\isCollection')->shouldReturn([5]);
330
    }
331
332
    function it_can_count_by()
333
    {
334
        $this
335
            ->countBy(function ($v) {
336
                return $v % 2 == 0 ? 'even' : 'odd';
337
            })
338
            ->toArray()
339
            ->shouldReturn(['odd' => 3, 'even' => 1]);
340
341
        $this
342
            ->countBy(function ($k, $v) {
343
                return ($k + $v) % 2 == 0 ? 'even' : 'odd';
344
            })
345
            ->toArray()
346
            ->shouldReturn(['odd' => 3, 'even' => 1]);
347
    }
348
349
    function it_can_index_by()
350
    {
351
        $this
352
            ->indexBy(function ($v) {
353
                return $v;
354
            })
355
            ->toArray()
356
            ->shouldReturn([1 => 1, 3 => 3, 2 => 2]);
357
358
        $this
359
            ->indexBy(function ($v, $k) {
360
                return $k . $v;
361
            })
362
            ->toArray()
363
            ->shouldReturn(['01' => 1, '13' => 3, '23' => 3, '32' => 2]);
364
    }
365
366
    function it_can_check_if_every_item_passes_predicament_test()
367
    {
368
        $this
369
            ->every(function ($v) {
370
                return $v > 0;
371
            })
372
            ->shouldReturn(true);
373
374
        $this
375
            ->every(function ($v) {
376
                return $v > 1;
377
            })
378
            ->shouldReturn(false);
379
380
        $this
381
            ->every(function ($v, $k) {
382
                return $v > 0 && $k >= 0;
383
            })
384
            ->shouldReturn(true);
385
386
        $this
387
            ->every(function ($v, $k) {
388
                return $v > 0 && $k > 0;
389
            })
390
            ->shouldReturn(false);
391
    }
392
393
    function it_can_check_if_some_items_pass_predicament_test()
394
    {
395
        $this
396
            ->some(function ($v) {
397
                return $v < -1;
398
            })
399
            ->shouldReturn(false);
400
401
        $this
402
            ->some(function ($v, $k) {
403
                return $v > 0 && $k < -1;
404
            })
405
            ->shouldReturn(false);
406
407
        $this
408
            ->some(function ($v) {
409
                return $v < 2;
410
            })
411
            ->shouldReturn(true);
412
413
        $this
414
            ->some(function ($v, $k) {
415
                return $v > 0 && $k > 0;
416
            })
417
            ->shouldReturn(true);
418
    }
419
420
    function it_can_check_if_it_contains_a_value()
421
    {
422
        $this
423
            ->contains(3)
424
            ->shouldReturn(true);
425
426
        $this
427
            ->contains(true)
428
            ->shouldReturn(false);
429
    }
430
431
    function it_can_reverse()
432
    {
433
        $it = $this->reverse();
434
435
        $it->rewind();
436
        $it->valid()->shouldReturn(true);
437
        $it->key()->shouldReturn(3);
438
        $it->current()->shouldReturn(2);
439
        $it->next();
440
        $it->valid()->shouldReturn(true);
441
        $it->key()->shouldReturn(2);
442
        $it->current()->shouldReturn(3);
443
        $it->next();
444
        $it->valid()->shouldReturn(true);
445
        $it->key()->shouldReturn(1);
446
        $it->current()->shouldReturn(3);
447
        $it->next();
448
        $it->valid()->shouldReturn(true);
449
        $it->key()->shouldReturn(0);
450
        $it->current()->shouldReturn(1);
451
        $it->next();
452
        $it->valid()->shouldReturn(false);
453
    }
454
455 View Code Duplication
    function it_can_reduce_from_right()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
456
    {
457
        $this
458
            ->reduceRight(
459
                function ($temp, $e) {
460
                    return $temp . $e;
461
                },
462
                0
463
            )
464
            ->shouldReturn('02331');
465
466
        $this
467
            ->reduceRight(
468
                function ($temp, $key, $item) {
469
                    return $temp + $key + $item;
470
                },
471
            0
472
            )
473
            ->shouldReturn(15);
474
475
        $this
476
            ->reduceRight(
477
                function (Collection $temp, $item) {
478
                    return $temp->append($item);
479
                },
480
                new Collection([])
481
            )
482
            ->toArray()
483
            ->shouldReturn([2, 3, 3, 1]);
484
    }
485
486
    function it_can_return_only_first_x_elements()
487
    {
488
        $this->take(2)
489
            ->toArray()
490
            ->shouldReturn([1, 3]);
491
    }
492
493
    function it_can_skip_first_x_elements()
494
    {
495
        $this->drop(2)
496
            ->toArray()
497
            ->shouldReturn([2 => 3, 3 => 2]);
498
    }
499
500
    function it_can_return_values()
501
    {
502
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
503
        $this->values()->toArray()->shouldReturn([1, 2]);
504
    }
505
506
507
    function it_can_reject_elements_from_collection()
508
    {
509
        $this
510
            ->reject(function ($v) {
511
                return $v == 3;
512
            })
513
            ->toArray()
514
            ->shouldReturn([1, 3 => 2]);
515
516
        $this
517
            ->reject(function ($v, $k) {
518
                return $k == 2 && $v == 3;
519
            })
520
            ->toArray()
521
            ->shouldReturn([1, 3, 3 => 2]);
522
    }
523
524
    function it_can_get_keys()
525
    {
526
        $this
527
            ->keys()
528
            ->toArray()
529
            ->shouldReturn([0, 1, 2, 3]);
530
    }
531
532 View Code Duplication
    function it_can_interpose()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
533
    {
534
        $this
535
            ->interpose('a')
536
            ->values()
537
            ->toArray()
538
            ->shouldReturn([1, 'a', 3, 'a', 3, 'a', 2]);
539
    }
540
541
    function it_can_drop_elements_from_end_of_the_collection()
542
    {
543
        $this
544
            ->dropLast()
545
            ->toArray()
546
            ->shouldReturn([1, 3, 3]);
547
548
        $this
549
            ->dropLast(2)
550
            ->toArray()
551
            ->shouldReturn([1, 3]);
552
    }
553
554
    function it_can_interleave_elements()
555
    {
556
        $this
557
            ->interleave(['a', 'b', 'c', 'd', 'e'])
558
            ->values()
559
            ->toArray()
560
            ->shouldReturn([1, 'a', 3, 'b', 3, 'c', 2, 'd', 'e']);
561
    }
562
563 View Code Duplication
    function it_can_repeat_items_of_collection_infinitely()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
564
    {
565
        $this
566
            ->cycle()
567
            ->take(8)
568
            ->values()
569
            ->toArray()
570
            ->shouldReturn([1, 3, 3, 2, 1, 3, 3, 2]);
571
    }
572
573
    function it_can_prepend_item()
574
    {
575
        $this
576
            ->prepend(1)
577
            ->values()
578
            ->toArray()
579
            ->shouldReturn([1, 1, 3, 3, 2]);
580
    }
581
582
    function it_can_prepend_item_with_key()
583
    {
584
        $this
585
            ->prepend(1, 'a')
586
            ->toArray()
587
            ->shouldReturn(['a' => 1, 0 => 1, 1 => 3, 2 => 3, 3 => 2]);
588
    }
589
590
    function it_can_append_item()
591
    {
592
        $this
593
            ->append(1)
594
            ->values()
595
            ->toArray()
596
            ->shouldReturn([1, 3, 3, 2, 1]);
597
    }
598
599
    function it_can_append_item_with_key()
600
    {
601
        $this
602
            ->append(1, 'a')
603
            ->toArray()
604
            ->shouldReturn([1, 3, 3, 2, 'a' => 1]);
605
    }
606
607
    function it_can_drop_items_while_predicament_is_true()
608
    {
609
        $this
610
            ->dropWhile(function ($v) {
611
                return $v < 3;
612
            })
613
            ->toArray()
614
            ->shouldReturn([1 => 3, 2 => 3, 3 => 2]);
615
616
        $this
617
            ->dropWhile(function ($v, $k) {
618
                return $k < 2 && $v < 3;
619
            })
620
            ->toArray()
621
            ->shouldReturn([1 => 3, 2 => 3, 3 => 2]);
622
    }
623
624
    function it_can_map_and_then_concatenate_a_collection()
625
    {
626
        $this
627
            ->mapcat(function ($v) {
628
                return [[$v]];
629
            })
630
            ->values()
631
            ->toArray()
632
            ->shouldReturn([[1], [3], [3], [2]]);
633
634
        $this
635
            ->mapcat(function ($v, $k) {
636
                return [[$k + $v]];
637
            })
638
            ->values()
639
            ->toArray()
640
            ->shouldReturn([[1], [4], [5], [5]]);
641
    }
642
643
    function it_can_take_items_while_predicament_is_true()
644
    {
645
        $this
646
            ->takeWhile(function ($v) {
647
                return $v < 3;
648
            })
649
            ->toArray()
650
            ->shouldReturn([1]);
651
652
        $this
653
            ->takeWhile(function ($v, $k) {
654
                return $k < 2 && $v < 3;
655
            })
656
            ->toArray()
657
            ->shouldReturn([1]);
658
    }
659
660
    function it_can_split_the_collection_at_nth_item()
661
    {
662
        $this->splitAt(2)->size()->shouldBe(2);
663
        $this->splitAt(2)->first()->toArray()->shouldBeLike([1, 3]);
664
        $this->splitAt(2)->getNth(1)->toArray()->shouldBeLike([2 => 3, 3 => 2]);
665
    }
666
667
    function it_can_split_the_collection_with_predicament()
668
    {
669
        $s1 = $this->splitWith(function ($v) {
670
            return $v < 3;
671
        });
672
673
        $s1->size()->shouldBe(2);
674
        $s1->first()->toArray()->shouldBe([1]);
675
        $s1->getNth(1)->toArray()->shouldBe([1 => 3, 2 => 3, 3 => 2]);
676
677
        $s2 = $this->splitWith(function ($v, $k) {
678
            return $v < 2 && $k < 3;
679
        });
680
681
        $s2->size()->shouldBe(2);
682
        $s2->first()->toArray()->shouldBe([1]);
683
        $s2->getNth(1)->toArray()->shouldBe([1 => 3, 2 => 3, 3 => 2]);
684
    }
685
686
    function it_can_replace_items_by_items_from_another_collection()
687
    {
688
        $this
689
            ->replace([3 => 'a'])
690
            ->toArray()
691
            ->shouldReturn([1, 'a', 'a', 2]);
692
    }
693
694
    function it_can_get_reduction_steps()
695
    {
696
        $this
697
            ->reductions(
698
                function ($tmp, $i) {
699
                    return $tmp + $i;
700
                },
701
                0
702
            )
703
            ->toArray()
704
            ->shouldReturn([0, 1, 4, 7, 9]);
705
    }
706
707
    function it_can_return_every_nth_item()
708
    {
709
        $this->takeNth(2)
710
            ->toArray()
711
            ->shouldReturn([1, 2 => 3]);
712
    }
713
714
    function it_can_shuffle_itself()
715
    {
716
        $this
717
            ->shuffle()
718
            ->reduce(
719
                function ($tmp, $i) {
720
                    return $tmp + $i;
721
                },
722
                0
723
            )
724
            ->shouldReturn(9);
725
    }
726
727
    function it_can_partition()
728
    {
729
        $s1 = $this->partition(3, 2, [0, 1]);
730
        $s1->size()->shouldBe(2);
731
        $s1->first()->toArray()->shouldBe([1, 3, 3]);
732
        $s1->getNth(1)->toArray()->shouldBe([2 => 3, 3 => 2, 0 => 0]);
733
734
        $s2 = $this->partition(3, 2);
735
        $s2->size()->shouldBe(2);
736
        $s2->first()->toArray()->shouldBe([1, 3, 3]);
737
        $s2->getNth(1)->toArray()->shouldBe([2 => 3, 3 => 2]);
738
739
        $s3 = $this->partition(3);
740
        $s3->size()->shouldBe(2);
741
        $s3->first()->toArray()->shouldBe([1, 3, 3]);
742
        $s3->getNth(1)->toArray()->shouldBe([3 => 2]);
743
744
        $s4 = $this->partition(1, 3);
745
        $s4->size()->shouldBe(2);
746
        $s4->first()->toArray()->shouldBe([1,]);
747
        $s4->getNth(1)->toArray()->shouldBe([3 => 2]);
748
    }
749
750
    function it_can_partition_by()
751
    {
752
        $this->beConstructedWith([1, 3, 3, 2]);
753
754
        $s1 = $this->partitionBy(function ($v) {
755
            return $v % 3 == 0;
756
        });
757
        $s1->size()->shouldBe(3);
758
        $s1->first()->toArray()->shouldBe([1]);
759
        $s1->getNth(1)->toArray()->shouldBe([1 => 3, 2 => 3]);
760
        $s1->getNth(2)->toArray()->shouldBe([3 => 2]);
761
762
763
        $s2 = $this->partitionBy(function ($v, $k) {
764
            return $k - $v;
765
        });
766
        $s2->size()->shouldBe(4);
767
        $s2->first()->toArray()->shouldBe([1]);
768
        $s2->getNth(1)->toArray()->shouldBe([1 => 3]);
769
        $s2->getNth(2)->toArray()->shouldBe([2 => 3]);
770
        $s2->getNth(3)->toArray()->shouldBe([3 => 2]);
771
    }
772
773
    function it_can_get_nth_value()
774
    {
775
        $this->getNth(0)->shouldReturn(1);
776
        $this->getNth(3)->shouldReturn(2);
777
    }
778
779
    function it_can_pluck()
780
    {
781
        $this->beConstructedWith([['a' => 1], ['a' => 2,  'b' => 3]]);
782
        $this->pluck('a')->values()->toArray()->shouldReturn([1, 2]);
783
    }
784
785
    function it_can_pluck_from_heterogenous_collection()
786
    {
787
        $this->beConstructedWith([['a' => 1], ['b' => 2], 1]);
788
        $this->pluck('a')->values()->toArray()->shouldReturn([1]);
789
790
    }
791
792
    function it_can_create_infinite_collection_of_repeated_values()
793
    {
794
        $this->beConstructedThrough('repeat', [1]);
795
        $this->take(3)->toArray()->shouldReturn([1, 1, 1]);
796
    }
797
798
    function it_can_create_finite_collection_of_repeated_values()
799
    {
800
        $this->beConstructedThrough('repeat', [1, 1]);
801
        $this->toArray()->shouldReturn([1]);
802
    }
803
804
    function it_can_create_range_from_value_to_infinity()
805
    {
806
        $this->beConstructedThrough('range', [5]);
807
        $this->take(2)->toArray()->shouldReturn([5, 6]);
808
    }
809
810
    function it_can_create_range_from_value_to_another_value()
811
    {
812
        $this->beConstructedThrough('range', [5, 6]);
813
        $this->take(4)->toArray()->shouldReturn([5, 6]);
814
    }
815
816
    function it_can_check_if_it_is_not_empty()
817
    {
818
        $this->isEmpty()->shouldReturn(false);
819
        $this->isNotEmpty()->shouldReturn(true);
820
    }
821
822
    function it_can_check_if_it_is_empty()
823
    {
824
        $this->beConstructedWith([]);
825
826
        $this->isEmpty()->shouldReturn(true);
827
        $this->isNotEmpty()->shouldReturn(false);
828
    }
829
830
    function it_can_check_frequency_of_distinct_items()
831
    {
832
        $this
833
            ->frequencies()
834
            ->toArray()
835
            ->shouldReturn([1 => 1, 3 => 2, 2 => 1]);
836
    }
837
838 View Code Duplication
    function it_can_get_first_item()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
839
    {
840
        $this->beConstructedWith([1, [2], 3]);
841
        $this->first()->shouldReturn(1);
842
        $this->drop(1)->first()->shouldReturn([2]);
843
        $this->drop(1)->first(true)->toArray()->shouldReturn([2]);
844
    }
845
846
    function it_will_throw_when_trying_to_get_first_item_of_empty_collection()
847
    {
848
        $this->beConstructedWith([]);
849
        $this->shouldThrow(ItemNotFound::class)->during('first');
850
    }
851
852 View Code Duplication
    function it_can_get_last_item()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
853
    {
854
        $this->beConstructedWith([1, [2], 3]);
855
        $this->last()->shouldReturn(3);
856
        $this->take(2)->last()->shouldReturn([2]);
857
        $this->take(2)->last(true)->toArray()->shouldReturn([2]);
858
    }
859
860
    function it_will_throw_when_trying_to_get_last_item_of_empty_collection()
861
    {
862
        $this->beConstructedWith([]);
863
        $this->shouldThrow(ItemNotFound::class)->during('last');
864
    }
865
866
    function it_can_realize_the_collection(PlusOneAdder $adder)
867
    {
868
        $adder->dynamicMethod(1)->willReturn(2);
869
        $adder->dynamicMethod(2)->willReturn(3);
870
871
        $this->beConstructedWith(function () use ($adder) {
872
            yield $adder->dynamicMethod(1);
873
            yield $adder->dynamicMethod(2);
874
        });
875
876
        $this->realize();
877
    }
878
879
    function it_can_combine_the_collection()
880
    {
881
        $this->beConstructedWith(['a', 'b']);
882
        $this->combine([1, 2])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
883
        $this->combine([1])->toArray()->shouldReturn(['a' => 1]);
884
        $this->combine([1, 2, 3])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
885
        $this->shouldThrow(NonEqualCollectionLength::class)->during('combine', [[1, 2, 3], true]);
886
    }
887
888
    function it_can_get_second_item()
889
    {
890
        $this->beConstructedWith([1, 2]);
891
        $this->second()->shouldReturn(2);
892
    }
893
894
    function it_throws_when_trying_to_get_non_existent_second_item()
895
    {
896
        $this->beConstructedWith([1]);
897
        $this->shouldThrow(ItemNotFound::class)->during('second');
898
    }
899
900
    function it_can_drop_item_by_key()
901
    {
902
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
903
        $this->except(['a', 'b'])->toArray()->shouldReturn([]);
904
    }
905
906
    function it_can_get_the_difference_between_collections()
907
    {
908
        $this->beConstructedWith([1, 2, 3, 4]);
909
        $this->difference([1, 2])->toArray()->shouldReturn([2 => 3, 3 => 4]);
910
        $this->difference([1, 2], [3])->toArray()->shouldReturn([3 => 4]);
911
    }
912
913
    function it_can_flip_the_collection()
914
    {
915
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
916
        $this->flip()->toArray()->shouldReturn([1 => 'a', 2 => 'b']);
917
    }
918
919
    function it_can_check_if_key_exits()
920
    {
921
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
922
        $this->has('a')->shouldReturn(true);
923
        $this->has('x')->shouldReturn(false);
924
    }
925
926
    function it_filters_by_keys()
927
    {
928
        $this->beConstructedWith(['a' => 1, 'b' => 2, 'c' => 3]);
929
        $this->only(['a', 'b'])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
930
        $this->only(['a', 'b', 'x'])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
931
    }
932
933
    function it_can_serialize_and_unserialize(Collection $c)
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
934
    {
935
        $original = Collection::from([1, 2, 3])->take(2);
936
        $this->beConstructedWith([1, 2, 3, 4]);
937
        $this->shouldHaveType(\Serializable::class);
938
        $this->unserialize($original->serialize());
939
        $this->toArray()->shouldReturn([1, 2]);
940
    }
941
}
942