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 ( 4d892a...befb2e )
by Dušan
02:58
created

CollectionSpec::it_can_reverse()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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