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
Push — develop ( 7af216...ebf010 )
by Baptiste
13:17
created

LazyTest::testDropEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Tests\Innmind\Immutable\Sequence;
5
6
use Innmind\Immutable\{
7
    Sequence\Lazy,
0 ignored issues
show
Bug introduced by
The type Innmind\Immutable\Sequence\Lazy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
    Sequence\Implementation,
9
    Map,
10
    Sequence,
11
    Str,
12
    Set,
13
    Exception\OutOfBoundException,
14
    Exception\CannotGroupEmptyStructure,
15
    Exception\ElementNotFound,
16
};
17
use function Innmind\Immutable\unwrap;
18
use PHPUnit\Framework\TestCase;
19
20
class LazyTest extends TestCase
21
{
22
    public function testInterface()
23
    {
24
        $this->assertInstanceOf(
25
            Implementation::class,
26
            new Lazy('mixed', fn() => yield),
27
        );
28
    }
29
30
    public function testGeneratorNotLoadedAtInstanciation()
31
    {
32
        $loaded = false;
33
        $sequence = new Lazy('int', function() use (&$loaded) {
0 ignored issues
show
Unused Code introduced by
The assignment to $sequence is dead and can be removed.
Loading history...
34
            yield 1;
35
            $loaded = true;
36
        });
37
38
        $this->assertFalse($loaded);
39
    }
40
41
    public function testType()
42
    {
43
        $this->assertSame('int', (new Lazy('int', fn() => yield))->type());
44
    }
45
46
    public function testSize()
47
    {
48
        $sequence = new Lazy('int', function() {
49
            yield 1;
50
            yield 1;
51
        });
52
53
        $this->assertSame(2, $sequence->size());
54
        $this->assertSame(2, $sequence->count());
55
    }
56
57
    public function testIterator()
58
    {
59
        $sequence = new Lazy('int', function() {
60
            yield 1;
61
            yield 2;
62
            yield 3;
63
        });
64
65
        $this->assertSame(
66
            [1, 2, 3],
67
            \iterator_to_array($sequence->iterator()),
68
        );
69
    }
70
71
    public function testGet()
72
    {
73
        $sequence = new Lazy('int', function() {
74
            yield 1;
75
            yield 42;
76
            yield 3;
77
        });
78
79
        $this->assertSame(42, $sequence->get(1));
80
    }
81
82
    public function testThrowWhenIndexNotFound()
83
    {
84
        $this->expectException(OutOfBoundException::class);
85
86
        $sequence = new Lazy('int', function() {
87
            if (false) {
88
                yield 1;
89
            }
90
        });
91
92
        $sequence->get(0);
93
    }
94
95
    public function testDiff()
96
    {
97
        $aLoaded = false;
98
        $bLoaded = false;
99
        $a = new Lazy('int', function() use (&$aLoaded) {
100
            yield 1;
101
            yield 2;
102
            $aLoaded = true;
103
        });
104
        $b = new Lazy('int', function() use (&$bLoaded) {
105
            yield 2;
106
            yield 3;
107
            $bLoaded = true;
108
        });
109
        $c = $a->diff($b);
110
111
        $this->assertFalse($aLoaded);
112
        $this->assertFalse($bLoaded);
113
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
114
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
115
        $this->assertInstanceOf(Lazy::class, $c);
116
        $this->assertSame([1], \iterator_to_array($c->iterator()));
117
        $this->assertTrue($aLoaded);
118
        $this->assertTrue($bLoaded);
119
    }
120
121
    public function testDistinct()
122
    {
123
        $loaded = false;
124
        $a = new Lazy('int', function() use (&$loaded) {
125
            yield 1;
126
            yield 2;
127
            yield 1;
128
            $loaded = true;
129
        });
130
        $b = $a->distinct();
131
132
        $this->assertFalse($loaded);
133
        $this->assertSame([1, 2, 1], \iterator_to_array($a->iterator()));
134
        $this->assertInstanceOf(Lazy::class, $b);
135
        $this->assertSame([1, 2], \iterator_to_array($b->iterator()));
136
        $this->assertTrue($loaded);
137
    }
138
139
    public function testDrop()
140
    {
141
        $loaded = false;
142
        $a = new Lazy('int', function() use (&$loaded) {
143
            yield 1;
144
            yield 2;
145
            yield 3;
146
            yield 4;
147
            $loaded = true;
148
        });
149
        $b = $a->drop(2);
150
151
        $this->assertFalse($loaded);
152
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
153
        $this->assertInstanceOf(Lazy::class, $b);
154
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
155
        $this->assertTrue($loaded);
156
    }
157
158
    public function testDropEnd()
159
    {
160
        $a = new Lazy('int', function() {
161
            yield 1;
162
            yield 2;
163
            yield 3;
164
            yield 4;
165
        });
166
        $b = $a->dropEnd(2);
167
168
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
169
        $this->assertInstanceOf(Implementation::class, $b);
170
        $this->assertSame([1, 2], \iterator_to_array($b->iterator()));
171
    }
172
173
    public function testEquals()
174
    {
175
        $a = new Lazy('int', function() {
176
            yield 1;
177
            yield 2;
178
        });
179
        $b = new Lazy('int', function() {
180
            yield 1;
181
            yield 2;
182
            yield 3;
183
        });
184
185
        $this->assertTrue($a->equals($a));
186
        $this->assertFalse($a->equals($b));
187
    }
188
189
    public function testFilter()
190
    {
191
        $loaded = false;
192
        $a = new Lazy('int', function() use (&$loaded) {
193
            yield 1;
194
            yield 2;
195
            yield 3;
196
            yield 4;
197
            $loaded = true;
198
        });
199
        $b = $a->filter(fn($i) => $i % 2 === 0);
200
201
        $this->assertFalse($loaded);
202
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
203
        $this->assertInstanceOf(Lazy::class, $b);
204
        $this->assertSame([2, 4], \iterator_to_array($b->iterator()));
205
        $this->assertTrue($loaded);
206
    }
207
208
    public function testForeach()
209
    {
210
        $sequence = new Lazy('int', function() {
211
            yield 1;
212
            yield 2;
213
            yield 3;
214
            yield 4;
215
        });
216
        $calls = 0;
217
        $sum = 0;
218
219
        $this->assertNull($sequence->foreach(function($i) use (&$calls, &$sum) {
220
            ++$calls;
221
            $sum += $i;
222
        }));
223
        $this->assertSame(4, $calls);
224
        $this->assertSame(10, $sum);
225
    }
226
227
    public function testThrowWhenTryingToGroupEmptySequence()
228
    {
229
        $this->expectException(CannotGroupEmptyStructure::class);
230
231
        $sequence = new Lazy('int', function() {
232
            if (false) {
233
                yield 1;
234
            }
235
        });
236
237
        $sequence->groupBy(fn($i) => $i);
238
    }
239
240
    public function testGroupBy()
241
    {
242
        $sequence = new Lazy('int', function() {
243
            yield 1;
244
            yield 2;
245
            yield 3;
246
            yield 4;
247
        });
248
        $groups = $sequence->groupBy(fn($i) => $i % 2);
249
250
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($sequence->iterator()));
251
        $this->assertInstanceOf(Map::class, $groups);
252
        $this->assertTrue($groups->isOfType('int', Sequence::class));
253
        $this->assertCount(2, $groups);
254
        $this->assertTrue($groups->get(0)->isOfType('int'));
255
        $this->assertTrue($groups->get(1)->isOfType('int'));
256
        $this->assertSame([2, 4], unwrap($groups->get(0)));
257
        $this->assertSame([1, 3], unwrap($groups->get(1)));
258
    }
259
260
    public function testThrowWhenTryingToAccessFirstElementOnEmptySequence()
261
    {
262
        $this->expectException(OutOfBoundException::class);
263
264
        $sequence = new Lazy('int', function() {
265
            if (false) {
266
                yield 1;
267
            }
268
        });
269
270
        $sequence->first();
271
    }
272
273
    public function testThrowWhenTryingToAccessLastElementOnEmptySequence()
274
    {
275
        $this->expectException(OutOfBoundException::class);
276
277
        $sequence = new Lazy('int', function() {
278
            if (false) {
279
                yield 1;
280
            }
281
        });
282
283
        $sequence->last();
284
    }
285
286
    public function testFirst()
287
    {
288
        $sequence = new Lazy('int', function() {
289
            yield 2;
290
            yield 3;
291
            yield 4;
292
        });
293
294
        $this->assertSame(2, $sequence->first());
295
    }
296
297
    public function testLast()
298
    {
299
        $sequence = new Lazy('int', function() {
300
            yield 1;
301
            yield 2;
302
            yield 3;
303
        });
304
305
        $this->assertSame(3, $sequence->last());
306
    }
307
308
    public function testContains()
309
    {
310
        $sequence = new Lazy('int', function() {
311
            yield 1;
312
            yield 2;
313
            yield 3;
314
        });
315
316
        $this->assertTrue($sequence->contains(2));
317
        $this->assertFalse($sequence->contains(4));
318
    }
319
320
    public function testIndexOf()
321
    {
322
        $sequence = new Lazy('int', function() {
323
            yield 1;
324
            yield 2;
325
            yield 4;
326
        });
327
328
        $this->assertSame(1, $sequence->indexOf(2));
329
        $this->assertSame(2, $sequence->indexOf(4));
330
    }
331
332
    public function testThrowWhenTryingToAccessIndexOfUnknownValue()
333
    {
334
        $this->expectException(ElementNotFound::class);
335
336
        $sequence = new Lazy('int', function() {
337
            if (false) {
338
                yield 1;
339
            }
340
        });
341
342
        $sequence->indexOf(1);
343
    }
344
345
    public function testIndices()
346
    {
347
        $loaded = false;
348
        $a = new Lazy('string', function() use (&$loaded) {
349
            yield '1';
350
            yield '2';
351
            $loaded = true;
352
        });
353
        $b = $a->indices();
354
355
        $this->assertFalse($loaded);
356
        $this->assertSame(['1', '2'], \iterator_to_array($a->iterator()));
357
        $this->assertInstanceOf(Lazy::class, $b);
358
        $this->assertSame('int', $b->type());
359
        $this->assertSame([0, 1], \iterator_to_array($b->iterator()));
360
        $this->assertTrue($loaded);
361
    }
362
363
    public function testIndicesOnEmptySequence()
364
    {
365
        $a = new Lazy('string', function() {
366
            if (false) {
367
                yield 1;
368
            }
369
        });
370
        $b = $a->indices();
371
372
        $this->assertSame([], \iterator_to_array($a->iterator()));
373
        $this->assertInstanceOf(Lazy::class, $b);
374
        $this->assertSame('int', $b->type());
375
        $this->assertSame([], \iterator_to_array($b->iterator()));
376
    }
377
378
    public function testMap()
379
    {
380
        $loaded = false;
381
        $a = new Lazy('int', function() use (&$loaded) {
382
            yield 1;
383
            yield 2;
384
            yield 3;
385
            $loaded = true;
386
        });
387
        $b = $a->map(fn($i) => $i * 2);
388
389
        $this->assertFalse($loaded);
390
        $this->assertSame([1, 2, 3], \iterator_to_array($a->iterator()));
391
        $this->assertInstanceOf(Lazy::class, $b);
392
        $this->assertSame([2, 4, 6], \iterator_to_array($b->iterator()));
393
        $this->assertTrue($loaded);
394
    }
395
396
    public function testThrowWhenTryingToModifyTheTypeWhenMapping()
397
    {
398
        $this->expectException(\TypeError::class);
399
        $this->expectExceptionMessage('Argument 1 must be of type int, string given');
400
401
        $sequence = new Lazy('int', function() {
402
            yield 1;
403
            yield 2;
404
            yield 3;
405
        });
406
407
        \iterator_to_array($sequence->map(fn($i) => (string) $i)->iterator());
408
    }
409
410
    public function testPad()
411
    {
412
        $loaded = false;
413
        $a = new Lazy('int', function() use (&$loaded) {
414
            yield 1;
415
            yield 2;
416
            $loaded = true;
417
        });
418
        $b = $a->pad(4, 0);
419
        $c = $a->pad(1, 0);
420
421
        $this->assertFalse($loaded);
422
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
423
        $this->assertInstanceOf(Lazy::class, $b);
424
        $this->assertInstanceOf(Lazy::class, $c);
425
        $this->assertSame([1, 2, 0, 0], \iterator_to_array($b->iterator()));
426
        $this->assertSame([1, 2], \iterator_to_array($c->iterator()));
427
        $this->assertTrue($loaded);
428
    }
429
430
    public function testPartition()
431
    {
432
        $sequence = new Lazy('int', function() {
433
            yield 1;
434
            yield 2;
435
            yield 3;
436
            yield 4;
437
        });
438
        $partition = $sequence->partition(fn($i) => $i % 2 === 0);
439
440
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($sequence->iterator()));
441
        $this->assertInstanceOf(Map::class, $partition);
442
        $this->assertTrue($partition->isOfType('bool', Sequence::class));
443
        $this->assertCount(2, $partition);
444
        $this->assertTrue($partition->get(true)->isOfType('int'));
445
        $this->assertTrue($partition->get(false)->isOfType('int'));
446
        $this->assertSame([2, 4], unwrap($partition->get(true)));
447
        $this->assertSame([1, 3], unwrap($partition->get(false)));
448
    }
449
450
    public function testSlice()
451
    {
452
        $loaded = false;
453
        $a = new Lazy('int', function() use (&$loaded) {
454
            yield 2;
455
            yield 3;
456
            yield 4;
457
            yield 5;
458
            $loaded = true;
459
        });
460
        $b = $a->slice(1, 3);
461
462
        $this->assertFalse($loaded);
463
        $this->assertSame([2, 3, 4, 5], \iterator_to_array($a->iterator()));
464
        $this->assertInstanceOf(Lazy::class, $b);
465
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
466
        $this->assertTrue($loaded);
467
    }
468
469
    public function testSplitAt()
470
    {
471
        $sequence = new Lazy('int', function() {
472
            yield 2;
473
            yield 3;
474
            yield 4;
475
            yield 5;
476
        });
477
        $parts = $sequence->splitAt(2);
478
479
        $this->assertSame([2, 3, 4, 5], \iterator_to_array($sequence->iterator()));
480
        $this->assertInstanceOf(Sequence::class, $parts);
481
        $this->assertTrue($parts->isOfType(Sequence::class));
482
        $this->assertCount(2, $parts);
483
        $this->assertTrue($parts->first()->isOfType('int'));
484
        $this->assertTrue($parts->last()->isOfType('int'));
485
        $this->assertSame([2, 3], unwrap($parts->first()));
486
        $this->assertSame([4, 5], unwrap($parts->last()));
487
    }
488
489
    public function testTake()
490
    {
491
        $loaded = false;
492
        $a = new Lazy('int', function() use (&$loaded) {
493
            yield 2;
494
            yield 3;
495
            yield 4;
496
            $loaded = true;
497
        });
498
        $b = $a->take(2);
499
500
        $this->assertFalse($loaded);
501
        $this->assertSame([2, 3, 4], \iterator_to_array($a->iterator()));
502
        $this->assertInstanceOf(Lazy::class, $b);
503
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
504
        $this->assertTrue($loaded);
505
    }
506
507
    public function testTakeEnd()
508
    {
509
        $a = new Lazy('int', function() {
510
            yield 2;
511
            yield 3;
512
            yield 4;
513
        });
514
        $b = $a->takeEnd(2);
515
516
        $this->assertSame([2, 3, 4], \iterator_to_array($a->iterator()));
517
        $this->assertInstanceOf(Implementation::class, $b);
518
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
519
    }
520
521
    public function testAppend()
522
    {
523
        $aLoaded = false;
524
        $bLoaded = false;
525
        $a = new Lazy('int', function() use (&$aLoaded) {
526
            yield 1;
527
            yield 2;
528
            $aLoaded = true;
529
        });
530
        $b = new Lazy('int', function() use (&$bLoaded) {
531
            yield 3;
532
            yield 4;
533
            $bLoaded = true;
534
        });
535
        $c = $a->append($b);
536
537
        $this->assertFalse($aLoaded);
538
        $this->assertFalse($bLoaded);
539
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
540
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
541
        $this->assertInstanceOf(Lazy::class, $c);
542
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($c->iterator()));
543
        $this->assertTrue($aLoaded);
544
        $this->assertTrue($bLoaded);
545
    }
546
547
    public function testIntersect()
548
    {
549
        $aLoaded = false;
550
        $bLoaded = false;
551
        $a = new Lazy('int', function() use (&$aLoaded) {
552
            yield 1;
553
            yield 2;
554
            $aLoaded = true;
555
        });
556
        $b = new Lazy('int', function() use (&$bLoaded) {
557
            yield 2;
558
            yield 3;
559
            $bLoaded = true;
560
        });
561
        $c = $a->intersect($b);
562
563
        $this->assertFalse($aLoaded);
564
        $this->assertFalse($bLoaded);
565
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
566
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
567
        $this->assertInstanceOf(Lazy::class, $c);
568
        $this->assertSame([2], \iterator_to_array($c->iterator()));
569
        $this->assertTrue($aLoaded);
570
        $this->assertTrue($bLoaded);
571
    }
572
573
    public function testAdd()
574
    {
575
        $loaded = false;
576
        $a = new Lazy('int', function() use (&$loaded) {
577
            yield 1;
578
            $loaded = true;
579
        });
580
        $b = ($a)(2);
581
582
        $this->assertFalse($loaded);
583
        $this->assertSame([1], \iterator_to_array($a->iterator()));
584
        $this->assertInstanceOf(Lazy::class, $b);
585
        $this->assertSame([1, 2], \iterator_to_array($b->iterator()));
586
        $this->assertTrue($loaded);
587
    }
588
589
    public function testSort()
590
    {
591
        $a = new Lazy('int', function() {
592
            yield 1;
593
            yield 4;
594
            yield 3;
595
            yield 2;
596
        });
597
        $b = $a->sort(fn($a, $b) => $a > $b);
598
599
        $this->assertSame([1, 4, 3, 2], \iterator_to_array($a->iterator()));
600
        $this->assertInstanceOf(Lazy::class, $b);
601
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($b->iterator()));
602
    }
603
604
    public function testReduce()
605
    {
606
        $sequence = new Lazy('int', function() {
607
            yield 1;
608
            yield 2;
609
            yield 3;
610
            yield 4;
611
        });
612
613
        $this->assertSame(10, $sequence->reduce(0, fn($sum, $i) => $sum + $i));
614
    }
615
616
    public function testClear()
617
    {
618
        $loaded = false;
619
        $a = new Lazy('int', function() use (&$loaded) {
620
            yield 1;
621
            yield 2;
622
            $loaded = true;
623
        });
624
        $b = $a->clear();
625
626
        $this->assertFalse($loaded);
627
        $this->assertInstanceOf(Implementation::class, $b);
628
        $this->assertSame([], \iterator_to_array($b->iterator()));
629
        $this->assertFalse($loaded);
630
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
631
    }
632
633
    public function testReverse()
634
    {
635
        $loaded = false;
636
        $a = new Lazy('int', function() use (&$loaded) {
637
            yield 1;
638
            yield 2;
639
            yield 3;
640
            yield 4;
641
            $loaded = true;
642
        });
643
        $b = $a->reverse();
644
645
        $this->assertFalse($loaded);
646
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
647
        $this->assertInstanceOf(Lazy::class, $b);
648
        $this->assertSame([4, 3, 2, 1], \iterator_to_array($b->iterator()));
649
        $this->assertTrue($loaded);
650
    }
651
652
    public function testEmpty()
653
    {
654
        $aLoaded = false;
655
        $bLoaded = false;
656
        $a = new Lazy('int', function() use (&$aLoaded) {
657
            yield 1;
658
            $aLoaded = true;
659
        });
660
        $b = new Lazy('int', function() use (&$bLoaded) {
661
            if (false) {
662
                yield 1;
663
            }
664
            $bLoaded = true;
665
        });
666
667
        $this->assertFalse($aLoaded);
668
        $this->assertFalse($bLoaded);
669
        $this->assertTrue($b->empty());
670
        $this->assertFalse($a->empty());
671
        $this->assertFalse($aLoaded); // still false as we don't need to load the full iterator to know if it's empty
672
        $this->assertTrue($bLoaded);
673
    }
674
675
    public function testToSequenceOf()
676
    {
677
        $loaded = false;
678
        $sequence = new Lazy('int', function() use (&$loaded) {
679
            yield 1;
680
            yield 2;
681
            yield 3;
682
            $loaded = true;
683
        });
684
        $sequence = $sequence->toSequenceOf('string|int', function($i) {
685
            yield (string) $i;
686
            yield $i;
687
        });
688
689
        $this->assertFalse($loaded);
690
        $this->assertInstanceOf(Sequence::class, $sequence);
691
        $this->assertSame(
692
            ['1', 1, '2', 2, '3', 3],
693
            unwrap($sequence),
694
        );
695
        $this->assertTrue($loaded);
696
    }
697
698
    public function testToSetOf()
699
    {
700
        $sequence = new Lazy('int', function() {
701
            yield 1;
702
            yield 2;
703
            yield 3;
704
        });
705
        $set = $sequence->toSetOf('string|int', function($i) {
706
            yield (string) $i;
707
            yield $i;
708
        });
709
710
        $this->assertInstanceOf(Set::class, $set);
711
        $this->assertSame(
712
            ['1', 1, '2', 2, '3', 3],
713
            unwrap($set),
714
        );
715
    }
716
717
    public function testToMapOf()
718
    {
719
        $sequence = new Lazy('int', function() {
720
            yield 1;
721
            yield 2;
722
            yield 3;
723
        });
724
        $map = $sequence->toMapOf('string', 'int', fn($i) => yield (string) $i => $i);
725
726
        $this->assertInstanceOf(Map::class, $map);
727
        $this->assertCount(3, $map);
728
        $this->assertSame(1, $map->get('1'));
729
        $this->assertSame(2, $map->get('2'));
730
        $this->assertSame(3, $map->get('3'));
731
    }
732
}
733