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

DeferTest::testClear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 15
rs 9.9
c 1
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\Defer,
0 ignored issues
show
Bug introduced by
The type Innmind\Immutable\Sequence\Defer 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 DeferTest extends TestCase
21
{
22
    public function testInterface()
23
    {
24
        $this->assertInstanceOf(
25
            Implementation::class,
26
            new Defer('mixed', (fn() => yield)()),
27
        );
28
    }
29
30
    public function testGeneratorNotLoadedAtInstanciation()
31
    {
32
        $loaded = false;
33
        $sequence = new Defer('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 Defer('int', (fn() => yield)()))->type());
44
    }
45
46
    public function testSize()
47
    {
48
        $sequence = new Defer('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 Defer('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 Defer('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 Defer('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 Defer('int', (function() use (&$aLoaded) {
100
            yield 1;
101
            yield 2;
102
            $aLoaded = true;
103
        })());
104
        $b = new Defer('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(Defer::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 Defer('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(Defer::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 Defer('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(Defer::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 Defer('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 Defer('int', (function() {
176
            yield 1;
177
            yield 2;
178
        })());
179
        $b = new Defer('int', (function() {
180
            yield 1;
181
            yield 2;
182
            yield 3;
183
        })());
184
        $this->assertTrue($a->equals($a));
185
        $this->assertFalse($a->equals($b));
186
    }
187
188
    public function testFilter()
189
    {
190
        $loaded = false;
191
        $a = new Defer('int', (function() use (&$loaded) {
192
            yield 1;
193
            yield 2;
194
            yield 3;
195
            yield 4;
196
            $loaded = true;
197
        })());
198
        $b = $a->filter(fn($i) => $i % 2 === 0);
199
200
        $this->assertFalse($loaded);
201
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
202
        $this->assertInstanceOf(Defer::class, $b);
203
        $this->assertSame([2, 4], \iterator_to_array($b->iterator()));
204
        $this->assertTrue($loaded);
205
    }
206
207
    public function testForeach()
208
    {
209
        $sequence = new Defer('int', (function() {
210
            yield 1;
211
            yield 2;
212
            yield 3;
213
            yield 4;
214
        })());
215
        $calls = 0;
216
        $sum = 0;
217
218
        $this->assertNull($sequence->foreach(function($i) use (&$calls, &$sum) {
219
            ++$calls;
220
            $sum += $i;
221
        }));
222
        $this->assertSame(4, $calls);
223
        $this->assertSame(10, $sum);
224
    }
225
226
    public function testThrowWhenTryingToGroupEmptySequence()
227
    {
228
        $this->expectException(CannotGroupEmptyStructure::class);
229
230
        $sequence = new Defer('int', (function() {
231
            if (false) {
232
                yield 1;
233
            }
234
        })());
235
236
        $sequence->groupBy(fn($i) => $i);
237
    }
238
239
    public function testGroupBy()
240
    {
241
        $sequence = new Defer('int', (function() {
242
            yield 1;
243
            yield 2;
244
            yield 3;
245
            yield 4;
246
        })());
247
        $groups = $sequence->groupBy(fn($i) => $i % 2);
248
249
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($sequence->iterator()));
250
        $this->assertInstanceOf(Map::class, $groups);
251
        $this->assertTrue($groups->isOfType('int', Sequence::class));
252
        $this->assertCount(2, $groups);
253
        $this->assertTrue($groups->get(0)->isOfType('int'));
254
        $this->assertTrue($groups->get(1)->isOfType('int'));
255
        $this->assertSame([2, 4], unwrap($groups->get(0)));
256
        $this->assertSame([1, 3], unwrap($groups->get(1)));
257
    }
258
259
    public function testThrowWhenTryingToAccessFirstElementOnEmptySequence()
260
    {
261
        $this->expectException(OutOfBoundException::class);
262
263
        $sequence = new Defer('int', (function() {
264
            if (false) {
265
                yield 1;
266
            }
267
        })());
268
269
        $sequence->first();
270
    }
271
272
    public function testThrowWhenTryingToAccessLastElementOnEmptySequence()
273
    {
274
        $this->expectException(OutOfBoundException::class);
275
276
        $sequence = new Defer('int', (function() {
277
            if (false) {
278
                yield 1;
279
            }
280
        })());
281
282
        $sequence->last();
283
    }
284
285
    public function testFirst()
286
    {
287
        $sequence = new Defer('int', (function() {
288
            yield 2;
289
            yield 3;
290
            yield 4;
291
        })());
292
293
        $this->assertSame(2, $sequence->first());
294
    }
295
296
    public function testLast()
297
    {
298
        $sequence = new Defer('int', (function() {
299
            yield 1;
300
            yield 2;
301
            yield 3;
302
        })());
303
304
        $this->assertSame(3, $sequence->last());
305
    }
306
307
    public function testContains()
308
    {
309
        $sequence = new Defer('int', (function() {
310
            yield 1;
311
            yield 2;
312
            yield 3;
313
        })());
314
315
        $this->assertTrue($sequence->contains(2));
316
        $this->assertFalse($sequence->contains(4));
317
    }
318
319
    public function testIndexOf()
320
    {
321
        $sequence = new Defer('int', (function() {
322
            yield 1;
323
            yield 2;
324
            yield 4;
325
        })());
326
327
        $this->assertSame(1, $sequence->indexOf(2));
328
        $this->assertSame(2, $sequence->indexOf(4));
329
    }
330
331
    public function testThrowWhenTryingToAccessIndexOfUnknownValue()
332
    {
333
        $this->expectException(ElementNotFound::class);
334
335
        $sequence = new Defer('int', (function() {
336
            if (false) {
337
                yield 1;
338
            }
339
        })());
340
341
        $sequence->indexOf(1);
342
    }
343
344
    public function testIndices()
345
    {
346
        $loaded = false;
347
        $a = new Defer('string', (function() use (&$loaded) {
348
            yield '1';
349
            yield '2';
350
            $loaded = true;
351
        })());
352
        $b = $a->indices();
353
354
        $this->assertFalse($loaded);
355
        $this->assertSame(['1', '2'], \iterator_to_array($a->iterator()));
356
        $this->assertInstanceOf(Defer::class, $b);
357
        $this->assertSame('int', $b->type());
358
        $this->assertSame([0, 1], \iterator_to_array($b->iterator()));
359
        $this->assertTrue($loaded);
360
    }
361
362
    public function testIndicesOnEmptySequence()
363
    {
364
        $a = new Defer('string', (function() {
365
            if (false) {
366
                yield 1;
367
            }
368
        })());
369
        $b = $a->indices();
370
371
        $this->assertSame([], \iterator_to_array($a->iterator()));
372
        $this->assertInstanceOf(Defer::class, $b);
373
        $this->assertSame('int', $b->type());
374
        $this->assertSame([], \iterator_to_array($b->iterator()));
375
    }
376
377
    public function testMap()
378
    {
379
        $loaded = false;
380
        $a = new Defer('int', (function() use (&$loaded) {
381
            yield 1;
382
            yield 2;
383
            yield 3;
384
            $loaded = true;
385
        })());
386
        $b = $a->map(fn($i) => $i * 2);
387
388
        $this->assertFalse($loaded);
389
        $this->assertSame([1, 2, 3], \iterator_to_array($a->iterator()));
390
        $this->assertInstanceOf(Defer::class, $b);
391
        $this->assertSame([2, 4, 6], \iterator_to_array($b->iterator()));
392
        $this->assertTrue($loaded);
393
    }
394
395
    public function testThrowWhenTryingToModifyTheTypeWhenMapping()
396
    {
397
        $this->expectException(\TypeError::class);
398
        $this->expectExceptionMessage('Argument 1 must be of type int, string given');
399
400
        $sequence = new Defer('int', (function() {
401
            yield 1;
402
            yield 2;
403
            yield 3;
404
        })());
405
406
        \iterator_to_array($sequence->map(fn($i) => (string) $i)->iterator());
407
    }
408
409
    public function testPad()
410
    {
411
        $loaded = false;
412
        $a = new Defer('int', (function() use (&$loaded) {
413
            yield 1;
414
            yield 2;
415
            $loaded = true;
416
        })());
417
        $b = $a->pad(4, 0);
418
        $c = $a->pad(1, 0);
419
420
        $this->assertFalse($loaded);
421
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
422
        $this->assertInstanceOf(Defer::class, $b);
423
        $this->assertInstanceOf(Defer::class, $c);
424
        $this->assertSame([1, 2, 0, 0], \iterator_to_array($b->iterator()));
425
        $this->assertSame([1, 2], \iterator_to_array($c->iterator()));
426
        $this->assertTrue($loaded);
427
    }
428
429
    public function testPartition()
430
    {
431
        $sequence = new Defer('int', (function() {
432
            yield 1;
433
            yield 2;
434
            yield 3;
435
            yield 4;
436
        })());
437
        $partition = $sequence->partition(fn($i) => $i % 2 === 0);
438
439
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($sequence->iterator()));
440
        $this->assertInstanceOf(Map::class, $partition);
441
        $this->assertTrue($partition->isOfType('bool', Sequence::class));
442
        $this->assertCount(2, $partition);
443
        $this->assertTrue($partition->get(true)->isOfType('int'));
444
        $this->assertTrue($partition->get(false)->isOfType('int'));
445
        $this->assertSame([2, 4], unwrap($partition->get(true)));
446
        $this->assertSame([1, 3], unwrap($partition->get(false)));
447
    }
448
449
    public function testSlice()
450
    {
451
        $loaded = false;
452
        $a = new Defer('int', (function() use (&$loaded) {
453
            yield 2;
454
            yield 3;
455
            yield 4;
456
            yield 5;
457
            $loaded = true;
458
        })());
459
        $b = $a->slice(1, 3);
460
461
        $this->assertFalse($loaded);
462
        $this->assertSame([2, 3, 4, 5], \iterator_to_array($a->iterator()));
463
        $this->assertInstanceOf(Defer::class, $b);
464
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
465
        $this->assertTrue($loaded);
466
    }
467
468
    public function testSplitAt()
469
    {
470
        $sequence = new Defer('int', (function() {
471
            yield 2;
472
            yield 3;
473
            yield 4;
474
            yield 5;
475
        })());
476
        $parts = $sequence->splitAt(2);
477
478
        $this->assertSame([2, 3, 4, 5], \iterator_to_array($sequence->iterator()));
479
        $this->assertInstanceOf(Sequence::class, $parts);
480
        $this->assertTrue($parts->isOfType(Sequence::class));
481
        $this->assertCount(2, $parts);
482
        $this->assertTrue($parts->first()->isOfType('int'));
483
        $this->assertTrue($parts->last()->isOfType('int'));
484
        $this->assertSame([2, 3], unwrap($parts->first()));
485
        $this->assertSame([4, 5], unwrap($parts->last()));
486
    }
487
488
    public function testTake()
489
    {
490
        $loaded = false;
491
        $a = new Defer('int', (function() use (&$loaded) {
492
            yield 2;
493
            yield 3;
494
            yield 4;
495
            $loaded = true;
496
        })());
497
        $b = $a->take(2);
498
499
        $this->assertFalse($loaded);
500
        $this->assertSame([2, 3, 4], \iterator_to_array($a->iterator()));
501
        $this->assertInstanceOf(Defer::class, $b);
502
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
503
        $this->assertTrue($loaded);
504
    }
505
506
    public function testTakeEnd()
507
    {
508
        $a = new Defer('int', (function() {
509
            yield 2;
510
            yield 3;
511
            yield 4;
512
        })());
513
        $b = $a->takeEnd(2);
514
515
        $this->assertSame([2, 3, 4], \iterator_to_array($a->iterator()));
516
        $this->assertInstanceOf(Implementation::class, $b);
517
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
518
    }
519
520
    public function testAppend()
521
    {
522
        $aLoaded = false;
523
        $bLoaded = false;
524
        $a = new Defer('int', (function() use (&$aLoaded) {
525
            yield 1;
526
            yield 2;
527
            $aLoaded = true;
528
        })());
529
        $b = new Defer('int', (function() use (&$bLoaded) {
530
            yield 3;
531
            yield 4;
532
            $bLoaded = true;
533
        })());
534
        $c = $a->append($b);
535
536
        $this->assertFalse($aLoaded);
537
        $this->assertFalse($bLoaded);
538
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
539
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
540
        $this->assertInstanceOf(Defer::class, $c);
541
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($c->iterator()));
542
        $this->assertTrue($aLoaded);
543
        $this->assertTrue($bLoaded);
544
    }
545
546
    public function testIntersect()
547
    {
548
        $aLoaded = false;
549
        $bLoaded = false;
550
        $a = new Defer('int', (function() use (&$aLoaded) {
551
            yield 1;
552
            yield 2;
553
            $aLoaded = true;
554
        })());
555
        $b = new Defer('int', (function() use (&$bLoaded) {
556
            yield 2;
557
            yield 3;
558
            $bLoaded = true;
559
        })());
560
        $c = $a->intersect($b);
561
562
        $this->assertFalse($aLoaded);
563
        $this->assertFalse($bLoaded);
564
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
565
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
566
        $this->assertInstanceOf(Defer::class, $c);
567
        $this->assertSame([2], \iterator_to_array($c->iterator()));
568
        $this->assertTrue($aLoaded);
569
        $this->assertTrue($bLoaded);
570
    }
571
572
    public function testAdd()
573
    {
574
        $loaded = false;
575
        $a = new Defer('int', (function() use (&$loaded) {
576
            yield 1;
577
            $loaded = true;
578
        })());
579
        $b = ($a)(2);
580
581
        $this->assertFalse($loaded);
582
        $this->assertSame([1], \iterator_to_array($a->iterator()));
583
        $this->assertInstanceOf(Defer::class, $b);
584
        $this->assertSame([1, 2], \iterator_to_array($b->iterator()));
585
        $this->assertTrue($loaded);
586
    }
587
588
    public function testSort()
589
    {
590
        $a = new Defer('int', (function() {
591
            yield 1;
592
            yield 4;
593
            yield 3;
594
            yield 2;
595
        })());
596
        $b = $a->sort(fn($a, $b) => $a > $b);
597
598
        $this->assertSame([1, 4, 3, 2], \iterator_to_array($a->iterator()));
599
        $this->assertInstanceOf(Defer::class, $b);
600
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($b->iterator()));
601
    }
602
603
    public function testReduce()
604
    {
605
        $sequence = new Defer('int', (function() {
606
            yield 1;
607
            yield 2;
608
            yield 3;
609
            yield 4;
610
        })());
611
612
        $this->assertSame(10, $sequence->reduce(0, fn($sum, $i) => $sum + $i));
613
    }
614
615
    public function testClear()
616
    {
617
        $loaded = false;
618
        $a = new Defer('int', (function() use (&$loaded) {
619
            yield 1;
620
            yield 2;
621
           $loaded = true;
622
        })());
623
        $b = $a->clear();
624
625
        $this->assertFalse($loaded);
626
        $this->assertInstanceOf(Implementation::class, $b);
627
        $this->assertSame([], \iterator_to_array($b->iterator()));
628
        $this->assertFalse($loaded);
629
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
630
    }
631
632
    public function testReverse()
633
    {
634
        $loaded = false;
635
        $a = new Defer('int', (function() use (&$loaded) {
636
            yield 1;
637
            yield 2;
638
            yield 3;
639
            yield 4;
640
            $loaded = true;
641
        })());
642
        $b = $a->reverse();
643
644
        $this->assertFalse($loaded);
645
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
646
        $this->assertInstanceOf(Defer::class, $b);
647
        $this->assertSame([4, 3, 2, 1], \iterator_to_array($b->iterator()));
648
        $this->assertTrue($loaded);
649
    }
650
651
    public function testEmpty()
652
    {
653
        $aLoaded = false;
654
        $bLoaded = false;
655
        $a = new Defer('int', (function() use (&$aLoaded) {
656
            yield 1;
657
            $aLoaded = true;
658
        })());
659
        $b = new Defer('int', (function() use (&$bLoaded) {
660
            if (false) {
661
                yield 1;
662
            }
663
            $bLoaded = true;
664
        })());
665
666
        $this->assertFalse($aLoaded);
667
        $this->assertFalse($bLoaded);
668
        $this->assertTrue($b->empty());
669
        $this->assertFalse($a->empty());
670
        $this->assertFalse($aLoaded); // still false as we don't need to load the full iterator to know if it's empty
671
        $this->assertTrue($bLoaded);
672
    }
673
674
    public function testToSequenceOf()
675
    {
676
        $loaded = false;
677
        $sequence = new Defer('int', (function() use (&$loaded) {
678
            yield 1;
679
            yield 2;
680
            yield 3;
681
            $loaded = true;
682
        })());
683
        $sequence = $sequence->toSequenceOf('string|int', function($i) {
684
            yield (string) $i;
685
            yield $i;
686
        });
687
688
        $this->assertFalse($loaded);
689
        $this->assertInstanceOf(Sequence::class, $sequence);
690
        $this->assertSame(
691
            ['1', 1, '2', 2, '3', 3],
692
            unwrap($sequence),
693
        );
694
        $this->assertTrue($loaded);
695
    }
696
697
    public function testToSetOf()
698
    {
699
        $sequence = new Defer('int', (function() {
700
            yield 1;
701
            yield 2;
702
            yield 3;
703
        })());
704
        $set = $sequence->toSetOf('string|int', function($i) {
705
            yield (string) $i;
706
            yield $i;
707
        });
708
709
        $this->assertInstanceOf(Set::class, $set);
710
        $this->assertSame(
711
            ['1', 1, '2', 2, '3', 3],
712
            unwrap($set),
713
        );
714
    }
715
716
    public function testToMapOf()
717
    {
718
        $sequence = new Defer('int', (function() {
719
            yield 1;
720
            yield 2;
721
            yield 3;
722
        })());
723
        $map = $sequence->toMapOf('string', 'int', fn($i) => yield (string) $i => $i);
724
725
        $this->assertInstanceOf(Map::class, $map);
726
        $this->assertCount(3, $map);
727
        $this->assertSame(1, $map->get('1'));
728
        $this->assertSame(2, $map->get('2'));
729
        $this->assertSame(3, $map->get('3'));
730
    }
731
}
732