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 ( 9bd2f0...277002 )
by Dušan
03:03
created

CollectionSpec::it_can_pluck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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