GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#40)
by Issei
11:11
created

CollectionSpec::it_should_be_countable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace spec\DusanKasan\Knapsack;
4
5
use ArrayIterator;
6
use Countable;
7
use DOMXPath;
8
use DusanKasan\Knapsack\Collection;
9
use DusanKasan\Knapsack\Exceptions\InvalidArgument;
10
use DusanKasan\Knapsack\Exceptions\InvalidReturnValue;
11
use DusanKasan\Knapsack\Exceptions\ItemNotFound;
12
use DusanKasan\Knapsack\Exceptions\NoMoreItems;
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(Countable::class);
30
        $this->shouldHaveType(Serializable::class);
31
    }
32
33
    function it_can_be_instantiated_from_iterator()
34
    {
35
        $iterator = new ArrayIterator([1, 2]);
36
        $this->beConstructedWith($iterator);
37
        $this->toArray()->shouldReturn([1, 2]);
38
    }
39
40
    function it_can_be_instantiated_from_iterator_aggregate(IteratorAggregate $iteratorAggregate)
41
    {
42
        $iterator = new ArrayIterator([1, 2]);
43
        $iteratorAggregate->getIterator()->willReturn($iterator);
44
        $this->beConstructedWith($iteratorAggregate);
45
        $this->toArray()->shouldReturn([1, 2]);
46
    }
47
48
    function it_can_be_instantiated_from_array()
49
    {
50
        $this->beConstructedWith([1, 2, 3]);
51
        $this->toArray()->shouldReturn([1, 2, 3]);
52
    }
53
54
    function it_will_throw_when_passed_something_other_than_array_or_traversable()
55
    {
56
        $this->beConstructedWith(1);
57
        $this->shouldThrow(InvalidArgument::class)->duringInstantiation();
58
    }
59
60
    function it_can_be_instantiated_from_callable_returning_an_array()
61
    {
62
        $this->beConstructedWith(function () { return [1, 2, 3]; });
63
        $this->toArray()->shouldReturn([1, 2, 3]);
64
    }
65
66
    function it_can_be_instantiated_from_callable_returning_an_iterator()
67
    {
68
        $this->beConstructedWith(function () { return new ArrayIterator([1, 2, 3]); });
69
        $this->toArray()->shouldReturn([1, 2, 3]);
70
    }
71
72 View Code Duplication
    function it_can_be_instantiated_from_callable_returning_a_generator()
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...
73
    {
74
        $this->beConstructedWith(function () {
75
            foreach ([1, 2, 3] as $value) {
76
                yield $value;
77
            }
78
        });
79
        $this->toArray()->shouldReturn([1, 2, 3]);
80
    }
81
82
    function it_will_throw_when_passed_callable_will_return_something_other_than_array_or_traversable()
83
    {
84
        $this->beConstructedWith(function () { return 1; });
85
        $this->shouldThrow(InvalidReturnValue::class)->duringInstantiation();
86
    }
87
88
    function it_can_be_created_statically()
89
    {
90
        $this->beConstructedThrough('from', [[1, 2]]);
91
        $this->toArray()->shouldReturn([1, 2]);
92
    }
93
94
    function it_can_be_created_to_iterate_over_function_infinitely()
95
    {
96
        $this->beConstructedThrough('iterate', [1, function ($i) {return $i + 1;}]);
97
        $this->take(2)->toArray()->shouldReturn([1, 2]);
98
    }
99
100
    function it_can_be_created_to_iterate_over_function_non_infinitely()
101
    {
102
        $this->beConstructedThrough(
103
            'iterate',
104
            [
105
                1,
106
                function ($i) {
107
                    if ($i > 3) {
108
                        throw new NoMoreItems;
109
                    }
110
111
                    return $i + 1;
112
                },
113
            ]
114
        );
115
        $this->toArray()->shouldReturn([1, 2, 3, 4]);
116
    }
117
118
    function it_can_be_created_to_repeat_a_value_infinite_times()
119
    {
120
        $this->beConstructedThrough('repeat', [1]);
121
        $this->take(2)->toArray()->shouldReturn([1, 1]);
122
    }
123
124
    function it_can_convert_to_array()
125
    {
126
        $iterator = new \ArrayIterator([
127
            'foo',
128
        ]);
129
130
        $this->beConstructedWith(function () use ($iterator) {
131
            yield 'no key';
132
            yield 'with key' => 'this value is overwritten by the same key';
133
            yield 'nested' => [
134
                'y' => 'z',
135
            ];
136
            yield 'iterator is not converted' => $iterator;
137
            yield 'with key' => 'x';
138
        });
139
140
        $this
141
            ->toArray()
142
            ->shouldReturn([
143
                'no key',
144
                'with key' => 'x',
145
                'nested' => [
146
                    'y' => 'z',
147
                ],
148
                'iterator is not converted' => $iterator,
149
            ]);
150
    }
151
152
    function it_can_filter()
153
    {
154
        $this->beConstructedWith([1, 3, 3, 2,]);
155
156
        $this
157
            ->filter(function ($item) {
158
                return $item > 2;
159
            })
160
            ->toArray()
161
            ->shouldReturn([1 => 3, 2 => 3]);
162
163
        $this
164
            ->filter(function ($item, $key) {
165
                return $key > 2 && $item < 3;
166
            })
167
            ->toArray()
168
            ->shouldReturn([3 => 2]);
169
    }
170
171
    function it_can_filter_falsy_values()
172
    {
173
        $this->beConstructedWith([false, null, '', 0, 0.0, []]);
174
175
        $this->filter()->isEmpty()->shouldReturn(true);
176
    }
177
178
    function it_can_distinct()
179
    {
180
        $this->beConstructedWith([1, 3, 3, 2,]);
181
182
        $this
183
            ->distinct()
184
            ->toArray()
185
            ->shouldReturn([1, 3, 3 => 2]);
186
    }
187
188
    function it_can_concat()
189
    {
190
        $this->beConstructedWith([1, 3, 3, 2,]);
191
192
        $collection = $this->concat([4, 5]);
193
        $collection->toArray()->shouldReturn([4, 5, 3, 2]);
194
        $collection->size()->shouldReturn(6);
195
    }
196
197 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...
198
    {
199
        $this->beConstructedWith([1, 3, 3, 2,]);
200
201
        $this
202
            ->map(function ($item) {
203
                return $item + 1;
204
            })
205
            ->toArray()
206
            ->shouldReturn([2, 4, 4, 3]);
207
    }
208
209
    function it_can_reduce()
210
    {
211
        $this->beConstructedWith([1, 3, 3, 2,]);
212
213
        $this
214
            ->reduce(
215
                function ($temp, $item) {
216
                    $temp[] = $item;
217
218
                    return $temp;
219
                },
220
                ['a' => [1]],
221
                true
222
            )
223
            ->values()
224
            ->toArray()
225
            ->shouldReturn([[1], 1, 3, 3, 2]);
226
227
        $this
228
            ->reduce(
229
                function ($temp, $item) {
230
                    return $temp + $item;
231
                },
232
                0
233
            )
234
            ->shouldReturn(9);
235
236
        $this
237
            ->reduce(
238
                function ($temp, $item, $key) {
239
                    return $temp + $key + $item;
240
                },
241
                0
242
            )
243
            ->shouldReturn(15);
244
245
        $this
246
            ->reduce(
247
                function (Collection $temp, $item) {
248
                    return $temp->append($item);
249
                },
250
                new Collection([])
251
            )
252
            ->toArray()
253
            ->shouldReturn([1, 3, 3, 2]);
254
    }
255
256
    function it_can_flatten()
257
    {
258
        $this->beConstructedWith([1, [2, [3]]]);
259
        $this->flatten()->values()->toArray()->shouldReturn([1, 2, 3]);
260
        $this->flatten(1)->values()->toArray()->shouldReturn([1, 2, [3]]);
261
    }
262
263
    function it_can_sort()
264
    {
265
        $this->beConstructedWith([3, 1, 2]);
266
267
        $this
268
            ->sort(function ($a, $b) {
269
                return $a > $b;
270
            })
271
            ->toArray()
272
            ->shouldReturn([1 => 1, 2 => 2, 0 => 3]);
273
274
        $this
275
            ->sort(function ($v1, $v2, $k1, $k2) {
276
                return $k1 < $k2 || $v1 == $v2;
277
            })
278
            ->toArray()
279
            ->shouldReturn([2 => 2, 1 => 1, 0 => 3]);
280
    }
281
282
    function it_can_slice()
283
    {
284
        $this->beConstructedWith([1, 2, 3, 4, 5]);
285
286
        $this
287
            ->slice(2, 4)
288
            ->toArray()
289
            ->shouldReturn([2 => 3, 3 => 4]);
290
291
        $this
292
            ->slice(4)
293
            ->toArray()
294
            ->shouldReturn([4 => 5]);
295
    }
296
297
    function it_can_group_by()
298
    {
299
        $this->beConstructedWith([1, 2, 3, 4, 5]);
300
301
        $collection = $this->groupBy(function ($i) {
302
            return $i % 2;
303
        });
304
305
        $collection->get(0)->toArray()->shouldReturn([2, 4]);
306
        $collection->get(1)->toArray()->shouldReturn([1, 3, 5]);
307
308
        $collection = $this->groupBy(function ($k, $i) {
309
            return ($k + $i) % 3;
310
        });
311
        $collection->get(0)->toArray()->shouldReturn([2, 5]);
312
        $collection->get(1)->toArray()->shouldReturn([1, 4]);
313
        $collection->get(2)->toArray()->shouldReturn([3]);
314
    }
315
316
    function it_can_group_by_key()
317
    {
318
        $this->beConstructedWith([
319
            'some' => 'thing',
320
            ['letter' => 'A', 'type' => 'caps'],
321
            ['letter' => 'a', 'type' => 'small'],
322
            ['letter' => 'B', 'type' => 'caps'],
323
            ['letter' => 'Z'],
324
        ]);
325
326
        $collection = $this->groupByKey('type');
327
        $collection->get('small')->toArray()->shouldReturn([
328
            ['letter' => 'a', 'type' => 'small'],
329
        ]);
330
        $collection->get('caps')->toArray()->shouldReturn([
331
            ['letter' => 'A', 'type' => 'caps'],
332
            ['letter' => 'B', 'type' => 'caps'],
333
        ]);
334
335
        $collection = $this->groupByKey('types');
336
        $collection->shouldThrow(new ItemNotFound)->during('get', ['caps']);
337
    }
338
339
    function it_can_execute_callback_for_each_item(DOMXPath $a)
340
    {
341
        $a->query('asd')->shouldBeCalled();
342
        $this->beConstructedWith([$a]);
343
344
        $this
345
            ->each(function (DOMXPath $i) {
346
                $i->query('asd');
347
            })
348
            ->toArray()
349
            ->shouldReturn([$a]);
350
    }
351
352
    function it_can_get_size()
353
    {
354
        $this->beConstructedWith([1, 3, 3, 2,]);
355
        $this->size()->shouldReturn(4);
356
    }
357
358
    function it_should_be_countable()
359
    {
360
        $this->beConstructedWith([1, 3, 3, 2,]);
361
        $this->shouldHaveType(Countable::class);
362
        $this->count()->shouldReturn(4);
363
    }
364
365
    function it_can_get_item_by_key()
366
    {
367
        $this->beConstructedWith([1, [2], 3]);
368
        $this->get(0)->shouldReturn(1);
369
        $this->get(1, true)->first()->shouldReturn(2);
370
        $this->get(1)->shouldReturn([2]);
371
        $this->shouldThrow(new ItemNotFound)->during('get', [5]);
372
    }
373
374
    function it_can_get_item_by_key_or_return_default()
375
    {
376
        $this->beConstructedWith([1, [2], 3]);
377
        $this->getOrDefault(0)->shouldReturn(1);
378
        $this->getOrDefault(1, null, true)->first()->shouldReturn(2);
379
        $this->getOrDefault(1, null)->shouldReturn([2]);
380
        $this->getOrDefault(5)->shouldReturn(null);
381
        $this->getOrDefault(5, 'not found')->shouldReturn('not found');
382
    }
383
384
    function it_can_find()
385
    {
386
        $this->beConstructedWith([1, 3, 3, 2, [5]]);
387
388
        $this
389
            ->find(function ($v) {
390
                return $v < 3;
391
            })
392
            ->shouldReturn(1);
393
394
        $this
395
            ->find(function ($v, $k) {
396
                return $v < 3 && $k > 1;
397
            })
398
            ->shouldReturn(2);
399
400
        $this
401
            ->find(function ($v) {
402
                return $v < 0;
403
            })
404
            ->shouldReturn(null);
405
406
        $this
407
            ->find(
408
                function ($v) {
409
                    return $v < 0;
410
                },
411
                'not found'
412
            )
413
            ->shouldReturn('not found');
414
415
        $this->find('\DusanKasan\Knapsack\isCollection', null, true)->first()->shouldReturn(5);
416
        $this->find('\DusanKasan\Knapsack\isCollection')->shouldReturn([5]);
417
    }
418
419
    function it_can_count_by()
420
    {
421
        $this->beConstructedWith([1, 3, 3, 2,]);
422
423
        $this
424
            ->countBy(function ($v) {
425
                return $v % 2 == 0 ? 'even' : 'odd';
426
            })
427
            ->toArray()
428
            ->shouldReturn(['odd' => 3, 'even' => 1]);
429
430
        $this
431
            ->countBy(function ($k, $v) {
432
                return ($k + $v) % 2 == 0 ? 'even' : 'odd';
433
            })
434
            ->toArray()
435
            ->shouldReturn(['odd' => 3, 'even' => 1]);
436
    }
437
438
    function it_can_index_by()
439
    {
440
        $this->beConstructedWith([1, 3, 3, 2,]);
441
442
        $this
443
            ->indexBy(function ($v) {
444
                return $v;
445
            })
446
            ->toArray()
447
            ->shouldReturn([1 => 1, 3 => 3, 2 => 2]);
448
449
        $this
450
            ->indexBy(function ($v, $k) {
451
                return $k . $v;
452
            })
453
            ->toArray()
454
            ->shouldReturn(['01' => 1, '13' => 3, '23' => 3, '32' => 2]);
455
    }
456
457
    function it_can_check_if_every_item_passes_predicament_test()
458
    {
459
        $this->beConstructedWith([1, 3, 3, 2,]);
460
461
        $this
462
            ->every(function ($v) {
463
                return $v > 0;
464
            })
465
            ->shouldReturn(true);
466
467
        $this
468
            ->every(function ($v) {
469
                return $v > 1;
470
            })
471
            ->shouldReturn(false);
472
473
        $this
474
            ->every(function ($v, $k) {
475
                return $v > 0 && $k >= 0;
476
            })
477
            ->shouldReturn(true);
478
479
        $this
480
            ->every(function ($v, $k) {
481
                return $v > 0 && $k > 0;
482
            })
483
            ->shouldReturn(false);
484
    }
485
486
    function it_can_check_if_some_items_pass_predicament_test()
487
    {
488
        $this->beConstructedWith([1, 3, 3, 2,]);
489
490
        $this
491
            ->some(function ($v) {
492
                return $v < -1;
493
            })
494
            ->shouldReturn(false);
495
496
        $this
497
            ->some(function ($v, $k) {
498
                return $v > 0 && $k < -1;
499
            })
500
            ->shouldReturn(false);
501
502
        $this
503
            ->some(function ($v) {
504
                return $v < 2;
505
            })
506
            ->shouldReturn(true);
507
508
        $this
509
            ->some(function ($v, $k) {
510
                return $v > 0 && $k > 0;
511
            })
512
            ->shouldReturn(true);
513
    }
514
515 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...
516
    {
517
        $this->beConstructedWith([1, 3, 3, 2,]);
518
519
        $this
520
            ->contains(3)
521
            ->shouldReturn(true);
522
523
        $this
524
            ->contains(true)
525
            ->shouldReturn(false);
526
    }
527
528 View Code Duplication
    function it_can_reverse()
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...
529
    {
530
        $this->beConstructedWith([1, 3, 3, 2,]);
531
532
        $this
533
            ->reverse()
534
            ->toArray()
535
            ->shouldReturn([
536
                3 => 2,
537
                2 => 3,
538
                1 => 3,
539
                0 => 1,
540
            ])
541
        ;
542
    }
543
544
    function it_can_reduce_from_right()
545
    {
546
        $this->beConstructedWith([1, 3, 3, 2,]);
547
548
        $this
549
            ->reduceRight(
550
                function ($temp, $e) {
551
                    return $temp . $e;
552
                },
553
                0
554
            )
555
            ->shouldReturn('02331');
556
557
        $this
558
            ->reduceRight(
559
                function ($temp, $key, $item) {
560
                    return $temp + $key + $item;
561
                },
562
                0
563
            )
564
            ->shouldReturn(15);
565
566
        $this
567
            ->reduceRight(
568
                function (Collection $temp, $item) {
569
                    return $temp->append($item);
570
                },
571
                new Collection([])
572
            )
573
            ->toArray()
574
            ->shouldReturn([2, 3, 3, 1]);
575
    }
576
577 View Code Duplication
    function it_can_return_only_first_x_elements()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
578
    {
579
        $this->beConstructedWith([1, 3, 3, 2,]);
580
581
        $this->take(2)
582
            ->toArray()
583
            ->shouldReturn([1, 3]);
584
    }
585
586
    function it_can_skip_first_x_elements()
587
    {
588
        $this->beConstructedWith([1, 3, 3, 2,]);
589
590
        $this->drop(2)
591
            ->toArray()
592
            ->shouldReturn([2 => 3, 3 => 2]);
593
    }
594
595
    function it_can_return_values()
596
    {
597
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
598
        $this->values()->toArray()->shouldReturn([1, 2]);
599
    }
600
601
    function it_can_reject_elements_from_collection()
602
    {
603
        $this->beConstructedWith([1, 3, 3, 2,]);
604
605
        $this
606
            ->reject(function ($v) {
607
                return $v == 3;
608
            })
609
            ->toArray()
610
            ->shouldReturn([1, 3 => 2]);
611
612
        $this
613
            ->reject(function ($v, $k) {
614
                return $k == 2 && $v == 3;
615
            })
616
            ->toArray()
617
            ->shouldReturn([1, 3, 3 => 2]);
618
    }
619
620
    function it_can_get_keys()
621
    {
622
        $this->beConstructedWith([1, 3, 3, 2,]);
623
624
        $this
625
            ->keys()
626
            ->toArray()
627
            ->shouldReturn([0, 1, 2, 3]);
628
    }
629
630 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...
631
    {
632
        $this->beConstructedWith([1, 3, 3, 2,]);
633
634
        $this
635
            ->interpose('a')
636
            ->values()
637
            ->toArray()
638
            ->shouldReturn([1, 'a', 3, 'a', 3, 'a', 2]);
639
    }
640
641
    function it_can_drop_elements_from_end_of_the_collection()
642
    {
643
        $this->beConstructedWith([1, 3, 3, 2,]);
644
645
        $this
646
            ->dropLast()
647
            ->toArray()
648
            ->shouldReturn([1, 3, 3]);
649
650
        $this
651
            ->dropLast(2)
652
            ->toArray()
653
            ->shouldReturn([1, 3]);
654
    }
655
656
    function it_can_interleave_elements()
657
    {
658
        $this->beConstructedWith([1, 3, 3, 2,]);
659
660
        $this
661
            ->interleave(['a', 'b', 'c', 'd', 'e'])
662
            ->values()
663
            ->toArray()
664
            ->shouldReturn([1, 'a', 3, 'b', 3, 'c', 2, 'd', 'e']);
665
    }
666
667 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...
668
    {
669
        $this->beConstructedWith([1, 3, 3, 2,]);
670
671
        $this
672
            ->cycle()
673
            ->take(8)
674
            ->values()
675
            ->toArray()
676
            ->shouldReturn([1, 3, 3, 2, 1, 3, 3, 2]);
677
    }
678
679 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...
680
    {
681
        $this->beConstructedWith([1, 3, 3, 2,]);
682
683
        $this
684
            ->prepend(1)
685
            ->values()
686
            ->toArray()
687
            ->shouldReturn([1, 1, 3, 3, 2]);
688
    }
689
690
    function it_can_prepend_item_with_key()
691
    {
692
        $this->beConstructedWith([1, 3, 3, 2,]);
693
694
        $this
695
            ->prepend(1, 'a')
696
            ->toArray()
697
            ->shouldReturn(['a' => 1, 0 => 1, 1 => 3, 2 => 3, 3 => 2]);
698
    }
699
700 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...
701
    {
702
        $this->beConstructedWith([1, 3, 3, 2,]);
703
704
        $this
705
            ->append(1)
706
            ->values()
707
            ->toArray()
708
            ->shouldReturn([1, 3, 3, 2, 1]);
709
    }
710
711
    function it_can_append_item_with_key()
712
    {
713
        $this->beConstructedWith([1, 3, 3, 2,]);
714
715
        $this
716
            ->append(1, 'a')
717
            ->toArray()
718
            ->shouldReturn([1, 3, 3, 2, 'a' => 1]);
719
    }
720
721
    function it_can_drop_items_while_predicament_is_true()
722
    {
723
        $this->beConstructedWith([1, 3, 3, 2,]);
724
725
        $this
726
            ->dropWhile(function ($v) {
727
                return $v < 3;
728
            })
729
            ->toArray()
730
            ->shouldReturn([1 => 3, 2 => 3, 3 => 2]);
731
732
        $this
733
            ->dropWhile(function ($v, $k) {
734
                return $k < 2 && $v < 3;
735
            })
736
            ->toArray()
737
            ->shouldReturn([1 => 3, 2 => 3, 3 => 2]);
738
    }
739
740
    function it_can_map_and_then_concatenate_a_collection()
741
    {
742
        $this->beConstructedWith([1, 3, 3, 2,]);
743
744
        $this
745
            ->mapcat(function ($v) {
746
                return [[$v]];
747
            })
748
            ->values()
749
            ->toArray()
750
            ->shouldReturn([[1], [3], [3], [2]]);
751
752
        $this
753
            ->mapcat(function ($v, $k) {
754
                return [[$k + $v]];
755
            })
756
            ->values()
757
            ->toArray()
758
            ->shouldReturn([[1], [4], [5], [5]]);
759
    }
760
761
    function it_can_take_items_while_predicament_is_true()
762
    {
763
        $this->beConstructedWith([1, 3, 3, 2,]);
764
765
        $this
766
            ->takeWhile(function ($v) {
767
                return $v < 3;
768
            })
769
            ->toArray()
770
            ->shouldReturn([1]);
771
772
        $this
773
            ->takeWhile(function ($v, $k) {
774
                return $k < 2 && $v < 3;
775
            })
776
            ->toArray()
777
            ->shouldReturn([1]);
778
    }
779
780
    function it_can_split_the_collection_at_nth_item()
781
    {
782
        $this->beConstructedWith([1, 3, 3, 2,]);
783
784
        $this->splitAt(2)->size()->shouldBe(2);
785
        $this->splitAt(2)->first()->toArray()->shouldBeLike([1, 3]);
786
        $this->splitAt(2)->second()->toArray()->shouldBeLike([2 => 3, 3 => 2]);
787
    }
788
789
    function it_can_split_the_collection_with_predicament()
790
    {
791
        $this->beConstructedWith([1, 3, 3, 2,]);
792
793
        $s1 = $this->splitWith(function ($v) {
794
            return $v < 3;
795
        });
796
797
        $s1->size()->shouldBe(2);
798
        $s1->first()->toArray()->shouldBe([1]);
799
        $s1->second()->toArray()->shouldBe([1 => 3, 2 => 3, 3 => 2]);
800
801
        $s2 = $this->splitWith(function ($v, $k) {
802
            return $v < 2 && $k < 3;
803
        });
804
805
        $s2->size()->shouldBe(2);
806
        $s2->first()->toArray()->shouldBe([1]);
807
        $s2->second()->toArray()->shouldBe([1 => 3, 2 => 3, 3 => 2]);
808
    }
809
810 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...
811
    {
812
        $this->beConstructedWith([1, 3, 3, 2,]);
813
814
        $this
815
            ->replace([3 => 'a'])
816
            ->toArray()
817
            ->shouldReturn([1, 'a', 'a', 2]);
818
    }
819
820 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...
821
    {
822
        $this->beConstructedWith([1, 3, 3, 2,]);
823
824
        $this
825
            ->reductions(
826
                function ($tmp, $i) {
827
                    return $tmp + $i;
828
                },
829
                0
830
            )
831
            ->toArray()
832
            ->shouldReturn([0, 1, 4, 7, 9]);
833
    }
834
835
    function it_can_return_every_nth_item()
836
    {
837
        $this->beConstructedWith([1, 3, 3, 2,]);
838
839
        $this->takeNth(2)
840
            ->toArray()
841
            ->shouldReturn([1, 2 => 3]);
842
    }
843
844
    function it_can_shuffle_itself()
845
    {
846
        $this->beConstructedWith([1, 3, 3, 2,]);
847
848
        $this
849
            ->shuffle()
850
            ->reduce(
851
                function ($tmp, $i) {
852
                    return $tmp + $i;
853
                },
854
                0
855
            )
856
            ->shouldReturn(9);
857
    }
858
859
    function it_can_partition()
860
    {
861
        $this->beConstructedWith([1, 3, 3, 2,]);
862
863
        $s1 = $this->partition(3, 2, [0, 1]);
864
        $s1->size()->shouldBe(2);
865
        $s1->first()->toArray()->shouldBe([1, 3, 3]);
866
        $s1->second()->toArray()->shouldBe([2 => 3, 3 => 2, 0 => 0]);
867
868
        $s2 = $this->partition(3, 2);
869
        $s2->size()->shouldBe(2);
870
        $s2->first()->toArray()->shouldBe([1, 3, 3]);
871
        $s2->second()->toArray()->shouldBe([2 => 3, 3 => 2]);
872
873
        $s3 = $this->partition(3);
874
        $s3->size()->shouldBe(2);
875
        $s3->first()->toArray()->shouldBe([1, 3, 3]);
876
        $s3->second()->toArray()->shouldBe([3 => 2]);
877
878
        $s4 = $this->partition(1, 3);
879
        $s4->size()->shouldBe(2);
880
        $s4->first()->toArray()->shouldBe([1,]);
881
        $s4->second()->toArray()->shouldBe([3 => 2]);
882
    }
883
884
    function it_can_partition_by()
885
    {
886
        $this->beConstructedWith([1, 3, 3, 2]);
887
888
        $s1 = $this->partitionBy(function ($v) {
889
            return $v % 3 == 0;
890
        });
891
        $s1->size()->shouldBe(3);
892
        $s1->first()->toArray()->shouldBe([1]);
893
        $s1->second()->toArray()->shouldBe([1 => 3, 2 => 3]);
894
        $s1->values()->get(2)->toArray()->shouldBe([3 => 2]);
895
896
        $s2 = $this->partitionBy(function ($v, $k) {
897
            return $k - $v;
898
        });
899
        $s2->size()->shouldBe(4);
900
        $s2->first()->toArray()->shouldBe([1]);
901
        $s2->values()->get(1)->toArray()->shouldBe([1 => 3]);
902
        $s2->values()->get(2)->toArray()->shouldBe([2 => 3]);
903
        $s2->values()->get(3)->toArray()->shouldBe([3 => 2]);
904
    }
905
906
    function it_can_get_nth_value()
907
    {
908
        $this->beConstructedWith([1, 3, 3, 2,]);
909
910
        $this->first(0)->shouldReturn(1);
911
        $this->values()->get(3)->shouldReturn(2);
912
    }
913
914
    function it_can_create_infinite_collection_of_repeated_values()
915
    {
916
        $this->beConstructedThrough('repeat', [1]);
917
        $this->take(3)->toArray()->shouldReturn([1, 1, 1]);
918
    }
919
920
    function it_can_create_finite_collection_of_repeated_values()
921
    {
922
        $this->beConstructedThrough('repeat', [1, 1]);
923
        $this->toArray()->shouldReturn([1]);
924
    }
925
926
    function it_can_create_range_from_value_to_infinity()
927
    {
928
        $this->beConstructedThrough('range', [5]);
929
        $this->take(2)->toArray()->shouldReturn([5, 6]);
930
    }
931
932
    function it_can_create_range_from_value_to_another_value()
933
    {
934
        $this->beConstructedThrough('range', [5, 6]);
935
        $this->take(4)->toArray()->shouldReturn([5, 6]);
936
    }
937
938
    function it_can_check_if_it_is_not_empty()
939
    {
940
        $this->beConstructedWith([1, 3, 3, 2,]);
941
942
        $this->isEmpty()->shouldReturn(false);
943
        $this->isNotEmpty()->shouldReturn(true);
944
    }
945
946
    function it_can_check_if_it_is_empty()
947
    {
948
        $this->beConstructedWith([]);
949
950
        $this->isEmpty()->shouldReturn(true);
951
        $this->isNotEmpty()->shouldReturn(false);
952
    }
953
954
    function it_can_check_frequency_of_distinct_items()
955
    {
956
        $this->beConstructedWith([1, 3, 3, 2,]);
957
958
        $this
959
            ->frequencies()
960
            ->toArray()
961
            ->shouldReturn([1 => 1, 3 => 2, 2 => 1]);
962
    }
963
964 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...
965
    {
966
        $this->beConstructedWith([1, [2], 3]);
967
        $this->first()->shouldReturn(1);
968
        $this->drop(1)->first()->shouldReturn([2]);
969
        $this->drop(1)->first(true)->toArray()->shouldReturn([2]);
970
    }
971
972
    function it_will_throw_when_trying_to_get_first_item_of_empty_collection()
973
    {
974
        $this->beConstructedWith([]);
975
        $this->shouldThrow(ItemNotFound::class)->during('first');
976
    }
977
978 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...
979
    {
980
        $this->beConstructedWith([1, [2], 3]);
981
        $this->last()->shouldReturn(3);
982
        $this->take(2)->last()->shouldReturn([2]);
983
        $this->take(2)->last(true)->toArray()->shouldReturn([2]);
984
    }
985
986
    function it_will_throw_when_trying_to_get_last_item_of_empty_collection()
987
    {
988
        $this->beConstructedWith([]);
989
        $this->shouldThrow(ItemNotFound::class)->during('last');
990
    }
991
992
    function it_can_realize_the_collection(PlusOneAdder $adder)
993
    {
994
        $adder->dynamicMethod(1)->willReturn(2);
995
        $adder->dynamicMethod(2)->willReturn(3);
996
997
        $this->beConstructedWith(function () use ($adder) {
998
            yield $adder->dynamicMethod(1);
999
            yield $adder->dynamicMethod(2);
1000
        });
1001
1002
        $this->realize();
1003
    }
1004
1005
    function it_can_combine_the_collection()
1006
    {
1007
        $this->beConstructedWith(['a', 'b']);
1008
        $this->combine([1, 2])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
1009
        $this->combine([1])->toArray()->shouldReturn(['a' => 1]);
1010
        $this->combine([1, 2, 3])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
1011
    }
1012
1013
    function it_can_get_second_item()
1014
    {
1015
        $this->beConstructedWith([1, 2]);
1016
        $this->second()->shouldReturn(2);
1017
    }
1018
1019
    function it_throws_when_trying_to_get_non_existent_second_item()
1020
    {
1021
        $this->beConstructedWith([1]);
1022
        $this->shouldThrow(ItemNotFound::class)->during('second');
1023
    }
1024
1025
    function it_can_drop_item_by_key()
1026
    {
1027
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
1028
        $this->except(['a', 'b'])->toArray()->shouldReturn([]);
1029
    }
1030
1031
    function it_can_get_the_difference_between_collections()
1032
    {
1033
        $this->beConstructedWith([1, 2, 3, 4]);
1034
        $this->diff([1, 2])->toArray()->shouldReturn([2 => 3, 3 => 4]);
1035
        $this->diff([1, 2], [3])->toArray()->shouldReturn([3 => 4]);
1036
    }
1037
1038
    function it_can_flip_the_collection()
1039
    {
1040
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
1041
        $this->flip()->toArray()->shouldReturn([1 => 'a', 2 => 'b']);
1042
    }
1043
1044
    function it_can_check_if_key_exits()
1045
    {
1046
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
1047
        $this->has('a')->shouldReturn(true);
1048
        $this->has('x')->shouldReturn(false);
1049
    }
1050
1051
    function it_filters_by_keys()
1052
    {
1053
        $this->beConstructedWith(['a' => 1, 'b' => 2, 'c' => 3]);
1054
        $this->only(['a', 'b'])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
1055
        $this->only(['a', 'b', 'x'])->toArray()->shouldReturn(['a' => 1, 'b' => 2]);
1056
    }
1057
1058
    function it_can_serialize_and_unserialize()
1059
    {
1060
        $original = Collection::from([1, 2, 3])->take(2);
1061
        $this->beConstructedWith([1, 2, 3, 4]);
1062
        $this->shouldHaveType(Serializable::class);
1063
        $this->unserialize($original->serialize());
1064
        $this->toArray()->shouldReturn([1, 2]);
1065
    }
1066
1067
    function it_can_zip()
1068
    {
1069
        $this->beConstructedWith([1, 2, 3]);
1070
        $this->zip(['a' => 1, 'b' => 2, 'c' => 4])
1071
            ->map('\DusanKasan\Knapsack\toArray')
1072
            ->toArray()
1073
            ->shouldReturn([[1, 'a' => 1], [1 => 2, 'b' => 2], [2 => 3, 'c' => 4]]);
1074
1075
        $this->zip([4, 5, 6], [7, 8, 9])
1076
            ->map('\DusanKasan\Knapsack\values')
1077
            ->map('\DusanKasan\Knapsack\toArray')
1078
            ->toArray()
1079
            ->shouldReturn([[1, 4, 7], [2, 5, 8], [3, 6, 9]]);
1080
1081
        $this->zip([4, 5])
1082
            ->map('\DusanKasan\Knapsack\values')
1083
            ->map('\DusanKasan\Knapsack\toArray')
1084
            ->toArray()
1085
            ->shouldReturn([[1, 4], [2, 5]]);
1086
    }
1087
1088
    function it_can_use_callable_as_transformer()
1089
    {
1090
        $this->beConstructedWith([1, 2, 3]);
1091
        $this
1092
            ->transform(function (Collection $collection) {
1093
                return $collection->map('\DusanKasan\Knapsack\increment');
1094
            })
1095
            ->toArray()
1096
            ->shouldReturn([2, 3, 4]);
1097
1098
        $this
1099
            ->shouldThrow(InvalidReturnValue::class)
1100
            ->during(
1101
                'transform',
1102
                [
1103
                    function (Collection $collection) {
1104
                        return $collection->first();
1105
                    },
1106
                ]
1107
            );
1108
    }
1109
1110
    function it_can_extract_data_from_nested_collections()
1111
    {
1112
        $input = [
1113
            [
1114
                'a' => [
1115
                    'b' => 1
1116
                ]
1117
            ],
1118
            [
1119
                'a' => [
1120
                    'b' => 2
1121
                ]
1122
            ],
1123
            [
1124
                '*' => [
1125
                    'b' => 3
1126
                ]
1127
            ],
1128
            [
1129
                '.' => [
1130
                    'b' => 4
1131
                ],
1132
                'c' => [
1133
                    'b' => 5
1134
                ],
1135
                [
1136
                    'a'
1137
                ]
1138
            ]
1139
        ];
1140
        $this->beConstructedWith($input);
1141
1142
        $this->extract('')->toArray()->shouldReturn($input);
1143
        $this->extract('a.b')->toArray()->shouldReturn([1, 2]);
1144
        $this->extract('*.b')->toArray()->shouldReturn([1, 2, 3, 4, 5]);
1145
        $this->extract('\*.b')->toArray()->shouldReturn([3]);
1146
        $this->extract('\..b')->toArray()->shouldReturn([4]);
1147
    }
1148
1149
    function it_can_get_the_intersect_of_collections()
1150
    {
1151
        $this->beConstructedWith([1, 2, 3]);
1152
        $this->intersect([1, 2])->values()->toArray()->shouldReturn([1, 2]);
1153
        $this->intersect([1], [3])->values()->toArray()->shouldReturn([1, 3]);
1154
    }
1155
1156 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...
1157
    {
1158
        $this->beConstructedWith([1, 2]);
1159
        $this->sizeIs(2)->shouldReturn(true);
1160
        $this->sizeIs(3)->shouldReturn(false);
1161
        $this->sizeIs(0)->shouldReturn(false);
1162
    }
1163
1164 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...
1165
    {
1166
        $this->beConstructedWith([1, 2]);
1167
        $this->sizeIsLessThan(0)->shouldReturn(false);
1168
        $this->sizeIsLessThan(2)->shouldReturn(false);
1169
        $this->sizeIsLessThan(3)->shouldReturn(true);
1170
    }
1171
1172 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...
1173
    {
1174
        $this->beConstructedWith([1, 2]);
1175
        $this->sizeIsGreaterThan(2)->shouldReturn(false);
1176
        $this->sizeIsGreaterThan(1)->shouldReturn(true);
1177
        $this->sizeIsGreaterThan(0)->shouldReturn(true);
1178
    }
1179
1180
    function it_can_check_if_size_is_between_n_and_m()
1181
    {
1182
        $this->beConstructedWith([1, 2]);
1183
        $this->sizeIsBetween(1, 3)->shouldReturn(true);
1184
        $this->sizeIsBetween(3, 4)->shouldReturn(false);
1185
        $this->sizeIsBetween(0, 0)->shouldReturn(false);
1186
        $this->sizeIsBetween(3, 1)->shouldReturn(true);
1187
    }
1188
1189
    function it_can_sum_the_collection()
1190
    {
1191
        $this->beConstructedWith([1, 2, 3, 4]);
1192
        $this->sum()->shouldReturn(10);
1193
        $this->append(1.5)->sum()->shouldReturn(11.5);
1194
    }
1195
1196
    function it_can_get_average_of_the_collection()
1197
    {
1198
        $this->beConstructedWith([1, 2, 2, 3]);
1199
        $this->average()->shouldReturn(2);
1200
        $this->append(3)->average()->shouldReturn(2.2);
1201
    }
1202
1203
    function it_will_return_zero_when_average_is_called_on_empty_collection()
1204
    {
1205
        $this->beConstructedWith([]);
1206
        $this->average()->shouldReturn(0);
1207
    }
1208
1209
    function it_can_get_maximal_value_in_the_colleciton()
1210
    {
1211
        $this->beConstructedWith([1, 2, 3, 2]);
1212
        $this->max()->shouldReturn(3);
1213
    }
1214
1215
    function it_will_return_null_when_max_is_called_on_empty_collection()
1216
    {
1217
        $this->beConstructedWith([]);
1218
        $this->max()->shouldReturn(null);
1219
    }
1220
1221
    function it_can_get_min_value_in_the_colleciton()
1222
    {
1223
        $this->beConstructedWith([2, 1, 3, 2]);
1224
        $this->min()->shouldReturn(1);
1225
    }
1226
1227
    function it_will_return_null_when_min_is_called_on_empty_collection()
1228
    {
1229
        $this->beConstructedWith([]);
1230
        $this->min()->shouldReturn(null);
1231
    }
1232
1233
    function it_can_convert_the_collection_to_string()
1234
    {
1235
        $this->beConstructedWith([2, 'a', 3, null]);
1236
        $this->toString()->shouldReturn('2a3');
1237
    }
1238
1239
    function it_can_replace_by_key()
1240
    {
1241
        $this->beConstructedWith(['a' => 1, 'b' => 2, 'c' => 3]);
1242
        $this->replaceByKeys(['b' => 3])->toArray()->shouldReturn(['a' => 1, 'b' => 3, 'c' => 3]);
1243
    }
1244
1245
    function it_can_transpose_collections_of_collections()
1246
    {
1247
        $this->beConstructedWith([
1248
            new Collection([1, 2, 3]),
1249
            new Collection([4, 5, new Collection(['foo', 'bar'])]),
1250
            new Collection([7, 8, 9]),
1251
        ]);
1252
1253
        $this->transpose()->toArray()->shouldBeLike([
1254
            new Collection([1, 4, 7]),
1255
            new Collection([2, 5, 8]),
1256
            new Collection([3, new Collection(['foo', 'bar']), 9]),
1257
        ]);
1258
    }
1259
1260
    function it_can_transpose_arrays_of_different_lengths()
1261
    {
1262
        $this->beConstructedWith([
1263
            new Collection(['a', 'b', 'c', 'd']),
1264
            new Collection(['apple', 'box', 'car']),
1265
        ]);
1266
1267
        $this->transpose()->toArray()->shouldBeLike([
1268
            new Collection(['a', 'apple']),
1269
            new Collection(['b', 'box']),
1270
            new Collection(['c', 'car']),
1271
            new Collection(['d', null]),
1272
        ]);
1273
    }
1274
1275
    function it_should_throw_an_invalid_argument_if_collection_items_are_not_collection()
1276
    {
1277
        $this->beConstructedWith([
1278
            [1, 2, 3],
1279
            [4, 5, 6],
1280
            [7, 8, 9],
1281
        ]);
1282
1283
        $this->shouldThrow(InvalidArgument::class)->during('transpose');
1284
    }
1285
1286
    function it_can_use_the_utility_methods()
1287
    {
1288
        $this->beConstructedWith([1, 3, 2]);
1289
1290
        $this
1291
            ->sort('\DusanKasan\Knapsack\compare')
1292
            ->values()
1293
            ->toArray()
1294
            ->shouldReturn([1, 2, 3]);
1295
1296
        $this
1297
            ->map('\DusanKasan\Knapsack\compare')
1298
            ->toArray()
1299
            ->shouldReturn([1, 1, 0]);
1300
1301
        $this
1302
            ->map('\DusanKasan\Knapsack\decrement')
1303
            ->toArray()
1304
            ->shouldReturn([0, 2, 1]);
1305
    }
1306
1307
    function it_can_dump_the_collection()
1308
    {
1309
        $this->beConstructedWith(
1310
            [
1311
                [
1312
                    [1, [2], 3],
1313
                    ['a' => 'b'],
1314
                    new ArrayIterator([1, 2, 3])
1315
                ],
1316
                [1, 2, 3],
1317
                new ArrayIterator(['a', 'b', 'c']),
1318
                true,
1319
                new \DusanKasan\Knapsack\Tests\Helpers\Car('sedan', 5),
1320
                \DusanKasan\Knapsack\concat([1], [1]),
1321
            ]
1322
        );
1323
1324
        $this->dump()->shouldReturn(
1325
            [
1326
                [
1327
                    [1, [2], 3],
1328
                    ['a' => 'b'],
1329
                    [1, 2, 3]
1330
                ],
1331
                [1, 2, 3],
1332
                ['a', 'b', 'c'],
1333
                true,
1334
                [
1335
                    'DusanKasan\Knapsack\Tests\Helpers\Car' => [
1336
                        'numberOfSeats' => 5,
1337
                     ],
1338
1339
                ],
1340
                [1, '0//1' => 1]
1341
            ]
1342
        );
1343
1344
        $this->dump(2)->shouldReturn(
1345
            [
1346
                [
1347
                    [1, [2], '>>>'],
1348
                    ['a' => 'b'],
1349
                    '>>>'
1350
                ],
1351
                [1, 2, '>>>'],
1352
                '>>>'
1353
            ]
1354
        );
1355
1356
        $this->dump(null, 3)->shouldReturn(
1357
            [
1358
                [
1359
                    [1, '^^^', 3],
1360
                    ['a' => 'b'],
1361
                    [1, 2, 3]
1362
                ],
1363
                [1, 2, 3],
1364
                ['a', 'b', 'c'],
1365
                true,
1366
                [
1367
                    'DusanKasan\Knapsack\Tests\Helpers\Car' => [
1368
                        'numberOfSeats' => 5,
1369
                    ],
1370
                ],
1371
                [1, '0//1' => 1]
1372
            ]
1373
        );
1374
1375
        $this->dump(2, 3)->shouldReturn(
1376
            [
1377
                [
1378
                    [1, '^^^', '>>>'],
1379
                    ['a' => 'b'],
1380
                    '>>>'
1381
                ],
1382
                [1, 2, '>>>'],
1383
                '>>>'
1384
            ]
1385
        );
1386
    }
1387
1388
    function it_can_print_dump()
1389
    {
1390
        $this->beConstructedWith([1, [2], 3]);
1391
1392
        ob_start();
1393
        $this->printDump()->shouldReturn($this);
1394
        $this->printDump(2)->shouldReturn($this);
1395
        $this->printDump(2, 2)->shouldReturn($this);
1396
        ob_clean();
1397
    }
1398
}
1399