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.
Failed Conditions
Pull Request — master (#53)
by
unknown
525:23
created

CollectionSpec::it_can_pluck_array()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

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