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 ( 5e0f3f...55b30d )
by Dušan
03:05
created

CollectionSpec::it_can_use_the_utility_methods()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

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