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 ( ebf010...27e4a8 )
by Baptiste
02:14
created

LazyTest::testThrowWhenYieldingInvalidType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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