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
Push — master ( 992b1f...446dee )
by Dušan
02:38
created

tests/spec/CollectionSpec.php (8 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\ItemNotFound;
10
use DusanKasan\Knapsack\Exceptions\NoMoreItems;
11
use DusanKasan\Knapsack\Tests\Helpers\PlusOneAdder;
12
use Iterator;
13
use IteratorAggregate;
14
use PhpSpec\ObjectBehavior;
15
use function DusanKasan\Knapsack\reverse;
16
17
/**
18
 * @mixin Collection
19
 */
20
class CollectionSpec extends ObjectBehavior
21
{
22
    function let()
23
    {
24
        $this->beConstructedWith([1, 3, 3, 2,]);
25
    }
26
27
    function it_is_initializable()
28
    {
29
        $this->shouldHaveType(Collection::class);
30
        $this->shouldHaveType(Iterator::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->shouldThrow(InvalidArgument::class)->during('__construct', [1]);
57
    }
58
59
    function it_can_be_created_statically()
60
    {
61
        $this->beConstructedThrough('from', [[1, 2]]);
62
        $this->toArray()->shouldReturn([1, 2]);
63
    }
64
65
    function it_can_be_created_to_iterate_over_function_infinitely()
66
    {
67
        $this->beConstructedThrough('iterate', [1, function($i) {return $i+1;}]);
68
        $this->take(2)->toArray()->shouldReturn([1, 2]);
69
    }
70
71
    function it_can_be_created_to_iterate_over_function_non_infinitely()
72
    {
73
        $this->beConstructedThrough(
74
            'iterate',
75
            [
76
                1,
77
                function($i) {
78
                    if ($i > 3) {
79
                        throw new NoMoreItems;
80
                    }
81
82
                    return $i+1;
83
                }
84
            ]
85
        );
86
        $this->toArray()->shouldReturn([1, 2, 3, 4]);
87
    }
88
89
    function it_can_be_created_to_repeat_a_value_infinite_times()
90
    {
91
        $this->beConstructedThrough('repeat', [1]);
92
        $this->take(2)->toArray()->shouldReturn([1, 1]);
93
    }
94
95
    function it_can_filter()
96
    {
97
        $this
1 ignored issue
show
The method filter() does not exist on spec\DusanKasan\Knapsack\CollectionSpec. Did you maybe mean it_can_filter()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
98
            ->filter(function ($item) {
99
                return $item > 2;
100
            })
101
            ->toArray()
102
            ->shouldReturn([1 => 3, 2 => 3]);
103
104
        $this
1 ignored issue
show
The method filter() does not exist on spec\DusanKasan\Knapsack\CollectionSpec. Did you maybe mean it_can_filter()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
105
            ->filter(function ($item, $key) {
106
                return $key > 2 && $item < 3;
107
            })
108
            ->toArray()
109
            ->shouldReturn([3 => 2]);
110
    }
111
112
    function it_can_distinct()
113
    {
114
        $this
115
            ->distinct()
116
            ->toArray()
117
            ->shouldReturn([1, 3, 3 => 2]);
118
    }
119
120
    function it_can_concat()
121
    {
122
        $c1 = $this->concat([4, 5]);
123
        $c1->toArray()->shouldReturn([4, 5, 3, 2]);
124
        $c1->size()->shouldReturn(6);
125
    }
126
127
    function it_can_map()
128
    {
129
        $this
130
            ->map(function ($item) {
131
                return $item + 1;
132
            })
133
            ->toArray()
134
            ->shouldReturn([2, 4, 4, 3]);
135
    }
136
137 View Code Duplication
    function it_can_reduce()
138
    {
139
        $this
1 ignored issue
show
The method reduce() does not exist on spec\DusanKasan\Knapsack\CollectionSpec. Did you maybe mean it_can_reduce()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
140
            ->reduce(
141
                function ($temp, $item) {
142
                    return $temp + $item;
143
                },
144
                0
145
            )
146
            ->shouldReturn(9);
147
148
        $this
1 ignored issue
show
The method reduce() does not exist on spec\DusanKasan\Knapsack\CollectionSpec. Did you maybe mean it_can_reduce()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
149
            ->reduce(
150
                function ($temp, $item, $key) {
151
                    return $temp + $key + $item;
152
                },
153
                0
154
            )
155
            ->shouldReturn(15);
156
157
        $this
1 ignored issue
show
The method reduce() does not exist on spec\DusanKasan\Knapsack\CollectionSpec. Did you maybe mean it_can_reduce()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
158
            ->reduce(
159
                function ($temp, $item) {
160
                    return array_merge([$item], $temp);
161
                },
162
                []
163
            )
164
            ->toArray()
165
            ->shouldReturn([2, 3, 3, 1]);
166
    }
167
168
    function it_can_flatten()
169
    {
170
        $this->beConstructedWith([1, [2, [3]]]);
171
        $this->flatten()->values()->toArray()->shouldReturn([1, 2, 3]);
172
        $this->flatten(1)->values()->toArray()->shouldReturn([1, 2, [3]]);
173
    }
174
175
    function it_can_sort()
176
    {
177
        $this->beConstructedWith([3, 1, 2]);
178
179
        $this
180
            ->sort(function ($a, $b) {
181
                return $a > $b;
182
            })
183
            ->toArray()
184
            ->shouldReturn([1 => 1, 2 => 2, 0 => 3]);
185
186
        $this
187
            ->sort(function ($v1, $v2, $k1, $k2) {
188
                return $k1 < $k2 || $v1 == $v2;
189
            })
190
            ->toArray()
191
            ->shouldReturn([2 => 2, 1 => 1, 0 => 3]);
192
    }
193
194
    function it_can_slice()
195
    {
196
        $this->beConstructedWith([1, 2, 3, 4, 5]);
197
198
        $this
199
            ->slice(2, 4)
200
            ->toArray()
201
            ->shouldReturn([2 => 3, 3 => 4]);
202
203
        $this
204
            ->slice(4)
205
            ->toArray()
206
            ->shouldReturn([4 => 5]);
207
    }
208
209
    function it_can_group_by()
210
    {
211
        $this->beConstructedWith([1, 2, 3, 4, 5]);
212
213
        $this
214
            ->groupBy(function ($i) {
215
                return $i % 2;
216
            })
217
            ->toArray()
218
            ->shouldReturn([1 => [1, 3, 5], 0 => [2, 4]]);
219
220
        $this
221
            ->groupBy(function ($k, $i) {
222
                return ($k + $i) % 3;
223
            })
224
            ->toArray()
225
            ->shouldReturn([1 => [1, 4], 0 => [2, 5], 2 => [3]]);
226
    }
227
228
    function it_can_execute_callback_for_each_item(DOMXPath $a)
229
    {
230
        $a->query('asd')->shouldBeCalled();
231
        $this->beConstructedWith([$a]);
232
233
        $this
234
            ->each(function (DOMXPath $i) {
235
                $i->query('asd');
236
            })
237
            ->toArray()
238
            ->shouldReturn([$a]);
239
    }
240
241
    function it_can_get_size()
242
    {
243
        $this->size()->shouldReturn(4);
244
    }
245
246
    function it_can_get_item_by_key()
247
    {
248
        $this->beConstructedWith([1, [2], 3]);
249
        $this->get(0)->shouldReturn(1);
250
        $this->get(1)->first()->shouldReturn(2);
251
        $this->shouldThrow(new ItemNotFound)->during('get', [5]);
252
    }
253
254
    function it_can_get_item_by_key_or_return_default()
255
    {
256
        $this->beConstructedWith([1, [2], 3]);
257
        $this->getOrDefault(0)->shouldReturn(1);
258
        $this->getOrDefault(1)->first()->shouldReturn(2);
259
        $this->getOrDefault(5)->shouldReturn(null);
260
        $this->getOrDefault(5, 'not found')->shouldReturn('not found');
261
    }
262
263
    function it_can_get_nth_item()
264
    {
265
        $this->beConstructedWith([1, [2], 3]);
266
        $this->getNth(0)->shouldReturn(1);
267
        $this->getNth(1)->first()->shouldReturn(2);
268
    }
269
270
    function it_can_find()
271
    {
272
        $this->beConstructedWith([1, 3, 3, 2, [5]]);
273
274
        $this
275
            ->find(function ($v) {
276
                return $v < 3;
277
            })
278
            ->shouldReturn(1);
279
280
        $this
281
            ->find(function ($v, $k) {
282
                return $v < 3 && $k > 1;
283
            })
284
            ->shouldReturn(2);
285
286
        $this
287
            ->find(function ($v) {
288
                return $v < 0;
289
            })
290
            ->shouldReturn(null);
291
292
        $this
293
            ->find(
294
                function ($v) {
295
                    return $v < 0;
296
                },
297
                'not found'
298
            )
299
            ->shouldReturn('not found');
300
301
        $this->find('\DusanKasan\Knapsack\isCollection')->first()->shouldReturn(5);
302
    }
303
304
    function it_can_count_by()
305
    {
306
        $this
307
            ->countBy(function ($v) {
308
                return $v % 2 == 0 ? 'even' : 'odd';
309
            })
310
            ->toArray()
311
            ->shouldReturn(['odd' => 3, 'even' => 1]);
312
313
        $this
314
            ->countBy(function ($k, $v) {
315
                return ($k + $v) % 2 == 0 ? 'even' : 'odd';
316
            })
317
            ->toArray()
318
            ->shouldReturn(['odd' => 3, 'even' => 1]);
319
    }
320
321
    function it_can_index_by()
322
    {
323
        $this
324
            ->indexBy(function ($v) {
325
                return $v;
326
            })
327
            ->toArray()
328
            ->shouldReturn([1 => 1, 3 => 3, 2 => 2]);
329
330
        $this
331
            ->indexBy(function ($v, $k) {
332
                return $k . $v;
333
            })
334
            ->toArray()
335
            ->shouldReturn(['01' => 1, '13' => 3, '23' => 3, '32' => 2]);
336
    }
337
338
    function it_can_check_if_every_item_passes_predicament_test()
339
    {
340
        $this
341
            ->every(function ($v) {
342
                return $v > 0;
343
            })
344
            ->shouldReturn(true);
345
346
        $this
347
            ->every(function ($v) {
348
                return $v > 1;
349
            })
350
            ->shouldReturn(false);
351
352
        $this
353
            ->every(function ($v, $k) {
354
                return $v > 0 && $k >= 0;
355
            })
356
            ->shouldReturn(true);
357
358
        $this
359
            ->every(function ($v, $k) {
360
                return $v > 0 && $k > 0;
361
            })
362
            ->shouldReturn(false);
363
    }
364
365
    function it_can_check_if_some_items_pass_predicament_test()
366
    {
367
        $this
368
            ->some(function ($v) {
369
                return $v < -1;
370
            })
371
            ->shouldReturn(false);
372
373
        $this
374
            ->some(function ($v, $k) {
375
                return $v > 0 && $k < -1;
376
            })
377
            ->shouldReturn(false);
378
379
        $this
380
            ->some(function ($v) {
381
                return $v < 2;
382
            })
383
            ->shouldReturn(true);
384
385
        $this
386
            ->some(function ($v, $k) {
387
                return $v > 0 && $k > 0;
388
            })
389
            ->shouldReturn(true);
390
    }
391
392
    function it_can_check_if_it_contains_a_value()
393
    {
394
        $this
395
            ->contains(3)
396
            ->shouldReturn(true);
397
398
        $this
399
            ->contains(true)
400
            ->shouldReturn(false);
401
    }
402
403
    function it_can_reverse()
404
    {
405
        $it = $this->reverse();
406
407
        $it->rewind();
408
        $it->valid()->shouldReturn(true);
409
        $it->key()->shouldReturn(3);
410
        $it->current()->shouldReturn(2);
411
        $it->next();
412
        $it->valid()->shouldReturn(true);
413
        $it->key()->shouldReturn(2);
414
        $it->current()->shouldReturn(3);
415
        $it->next();
416
        $it->valid()->shouldReturn(true);
417
        $it->key()->shouldReturn(1);
418
        $it->current()->shouldReturn(3);
419
        $it->next();
420
        $it->valid()->shouldReturn(true);
421
        $it->key()->shouldReturn(0);
422
        $it->current()->shouldReturn(1);
423
        $it->next();
424
        $it->valid()->shouldReturn(false);
425
    }
426
427 View Code Duplication
    function it_can_reduce_from_right()
428
    {
429
        $this
430
            ->reduceRight(
431
                function ($temp, $e) {
432
                    return $temp . $e;
433
                },
434
                0
435
            )
436
            ->shouldReturn('02331');
437
438
        $this
439
            ->reduceRight(
440
                function ($temp, $key, $item) {
441
                    return $temp + $key + $item;
442
                },
443
            0
444
            )
445
            ->shouldReturn(15);
446
447
        $this
448
            ->reduceRight(
449
                function ($temp, $item) {
450
                    return array_merge($temp, [$item]);
451
                },
452
                []
453
            )
454
            ->toArray()
455
            ->shouldReturn([2, 3, 3, 1]);
456
    }
457
458
    function it_can_return_only_first_x_elements()
459
    {
460
        $this->take(2)
461
            ->toArray()
462
            ->shouldReturn([1, 3]);
463
    }
464
465
    function it_can_skip_first_x_elements()
466
    {
467
        $this->drop(2)
468
            ->toArray()
469
            ->shouldReturn([2 => 3, 3 => 2]);
470
    }
471
472
    function it_can_return_values()
473
    {
474
        $this->beConstructedWith(['a' => 1, 'b' => 2]);
475
        $this->values()->toArray()->shouldReturn([1, 2]);
476
    }
477
478
479
    function it_can_reject_elements_from_collection()
480
    {
481
        $this
1 ignored issue
show
The method reject() does not exist on spec\DusanKasan\Knapsack\CollectionSpec. Did you maybe mean it_can_reject_elements_from_collection()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
482
            ->reject(function ($v) {
483
                return $v == 3;
484
            })
485
            ->toArray()
486
            ->shouldReturn([1, 3 => 2]);
487
488
        $this
1 ignored issue
show
The method reject() does not exist on spec\DusanKasan\Knapsack\CollectionSpec. Did you maybe mean it_can_reject_elements_from_collection()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
489
            ->reject(function ($v, $k) {
490
                return $k == 2 && $v == 3;
491
            })
492
            ->toArray()
493
            ->shouldReturn([1, 3, 3 => 2]);
494
    }
495
496
    function it_can_get_keys()
497
    {
498
        $this
499
            ->keys()
500
            ->toArray()
501
            ->shouldReturn([0, 1, 2, 3]);
502
    }
503
504 View Code Duplication
    function it_can_interpose()
505
    {
506
        $this
507
            ->interpose('a')
508
            ->values()
509
            ->toArray()
510
            ->shouldReturn([1, 'a', 3, 'a', 3, 'a', 2]);
511
    }
512
513
    function it_can_drop_elements_from_end_of_the_collection()
514
    {
515
        $this
516
            ->dropLast()
517
            ->toArray()
518
            ->shouldReturn([1, 3, 3]);
519
520
        $this
521
            ->dropLast(2)
522
            ->toArray()
523
            ->shouldReturn([1, 3]);
524
    }
525
526
    function it_can_interleave_elements()
527
    {
528
        $this
529
            ->interleave(['a', 'b', 'c', 'd', 'e'])
530
            ->values()
531
            ->toArray()
532
            ->shouldReturn([1, 'a', 3, 'b', 3, 'c', 2, 'd', 'e']);
533
    }
534
535 View Code Duplication
    function it_can_repeat_items_of_collection_infinitely()
536
    {
537
        $this
538
            ->cycle()
539
            ->take(8)
540
            ->values()
541
            ->toArray()
542
            ->shouldReturn([1, 3, 3, 2, 1, 3, 3, 2]);
543
    }
544
545
    function it_can_prepend_item()
546
    {
547
        $this
548
            ->prepend(1)
549
            ->values()
550
            ->toArray()
551
            ->shouldReturn([1, 1, 3, 3, 2]);
552
    }
553
554
    function it_can_prepend_item_with_key()
555
    {
556
        $this
557
            ->prepend(1, 'a')
558
            ->toArray()
559
            ->shouldReturn(['a' => 1, 0 => 1, 1 => 3, 2 => 3, 3 => 2]);
560
    }
561
562
    function it_can_append_item()
563
    {
564
        $this
565
            ->append(1)
566
            ->values()
567
            ->toArray()
568
            ->shouldReturn([1, 3, 3, 2, 1]);
569
    }
570
571
    function it_can_append_item_with_key()
572
    {
573
        $this
574
            ->append(1, 'a')
575
            ->toArray()
576
            ->shouldReturn([1, 3, 3, 2, 'a' => 1]);
577
    }
578
579
    function it_can_drop_items_while_predicament_is_true()
580
    {
581
        $this
582
            ->dropWhile(function ($v) {
583
                return $v < 3;
584
            })
585
            ->toArray()
586
            ->shouldReturn([1 => 3, 2 => 3, 3 => 2]);
587
588
        $this
589
            ->dropWhile(function ($v, $k) {
590
                return $k < 2 && $v < 3;
591
            })
592
            ->toArray()
593
            ->shouldReturn([1 => 3, 2 => 3, 3 => 2]);
594
    }
595
596
    function it_can_map_and_then_concatenate_a_collection()
597
    {
598
        $this
599
            ->mapcat(function ($v) {
600
                return [[$v]];
601
            })
602
            ->values()
603
            ->toArray()
604
            ->shouldReturn([[1], [3], [3], [2]]);
605
606
        $this
607
            ->mapcat(function ($v, $k) {
608
                return [[$k + $v]];
609
            })
610
            ->values()
611
            ->toArray()
612
            ->shouldReturn([[1], [4], [5], [5]]);
613
    }
614
615
    function it_can_take_items_while_predicament_is_true()
616
    {
617
        $this
618
            ->takeWhile(function ($v) {
619
                return $v < 3;
620
            })
621
            ->toArray()
622
            ->shouldReturn([1]);
623
624
        $this
625
            ->takeWhile(function ($v, $k) {
626
                return $k < 2 && $v < 3;
627
            })
628
            ->toArray()
629
            ->shouldReturn([1]);
630
    }
631
632
    function it_can_split_the_collection_at_nth_item()
633
    {
634
        $this->splitAt(2)->size()->shouldBe(2);
635
        $this->splitAt(2)->first()->toArray()->shouldBeLike([1, 3]);
636
        $this->splitAt(2)->getNth(1)->toArray()->shouldBeLike([2 => 3, 3 => 2]);
637
    }
638
639
    function it_can_split_the_collection_with_predicament()
640
    {
641
        $s1 = $this->splitWith(function ($v) {
642
            return $v < 3;
643
        });
644
645
        $s1->size()->shouldBe(2);
646
        $s1->first()->toArray()->shouldBe([1]);
647
        $s1->getNth(1)->toArray()->shouldBe([1 => 3, 2 => 3, 3 => 2]);
648
649
        $s2 = $this->splitWith(function ($v, $k) {
650
            return $v < 2 && $k < 3;
651
        });
652
653
        $s2->size()->shouldBe(2);
654
        $s2->first()->toArray()->shouldBe([1]);
655
        $s2->getNth(1)->toArray()->shouldBe([1 => 3, 2 => 3, 3 => 2]);
656
    }
657
658
    function it_can_replace_items_by_items_from_another_collection()
659
    {
660
        $this
661
            ->replace([3 => 'a'])
662
            ->toArray()
663
            ->shouldReturn([1, 'a', 'a', 2]);
664
    }
665
666
    function it_can_get_reduction_steps()
667
    {
668
        $this
669
            ->reductions(
670
                function ($tmp, $i) {
671
                    return $tmp + $i;
672
                },
673
                0
674
            )
675
            ->toArray()
676
            ->shouldReturn([0, 1, 4, 7, 9]);
677
    }
678
679
    function it_can_return_every_nth_item()
680
    {
681
        $this->takeNth(2)
682
            ->toArray()
683
            ->shouldReturn([1, 2 => 3]);
684
    }
685
686
    function it_can_shuffle_itself()
687
    {
688
        $this
689
            ->shuffle()
690
            ->reduce(
691
                function ($tmp, $i) {
692
                    return $tmp + $i;
693
                },
694
                0
695
            )
696
            ->shouldReturn(9);
697
    }
698
699
    function it_can_partition()
700
    {
701
        $s1 = $this->partition(3, 2, [0, 1]);
702
        $s1->size()->shouldBe(2);
703
        $s1->first()->toArray()->shouldBe([1, 3, 3]);
704
        $s1->getNth(1)->toArray()->shouldBe([2 => 3, 3 => 2, 0 => 0]);
705
706
        $s2 = $this->partition(3, 2);
707
        $s2->size()->shouldBe(2);
708
        $s2->first()->toArray()->shouldBe([1, 3, 3]);
709
        $s2->getNth(1)->toArray()->shouldBe([2 => 3, 3 => 2]);
710
711
        $s3 = $this->partition(3);
712
        $s3->size()->shouldBe(2);
713
        $s3->first()->toArray()->shouldBe([1, 3, 3]);
714
        $s3->getNth(1)->toArray()->shouldBe([3 => 2]);
715
716
        $s4 = $this->partition(1, 3);
717
        $s4->size()->shouldBe(2);
718
        $s4->first()->toArray()->shouldBe([1,]);
719
        $s4->getNth(1)->toArray()->shouldBe([3 => 2]);
720
    }
721
722
    function it_can_partition_by()
723
    {
724
        $this->beConstructedWith([1, 3, 3, 2]);
725
726
        $s1 = $this->partitionBy(function ($v) {
727
            return $v % 3 == 0;
728
        });
729
        $s1->size()->shouldBe(3);
730
        $s1->first()->toArray()->shouldBe([1]);
731
        $s1->getNth(1)->toArray()->shouldBe([1 => 3, 2 => 3]);
732
        $s1->getNth(2)->toArray()->shouldBe([3 => 2]);
733
734
735
        $s2 = $this->partitionBy(function ($v, $k) {
736
            return $k - $v;
737
        });
738
        $s2->size()->shouldBe(4);
739
        $s2->first()->toArray()->shouldBe([1]);
740
        $s2->getNth(1)->toArray()->shouldBe([1 => 3]);
741
        $s2->getNth(2)->toArray()->shouldBe([2 => 3]);
742
        $s2->getNth(3)->toArray()->shouldBe([3 => 2]);
743
    }
744
745
    function it_can_get_nth_value()
746
    {
747
        $this->getNth(0)->shouldReturn(1);
748
        $this->getNth(3)->shouldReturn(2);
749
    }
750
751
    function it_can_pluck()
752
    {
753
        $this->beConstructedWith([['a' => 1], ['a' => 2,  'b' => 3]]);
754
        $this->pluck('a')->values()->toArray()->shouldReturn([1, 2]);
755
    }
756
757
    function it_can_create_infinite_collection_of_repeated_values()
758
    {
759
        $this->beConstructedThrough('repeat', [1]);
760
        $this->take(3)->toArray()->shouldReturn([1, 1, 1]);
761
    }
762
763
    function it_can_create_finite_collection_of_repeated_values()
764
    {
765
        $this->beConstructedThrough('repeat', [1, 1]);
766
        $this->toArray()->shouldReturn([1]);
767
    }
768
769
    function it_can_create_range_from_value_to_infinity()
770
    {
771
        $this->beConstructedThrough('range', [5]);
772
        $this->take(2)->toArray()->shouldReturn([5, 6]);
773
    }
774
775
    function it_can_create_range_from_value_to_another_value()
776
    {
777
        $this->beConstructedThrough('range', [5, 6]);
778
        $this->take(4)->toArray()->shouldReturn([5, 6]);
779
    }
780
781
    function it_can_check_if_it_is_not_empty()
782
    {
783
        $this->isEmpty()->shouldReturn(false);
784
        $this->isNotEmpty()->shouldReturn(true);
785
    }
786
787
    function it_can_check_if_it_is_empty()
788
    {
789
        $this->beConstructedWith([]);
790
791
        $this->isEmpty()->shouldReturn(true);
792
        $this->isNotEmpty()->shouldReturn(false);
793
    }
794
795
    function it_can_check_frequency_of_distinct_items()
796
    {
797
        $this
798
            ->frequencies()
799
            ->toArray()
800
            ->shouldReturn([1 => 1, 3 => 2, 2 => 1]);
801
    }
802
803
    function it_can_get_first_item()
804
    {
805
        $this->beConstructedWith([1, [2], 3]);
806
        $this->first()->shouldReturn(1);
807
        $this->drop(1)->first()->toArray()->shouldReturn([2]);
808
    }
809
810
    function it_will_throw_when_trying_to_get_first_item_of_empty_collection()
811
    {
812
        $this->beConstructedWith([]);
813
        $this->shouldThrow(ItemNotFound::class)->during('first');
814
    }
815
816
    function it_can_get_last_item()
817
    {
818
        $this->beConstructedWith([1, [2], 3]);
819
        $this->last()->shouldReturn(3);
820
        $this->take(2)->last()->toArray()->shouldReturn([2]);
821
    }
822
823
    function it_will_throw_when_trying_to_get_last_item_of_empty_collection()
824
    {
825
        $this->beConstructedWith([]);
826
        $this->shouldThrow(ItemNotFound::class)->during('last');
827
    }
828
829
    function it_can_realize_the_collection(PlusOneAdder $adder)
830
    {
831
        $adder->dynamicMethod(1)->willReturn(2);
832
        $adder->dynamicMethod(2)->willReturn(3);
833
834
        $this->beConstructedWith(function () use ($adder) {
835
            yield $adder->dynamicMethod(1);
836
            yield $adder->dynamicMethod(2);
837
        });
838
839
        $this->realize();
1 ignored issue
show
The method realize() does not exist on spec\DusanKasan\Knapsack\CollectionSpec. Did you maybe mean it_can_realize_the_collection()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
840
    }
841
}
842