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.

DeferTest::testFind()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 41
rs 9.488
1
<?php
2
declare(strict_types = 1);
3
4
namespace Tests\Innmind\Immutable\Sequence;
5
6
use Innmind\Immutable\{
7
    Sequence\Defer,
8
    Sequence\Implementation,
9
    Map,
10
    Sequence,
11
    Str,
12
    Set,
13
    SideEffect,
14
};
15
use PHPUnit\Framework\TestCase;
16
17
class DeferTest extends TestCase
18
{
19
    public function testInterface()
20
    {
21
        $this->assertInstanceOf(
22
            Implementation::class,
23
            new Defer((static fn() => yield)()),
24
        );
25
    }
26
27
    public function testGeneratorNotLoadedAtInstanciation()
28
    {
29
        $loaded = false;
30
        $sequence = new Defer((static function() use (&$loaded) {
0 ignored issues
show
Unused Code introduced by
The assignment to $sequence is dead and can be removed.
Loading history...
31
            yield 1;
32
            $loaded = true;
33
        })());
34
35
        $this->assertFalse($loaded);
36
    }
37
38
    public function testSize()
39
    {
40
        $sequence = new Defer((static function() {
41
            yield 1;
42
            yield 1;
43
        })());
44
45
        $this->assertSame(2, $sequence->size());
46
        $this->assertSame(2, $sequence->count());
47
    }
48
49
    public function testIterator()
50
    {
51
        $sequence = new Defer((static function() {
52
            yield 1;
53
            yield 2;
54
            yield 3;
55
        })());
56
57
        $this->assertSame(
58
            [1, 2, 3],
59
            \iterator_to_array($sequence->iterator()),
60
        );
61
    }
62
63
    public function testGet()
64
    {
65
        $sequence = new Defer((static function() {
66
            yield 1;
67
            yield 42;
68
            yield 3;
69
        })());
70
71
        $this->assertSame(42, $this->get($sequence, 1));
72
    }
73
74
    public function testReturnNothinWhenIndexNotFound()
75
    {
76
        $sequence = new Defer((static function() {
77
            if (false) {
78
                yield 1;
79
            }
80
        })());
81
82
        $this->assertNull($this->get($sequence, 0));
83
    }
84
85
    public function testDiff()
86
    {
87
        $aLoaded = false;
88
        $bLoaded = false;
89
        $a = new Defer((static function() use (&$aLoaded) {
90
            yield 1;
91
            yield 2;
92
            $aLoaded = true;
93
        })());
94
        $b = new Defer((static function() use (&$bLoaded) {
95
            yield 2;
96
            yield 3;
97
            $bLoaded = true;
98
        })());
99
        $c = $a->diff($b);
100
101
        $this->assertFalse($aLoaded);
102
        $this->assertFalse($bLoaded);
103
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
104
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
105
        $this->assertInstanceOf(Defer::class, $c);
106
        $this->assertSame([1], \iterator_to_array($c->iterator()));
107
        $this->assertTrue($aLoaded);
108
        $this->assertTrue($bLoaded);
109
    }
110
111
    public function testDistinct()
112
    {
113
        $loaded = false;
114
        $a = new Defer((static function() use (&$loaded) {
115
            yield 1;
116
            yield 2;
117
            yield 1;
118
            $loaded = true;
119
        })());
120
        $b = $a->distinct();
121
122
        $this->assertFalse($loaded);
123
        $this->assertSame([1, 2, 1], \iterator_to_array($a->iterator()));
124
        $this->assertInstanceOf(Defer::class, $b);
125
        $this->assertSame([1, 2], \iterator_to_array($b->iterator()));
126
        $this->assertTrue($loaded);
127
    }
128
129
    public function testDrop()
130
    {
131
        $loaded = false;
132
        $a = new Defer((static function() use (&$loaded) {
133
            yield 1;
134
            yield 2;
135
            yield 3;
136
            yield 4;
137
            $loaded = true;
138
        })());
139
        $b = $a->drop(2);
140
141
        $this->assertFalse($loaded);
142
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
143
        $this->assertInstanceOf(Defer::class, $b);
144
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
145
        $this->assertTrue($loaded);
146
    }
147
148
    public function testDropEnd()
149
    {
150
        $a = new Defer((static function() {
151
            yield 1;
152
            yield 2;
153
            yield 3;
154
            yield 4;
155
        })());
156
        $b = $a->dropEnd(2);
157
158
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
159
        $this->assertInstanceOf(Implementation::class, $b);
160
        $this->assertSame([1, 2], \iterator_to_array($b->iterator()));
161
    }
162
163
    public function testEquals()
164
    {
165
        $a = new Defer((static function() {
166
            yield 1;
167
            yield 2;
168
        })());
169
        $b = new Defer((static function() {
170
            yield 1;
171
            yield 2;
172
            yield 3;
173
        })());
174
        $this->assertTrue($a->equals($a));
175
        $this->assertFalse($a->equals($b));
176
    }
177
178
    public function testFilter()
179
    {
180
        $loaded = false;
181
        $a = new Defer((static function() use (&$loaded) {
182
            yield 1;
183
            yield 2;
184
            yield 3;
185
            yield 4;
186
            $loaded = true;
187
        })());
188
        $b = $a->filter(static fn($i) => $i % 2 === 0);
189
190
        $this->assertFalse($loaded);
191
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
192
        $this->assertInstanceOf(Defer::class, $b);
193
        $this->assertSame([2, 4], \iterator_to_array($b->iterator()));
194
        $this->assertTrue($loaded);
195
    }
196
197
    public function testForeach()
198
    {
199
        $sequence = new Defer((static function() {
200
            yield 1;
201
            yield 2;
202
            yield 3;
203
            yield 4;
204
        })());
205
        $calls = 0;
206
        $sum = 0;
207
208
        $this->assertInstanceOf(
209
            SideEffect::class,
210
            $sequence->foreach(static function($i) use (&$calls, &$sum) {
211
                ++$calls;
212
                $sum += $i;
213
            }),
214
        );
215
        $this->assertSame(4, $calls);
216
        $this->assertSame(10, $sum);
217
    }
218
219
    public function testGroupEmptySequence()
220
    {
221
        $sequence = new Defer((static function() {
222
            if (false) {
223
                yield 1;
224
            }
225
        })());
226
227
        $this->assertTrue(
228
            $sequence
229
                ->groupBy(static fn($i) => $i)
230
                ->equals(Map::of()),
231
        );
232
    }
233
234
    public function testGroupBy()
235
    {
236
        $sequence = new Defer((static function() {
237
            yield 1;
238
            yield 2;
239
            yield 3;
240
            yield 4;
241
        })());
242
        $groups = $sequence->groupBy(static fn($i) => $i % 2);
243
244
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($sequence->iterator()));
245
        $this->assertInstanceOf(Map::class, $groups);
246
        $this->assertCount(2, $groups);
247
        $this->assertSame([2, 4], $this->get($groups, 0)->toList());
248
        $this->assertSame([1, 3], $this->get($groups, 1)->toList());
249
    }
250
251
    public function testReturnNothingWhenTryingToAccessFirstElementOnEmptySequence()
252
    {
253
        $sequence = new Defer((static function() {
254
            if (false) {
255
                yield 1;
256
            }
257
        })());
258
259
        $this->assertNull(
260
            $sequence->first()->match(
261
                static fn($value) => $value,
262
                static fn() => null,
263
            ),
264
        );
265
    }
266
267
    public function testReturnWhenTryingToAccessLastElementOnEmptySequence()
268
    {
269
        $sequence = new Defer((static function() {
270
            if (false) {
271
                yield 1;
272
            }
273
        })());
274
275
        $this->assertNull(
276
            $sequence->last()->match(
277
                static fn($value) => $value,
278
                static fn() => null,
279
            ),
280
        );
281
    }
282
283
    public function testFirst()
284
    {
285
        $sequence = new Defer((static function() {
286
            yield 2;
287
            yield 3;
288
            yield 4;
289
        })());
290
291
        $this->assertSame(
292
            2,
293
            $sequence->first()->match(
294
                static fn($value) => $value,
295
                static fn() => null,
296
            ),
297
        );
298
    }
299
300
    public function testLast()
301
    {
302
        $sequence = new Defer((static function() {
303
            yield 1;
304
            yield 2;
305
            yield 3;
306
        })());
307
308
        $this->assertSame(
309
            3,
310
            $sequence->last()->match(
311
                static fn($value) => $value,
312
                static fn() => null,
313
            ),
314
        );
315
    }
316
317
    public function testContains()
318
    {
319
        $sequence = new Defer((static function() {
320
            yield 1;
321
            yield 2;
322
            yield 3;
323
        })());
324
325
        $this->assertTrue($sequence->contains(2));
0 ignored issues
show
Bug introduced by
2 of type integer is incompatible with the type Innmind\Immutable\Sequence\T expected by parameter $element of Innmind\Immutable\Sequence\Defer::contains(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

325
        $this->assertTrue($sequence->contains(/** @scrutinizer ignore-type */ 2));
Loading history...
326
        $this->assertFalse($sequence->contains(4));
327
    }
328
329
    public function testIndexOf()
330
    {
331
        $sequence = new Defer((static function() {
332
            yield 1;
333
            yield 2;
334
            yield 4;
335
        })());
336
337
        $this->assertSame(
338
            1,
339
            $sequence->indexOf(2)->match(
0 ignored issues
show
Bug introduced by
2 of type integer is incompatible with the type Innmind\Immutable\Sequence\T expected by parameter $element of Innmind\Immutable\Sequence\Defer::indexOf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

339
            $sequence->indexOf(/** @scrutinizer ignore-type */ 2)->match(
Loading history...
340
                static fn($value) => $value,
341
                static fn() => null,
342
            ),
343
        );
344
        $this->assertSame(
345
            2,
346
            $sequence->indexOf(4)->match(
347
                static fn($value) => $value,
348
                static fn() => null,
349
            ),
350
        );
351
    }
352
353
    public function testReturnNothingWhenTryingToAccessIndexOfUnknownValue()
354
    {
355
        $sequence = new Defer((static function() {
356
            if (false) {
357
                yield 1;
358
            }
359
        })());
360
361
        $this->assertNull(
362
            $sequence->indexOf(1)->match(
0 ignored issues
show
Bug introduced by
1 of type integer is incompatible with the type Innmind\Immutable\Sequence\T expected by parameter $element of Innmind\Immutable\Sequence\Defer::indexOf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

362
            $sequence->indexOf(/** @scrutinizer ignore-type */ 1)->match(
Loading history...
363
                static fn($value) => $value,
364
                static fn() => null,
365
            ),
366
        );
367
    }
368
369
    public function testIndices()
370
    {
371
        $loaded = false;
372
        $a = new Defer((static function() use (&$loaded) {
373
            yield '1';
374
            yield '2';
375
            $loaded = true;
376
        })());
377
        $b = $a->indices();
378
379
        $this->assertFalse($loaded);
380
        $this->assertSame(['1', '2'], \iterator_to_array($a->iterator()));
381
        $this->assertInstanceOf(Defer::class, $b);
382
        $this->assertSame([0, 1], \iterator_to_array($b->iterator()));
383
        $this->assertTrue($loaded);
384
    }
385
386
    public function testIndicesOnEmptySequence()
387
    {
388
        $a = new Defer((static function() {
389
            if (false) {
390
                yield 1;
391
            }
392
        })());
393
        $b = $a->indices();
394
395
        $this->assertSame([], \iterator_to_array($a->iterator()));
396
        $this->assertInstanceOf(Defer::class, $b);
397
        $this->assertSame([], \iterator_to_array($b->iterator()));
398
    }
399
400
    public function testMap()
401
    {
402
        $loaded = false;
403
        $a = new Defer((static function() use (&$loaded) {
404
            yield 1;
405
            yield 2;
406
            yield 3;
407
            $loaded = true;
408
        })());
409
        $b = $a->map(static fn($i) => $i * 2);
410
411
        $this->assertFalse($loaded);
412
        $this->assertSame([1, 2, 3], \iterator_to_array($a->iterator()));
413
        $this->assertInstanceOf(Defer::class, $b);
414
        $this->assertSame([2, 4, 6], \iterator_to_array($b->iterator()));
415
        $this->assertTrue($loaded);
416
    }
417
418
    public function testPad()
419
    {
420
        $loaded = false;
421
        $a = new Defer((static function() use (&$loaded) {
422
            yield 1;
423
            yield 2;
424
            $loaded = true;
425
        })());
426
        $b = $a->pad(4, 0);
0 ignored issues
show
Bug introduced by
0 of type integer is incompatible with the type Innmind\Immutable\Sequence\T expected by parameter $element of Innmind\Immutable\Sequence\Defer::pad(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

426
        $b = $a->pad(4, /** @scrutinizer ignore-type */ 0);
Loading history...
427
        $c = $a->pad(1, 0);
428
429
        $this->assertFalse($loaded);
430
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
431
        $this->assertInstanceOf(Defer::class, $b);
432
        $this->assertInstanceOf(Defer::class, $c);
433
        $this->assertSame([1, 2, 0, 0], \iterator_to_array($b->iterator()));
434
        $this->assertSame([1, 2], \iterator_to_array($c->iterator()));
435
        $this->assertTrue($loaded);
436
    }
437
438
    public function testPartition()
439
    {
440
        $sequence = new Defer((static function() {
441
            yield 1;
442
            yield 2;
443
            yield 3;
444
            yield 4;
445
        })());
446
        $partition = $sequence->partition(static fn($i) => $i % 2 === 0);
447
448
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($sequence->iterator()));
449
        $this->assertInstanceOf(Map::class, $partition);
450
        $this->assertCount(2, $partition);
451
        $this->assertSame([2, 4], $this->get($partition, true)->toList());
452
        $this->assertSame([1, 3], $this->get($partition, false)->toList());
453
    }
454
455
    public function testSlice()
456
    {
457
        $loaded = false;
458
        $a = new Defer((static function() use (&$loaded) {
459
            yield 2;
460
            yield 3;
461
            yield 4;
462
            yield 5;
463
            $loaded = true;
464
        })());
465
        $b = $a->slice(1, 3);
466
467
        $this->assertFalse($loaded);
468
        $this->assertSame([2, 3, 4, 5], \iterator_to_array($a->iterator()));
469
        $this->assertInstanceOf(Defer::class, $b);
470
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
471
        $this->assertTrue($loaded);
472
    }
473
474
    public function testTake()
475
    {
476
        $loaded = false;
477
        $a = new Defer((static function() use (&$loaded) {
478
            yield 2;
479
            yield 3;
480
            yield 4;
481
            $loaded = true;
482
        })());
483
        $b = $a->take(2);
484
485
        $this->assertFalse($loaded);
486
        $this->assertSame([2, 3, 4], \iterator_to_array($a->iterator()));
487
        $this->assertInstanceOf(Defer::class, $b);
488
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
489
        $this->assertTrue($loaded);
490
    }
491
492
    public function testSequenceNotCompletelyLoadedWhenTakingFewerThanItsSize()
493
    {
494
        $loaded = false;
495
        $a = new Defer((static function() use (&$loaded) {
496
            yield 2;
497
            yield 3;
498
            yield 4;
499
            $loaded = true;
500
        })());
501
        $b = $a->take(2);
502
503
        $this->assertFalse($loaded);
504
        $this->assertInstanceOf(Defer::class, $b);
505
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
506
        $this->assertFalse($loaded);
507
    }
508
509
    public function testTakeEnd()
510
    {
511
        $a = new Defer((static function() {
512
            yield 2;
513
            yield 3;
514
            yield 4;
515
        })());
516
        $b = $a->takeEnd(2);
517
518
        $this->assertSame([2, 3, 4], \iterator_to_array($a->iterator()));
519
        $this->assertInstanceOf(Implementation::class, $b);
520
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
521
    }
522
523
    public function testAppend()
524
    {
525
        $aLoaded = false;
526
        $bLoaded = false;
527
        $a = new Defer((static function() use (&$aLoaded) {
528
            yield 1;
529
            yield 2;
530
            $aLoaded = true;
531
        })());
532
        $b = new Defer((static function() use (&$bLoaded) {
533
            yield 3;
534
            yield 4;
535
            $bLoaded = true;
536
        })());
537
        $c = $a->append($b);
538
539
        $this->assertFalse($aLoaded);
540
        $this->assertFalse($bLoaded);
541
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
542
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
543
        $this->assertInstanceOf(Defer::class, $c);
544
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($c->iterator()));
545
        $this->assertTrue($aLoaded);
546
        $this->assertTrue($bLoaded);
547
    }
548
549
    public function testIntersect()
550
    {
551
        $aLoaded = false;
552
        $bLoaded = false;
553
        $a = new Defer((static function() use (&$aLoaded) {
554
            yield 1;
555
            yield 2;
556
            $aLoaded = true;
557
        })());
558
        $b = new Defer((static function() use (&$bLoaded) {
559
            yield 2;
560
            yield 3;
561
            $bLoaded = true;
562
        })());
563
        $c = $a->intersect($b);
564
565
        $this->assertFalse($aLoaded);
566
        $this->assertFalse($bLoaded);
567
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
568
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
569
        $this->assertInstanceOf(Defer::class, $c);
570
        $this->assertSame([2], \iterator_to_array($c->iterator()));
571
        $this->assertTrue($aLoaded);
572
        $this->assertTrue($bLoaded);
573
    }
574
575
    public function testAdd()
576
    {
577
        $loaded = false;
578
        $a = new Defer((static function() use (&$loaded) {
579
            yield 1;
580
            $loaded = true;
581
        })());
582
        $b = ($a)(2);
583
584
        $this->assertFalse($loaded);
585
        $this->assertSame([1], \iterator_to_array($a->iterator()));
586
        $this->assertInstanceOf(Defer::class, $b);
587
        $this->assertSame([1, 2], \iterator_to_array($b->iterator()));
588
        $this->assertTrue($loaded);
589
    }
590
591
    public function testSort()
592
    {
593
        $a = new Defer((static function() {
594
            yield 1;
595
            yield 4;
596
            yield 3;
597
            yield 2;
598
        })());
599
        $b = $a->sort(static fn($a, $b) => $a > $b ? 1 : -1);
600
601
        $this->assertSame([1, 4, 3, 2], \iterator_to_array($a->iterator()));
602
        $this->assertInstanceOf(Defer::class, $b);
603
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($b->iterator()));
604
    }
605
606
    public function testReduce()
607
    {
608
        $sequence = new Defer((static function() {
609
            yield 1;
610
            yield 2;
611
            yield 3;
612
            yield 4;
613
        })());
614
615
        $this->assertSame(10, $sequence->reduce(0, static fn($sum, $i) => $sum + $i));
0 ignored issues
show
Bug introduced by
0 of type integer is incompatible with the type Innmind\Immutable\Sequence\R expected by parameter $carry of Innmind\Immutable\Sequence\Defer::reduce(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

615
        $this->assertSame(10, $sequence->reduce(/** @scrutinizer ignore-type */ 0, static fn($sum, $i) => $sum + $i));
Loading history...
616
    }
617
618
    public function testClear()
619
    {
620
        $loaded = false;
621
        $a = new Defer((static function() use (&$loaded) {
622
            yield 1;
623
            yield 2;
624
            $loaded = true;
625
        })());
626
        $b = $a->clear();
627
628
        $this->assertFalse($loaded);
629
        $this->assertInstanceOf(Implementation::class, $b);
630
        $this->assertSame([], \iterator_to_array($b->iterator()));
631
        $this->assertFalse($loaded);
632
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
633
    }
634
635
    public function testReverse()
636
    {
637
        $loaded = false;
638
        $a = new Defer((static function() use (&$loaded) {
639
            yield 1;
640
            yield 2;
641
            yield 3;
642
            yield 4;
643
            $loaded = true;
644
        })());
645
        $b = $a->reverse();
646
647
        $this->assertFalse($loaded);
648
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
649
        $this->assertInstanceOf(Defer::class, $b);
650
        $this->assertSame([4, 3, 2, 1], \iterator_to_array($b->iterator()));
651
        $this->assertTrue($loaded);
652
    }
653
654
    public function testEmpty()
655
    {
656
        $aLoaded = false;
657
        $bLoaded = false;
658
        $a = new Defer((static function() use (&$aLoaded) {
659
            yield 1;
660
            $aLoaded = true;
661
        })());
662
        $b = new Defer((static function() use (&$bLoaded) {
663
            if (false) {
664
                yield 1;
665
            }
666
            $bLoaded = true;
667
        })());
668
669
        $this->assertFalse($aLoaded);
670
        $this->assertFalse($bLoaded);
671
        $this->assertTrue($b->empty());
672
        $this->assertFalse($a->empty());
673
        $this->assertFalse($aLoaded); // still false as we don't need to load the full iterator to know if it's empty
674
        $this->assertTrue($bLoaded);
675
    }
676
677
    public function testFind()
678
    {
679
        $count = 0;
680
        $sequence = new Defer((static function() use (&$count) {
681
            ++$count;
682
            yield 1;
683
            ++$count;
684
            yield 2;
685
            ++$count;
686
            yield 3;
687
        })());
688
689
        $this->assertSame(
690
            1,
691
            $sequence->find(static fn($i) => $i === 1)->match(
692
                static fn($i) => $i,
693
                static fn() => null,
694
            ),
695
        );
696
        $this->assertSame(1, $count);
697
        $this->assertSame(
698
            2,
699
            $sequence->find(static fn($i) => $i === 2)->match(
700
                static fn($i) => $i,
701
                static fn() => null,
702
            ),
703
        );
704
        $this->assertSame(2, $count);
705
        $this->assertSame(
706
            3,
707
            $sequence->find(static fn($i) => $i === 3)->match(
708
                static fn($i) => $i,
709
                static fn() => null,
710
            ),
711
        );
712
        $this->assertSame(3, $count);
713
714
        $this->assertNull(
715
            $sequence->find(static fn($i) => $i === 0)->match(
716
                static fn($i) => $i,
717
                static fn() => null,
718
            ),
719
        );
720
    }
721
722
    public function get($map, $index)
723
    {
724
        return $map->get($index)->match(
725
            static fn($value) => $value,
726
            static fn() => null,
727
        );
728
    }
729
}
730