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 (#50)
by
unknown
02:32
created

CollectionSpec::it_can_reduce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

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