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   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 417
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 259
dl 0
loc 417
rs 10
c 0
b 0
f 0
wmc 25

24 Methods

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