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.
Passed
Push — master ( 973890...0cbf54 )
by Dušan
43s
created

CollectionSpec::it_can_group_by_key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 9.2
c 1
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
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\Tests\Helpers\PlusOneAdder;
12
use Iterator;
13
use IteratorAggregate;
14
use PhpSpec\ObjectBehavior;
15
use function DusanKasan\Knapsack\reverse;
16
17
/**
18
 * @mixin Collection
19
 */
20
class CollectionSpec extends ObjectBehavior
21
{
22
    function let()
23
    {
24
        $this->beConstructedWith([1, 3, 3, 2,]);
25
    }
26
27
    function it_is_initializable()
28
    {
29
        $this->shouldHaveType(Collection::class);
30
        $this->shouldHaveType(Iterator::class);
31
    }
32
33
    function it_can_be_instantiated_from_iterator()
34
    {
35
        $iterator = new ArrayIterator([1, 2]);
36
        $this->beConstructedWith($iterator);
37
        $this->toArray()->shouldReturn([1, 2]);
38
    }
39
40
    function it_can_be_instantiated_from_iterator_aggregate(IteratorAggregate $iteratorAggregate)
41
    {
42
        $iterator = new ArrayIterator([1, 2]);
43
        $iteratorAggregate->getIterator()->willReturn($iterator);
44
        $this->beConstructedWith($iteratorAggregate);
45
        $this->toArray()->shouldReturn([1, 2]);
46
    }
47
48
    function it_can_be_instantiated_from_array()
49
    {
50
        $this->beConstructedWith([1, 2, 3]);
51
        $this->toArray()->shouldReturn([1, 2, 3]);
52
    }
53
54
    function it_will_throw_when_passed_something_other_than_array_or_traversable()
55
    {
56
        $this->shouldThrow(InvalidArgument::class)->during('__construct', [1]);
57
    }
58
59
    function it_can_be_created_statically()
60
    {
61
        $this->beConstructedThrough('from', [[1, 2]]);
62
        $this->toArray()->shouldReturn([1, 2]);
63
    }
64
65
    function it_can_be_created_to_iterate_over_function_infinitely()
66
    {
67
        $this->beConstructedThrough('iterate', [1, function($i) {return $i+1;}]);
68
        $this->take(2)->toArray()->shouldReturn([1, 2]);
69
    }
70
71
    function it_can_be_created_to_iterate_over_function_non_infinitely()
72
    {
73
        $this->beConstructedThrough(
74
            'iterate',
75
            [
76
                1,
77
                function($i) {
78
                    if ($i > 3) {
79
                        throw new NoMoreItems;
80
                    }
81
82
                    return $i+1;
83
                }
84
            ]
85
        );
86
        $this->toArray()->shouldReturn([1, 2, 3, 4]);
87
    }
88
89
    function it_can_be_created_to_repeat_a_value_infinite_times()
90
    {
91
        $this->beConstructedThrough('repeat', [1]);
92
        $this->take(2)->toArray()->shouldReturn([1, 1]);
93
    }
94
95
    function it_can_filter()
96
    {
97
        $this
98
            ->filter(function ($item) {
99
                return $item > 2;
100
            })
101
            ->toArray()
102
            ->shouldReturn([1 => 3, 2 => 3]);
103
104
        $this
105
            ->filter(function ($item, $key) {
106
                return $key > 2 && $item < 3;
107
            })
108
            ->toArray()
109
            ->shouldReturn([3 => 2]);
110
    }
111
112
    function it_can_distinct()
113
    {
114
        $this
115
            ->distinct()
116
            ->toArray()
117
            ->shouldReturn([1, 3, 3 => 2]);
118
    }
119
120
    function it_can_concat()
121
    {
122
        $c1 = $this->concat([4, 5]);
123
        $c1->toArray()->shouldReturn([4, 5, 3, 2]);
124
        $c1->size()->shouldReturn(6);
125
    }
126
127
    function it_can_map()
128
    {
129
        $this
130
            ->map(function ($item) {
131
                return $item + 1;
132
            })
133
            ->toArray()
134
            ->shouldReturn([2, 4, 4, 3]);
135
    }
136
137 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...
138
    {
139
        $this
140
            ->reduce(
141
                function ($temp, $item) {
142
                    return $temp + $item;
143
                },
144
                0
145
            )
146
            ->shouldReturn(9);
147
148
        $this
149
            ->reduce(
150
                function ($temp, $item, $key) {
151
                    return $temp + $key + $item;
152
                },
153
                0
154
            )
155
            ->shouldReturn(15);
156
157
        $this
158
            ->reduce(
159
                function (Collection $temp, $item) {
160
                    return $temp->append($item);
161
                },
162
                new Collection([])
163
            )
164
            ->toArray()
165
            ->shouldReturn([1, 3, 3, 2]);
166
    }
167
168
    function it_can_flatten()
169
    {
170
        $this->beConstructedWith([1, [2, [3]]]);
171
        $this->flatten()->values()->toArray()->shouldReturn([1, 2, 3]);
172
        $this->flatten(1)->values()->toArray()->shouldReturn([1, 2, [3]]);
173
    }
174
175
    function it_can_sort()
176
    {
177
        $this->beConstructedWith([3, 1, 2]);
178
179
        $this
180
            ->sort(function ($a, $b) {
181
                return $a > $b;
182
            })
183
            ->toArray()
184
            ->shouldReturn([1 => 1, 2 => 2, 0 => 3]);
185
186
        $this
187
            ->sort(function ($v1, $v2, $k1, $k2) {
188
                return $k1 < $k2 || $v1 == $v2;
189
            })
190
            ->toArray()
191
            ->shouldReturn([2 => 2, 1 => 1, 0 => 3]);
192
    }
193
194
    function it_can_slice()
195
    {
196
        $this->beConstructedWith([1, 2, 3, 4, 5]);
197
198
        $this
199
            ->slice(2, 4)
200
            ->toArray()
201
            ->shouldReturn([2 => 3, 3 => 4]);
202
203
        $this
204
            ->slice(4)
205
            ->toArray()
206
            ->shouldReturn([4 => 5]);
207
    }
208
209
    function it_can_group_by()
210
    {
211
        $this->beConstructedWith([1, 2, 3, 4, 5]);
212
213
        $collection = $this->groupBy(function ($i) {
214
            return $i % 2;
215
        });
216
217
        $collection->get(0)->toArray()->shouldReturn([2, 4]);
218
        $collection->get(1)->toArray()->shouldReturn([1, 3, 5]);
219
220
        $collection = $this->groupBy(function ($k, $i) {
221
            return ($k + $i) % 3;
222
        });
223
        $collection->get(0)->toArray()->shouldReturn([2, 5]);
224
        $collection->get(1)->toArray()->shouldReturn([1, 4]);
225
        $collection->get(2)->toArray()->shouldReturn([3]);
226
    }
227
228
    function it_can_group_by_key()
229
    {
230
        $this->beConstructedWith([
231
            'some' => 'thing',
232
            ['letter' => 'A', 'type' => 'caps'],
233
            ['letter' => 'a', 'type' => 'small'],
234
            ['letter' => 'B', 'type' => 'caps'],
235
            ['letter' => 'Z']
236
        ]);
237
238
        $collection = $this->groupByKey('type');
239
        $collection->get('small')->toArray()->shouldReturn([
240
            ['letter' => 'a', 'type' => 'small']
241
        ]);
242
        $collection->get('caps')->toArray()->shouldReturn([
243
            ['letter' => 'A', 'type' => 'caps'],
244
            ['letter' => 'B', 'type' => 'caps']
245
        ]);
246
247
        $collection = $this->groupByKey('types');
248
        $collection->shouldThrow(new ItemNotFound)->during('get', ['caps']);
249
    }
250
251
    function it_can_execute_callback_for_each_item(DOMXPath $a)
252
    {
253
        $a->query('asd')->shouldBeCalled();
254
        $this->beConstructedWith([$a]);
255
256
        $this
257
            ->each(function (DOMXPath $i) {
258
                $i->query('asd');
259
            })
260
            ->toArray()
261
            ->shouldReturn([$a]);
262
    }
263
264
    function it_can_get_size()
265
    {
266
        $this->size()->shouldReturn(4);
267
    }
268
269
    function it_can_get_item_by_key()
270
    {
271
        $this->beConstructedWith([1, [2], 3]);
272
        $this->get(0)->shouldReturn(1);
273
        $this->get(1, true)->first()->shouldReturn(2);
274
        $this->get(1)->shouldReturn([2]);
275
        $this->shouldThrow(new ItemNotFound)->during('get', [5]);
276
    }
277
278
    function it_can_get_item_by_key_or_return_default()
279
    {
280
        $this->beConstructedWith([1, [2], 3]);
281
        $this->getOrDefault(0)->shouldReturn(1);
282
        $this->getOrDefault(1, null, true)->first()->shouldReturn(2);
283
        $this->getOrDefault(1, null)->shouldReturn([2]);
284
        $this->getOrDefault(5)->shouldReturn(null);
285
        $this->getOrDefault(5, 'not found')->shouldReturn('not found');
286
    }
287
288 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...
289
    {
290
        $this->beConstructedWith([1, [2], 3]);
291
        $this->getNth(0)->shouldReturn(1);
292
        $this->getNth(1, true)->first()->shouldReturn(2);
293
        $this->getNth(1)->shouldReturn([2]);
294
    }
295
296
    function it_can_find()
297
    {
298
        $this->beConstructedWith([1, 3, 3, 2, [5]]);
299
300
        $this
301
            ->find(function ($v) {
302
                return $v < 3;
303
            })
304
            ->shouldReturn(1);
305
306
        $this
307
            ->find(function ($v, $k) {
308
                return $v < 3 && $k > 1;
309
            })
310
            ->shouldReturn(2);
311
312
        $this
313
            ->find(function ($v) {
314
                return $v < 0;
315
            })
316
            ->shouldReturn(null);
317
318
        $this
319
            ->find(
320
                function ($v) {
321
                    return $v < 0;
322
                },
323
                'not found'
324
            )
325
            ->shouldReturn('not found');
326
327
        $this->find('\DusanKasan\Knapsack\isCollection', null, true)->first()->shouldReturn(5);
328
        $this->find('\DusanKasan\Knapsack\isCollection')->shouldReturn([5]);
329
    }
330
331
    function it_can_count_by()
332
    {
333
        $this
334
            ->countBy(function ($v) {
335
                return $v % 2 == 0 ? 'even' : 'odd';
336
            })
337
            ->toArray()
338
            ->shouldReturn(['odd' => 3, 'even' => 1]);
339
340
        $this
341
            ->countBy(function ($k, $v) {
342
                return ($k + $v) % 2 == 0 ? 'even' : 'odd';
343
            })
344
            ->toArray()
345
            ->shouldReturn(['odd' => 3, 'even' => 1]);
346
    }
347
348
    function it_can_index_by()
349
    {
350
        $this
351
            ->indexBy(function ($v) {
352
                return $v;
353
            })
354
            ->toArray()
355
            ->shouldReturn([1 => 1, 3 => 3, 2 => 2]);
356
357
        $this
358
            ->indexBy(function ($v, $k) {
359
                return $k . $v;
360
            })
361
            ->toArray()
362
            ->shouldReturn(['01' => 1, '13' => 3, '23' => 3, '32' => 2]);
363
    }
364
365
    function it_can_check_if_every_item_passes_predicament_test()
366
    {
367
        $this
368
            ->every(function ($v) {
369
                return $v > 0;
370
            })
371
            ->shouldReturn(true);
372
373
        $this
374
            ->every(function ($v) {
375
                return $v > 1;
376
            })
377
            ->shouldReturn(false);
378
379
        $this
380
            ->every(function ($v, $k) {
381
                return $v > 0 && $k >= 0;
382
            })
383
            ->shouldReturn(true);
384
385
        $this
386
            ->every(function ($v, $k) {
387
                return $v > 0 && $k > 0;
388
            })
389
            ->shouldReturn(false);
390
    }
391
392
    function it_can_check_if_some_items_pass_predicament_test()
393
    {
394
        $this
395
            ->some(function ($v) {
396
                return $v < -1;
397
            })
398
            ->shouldReturn(false);
399
400
        $this
401
            ->some(function ($v, $k) {
402
                return $v > 0 && $k < -1;
403
            })
404
            ->shouldReturn(false);
405
406
        $this
407
            ->some(function ($v) {
408
                return $v < 2;
409
            })
410
            ->shouldReturn(true);
411
412
        $this
413
            ->some(function ($v, $k) {
414
                return $v > 0 && $k > 0;
415
            })
416
            ->shouldReturn(true);
417
    }
418
419
    function it_can_check_if_it_contains_a_value()
420
    {
421
        $this
422
            ->contains(3)
423
            ->shouldReturn(true);
424
425
        $this
426
            ->contains(true)
427
            ->shouldReturn(false);
428
    }
429
430
    function it_can_reverse()
431
    {
432
        $it = $this->reverse();
433
434
        $it->rewind();
435
        $it->valid()->shouldReturn(true);
436
        $it->key()->shouldReturn(3);
437
        $it->current()->shouldReturn(2);
438
        $it->next();
439
        $it->valid()->shouldReturn(true);
440
        $it->key()->shouldReturn(2);
441
        $it->current()->shouldReturn(3);
442
        $it->next();
443
        $it->valid()->shouldReturn(true);
444
        $it->key()->shouldReturn(1);
445
        $it->current()->shouldReturn(3);
446
        $it->next();
447
        $it->valid()->shouldReturn(true);
448
        $it->key()->shouldReturn(0);
449
        $it->current()->shouldReturn(1);
450
        $it->next();
451
        $it->valid()->shouldReturn(false);
452
    }
453
454 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...
455
    {
456
        $this
457
            ->reduceRight(
458
                function ($temp, $e) {
459
                    return $temp . $e;
460
                },
461
                0
462
            )
463
            ->shouldReturn('02331');
464
465
        $this
466
            ->reduceRight(
467
                function ($temp, $key, $item) {
468
                    return $temp + $key + $item;
469
                },
470
            0
471
            )
472
            ->shouldReturn(15);
473
474
        $this
475
            ->reduceRight(
476
                function (Collection $temp, $item) {
477
                    return $temp->append($item);
478
                },
479
                new Collection([])
480
            )
481
            ->toArray()
482
            ->shouldReturn([2, 3, 3, 1]);
483
    }
484
485
    function it_can_return_only_first_x_elements()
486
    {
487
        $this->take(2)
488
            ->toArray()
489
            ->shouldReturn([1, 3]);
490
    }
491
492
    function it_can_skip_first_x_elements()
493
    {
494
        $this->drop(2)
495
            ->toArray()
496
            ->shouldReturn([2 => 3, 3 => 2]);
497
    }
498
499
    function it_can_return_values()
500
    {
501
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
502
        $this->values()->toArray()->shouldReturn([1, 2]);
503
    }
504
505
506
    function it_can_reject_elements_from_collection()
507
    {
508
        $this
509
            ->reject(function ($v) {
510
                return $v == 3;
511
            })
512
            ->toArray()
513
            ->shouldReturn([1, 3 => 2]);
514
515
        $this
516
            ->reject(function ($v, $k) {
517
                return $k == 2 && $v == 3;
518
            })
519
            ->toArray()
520
            ->shouldReturn([1, 3, 3 => 2]);
521
    }
522
523
    function it_can_get_keys()
524
    {
525
        $this
526
            ->keys()
527
            ->toArray()
528
            ->shouldReturn([0, 1, 2, 3]);
529
    }
530
531 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...
532
    {
533
        $this
534
            ->interpose('a')
535
            ->values()
536
            ->toArray()
537
            ->shouldReturn([1, 'a', 3, 'a', 3, 'a', 2]);
538
    }
539
540
    function it_can_drop_elements_from_end_of_the_collection()
541
    {
542
        $this
543
            ->dropLast()
544
            ->toArray()
545
            ->shouldReturn([1, 3, 3]);
546
547
        $this
548
            ->dropLast(2)
549
            ->toArray()
550
            ->shouldReturn([1, 3]);
551
    }
552
553
    function it_can_interleave_elements()
554
    {
555
        $this
556
            ->interleave(['a', 'b', 'c', 'd', 'e'])
557
            ->values()
558
            ->toArray()
559
            ->shouldReturn([1, 'a', 3, 'b', 3, 'c', 2, 'd', 'e']);
560
    }
561
562 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...
563
    {
564
        $this
565
            ->cycle()
566
            ->take(8)
567
            ->values()
568
            ->toArray()
569
            ->shouldReturn([1, 3, 3, 2, 1, 3, 3, 2]);
570
    }
571
572
    function it_can_prepend_item()
573
    {
574
        $this
575
            ->prepend(1)
576
            ->values()
577
            ->toArray()
578
            ->shouldReturn([1, 1, 3, 3, 2]);
579
    }
580
581
    function it_can_prepend_item_with_key()
582
    {
583
        $this
584
            ->prepend(1, 'a')
585
            ->toArray()
586
            ->shouldReturn(['a' => 1, 0 => 1, 1 => 3, 2 => 3, 3 => 2]);
587
    }
588
589
    function it_can_append_item()
590
    {
591
        $this
592
            ->append(1)
593
            ->values()
594
            ->toArray()
595
            ->shouldReturn([1, 3, 3, 2, 1]);
596
    }
597
598
    function it_can_append_item_with_key()
599
    {
600
        $this
601
            ->append(1, 'a')
602
            ->toArray()
603
            ->shouldReturn([1, 3, 3, 2, 'a' => 1]);
604
    }
605
606
    function it_can_drop_items_while_predicament_is_true()
607
    {
608
        $this
609
            ->dropWhile(function ($v) {
610
                return $v < 3;
611
            })
612
            ->toArray()
613
            ->shouldReturn([1 => 3, 2 => 3, 3 => 2]);
614
615
        $this
616
            ->dropWhile(function ($v, $k) {
617
                return $k < 2 && $v < 3;
618
            })
619
            ->toArray()
620
            ->shouldReturn([1 => 3, 2 => 3, 3 => 2]);
621
    }
622
623
    function it_can_map_and_then_concatenate_a_collection()
624
    {
625
        $this
626
            ->mapcat(function ($v) {
627
                return [[$v]];
628
            })
629
            ->values()
630
            ->toArray()
631
            ->shouldReturn([[1], [3], [3], [2]]);
632
633
        $this
634
            ->mapcat(function ($v, $k) {
635
                return [[$k + $v]];
636
            })
637
            ->values()
638
            ->toArray()
639
            ->shouldReturn([[1], [4], [5], [5]]);
640
    }
641
642
    function it_can_take_items_while_predicament_is_true()
643
    {
644
        $this
645
            ->takeWhile(function ($v) {
646
                return $v < 3;
647
            })
648
            ->toArray()
649
            ->shouldReturn([1]);
650
651
        $this
652
            ->takeWhile(function ($v, $k) {
653
                return $k < 2 && $v < 3;
654
            })
655
            ->toArray()
656
            ->shouldReturn([1]);
657
    }
658
659
    function it_can_split_the_collection_at_nth_item()
660
    {
661
        $this->splitAt(2)->size()->shouldBe(2);
662
        $this->splitAt(2)->first()->toArray()->shouldBeLike([1, 3]);
663
        $this->splitAt(2)->getNth(1)->toArray()->shouldBeLike([2 => 3, 3 => 2]);
664
    }
665
666
    function it_can_split_the_collection_with_predicament()
667
    {
668
        $s1 = $this->splitWith(function ($v) {
669
            return $v < 3;
670
        });
671
672
        $s1->size()->shouldBe(2);
673
        $s1->first()->toArray()->shouldBe([1]);
674
        $s1->getNth(1)->toArray()->shouldBe([1 => 3, 2 => 3, 3 => 2]);
675
676
        $s2 = $this->splitWith(function ($v, $k) {
677
            return $v < 2 && $k < 3;
678
        });
679
680
        $s2->size()->shouldBe(2);
681
        $s2->first()->toArray()->shouldBe([1]);
682
        $s2->getNth(1)->toArray()->shouldBe([1 => 3, 2 => 3, 3 => 2]);
683
    }
684
685
    function it_can_replace_items_by_items_from_another_collection()
686
    {
687
        $this
688
            ->replace([3 => 'a'])
689
            ->toArray()
690
            ->shouldReturn([1, 'a', 'a', 2]);
691
    }
692
693
    function it_can_get_reduction_steps()
694
    {
695
        $this
696
            ->reductions(
697
                function ($tmp, $i) {
698
                    return $tmp + $i;
699
                },
700
                0
701
            )
702
            ->toArray()
703
            ->shouldReturn([0, 1, 4, 7, 9]);
704
    }
705
706
    function it_can_return_every_nth_item()
707
    {
708
        $this->takeNth(2)
709
            ->toArray()
710
            ->shouldReturn([1, 2 => 3]);
711
    }
712
713
    function it_can_shuffle_itself()
714
    {
715
        $this
716
            ->shuffle()
717
            ->reduce(
718
                function ($tmp, $i) {
719
                    return $tmp + $i;
720
                },
721
                0
722
            )
723
            ->shouldReturn(9);
724
    }
725
726
    function it_can_partition()
727
    {
728
        $s1 = $this->partition(3, 2, [0, 1]);
729
        $s1->size()->shouldBe(2);
730
        $s1->first()->toArray()->shouldBe([1, 3, 3]);
731
        $s1->getNth(1)->toArray()->shouldBe([2 => 3, 3 => 2, 0 => 0]);
732
733
        $s2 = $this->partition(3, 2);
734
        $s2->size()->shouldBe(2);
735
        $s2->first()->toArray()->shouldBe([1, 3, 3]);
736
        $s2->getNth(1)->toArray()->shouldBe([2 => 3, 3 => 2]);
737
738
        $s3 = $this->partition(3);
739
        $s3->size()->shouldBe(2);
740
        $s3->first()->toArray()->shouldBe([1, 3, 3]);
741
        $s3->getNth(1)->toArray()->shouldBe([3 => 2]);
742
743
        $s4 = $this->partition(1, 3);
744
        $s4->size()->shouldBe(2);
745
        $s4->first()->toArray()->shouldBe([1,]);
746
        $s4->getNth(1)->toArray()->shouldBe([3 => 2]);
747
    }
748
749
    function it_can_partition_by()
750
    {
751
        $this->beConstructedWith([1, 3, 3, 2]);
752
753
        $s1 = $this->partitionBy(function ($v) {
754
            return $v % 3 == 0;
755
        });
756
        $s1->size()->shouldBe(3);
757
        $s1->first()->toArray()->shouldBe([1]);
758
        $s1->getNth(1)->toArray()->shouldBe([1 => 3, 2 => 3]);
759
        $s1->getNth(2)->toArray()->shouldBe([3 => 2]);
760
761
762
        $s2 = $this->partitionBy(function ($v, $k) {
763
            return $k - $v;
764
        });
765
        $s2->size()->shouldBe(4);
766
        $s2->first()->toArray()->shouldBe([1]);
767
        $s2->getNth(1)->toArray()->shouldBe([1 => 3]);
768
        $s2->getNth(2)->toArray()->shouldBe([2 => 3]);
769
        $s2->getNth(3)->toArray()->shouldBe([3 => 2]);
770
    }
771
772
    function it_can_get_nth_value()
773
    {
774
        $this->getNth(0)->shouldReturn(1);
775
        $this->getNth(3)->shouldReturn(2);
776
    }
777
778
    function it_can_pluck()
779
    {
780
        $this->beConstructedWith([['a' => 1], ['a' => 2,  'b' => 3]]);
781
        $this->pluck('a')->values()->toArray()->shouldReturn([1, 2]);
782
    }
783
784
    function it_can_pluck_from_heterogenous_collection()
785
    {
786
        $this->beConstructedWith([['a' => 1], ['b' => 2], 1]);
787
        $this->pluck('a')->values()->toArray()->shouldReturn([1]);
788
789
    }
790
791
    function it_can_create_infinite_collection_of_repeated_values()
792
    {
793
        $this->beConstructedThrough('repeat', [1]);
794
        $this->take(3)->toArray()->shouldReturn([1, 1, 1]);
795
    }
796
797
    function it_can_create_finite_collection_of_repeated_values()
798
    {
799
        $this->beConstructedThrough('repeat', [1, 1]);
800
        $this->toArray()->shouldReturn([1]);
801
    }
802
803
    function it_can_create_range_from_value_to_infinity()
804
    {
805
        $this->beConstructedThrough('range', [5]);
806
        $this->take(2)->toArray()->shouldReturn([5, 6]);
807
    }
808
809
    function it_can_create_range_from_value_to_another_value()
810
    {
811
        $this->beConstructedThrough('range', [5, 6]);
812
        $this->take(4)->toArray()->shouldReturn([5, 6]);
813
    }
814
815
    function it_can_check_if_it_is_not_empty()
816
    {
817
        $this->isEmpty()->shouldReturn(false);
818
        $this->isNotEmpty()->shouldReturn(true);
819
    }
820
821
    function it_can_check_if_it_is_empty()
822
    {
823
        $this->beConstructedWith([]);
824
825
        $this->isEmpty()->shouldReturn(true);
826
        $this->isNotEmpty()->shouldReturn(false);
827
    }
828
829
    function it_can_check_frequency_of_distinct_items()
830
    {
831
        $this
832
            ->frequencies()
833
            ->toArray()
834
            ->shouldReturn([1 => 1, 3 => 2, 2 => 1]);
835
    }
836
837 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...
838
    {
839
        $this->beConstructedWith([1, [2], 3]);
840
        $this->first()->shouldReturn(1);
841
        $this->drop(1)->first()->shouldReturn([2]);
842
        $this->drop(1)->first(true)->toArray()->shouldReturn([2]);
843
    }
844
845
    function it_will_throw_when_trying_to_get_first_item_of_empty_collection()
846
    {
847
        $this->beConstructedWith([]);
848
        $this->shouldThrow(ItemNotFound::class)->during('first');
849
    }
850
851 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...
852
    {
853
        $this->beConstructedWith([1, [2], 3]);
854
        $this->last()->shouldReturn(3);
855
        $this->take(2)->last()->shouldReturn([2]);
856
        $this->take(2)->last(true)->toArray()->shouldReturn([2]);
857
    }
858
859
    function it_will_throw_when_trying_to_get_last_item_of_empty_collection()
860
    {
861
        $this->beConstructedWith([]);
862
        $this->shouldThrow(ItemNotFound::class)->during('last');
863
    }
864
865
    function it_can_realize_the_collection(PlusOneAdder $adder)
866
    {
867
        $adder->dynamicMethod(1)->willReturn(2);
868
        $adder->dynamicMethod(2)->willReturn(3);
869
870
        $this->beConstructedWith(function () use ($adder) {
871
            yield $adder->dynamicMethod(1);
872
            yield $adder->dynamicMethod(2);
873
        });
874
875
        $this->realize();
876
    }
877
878
    function it_can_combine_the_collection()
879
    {
880
        $this->beConstructedWith(['a', 'b']);
881
        $this->combine([1, 2])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
882
        $this->combine([1])->toArray()->shouldReturn(['a' => 1]);
883
        $this->combine([1, 2, 3])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
884
        $this->shouldThrow(ItemNotFound::class)->during('combine', [[1, 2, 3], true]);
885
    }
886
887
    function it_can_get_second_item()
888
    {
889
        $this->beConstructedWith([1, 2]);
890
        $this->second()->shouldReturn(2);
891
    }
892
893
    function it_throws_when_trying_to_get_non_existent_second_item()
894
    {
895
        $this->beConstructedWith([1]);
896
        $this->shouldThrow(ItemNotFound::class)->during('second');
897
    }
898
899
    function it_can_drop_item_by_key()
900
    {
901
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
902
        $this->except(['a', 'b'])->toArray()->shouldReturn([]);
903
    }
904
905
    function it_can_get_the_difference_between_collections()
906
    {
907
        $this->beConstructedWith([1, 2, 3, 4]);
908
        $this->difference([1, 2])->toArray()->shouldReturn([2 => 3, 3 => 4]);
909
        $this->difference([1, 2], [3])->toArray()->shouldReturn([3 => 4]);
910
    }
911
912
    function it_can_flip_the_collection()
913
    {
914
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
915
        $this->flip()->toArray()->shouldReturn([1 => 'a', 2 => 'b']);
916
    }
917
918
    function it_can_check_if_key_exits()
919
    {
920
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
921
        $this->has('a')->shouldReturn(true);
922
        $this->has('x')->shouldReturn(false);
923
    }
924
925
    function it_filters_by_keys()
926
    {
927
        $this->beConstructedWith(['a' => 1, 'b' => 2, 'c' => 3]);
928
        $this->only(['a', 'b'])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
929
        $this->only(['a', 'b', 'x'])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
930
    }
931
}
932