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 ( 27e4a8...e4554d )
by Baptiste
02:51 queued 39s
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 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
declare(strict_types = 1);
3
4
namespace Tests\Innmind\Immutable\Set;
5
6
use Innmind\Immutable\{
7
    Set\Lazy,
0 ignored issues
show
Bug introduced by
The type Innmind\Immutable\Set\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
    Set\Implementation,
9
    Set,
10
    Map,
11
    Str,
12
    Sequence,
13
};
14
use function Innmind\Immutable\unwrap;
15
use PHPUnit\Framework\TestCase;
16
17
class LazyTest extends TestCase
18
{
19
    public function testInterface()
20
    {
21
        $this->assertInstanceOf(
22
            Implementation::class,
23
            new Lazy('int', function() {
24
                yield;
25
            }),
26
        );
27
    }
28
29
    public function testThrowWhenYieldingInvalidType()
30
    {
31
        $this->expectException(\TypeError::class);
32
        $this->expectExceptionMessage('Argument 1 must be of type int, string given');
33
34
        $sequence = new Lazy('int', function() {
35
            yield '1';
36
        });
37
        \iterator_to_array($sequence->iterator());
38
    }
39
40
    public function testType()
41
    {
42
        $this->assertSame(
43
            'int',
44
            (new Lazy('int', function() {
45
                yield;
46
            }))->type()
47
        );
48
    }
49
50
    public function testSize()
51
    {
52
        $set = new Lazy('int', function() {
53
            yield 1;
54
            yield 2;
55
        });
56
57
        $this->assertSame(2, $set->size());
58
        $this->assertSame(2, $set->count());
59
    }
60
61
    public function testIterator()
62
    {
63
        $set = new Lazy('int', function() {
64
            yield 1;
65
            yield 2;
66
        });
67
68
        $this->assertSame([1, 2], \iterator_to_array($set->iterator()));
69
    }
70
71
    public function testIntersect()
72
    {
73
        $aLoaded = false;
74
        $bLoaded = false;
75
        $a = new Lazy('int', function() use (&$aLoaded) {
76
            yield 1;
77
            yield 2;
78
            $aLoaded = true;
79
        });
80
        $b = new Lazy('int', function() use (&$bLoaded) {
81
            yield 2;
82
            yield 3;
83
            $bLoaded = true;
84
        });
85
        $c = $a->intersect($b);
86
87
        $this->assertFalse($aLoaded);
88
        $this->assertFalse($bLoaded);
89
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
90
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
91
        $this->assertInstanceOf(Lazy::class, $c);
92
        $this->assertSame([2], \iterator_to_array($c->iterator()));
93
        $this->assertTrue($aLoaded);
94
        $this->assertTrue($bLoaded);
95
    }
96
97
    public function testAdd()
98
    {
99
        $loaded = false;
100
        $a = new Lazy('int', function() use (&$loaded) {
101
            yield 1;
102
            $loaded = true;
103
        });
104
        $b = ($a)(2);
105
106
        $this->assertFalse($loaded);
107
        $this->assertSame([1], \iterator_to_array($a->iterator()));
108
        $this->assertInstanceOf(Lazy::class, $b);
109
        $this->assertSame([1, 2], \iterator_to_array($b->iterator()));
110
        $this->assertSame([1, 2], \iterator_to_array(($b)(2)->iterator()));
111
        $this->assertTrue($loaded);
112
    }
113
114
    public function testContains()
115
    {
116
        $set = new Lazy('int', function() {
117
            yield 1;
118
        });
119
120
        $this->assertTrue($set->contains(1));
121
        $this->assertFalse($set->contains(2));
122
    }
123
124
    public function testRemove()
125
    {
126
        $a = new Lazy('int', function() {
127
            yield 1;
128
            yield 2;
129
            yield 3;
130
            yield 4;
131
        });
132
        $b = $a->remove(3);
133
134
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
135
        $this->assertInstanceOf(Lazy::class, $b);
136
        $this->assertSame([1, 2, 4], \iterator_to_array($b->iterator()));
137
        $this->assertSame($a, $a->remove(5));
138
    }
139
140
    public function testDiff()
141
    {
142
        $aLoaded = false;
143
        $a = new Lazy('int', function() use (&$aLoaded) {
144
            yield 1;
145
            yield 2;
146
            yield 3;
147
            $aLoaded = true;
148
        });
149
        $bLoaded = false;
150
        $b = new Lazy('int', function() use (&$bLoaded) {
151
            yield 2;
152
            yield 4;
153
            $bLoaded = true;
154
        });
155
        $c = $a->diff($b);
156
157
        $this->assertFalse($aLoaded);
158
        $this->assertFalse($bLoaded);
159
        $this->assertSame([1, 2, 3], \iterator_to_array($a->iterator()));
160
        $this->assertSame([2, 4], \iterator_to_array($b->iterator()));
161
        $this->assertInstanceOf(Lazy::class, $c);
162
        $this->assertSame([1, 3], \iterator_to_array($c->iterator()));
163
        $this->assertTrue($aLoaded);
164
        $this->assertTrue($bLoaded);
165
    }
166
167
    public function testEquals()
168
    {
169
        $a = new Lazy('int', function() {
170
            yield 1;
171
            yield 2;
172
        });
173
        $aBis = new Lazy('int', function() {
174
            yield 1;
175
            yield 2;
176
        });
177
        $b = new Lazy('int', function() {
178
            yield 1;
179
        });
180
        $c = new Lazy('int', function() {
181
            yield 1;
182
            yield 2;
183
            yield 3;
184
        });
185
186
        $this->assertTrue($a->equals($a));
187
        $this->assertTrue($a->equals($aBis));
188
        $this->assertFalse($a->equals($b));
189
        $this->assertFalse($a->equals($c));
190
    }
191
192
    public function testFilter()
193
    {
194
        $loaded = false;
195
        $a = new Lazy('int', function() use (&$loaded) {
196
            yield 1;
197
            yield 2;
198
            yield 3;
199
            yield 4;
200
            $loaded = true;
201
        });
202
        $b = $a->filter(fn($i) => $i % 2 === 0);
203
204
        $this->assertFalse($loaded);
205
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
206
        $this->assertInstanceOf(Lazy::class, $b);
207
        $this->assertSame([2, 4], \iterator_to_array($b->iterator()));
208
        $this->assertTrue($loaded);
209
    }
210
211
    public function testForeach()
212
    {
213
        $set = new Lazy('int', function() {
214
            yield 1;
215
            yield 2;
216
            yield 3;
217
            yield 4;
218
        });
219
        $calls = 0;
220
        $sum = 0;
221
222
        $this->assertNull($set->foreach(function($i) use (&$calls, &$sum) {
223
            ++$calls;
224
            $sum += $i;
225
        }));
226
227
        $this->assertSame(4, $calls);
228
        $this->assertSame(10, $sum);
229
    }
230
231
    public function testGroupBy()
232
    {
233
        $set = new Lazy('int', function() {
234
            yield 1;
235
            yield 2;
236
            yield 3;
237
            yield 4;
238
        });
239
        $groups = $set->groupBy(fn($i) => $i % 2);
240
241
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($set->iterator()));
242
        $this->assertInstanceOf(Map::class, $groups);
243
        $this->assertTrue($groups->isOfType('int', Set::class));
244
        $this->assertCount(2, $groups);
245
        $this->assertTrue($groups->get(0)->isOfType('int'));
246
        $this->assertTrue($groups->get(1)->isOfType('int'));
247
        $this->assertSame([2, 4], unwrap($groups->get(0)));
248
        $this->assertSame([1, 3], unwrap($groups->get(1)));
249
    }
250
251
    public function testMap()
252
    {
253
        $loaded = false;
254
        $a = new Lazy('int', function() use (&$loaded) {
255
            yield 1;
256
            yield 2;
257
            yield 3;
258
            $loaded = true;
259
        });
260
        $b = $a->map(fn($i) => $i * 2);
261
262
        $this->assertFalse($loaded);
263
        $this->assertSame([1, 2, 3], \iterator_to_array($a->iterator()));
264
        $this->assertInstanceOf(Lazy::class, $b);
265
        $this->assertSame([2, 4, 6], \iterator_to_array($b->iterator()));
266
        $this->assertTrue($loaded);
267
    }
268
269
    public function testThrowWhenTryingToModifyTheTypeWhenMapping()
270
    {
271
        $this->expectException(\TypeError::class);
272
        $this->expectExceptionMessage('Argument 1 must be of type int, string given');
273
274
        $set = new Lazy('int', function() {
275
            yield 1;
276
        });
277
278
        \iterator_to_array($set->map(fn($i) => (string) $i)->iterator());
279
    }
280
281
    public function testPartition()
282
    {
283
        $set = new Lazy('int', function() {
284
            yield 1;
285
            yield 2;
286
            yield 3;
287
            yield 4;
288
        });
289
        $groups = $set->partition(fn($i) => $i % 2 === 0);
290
291
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($set->iterator()));
292
        $this->assertInstanceOf(Map::class, $groups);
293
        $this->assertTrue($groups->isOfType('bool', Set::class));
294
        $this->assertCount(2, $groups);
295
        $this->assertTrue($groups->get(true)->isOfType('int'));
296
        $this->assertTrue($groups->get(false)->isOfType('int'));
297
        $this->assertSame([2, 4], unwrap($groups->get(true)));
298
        $this->assertSame([1, 3], unwrap($groups->get(false)));
299
    }
300
301
    public function testSort()
302
    {
303
        $loaded = false;
304
        $set = new Lazy('int', function() use (&$loaded) {
305
            yield 1;
306
            yield 4;
307
            yield 3;
308
            yield 2;
309
            $loaded = true;
310
        });
311
        $sorted = $set->sort(fn($a, $b) => $a > $b);
312
313
        $this->assertFalse($loaded);
314
        $this->assertSame([1, 4, 3, 2], \iterator_to_array($set->iterator()));
315
        $this->assertInstanceOf(Sequence::class, $sorted);
316
        $this->assertSame([1, 2, 3, 4], unwrap($sorted));
317
        $this->assertTrue($loaded);
318
    }
319
320
    public function testMerge()
321
    {
322
        $aLoaded = false;
323
        $a = new Lazy('int', function() use (&$aLoaded) {
324
            yield 1;
325
            yield 2;
326
            $aLoaded = true;
327
        });
328
        $bLoaded = false;
329
        $b = new Lazy('int', function() use (&$bLoaded) {
330
            yield 2;
331
            yield 3;
332
            $bLoaded = true;
333
        });
334
        $c = $a->merge($b);
335
336
        $this->assertFalse($aLoaded);
337
        $this->assertFalse($bLoaded);
338
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
339
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
340
        $this->assertInstanceOf(Lazy::class, $c);
341
        $this->assertSame([1, 2, 3], \iterator_to_array($c->iterator()));
342
        $this->assertTrue($aLoaded);
343
        $this->assertTrue($bLoaded);
344
    }
345
346
    public function testReduce()
347
    {
348
        $set = new Lazy('int', function() {
349
            yield 1;
350
            yield 2;
351
            yield 3;
352
            yield 4;
353
        });
354
355
        $this->assertSame(10, $set->reduce(0, fn($sum, $i) => $sum + $i));
356
    }
357
358
    public function testClear()
359
    {
360
        $a = new Lazy('int', function() {
361
            yield 1;
362
        });
363
        $b = $a->clear();
364
365
        $this->assertSame([1], \iterator_to_array($a->iterator()));
366
        $this->assertInstanceOf(Implementation::class, $b);
367
        $this->assertSame([], \iterator_to_array($b->iterator()));
368
    }
369
370
    public function testEmpty()
371
    {
372
        $a = new Lazy('int', function() {
373
            yield 1;
374
        });
375
        $b = new Lazy('int', function() {
376
            if (false) {
377
                yield 1;
378
            }
379
        });
380
381
        $this->assertTrue($b->empty());
382
        $this->assertFalse($a->empty());
383
    }
384
385
    public function testToSequenceOf()
386
    {
387
        $loaded = false;
388
        $set = new Lazy('int', function() use (&$loaded) {
389
            yield 1;
390
            yield 2;
391
            yield 3;
392
            $loaded = true;
393
        });
394
        $sequence = $set->toSequenceOf('string|int', function($i) {
395
            yield (string) $i;
396
            yield $i;
397
        });
398
399
        $this->assertFalse($loaded);
400
        $this->assertInstanceOf(Sequence::class, $sequence);
401
        $this->assertSame(
402
            ['1', 1, '2', 2, '3', 3],
403
            unwrap($sequence),
404
        );
405
        $this->assertTrue($loaded);
406
    }
407
408
    public function testToSetOf()
409
    {
410
        $loaded = false;
411
        $set = new Lazy('int', function() use (&$loaded) {
412
            yield 1;
413
            yield 2;
414
            yield 3;
415
            $loaded = true;
416
        });
417
        $set = $set->toSetOf('string|int', function($i) {
418
            yield (string) $i;
419
            yield $i;
420
        });
421
422
        $this->assertFalse($loaded);
423
        $this->assertInstanceOf(Set::class, $set);
424
        $this->assertSame(
425
            ['1', 1, '2', 2, '3', 3],
426
            unwrap($set),
427
        );
428
        $this->assertTrue($loaded);
429
    }
430
431
    public function testToMapOf()
432
    {
433
        $set = new Lazy('int', function() {
434
            yield 1;
435
            yield 2;
436
            yield 3;
437
        });
438
        $map = $set->toMapOf('string', 'int', fn($i) => yield (string) $i => $i);
439
440
        $this->assertInstanceOf(Map::class, $map);
441
        $this->assertCount(3, $map);
442
        $this->assertSame(1, $map->get('1'));
443
        $this->assertSame(2, $map->get('2'));
444
        $this->assertSame(3, $map->get('3'));
445
    }
446
}
447