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.
Test Failed
Push — master ( fafbcf...84e962 )
by Dušan
04:13
created

CollectionSpec::it_can_dump_the_collection()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 88
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 49
nc 1
nop 0
dl 0
loc 88
rs 8.6012
c 1
b 0
f 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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_filter_falsy_values()
114
    {
115
        $this->beConstructedWith([false, null, '', 0, 0.0, []]);
116
117
        $this->filter()->isEmpty()->shouldReturn(true);
118
119
    }
120
121
    function it_can_distinct()
122
    {
123
        $this->beConstructedWith([1, 3, 3, 2,]);
124
125
        $this
126
            ->distinct()
127
            ->toArray()
128
            ->shouldReturn([1, 3, 3 => 2]);
129
    }
130
131
    function it_can_concat()
132
    {
133
        $this->beConstructedWith([1, 3, 3, 2,]);
134
135
        $collection = $this->concat([4, 5]);
136
        $collection->toArray()->shouldReturn([4, 5, 3, 2]);
137
        $collection->size()->shouldReturn(6);
138
    }
139
140 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...
141
    {
142
        $this->beConstructedWith([1, 3, 3, 2,]);
143
144
        $this
145
            ->map(function ($item) {
146
                return $item + 1;
147
            })
148
            ->toArray()
149
            ->shouldReturn([2, 4, 4, 3]);
150
    }
151
152
    function it_can_reduce()
153
    {
154
        $this->beConstructedWith([1, 3, 3, 2,]);
155
156
        $this
157
            ->reduce(
158
                function ($temp, $item) {
159
                    $temp[] = $item;
160
161
                    return $temp;
162
                },
163
                ['a' => [1]],
164
                true
165
            )
166
            ->values()
167
            ->toArray()
168
            ->shouldReturn([[1], 1, 3, 3, 2]);
169
170
        $this
171
            ->reduce(
172
                function ($temp, $item) {
173
                    return $temp + $item;
174
                },
175
                0
176
            )
177
            ->shouldReturn(9);
178
179
        $this
180
            ->reduce(
181
                function ($temp, $item, $key) {
182
                    return $temp + $key + $item;
183
                },
184
                0
185
            )
186
            ->shouldReturn(15);
187
188
        $this
189
            ->reduce(
190
                function (Collection $temp, $item) {
191
                    return $temp->append($item);
192
                },
193
                new Collection([])
194
            )
195
            ->toArray()
196
            ->shouldReturn([1, 3, 3, 2]);
197
    }
198
199
    function it_can_flatten()
200
    {
201
        $this->beConstructedWith([1, [2, [3]]]);
202
        $this->flatten()->values()->toArray()->shouldReturn([1, 2, 3]);
203
        $this->flatten(1)->values()->toArray()->shouldReturn([1, 2, [3]]);
204
    }
205
206
    function it_can_sort()
207
    {
208
        $this->beConstructedWith([3, 1, 2]);
209
210
        $this
211
            ->sort(function ($a, $b) {
212
                return $a > $b;
213
            })
214
            ->toArray()
215
            ->shouldReturn([1 => 1, 2 => 2, 0 => 3]);
216
217
        $this
218
            ->sort(function ($v1, $v2, $k1, $k2) {
219
                return $k1 < $k2 || $v1 == $v2;
220
            })
221
            ->toArray()
222
            ->shouldReturn([2 => 2, 1 => 1, 0 => 3]);
223
    }
224
225
    function it_can_slice()
226
    {
227
        $this->beConstructedWith([1, 2, 3, 4, 5]);
228
229
        $this
230
            ->slice(2, 4)
231
            ->toArray()
232
            ->shouldReturn([2 => 3, 3 => 4]);
233
234
        $this
235
            ->slice(4)
236
            ->toArray()
237
            ->shouldReturn([4 => 5]);
238
    }
239
240
    function it_can_group_by()
241
    {
242
        $this->beConstructedWith([1, 2, 3, 4, 5]);
243
244
        $collection = $this->groupBy(function ($i) {
245
            return $i % 2;
246
        });
247
248
        $collection->get(0)->toArray()->shouldReturn([2, 4]);
249
        $collection->get(1)->toArray()->shouldReturn([1, 3, 5]);
250
251
        $collection = $this->groupBy(function ($k, $i) {
252
            return ($k + $i) % 3;
253
        });
254
        $collection->get(0)->toArray()->shouldReturn([2, 5]);
255
        $collection->get(1)->toArray()->shouldReturn([1, 4]);
256
        $collection->get(2)->toArray()->shouldReturn([3]);
257
    }
258
259
    function it_can_group_by_key()
260
    {
261
        $this->beConstructedWith([
262
            'some' => 'thing',
263
            ['letter' => 'A', 'type' => 'caps'],
264
            ['letter' => 'a', 'type' => 'small'],
265
            ['letter' => 'B', 'type' => 'caps'],
266
            ['letter' => 'Z']
267
        ]);
268
269
        $collection = $this->groupByKey('type');
270
        $collection->get('small')->toArray()->shouldReturn([
271
            ['letter' => 'a', 'type' => 'small']
272
        ]);
273
        $collection->get('caps')->toArray()->shouldReturn([
274
            ['letter' => 'A', 'type' => 'caps'],
275
            ['letter' => 'B', 'type' => 'caps']
276
        ]);
277
278
        $collection = $this->groupByKey('types');
279
        $collection->shouldThrow(new ItemNotFound)->during('get', ['caps']);
280
    }
281
282
    function it_can_execute_callback_for_each_item(DOMXPath $a)
283
    {
284
        $a->query('asd')->shouldBeCalled();
285
        $this->beConstructedWith([$a]);
286
287
        $this
288
            ->each(function (DOMXPath $i) {
289
                $i->query('asd');
290
            })
291
            ->toArray()
292
            ->shouldReturn([$a]);
293
    }
294
295
    function it_can_get_size()
296
    {
297
        $this->beConstructedWith([1, 3, 3, 2,]);
298
        $this->size()->shouldReturn(4);
299
    }
300
301
    function it_can_get_item_by_key()
302
    {
303
        $this->beConstructedWith([1, [2], 3]);
304
        $this->get(0)->shouldReturn(1);
305
        $this->get(1, true)->first()->shouldReturn(2);
306
        $this->get(1)->shouldReturn([2]);
307
        $this->shouldThrow(new ItemNotFound)->during('get', [5]);
308
    }
309
310
    function it_can_get_item_by_key_or_return_default()
311
    {
312
        $this->beConstructedWith([1, [2], 3]);
313
        $this->getOrDefault(0)->shouldReturn(1);
314
        $this->getOrDefault(1, null, true)->first()->shouldReturn(2);
315
        $this->getOrDefault(1, null)->shouldReturn([2]);
316
        $this->getOrDefault(5)->shouldReturn(null);
317
        $this->getOrDefault(5, 'not found')->shouldReturn('not found');
318
    }
319
320
    function it_can_find()
321
    {
322
        $this->beConstructedWith([1, 3, 3, 2, [5]]);
323
324
        $this
325
            ->find(function ($v) {
326
                return $v < 3;
327
            })
328
            ->shouldReturn(1);
329
330
        $this
331
            ->find(function ($v, $k) {
332
                return $v < 3 && $k > 1;
333
            })
334
            ->shouldReturn(2);
335
336
        $this
337
            ->find(function ($v) {
338
                return $v < 0;
339
            })
340
            ->shouldReturn(null);
341
342
        $this
343
            ->find(
344
                function ($v) {
345
                    return $v < 0;
346
                },
347
                'not found'
348
            )
349
            ->shouldReturn('not found');
350
351
        $this->find('\DusanKasan\Knapsack\isCollection', null, true)->first()->shouldReturn(5);
352
        $this->find('\DusanKasan\Knapsack\isCollection')->shouldReturn([5]);
353
    }
354
355
    function it_can_count_by()
356
    {
357
        $this->beConstructedWith([1, 3, 3, 2,]);
358
359
        $this
360
            ->countBy(function ($v) {
361
                return $v % 2 == 0 ? 'even' : 'odd';
362
            })
363
            ->toArray()
364
            ->shouldReturn(['odd' => 3, 'even' => 1]);
365
366
        $this
367
            ->countBy(function ($k, $v) {
368
                return ($k + $v) % 2 == 0 ? 'even' : 'odd';
369
            })
370
            ->toArray()
371
            ->shouldReturn(['odd' => 3, 'even' => 1]);
372
    }
373
374
    function it_can_index_by()
375
    {
376
        $this->beConstructedWith([1, 3, 3, 2,]);
377
378
        $this
379
            ->indexBy(function ($v) {
380
                return $v;
381
            })
382
            ->toArray()
383
            ->shouldReturn([1 => 1, 3 => 3, 2 => 2]);
384
385
        $this
386
            ->indexBy(function ($v, $k) {
387
                return $k . $v;
388
            })
389
            ->toArray()
390
            ->shouldReturn(['01' => 1, '13' => 3, '23' => 3, '32' => 2]);
391
    }
392
393
    function it_can_check_if_every_item_passes_predicament_test()
394
    {
395
        $this->beConstructedWith([1, 3, 3, 2,]);
396
397
        $this
398
            ->every(function ($v) {
399
                return $v > 0;
400
            })
401
            ->shouldReturn(true);
402
403
        $this
404
            ->every(function ($v) {
405
                return $v > 1;
406
            })
407
            ->shouldReturn(false);
408
409
        $this
410
            ->every(function ($v, $k) {
411
                return $v > 0 && $k >= 0;
412
            })
413
            ->shouldReturn(true);
414
415
        $this
416
            ->every(function ($v, $k) {
417
                return $v > 0 && $k > 0;
418
            })
419
            ->shouldReturn(false);
420
    }
421
422
    function it_can_check_if_some_items_pass_predicament_test()
423
    {
424
        $this->beConstructedWith([1, 3, 3, 2,]);
425
426
        $this
427
            ->some(function ($v) {
428
                return $v < -1;
429
            })
430
            ->shouldReturn(false);
431
432
        $this
433
            ->some(function ($v, $k) {
434
                return $v > 0 && $k < -1;
435
            })
436
            ->shouldReturn(false);
437
438
        $this
439
            ->some(function ($v) {
440
                return $v < 2;
441
            })
442
            ->shouldReturn(true);
443
444
        $this
445
            ->some(function ($v, $k) {
446
                return $v > 0 && $k > 0;
447
            })
448
            ->shouldReturn(true);
449
    }
450
451 View Code Duplication
    function it_can_check_if_it_contains_a_value()
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...
452
    {
453
        $this->beConstructedWith([1, 3, 3, 2,]);
454
455
        $this
456
            ->contains(3)
457
            ->shouldReturn(true);
458
459
        $this
460
            ->contains(true)
461
            ->shouldReturn(false);
462
    }
463
464
    function it_can_reverse()
465
    {
466
        $this->beConstructedWith([1, 3, 3, 2,]);
467
468
        $collection = $this->reverse();
469
470
        $collection->rewind();
471
        $collection->valid()->shouldReturn(true);
472
        $collection->key()->shouldReturn(3);
473
        $collection->current()->shouldReturn(2);
474
        $collection->next();
475
        $collection->valid()->shouldReturn(true);
476
        $collection->key()->shouldReturn(2);
477
        $collection->current()->shouldReturn(3);
478
        $collection->next();
479
        $collection->valid()->shouldReturn(true);
480
        $collection->key()->shouldReturn(1);
481
        $collection->current()->shouldReturn(3);
482
        $collection->next();
483
        $collection->valid()->shouldReturn(true);
484
        $collection->key()->shouldReturn(0);
485
        $collection->current()->shouldReturn(1);
486
        $collection->next();
487
        $collection->valid()->shouldReturn(false);
488
    }
489
490
    function it_can_reduce_from_right()
491
    {
492
        $this->beConstructedWith([1, 3, 3, 2,]);
493
494
        $this
495
            ->reduceRight(
496
                function ($temp, $e) {
497
                    return $temp . $e;
498
                },
499
                0
500
            )
501
            ->shouldReturn('02331');
502
503
        $this
504
            ->reduceRight(
505
                function ($temp, $key, $item) {
506
                    return $temp + $key + $item;
507
                },
508
            0
509
            )
510
            ->shouldReturn(15);
511
512
        $this
513
            ->reduceRight(
514
                function (Collection $temp, $item) {
515
                    return $temp->append($item);
516
                },
517
                new Collection([])
518
            )
519
            ->toArray()
520
            ->shouldReturn([2, 3, 3, 1]);
521
    }
522
523
    function it_can_return_only_first_x_elements()
524
    {
525
        $this->beConstructedWith([1, 3, 3, 2,]);
526
527
        $this->take(2)
528
            ->toArray()
529
            ->shouldReturn([1, 3]);
530
    }
531
532
    function it_can_skip_first_x_elements()
533
    {
534
        $this->beConstructedWith([1, 3, 3, 2,]);
535
536
        $this->drop(2)
537
            ->toArray()
538
            ->shouldReturn([2 => 3, 3 => 2]);
539
    }
540
541
    function it_can_return_values()
542
    {
543
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
544
        $this->values()->toArray()->shouldReturn([1, 2]);
545
    }
546
547
548
    function it_can_reject_elements_from_collection()
549
    {
550
        $this->beConstructedWith([1, 3, 3, 2,]);
551
552
        $this
553
            ->reject(function ($v) {
554
                return $v == 3;
555
            })
556
            ->toArray()
557
            ->shouldReturn([1, 3 => 2]);
558
559
        $this
560
            ->reject(function ($v, $k) {
561
                return $k == 2 && $v == 3;
562
            })
563
            ->toArray()
564
            ->shouldReturn([1, 3, 3 => 2]);
565
    }
566
567
    function it_can_get_keys()
568
    {
569
        $this->beConstructedWith([1, 3, 3, 2,]);
570
571
        $this
572
            ->keys()
573
            ->toArray()
574
            ->shouldReturn([0, 1, 2, 3]);
575
    }
576
577 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...
578
    {
579
        $this->beConstructedWith([1, 3, 3, 2,]);
580
581
        $this
582
            ->interpose('a')
583
            ->values()
584
            ->toArray()
585
            ->shouldReturn([1, 'a', 3, 'a', 3, 'a', 2]);
586
    }
587
588
    function it_can_drop_elements_from_end_of_the_collection()
589
    {
590
        $this->beConstructedWith([1, 3, 3, 2,]);
591
592
        $this
593
            ->dropLast()
594
            ->toArray()
595
            ->shouldReturn([1, 3, 3]);
596
597
        $this
598
            ->dropLast(2)
599
            ->toArray()
600
            ->shouldReturn([1, 3]);
601
    }
602
603
    function it_can_interleave_elements()
604
    {
605
        $this->beConstructedWith([1, 3, 3, 2,]);
606
607
        $this
608
            ->interleave(['a', 'b', 'c', 'd', 'e'])
609
            ->values()
610
            ->toArray()
611
            ->shouldReturn([1, 'a', 3, 'b', 3, 'c', 2, 'd', 'e']);
612
    }
613
614 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...
615
    {
616
        $this->beConstructedWith([1, 3, 3, 2,]);
617
618
        $this
619
            ->cycle()
620
            ->take(8)
621
            ->values()
622
            ->toArray()
623
            ->shouldReturn([1, 3, 3, 2, 1, 3, 3, 2]);
624
    }
625
626 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...
627
    {
628
        $this->beConstructedWith([1, 3, 3, 2,]);
629
630
        $this
631
            ->prepend(1)
632
            ->values()
633
            ->toArray()
634
            ->shouldReturn([1, 1, 3, 3, 2]);
635
    }
636
637
    function it_can_prepend_item_with_key()
638
    {
639
        $this->beConstructedWith([1, 3, 3, 2,]);
640
641
        $this
642
            ->prepend(1, 'a')
643
            ->toArray()
644
            ->shouldReturn(['a' => 1, 0 => 1, 1 => 3, 2 => 3, 3 => 2]);
645
    }
646
647 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...
648
    {
649
        $this->beConstructedWith([1, 3, 3, 2,]);
650
651
        $this
652
            ->append(1)
653
            ->values()
654
            ->toArray()
655
            ->shouldReturn([1, 3, 3, 2, 1]);
656
    }
657
658
    function it_can_append_item_with_key()
659
    {
660
        $this->beConstructedWith([1, 3, 3, 2,]);
661
662
        $this
663
            ->append(1, 'a')
664
            ->toArray()
665
            ->shouldReturn([1, 3, 3, 2, 'a' => 1]);
666
    }
667
668
    function it_can_drop_items_while_predicament_is_true()
669
    {
670
        $this->beConstructedWith([1, 3, 3, 2,]);
671
672
        $this
673
            ->dropWhile(function ($v) {
674
                return $v < 3;
675
            })
676
            ->toArray()
677
            ->shouldReturn([1 => 3, 2 => 3, 3 => 2]);
678
679
        $this
680
            ->dropWhile(function ($v, $k) {
681
                return $k < 2 && $v < 3;
682
            })
683
            ->toArray()
684
            ->shouldReturn([1 => 3, 2 => 3, 3 => 2]);
685
    }
686
687
    function it_can_map_and_then_concatenate_a_collection()
688
    {
689
        $this->beConstructedWith([1, 3, 3, 2,]);
690
691
        $this
692
            ->mapcat(function ($v) {
693
                return [[$v]];
694
            })
695
            ->values()
696
            ->toArray()
697
            ->shouldReturn([[1], [3], [3], [2]]);
698
699
        $this
700
            ->mapcat(function ($v, $k) {
701
                return [[$k + $v]];
702
            })
703
            ->values()
704
            ->toArray()
705
            ->shouldReturn([[1], [4], [5], [5]]);
706
    }
707
708
    function it_can_take_items_while_predicament_is_true()
709
    {
710
        $this->beConstructedWith([1, 3, 3, 2,]);
711
712
        $this
713
            ->takeWhile(function ($v) {
714
                return $v < 3;
715
            })
716
            ->toArray()
717
            ->shouldReturn([1]);
718
719
        $this
720
            ->takeWhile(function ($v, $k) {
721
                return $k < 2 && $v < 3;
722
            })
723
            ->toArray()
724
            ->shouldReturn([1]);
725
    }
726
727
    function it_can_split_the_collection_at_nth_item()
728
    {
729
        $this->beConstructedWith([1, 3, 3, 2,]);
730
731
        $this->splitAt(2)->size()->shouldBe(2);
732
        $this->splitAt(2)->first()->toArray()->shouldBeLike([1, 3]);
733
        $this->splitAt(2)->second()->toArray()->shouldBeLike([2 => 3, 3 => 2]);
734
    }
735
736
    function it_can_split_the_collection_with_predicament()
737
    {
738
        $this->beConstructedWith([1, 3, 3, 2,]);
739
740
        $s1 = $this->splitWith(function ($v) {
741
            return $v < 3;
742
        });
743
744
        $s1->size()->shouldBe(2);
745
        $s1->first()->toArray()->shouldBe([1]);
746
        $s1->second()->toArray()->shouldBe([1 => 3, 2 => 3, 3 => 2]);
747
748
        $s2 = $this->splitWith(function ($v, $k) {
749
            return $v < 2 && $k < 3;
750
        });
751
752
        $s2->size()->shouldBe(2);
753
        $s2->first()->toArray()->shouldBe([1]);
754
        $s2->second()->toArray()->shouldBe([1 => 3, 2 => 3, 3 => 2]);
755
    }
756
757 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...
758
    {
759
        $this->beConstructedWith([1, 3, 3, 2,]);
760
761
        $this
762
            ->replace([3 => 'a'])
763
            ->toArray()
764
            ->shouldReturn([1, 'a', 'a', 2]);
765
    }
766
767 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...
768
    {
769
        $this->beConstructedWith([1, 3, 3, 2,]);
770
771
        $this
772
            ->reductions(
773
                function ($tmp, $i) {
774
                    return $tmp + $i;
775
                },
776
                0
777
            )
778
            ->toArray()
779
            ->shouldReturn([0, 1, 4, 7, 9]);
780
    }
781
782
    function it_can_return_every_nth_item()
783
    {
784
        $this->beConstructedWith([1, 3, 3, 2,]);
785
786
        $this->takeNth(2)
787
            ->toArray()
788
            ->shouldReturn([1, 2 => 3]);
789
    }
790
791
    function it_can_shuffle_itself()
792
    {
793
        $this->beConstructedWith([1, 3, 3, 2,]);
794
795
        $this
796
            ->shuffle()
797
            ->reduce(
798
                function ($tmp, $i) {
799
                    return $tmp + $i;
800
                },
801
                0
802
            )
803
            ->shouldReturn(9);
804
    }
805
806
    function it_can_partition()
807
    {
808
        $this->beConstructedWith([1, 3, 3, 2,]);
809
810
        $s1 = $this->partition(3, 2, [0, 1]);
811
        $s1->size()->shouldBe(2);
812
        $s1->first()->toArray()->shouldBe([1, 3, 3]);
813
        $s1->second()->toArray()->shouldBe([2 => 3, 3 => 2, 0 => 0]);
814
815
        $s2 = $this->partition(3, 2);
816
        $s2->size()->shouldBe(2);
817
        $s2->first()->toArray()->shouldBe([1, 3, 3]);
818
        $s2->second()->toArray()->shouldBe([2 => 3, 3 => 2]);
819
820
        $s3 = $this->partition(3);
821
        $s3->size()->shouldBe(2);
822
        $s3->first()->toArray()->shouldBe([1, 3, 3]);
823
        $s3->second()->toArray()->shouldBe([3 => 2]);
824
825
        $s4 = $this->partition(1, 3);
826
        $s4->size()->shouldBe(2);
827
        $s4->first()->toArray()->shouldBe([1,]);
828
        $s4->second()->toArray()->shouldBe([3 => 2]);
829
    }
830
831
    function it_can_partition_by()
832
    {
833
        $this->beConstructedWith([1, 3, 3, 2]);
834
835
        $s1 = $this->partitionBy(function ($v) {
836
            return $v % 3 == 0;
837
        });
838
        $s1->size()->shouldBe(3);
839
        $s1->first()->toArray()->shouldBe([1]);
840
        $s1->second()->toArray()->shouldBe([1 => 3, 2 => 3]);
841
        $s1->values()->get(2)->toArray()->shouldBe([3 => 2]);
842
843
844
        $s2 = $this->partitionBy(function ($v, $k) {
845
            return $k - $v;
846
        });
847
        $s2->size()->shouldBe(4);
848
        $s2->first()->toArray()->shouldBe([1]);
849
        $s2->values()->get(1)->toArray()->shouldBe([1 => 3]);
850
        $s2->values()->get(2)->toArray()->shouldBe([2 => 3]);
851
        $s2->values()->get(3)->toArray()->shouldBe([3 => 2]);
852
    }
853
854
    function it_can_get_nth_value()
855
    {
856
        $this->beConstructedWith([1, 3, 3, 2,]);
857
858
        $this->first(0)->shouldReturn(1);
859
        $this->values()->get(3)->shouldReturn(2);
860
    }
861
862
    function it_can_create_infinite_collection_of_repeated_values()
863
    {
864
        $this->beConstructedThrough('repeat', [1]);
865
        $this->take(3)->toArray()->shouldReturn([1, 1, 1]);
866
    }
867
868
    function it_can_create_finite_collection_of_repeated_values()
869
    {
870
        $this->beConstructedThrough('repeat', [1, 1]);
871
        $this->toArray()->shouldReturn([1]);
872
    }
873
874
    function it_can_create_range_from_value_to_infinity()
875
    {
876
        $this->beConstructedThrough('range', [5]);
877
        $this->take(2)->toArray()->shouldReturn([5, 6]);
878
    }
879
880
    function it_can_create_range_from_value_to_another_value()
881
    {
882
        $this->beConstructedThrough('range', [5, 6]);
883
        $this->take(4)->toArray()->shouldReturn([5, 6]);
884
    }
885
886
    function it_can_check_if_it_is_not_empty()
887
    {
888
        $this->beConstructedWith([1, 3, 3, 2,]);
889
890
        $this->isEmpty()->shouldReturn(false);
891
        $this->isNotEmpty()->shouldReturn(true);
892
    }
893
894
    function it_can_check_if_it_is_empty()
895
    {
896
        $this->beConstructedWith([]);
897
898
        $this->isEmpty()->shouldReturn(true);
899
        $this->isNotEmpty()->shouldReturn(false);
900
    }
901
902
    function it_can_check_frequency_of_distinct_items()
903
    {
904
        $this->beConstructedWith([1, 3, 3, 2,]);
905
906
        $this
907
            ->frequencies()
908
            ->toArray()
909
            ->shouldReturn([1 => 1, 3 => 2, 2 => 1]);
910
    }
911
912 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...
913
    {
914
        $this->beConstructedWith([1, [2], 3]);
915
        $this->first()->shouldReturn(1);
916
        $this->drop(1)->first()->shouldReturn([2]);
917
        $this->drop(1)->first(true)->toArray()->shouldReturn([2]);
918
    }
919
920
    function it_will_throw_when_trying_to_get_first_item_of_empty_collection()
921
    {
922
        $this->beConstructedWith([]);
923
        $this->shouldThrow(ItemNotFound::class)->during('first');
924
    }
925
926 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...
927
    {
928
        $this->beConstructedWith([1, [2], 3]);
929
        $this->last()->shouldReturn(3);
930
        $this->take(2)->last()->shouldReturn([2]);
931
        $this->take(2)->last(true)->toArray()->shouldReturn([2]);
932
    }
933
934
    function it_will_throw_when_trying_to_get_last_item_of_empty_collection()
935
    {
936
        $this->beConstructedWith([]);
937
        $this->shouldThrow(ItemNotFound::class)->during('last');
938
    }
939
940
    function it_can_realize_the_collection(PlusOneAdder $adder)
941
    {
942
        $adder->dynamicMethod(1)->willReturn(2);
943
        $adder->dynamicMethod(2)->willReturn(3);
944
945
        $this->beConstructedWith(function () use ($adder) {
946
            yield $adder->dynamicMethod(1);
947
            yield $adder->dynamicMethod(2);
948
        });
949
950
        $this->realize();
951
    }
952
953
    function it_can_combine_the_collection()
954
    {
955
        $this->beConstructedWith(['a', 'b']);
956
        $this->combine([1, 2])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
957
        $this->combine([1])->toArray()->shouldReturn(['a' => 1]);
958
        $this->combine([1, 2, 3])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
959
    }
960
961
    function it_can_get_second_item()
962
    {
963
        $this->beConstructedWith([1, 2]);
964
        $this->second()->shouldReturn(2);
965
    }
966
967
    function it_throws_when_trying_to_get_non_existent_second_item()
968
    {
969
        $this->beConstructedWith([1]);
970
        $this->shouldThrow(ItemNotFound::class)->during('second');
971
    }
972
973
    function it_can_drop_item_by_key()
974
    {
975
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
976
        $this->except(['a', 'b'])->toArray()->shouldReturn([]);
977
    }
978
979
    function it_can_get_the_difference_between_collections()
980
    {
981
        $this->beConstructedWith([1, 2, 3, 4]);
982
        $this->diff([1, 2])->toArray()->shouldReturn([2 => 3, 3 => 4]);
983
        $this->diff([1, 2], [3])->toArray()->shouldReturn([3 => 4]);
984
    }
985
986
    function it_can_flip_the_collection()
987
    {
988
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
989
        $this->flip()->toArray()->shouldReturn([1 => 'a', 2 => 'b']);
990
    }
991
992
    function it_can_check_if_key_exits()
993
    {
994
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
995
        $this->has('a')->shouldReturn(true);
996
        $this->has('x')->shouldReturn(false);
997
    }
998
999
    function it_filters_by_keys()
1000
    {
1001
        $this->beConstructedWith(['a' => 1, 'b' => 2, 'c' => 3]);
1002
        $this->only(['a', 'b'])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
1003
        $this->only(['a', 'b', 'x'])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
1004
    }
1005
1006
    function it_can_serialize_and_unserialize()
1007
    {
1008
        $original = Collection::from([1, 2, 3])->take(2);
1009
        $this->beConstructedWith([1, 2, 3, 4]);
1010
        $this->shouldHaveType(Serializable::class);
1011
        $this->unserialize($original->serialize());
1012
        $this->toArray()->shouldReturn([1, 2]);
1013
    }
1014
1015
    function it_can_zip()
1016
    {
1017
        $this->beConstructedWith([1, 2, 3]);
1018
        $this->zip(['a' => 1, 'b' => 2, 'c' => 4])
1019
            ->map('\DusanKasan\Knapsack\toArray')
1020
            ->toArray()
1021
            ->shouldReturn([[1, 'a' => 1], [1 => 2, 'b' => 2], [2 => 3, 'c' => 4]]);
1022
1023
        $this->zip([4, 5, 6], [7, 8, 9])
1024
            ->map('\DusanKasan\Knapsack\values')
1025
            ->map('\DusanKasan\Knapsack\toArray')
1026
            ->toArray()
1027
            ->shouldReturn([[1, 4, 7], [2, 5, 8], [3, 6, 9]]);
1028
1029
        $this->zip([4, 5])
1030
            ->map('\DusanKasan\Knapsack\values')
1031
            ->map('\DusanKasan\Knapsack\toArray')
1032
            ->toArray()
1033
            ->shouldReturn([[1, 4], [2, 5]]);
1034
    }
1035
1036
    function it_can_use_callable_as_transformer()
1037
    {
1038
        $this->beConstructedWith([1, 2, 3]);
1039
        $this
1040
            ->transform(function (Collection $collection) {
1041
                return $collection->map('\DusanKasan\Knapsack\increment');
1042
            })
1043
            ->toArray()
1044
            ->shouldReturn([2, 3, 4]);
1045
1046
        $this
1047
            ->shouldThrow(InvalidReturnValue::class)
1048
            ->during(
1049
                'transform',
1050
                [
1051
                    function (Collection $collection) {
1052
                        return $collection->first();
1053
                    }
1054
                ]
1055
            );
1056
    }
1057
1058
    function it_can_extract_data_from_nested_collections()
1059
    {
1060
        $input = [
1061
            [
1062
                'a' => [
1063
                    'b' => 1
1064
                ]
1065
            ],
1066
            [
1067
                'a' => [
1068
                    'b' => 2
1069
                ]
1070
            ],
1071
            [
1072
                '*' => [
1073
                    'b' => 3
1074
                ]
1075
            ],
1076
            [
1077
                '.' => [
1078
                    'b' => 4
1079
                ],
1080
                'c' => [
1081
                    'b' => 5
1082
                ],
1083
                [
1084
                    'a'
1085
                ]
1086
            ]
1087
        ];
1088
        $this->beConstructedWith($input);
1089
1090
        $this->extract('')->toArray()->shouldReturn($input);
1091
        $this->extract('a.b')->toArray()->shouldReturn([1, 2]);
1092
        $this->extract('*.b')->toArray()->shouldReturn([1, 2, 3, 4, 5]);
1093
        $this->extract('\*.b')->toArray()->shouldReturn([3]);
1094
        $this->extract('\..b')->toArray()->shouldReturn([4]);
1095
    }
1096
1097
    function it_can_get_the_intersect_of_collections()
1098
    {
1099
        $this->beConstructedWith([1, 2, 3]);
1100
        $this->intersect([1, 2])->values()->toArray()->shouldReturn([1, 2]);
1101
        $this->intersect([1], [3])->values()->toArray()->shouldReturn([1, 3]);
1102
    }
1103
1104 View Code Duplication
    function it_can_check_if_size_is_exactly_n()
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...
1105
    {
1106
        $this->beConstructedWith([1, 2]);
1107
        $this->sizeIs(2)->shouldReturn(true);
1108
        $this->sizeIs(3)->shouldReturn(false);
1109
        $this->sizeIs(0)->shouldReturn(false);
1110
    }
1111
1112 View Code Duplication
    function it_can_check_if_size_is_less_than_n()
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...
1113
    {
1114
        $this->beConstructedWith([1, 2]);
1115
        $this->sizeIsLessThan(0)->shouldReturn(false);
1116
        $this->sizeIsLessThan(2)->shouldReturn(false);
1117
        $this->sizeIsLessThan(3)->shouldReturn(true);
1118
    }
1119
1120 View Code Duplication
    function it_can_check_if_size_is_greater_than_n()
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...
1121
    {
1122
        $this->beConstructedWith([1, 2]);
1123
        $this->sizeIsGreaterThan(2)->shouldReturn(false);
1124
        $this->sizeIsGreaterThan(1)->shouldReturn(true);
1125
        $this->sizeIsGreaterThan(0)->shouldReturn(true);
1126
    }
1127
1128
    function it_can_check_if_size_is_between_n_and_m()
1129
    {
1130
        $this->beConstructedWith([1, 2]);
1131
        $this->sizeIsBetween(1, 3)->shouldReturn(true);
1132
        $this->sizeIsBetween(3, 4)->shouldReturn(false);
1133
        $this->sizeIsBetween(0, 0)->shouldReturn(false);
1134
        $this->sizeIsBetween(3, 1)->shouldReturn(true);
1135
    }
1136
1137
    function it_can_sum_the_collection()
1138
    {
1139
        $this->beConstructedWith([1, 2, 3, 4]);
1140
        $this->sum()->shouldReturn(10);
1141
        $this->append(1.5)->sum()->shouldReturn(11.5);
1142
    }
1143
1144
    function it_can_get_average_of_the_collection()
1145
    {
1146
        $this->beConstructedWith([1, 2, 2, 3]);
1147
        $this->average()->shouldReturn(2);
1148
        $this->append(3)->average()->shouldReturn(2.2);
1149
    }
1150
1151
    function it_will_return_zero_when_average_is_called_on_empty_collection()
1152
    {
1153
        $this->beConstructedWith([]);
1154
        $this->average()->shouldReturn(0);
1155
    }
1156
1157
    function it_can_get_maximal_value_in_the_colleciton()
1158
    {
1159
        $this->beConstructedWith([1, 2, 3, 2]);
1160
        $this->max()->shouldReturn(3);
1161
    }
1162
1163
    function it_will_return_null_when_max_is_called_on_empty_collection()
1164
    {
1165
        $this->beConstructedWith([]);
1166
        $this->max()->shouldReturn(null);
1167
    }
1168
1169
    function it_can_get_min_value_in_the_colleciton()
1170
    {
1171
        $this->beConstructedWith([2, 1, 3, 2]);
1172
        $this->min()->shouldReturn(1);
1173
    }
1174
1175
    function it_will_return_null_when_min_is_called_on_empty_collection()
1176
    {
1177
        $this->beConstructedWith([]);
1178
        $this->min()->shouldReturn(null);
1179
    }
1180
1181
    function it_can_convert_the_collection_to_string()
1182
    {
1183
        $this->beConstructedWith([2, 'a', 3, null]);
1184
        $this->toString()->shouldReturn('2a3');
1185
    }
1186
1187
    function it_can_replace_by_key()
1188
    {
1189
        $this->beConstructedWith(['a' => 1, 'b' => 2, 'c' => 3]);
1190
        $this->replaceByKeys(['b' => 3])->toArray()->shouldReturn(['a' => 1, 'b' => 3, 'c' => 3]);
1191
    }
1192
1193
    function it_can_use_the_utility_methods()
1194
    {
1195
        $this->beConstructedWith([1, 3, 2]);
1196
1197
        $this
1198
            ->sort('\DusanKasan\Knapsack\compare')
1199
            ->values()
1200
            ->toArray()
1201
            ->shouldReturn([1, 2, 3]);
1202
1203
        $this
1204
            ->map('\DusanKasan\Knapsack\compare')
1205
            ->toArray()
1206
            ->shouldReturn([1, 1, 0]);
1207
1208
        $this
1209
            ->map('\DusanKasan\Knapsack\decrement')
1210
            ->toArray()
1211
            ->shouldReturn([0, 2, 1]);
1212
    }
1213
1214
    function it_can_dump_the_collection()
1215
    {
1216
        $datetime = new \DateTime(
1217
            '2016-06-08 19:57:02.000000',
1218
            new \DateTimeZone('Europe/Berlin')
1219
        );
1220
1221
        $this->beConstructedWith(
1222
            [
1223
                [
1224
                    [1, [2], 3],
1225
                    ['a' => 'b'],
1226
                    new ArrayIterator([1, 2, 3])
1227
                ],
1228
                [1, 2, 3],
1229
                new ArrayIterator(['a', 'b', 'c']),
1230
                true,
1231
                $datetime,
1232
                \DusanKasan\Knapsack\concat([1], [1])
1233
            ]
1234
        );
1235
1236
        $this->dump()->shouldReturn(
1237
            [
1238
                [
1239
                    [1, [2], 3],
1240
                    ['a' => 'b'],
1241
                    [1, 2, 3]
1242
                ],
1243
                [1, 2, 3],
1244
                ['a', 'b', 'c'],
1245
                true,
1246
                [
1247
                    'DateTime' => [
1248
                        'date' => "2016-06-08 19:57:02.000000",
1249
                        'timezone_type' => 3,
1250
                        'timezone' => "Europe/Berlin",
1251
                    ],
1252
                ],
1253
                [1, '0//1' => 1]
1254
            ]
1255
        );
1256
1257
        $this->dump(2)->shouldReturn(
1258
            [
1259
                [
1260
                    [1, [2], '>>>'],
1261
                    ['a' => 'b'],
1262
                    '>>>'
1263
                ],
1264
                [1, 2, '>>>'],
1265
                '>>>'
1266
            ]
1267
        );
1268
1269
        $this->dump(null, 3)->shouldReturn(
1270
            [
1271
                [
1272
                    [1, '^^^', 3],
1273
                    ['a' => 'b'],
1274
                    [1, 2, 3]
1275
                ],
1276
                [1, 2, 3],
1277
                ['a', 'b', 'c'],
1278
                true,
1279
                [
1280
                    'DateTime' => [
1281
                        'date' => "2016-06-08 19:57:02.000000",
1282
                        'timezone_type' => 3,
1283
                        'timezone' => "Europe/Berlin",
1284
                    ],
1285
                ],
1286
                [1, '0//1' => 1]
1287
            ]
1288
        );
1289
1290
        $this->dump(2, 3)->shouldReturn(
1291
            [
1292
                [
1293
                    [1, '^^^', '>>>'],
1294
                    ['a' => 'b'],
1295
                    '>>>'
1296
                ],
1297
                [1, 2, '>>>'],
1298
                '>>>'
1299
            ]
1300
        );
1301
    }
1302
}
1303
1304