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 ( 2e42f3...4471cd )
by Baptiste
01:50
created

tests/SetTest.php (3 issues)

Labels
Severity
1
<?php
2
declare(strict_types = 1);
3
4
namespace Tests\Innmind\Immutable;
5
6
use Innmind\Immutable\{
7
    Set,
8
    Map,
9
    Str,
10
    Sequence,
11
};
12
use function Innmind\Immutable\unwrap;
13
use PHPUnit\Framework\TestCase;
14
15
class SetTest extends TestCase
16
{
17
    public function testInterface()
18
    {
19
        $s = Set::of('int');
20
21
        $this->assertInstanceOf(\Countable::class, $s);
22
        $this->assertSame('int', $s->type());
23
    }
24
25
    public function testOf()
26
    {
27
        $this->assertTrue(
28
            Set::of('int', 1, 1, 2, 3)->equals(
29
                Set::of('int')
30
                    ->add(1)
31
                    ->add(2)
32
                    ->add(3)
33
            )
34
        );
35
    }
36
37
    public function testDefer()
38
    {
39
        $loaded = false;
40
        $set = Set::defer('int', (function() use (&$loaded) {
41
            yield 1;
42
            yield 2;
43
            yield 3;
44
            $loaded = true;
45
        })());
46
47
        $this->assertInstanceOf(Set::class, $set);
48
        $this->assertFalse($loaded);
49
        $this->assertSame([1, 2, 3], unwrap($set));
50
        $this->assertTrue($loaded);
51
    }
52
53
    public function testLazy()
54
    {
55
        $loaded = false;
56
        $set = Set::lazy('int', function() use (&$loaded) {
57
            yield 1;
58
            yield 2;
59
            yield 3;
60
            $loaded = true;
61
        });
62
63
        $this->assertInstanceOf(Set::class, $set);
64
        $this->assertFalse($loaded);
65
        $this->assertSame([1, 2, 3], unwrap($set));
66
        $this->assertTrue($loaded);
67
    }
68
69
    public function testMixed()
70
    {
71
        $set = Set::mixed(1, '2', 3, 1);
72
73
        $this->assertInstanceOf(Set::class, $set);
74
        $this->assertSame('mixed', $set->type());
75
        $this->assertSame([1, '2', 3], unwrap($set));
76
    }
77
78
    public function testInts()
79
    {
80
        $set = Set::ints(1, 2, 3, 1);
81
82
        $this->assertInstanceOf(Set::class, $set);
83
        $this->assertSame('int', $set->type());
84
        $this->assertSame([1, 2, 3], unwrap($set));
85
    }
86
87
    public function testFloats()
88
    {
89
        $set = Set::floats(1, 2, 3.2, 1);
90
91
        $this->assertInstanceOf(Set::class, $set);
92
        $this->assertSame('float', $set->type());
93
        $this->assertSame([1.0, 2.0, 3.2], unwrap($set));
94
    }
95
96
    public function testStrings()
97
    {
98
        $set = Set::strings('1', '2', '3', '1');
99
100
        $this->assertInstanceOf(Set::class, $set);
101
        $this->assertSame('string', $set->type());
102
        $this->assertSame(['1', '2', '3'], unwrap($set));
103
    }
104
105
    public function testObjects()
106
    {
107
        $a = new \stdClass;
108
        $b = new \stdClass;
109
        $c = new \stdClass;
110
        $set = Set::objects($a, $b, $c, $a);
111
112
        $this->assertInstanceOf(Set::class, $set);
113
        $this->assertSame('object', $set->type());
114
        $this->assertSame([$a, $b, $c], unwrap($set));
115
    }
116
117
    public function testAdd()
118
    {
119
        $this->assertSame(0, Set::of('in')->size());
120
121
        $s = Set::of('int')->add(42);
122
123
        $this->assertSame(1, $s->size());
124
        $this->assertSame(1, $s->count());
125
        $s->add(24);
126
        $this->assertSame(1, $s->size());
127
        $s = $s->add(24);
128
        $this->assertInstanceOf(Set::class, $s);
129
        $this->assertSame(2, $s->size());
130
        $s = $s->add(24);
131
        $this->assertSame(2, $s->size());
132
        $this->assertSame([42, 24], unwrap($s));
133
134
        $this->assertSame(
135
            [1, 2, 3],
136
            unwrap(Set::ints(1)(2)(3)),
137
        );
138
    }
139
140
    public function testThrowWhenAddindInvalidElementType()
141
    {
142
        $this->expectException(\TypeError::class);
143
        $this->expectExceptionMessage('Argument 1 must be of type int, float given');
144
145
        Set::of('int')->add(42.0);
146
    }
147
148
    public function testIntersect()
149
    {
150
        $s = Set::of('int')
151
            ->add(24)
152
            ->add(42)
153
            ->add(66);
154
155
        $s2 = $s->intersect(Set::of('int')->add(42));
156
        $this->assertNotSame($s, $s2);
157
        $this->assertInstanceOf(Set::class, $s2);
158
        $this->assertSame($s->type(), $s2->type());
159
        $this->assertSame([24, 42, 66], unwrap($s));
160
        $this->assertSame([42], unwrap($s2));
161
    }
162
163
    public function testThrowWhenIntersectingSetsOfDifferentTypes()
164
    {
165
        $this->expectException(\TypeError::class);
166
        $this->expectExceptionMessage('Argument 1 must be of type Set<int>, Set<float> given');
167
168
        Set::of('int')->intersect(Set::of('float'));
169
    }
170
171
    public function testContains()
172
    {
173
        $s = Set::of('int');
174
175
        $this->assertFalse($s->contains(42));
176
        $s = $s->add(42);
177
        $this->assertTrue($s->contains(42));
178
    }
179
180
    public function testRemove()
181
    {
182
        $s = Set::of('int')
183
            ->add(24)
184
            ->add(42)
185
            ->add(66)
186
            ->add(90)
187
            ->add(114);
188
189
        $s2 = $s->remove(42);
190
        $this->assertNotSame($s, $s2);
191
        $this->assertInstanceOf(Set::class, $s2);
192
        $this->assertSame($s->type(), $s2->type());
193
        $this->assertSame([24, 42, 66, 90, 114], unwrap($s));
194
        $this->assertSame([24, 66, 90, 114], unwrap($s2));
195
        $this->assertSame([42, 66, 90, 114], unwrap($s->remove(24)));
196
        $this->assertSame([24, 42, 90, 114], unwrap($s->remove(66)));
197
        $this->assertSame([24, 42, 66, 114], unwrap($s->remove(90)));
198
        $this->assertSame([24, 42, 66, 90], unwrap($s->remove(114)));
199
    }
200
201
    public function testDiff()
202
    {
203
        $s = Set::of('int')
204
            ->add(24)
205
            ->add(42)
206
            ->add(66);
207
208
        $s2 = $s->diff(Set::of('int')->add(42));
209
        $this->assertNotSame($s, $s2);
210
        $this->assertInstanceOf(Set::class, $s2);
211
        $this->assertSame($s->type(), $s2->type());
212
        $this->assertSame([24, 42, 66], unwrap($s));
213
        $this->assertSame([24, 66], unwrap($s2));
214
    }
215
216
    public function testThrowWhenDiffingSetsOfDifferentType()
217
    {
218
        $this->expectException(\TypeError::class);
219
        $this->expectExceptionMessage('Argument 1 must be of type Set<int>, Set<float> given');
220
221
        Set::of('int')->diff(Set::of('float'));
222
    }
223
224
    public function testEquals()
225
    {
226
        $s = Set::of('int')
227
            ->add(24)
228
            ->add(42)
229
            ->add(66);
230
231
        $this->assertTrue(
232
            $s->equals(
233
                Set::of('int')
234
                    ->add(24)
235
                    ->add(66)
236
                    ->add(42)
237
            )
238
        );
239
        $this->assertTrue(Set::of('int')->equals(Set::of('int')));
240
        $this->assertFalse(
241
            $s->equals(
242
                Set::of('int')
243
                    ->add(24)
244
                    ->add(66)
245
            )
246
        );
247
    }
248
249
    public function testThrowWhenCheckingEqualityBetweenSetsOfDifferentType()
250
    {
251
        $this->expectException(\TypeError::class);
252
        $this->expectExceptionMessage('Argument 1 must be of type Set<int>, Set<float> given');
253
254
        Set::of('int')->equals(Set::of('float'));
255
    }
256
257
    public function testFilter()
258
    {
259
        $s = Set::of('int')
260
            ->add(1)
261
            ->add(2)
262
            ->add(3)
263
            ->add(4);
264
265
        $s2 = $s->filter(function(int $v) {
266
            return $v % 2 === 0;
267
        });
268
        $this->assertNotSame($s, $s2);
269
        $this->assertInstanceOf(Set::class, $s2);
270
        $this->assertSame($s->type(), $s2->type());
271
        $this->assertSame([1, 2, 3, 4], unwrap($s));
272
        $this->assertSame([2, 4], unwrap($s2));
273
    }
274
275
    public function testForeach()
276
    {
277
        $s = Set::of('int')
278
            ->add(1)
279
            ->add(2)
280
            ->add(3)
281
            ->add(4);
282
        $count = 0;
283
284
        $s->foreach(function(int $v) use (&$count) {
285
            $this->assertSame(++$count, $v);
286
        });
287
        $this->assertSame(4, $count);
288
    }
289
290
    public function testGroupBy()
291
    {
292
        $s = Set::of('int')
293
            ->add(1)
294
            ->add(2)
295
            ->add(3)
296
            ->add(4);
297
298
        $m = $s->groupBy(function(int $v) {
299
            return $v % 2;
300
        });
301
        $this->assertInstanceOf(Map::class, $m);
302
        $this->assertSame('int', $m->keyType());
303
        $this->assertSame(Set::class, $m->valueType());
304
        $this->assertSame('int', $m->get(0)->type());
305
        $this->assertSame('int', $m->get(1)->type());
306
        $this->assertSame([1, 0], unwrap($m->keys()));
307
        $this->assertSame([1, 3], unwrap($m->get(1)));
308
        $this->assertSame([2, 4], unwrap($m->get(0)));
309
    }
310
311
    public function testMap()
312
    {
313
        $s = Set::of('int')
314
            ->add(1)
315
            ->add(2)
316
            ->add(3)
317
            ->add(4);
318
319
        $s2 = $s->map(function(int $v) {
320
            return $v**2;
321
        });
322
        $this->assertNotSame($s, $s2);
323
        $this->assertInstanceOf(Set::class, $s2);
324
        $this->assertSame($s->type(), $s2->type());
325
        $this->assertSame([1, 2, 3, 4], unwrap($s));
326
        $this->assertSame([1, 4, 9, 16], unwrap($s2));
327
    }
328
329
    public function testThrowWhenTryingToModifyValueTypeInMap()
330
    {
331
        $this->expectException(\TypeError::class);
332
        $this->expectExceptionMessage('Argument 1 must be of type int, string given');
333
334
        Set::of('int')
335
            ->add(1)
336
            ->map(function(int $value) {
337
                return (string) $value;
338
            });
339
    }
340
341
    public function testPartition()
342
    {
343
        $s = Set::of('int')
344
            ->add(1)
345
            ->add(2)
346
            ->add(3)
347
            ->add(4);
348
349
        $s2 = $s->partition(function(int $v) {
350
            return $v % 2 === 0;
351
        });
352
        $this->assertNotSame($s, $s2);
353
        $this->assertInstanceOf(Map::class, $s2);
354
        $this->assertSame('bool', $s2->keyType());
355
        $this->assertSame(Set::class, $s2->valueType());
356
        $this->assertSame([1, 2, 3, 4], unwrap($s));
357
        $this->assertInstanceOf(Set::class, $s2->get(true));
358
        $this->assertInstanceOf(Set::class, $s2->get(false));
359
        $this->assertSame($s->type(), $s2->get(true)->type());
360
        $this->assertSame($s->type(), $s2->get(false)->type());
361
        $this->assertSame([2, 4], unwrap($s2->get(true)));
362
        $this->assertSame([1, 3], unwrap($s2->get(false)));
363
    }
364
365
    public function testSort()
366
    {
367
        $s = Set::of('int')
368
            ->add(1)
369
            ->add(2)
370
            ->add(3)
371
            ->add(4);
372
373
        $s2 = $s->sort(function(int $a, int $b) {
374
            return $a < $b;
375
        });
376
        $this->assertInstanceOf(Sequence::class, $s2);
377
        $this->assertSame('int', $s2->type());
378
        $this->assertSame([1, 2, 3, 4], unwrap($s));
379
        $this->assertSame([4, 3, 2, 1], unwrap($s2));
0 ignored issues
show
$s2 of type Innmind\Immutable\Sequence is incompatible with the type Innmind\Immutable\Set expected by parameter $structure of Innmind\Immutable\unwrap(). ( Ignorable by Annotation )

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

379
        $this->assertSame([4, 3, 2, 1], unwrap(/** @scrutinizer ignore-type */ $s2));
Loading history...
380
    }
381
382
    public function testMerge()
383
    {
384
        $s = Set::of('int')
385
            ->add(24)
386
            ->add(42)
387
            ->add(66);
388
389
        $this->assertTrue(
390
            $s
391
                ->merge(
392
                    Set::of('int')
393
                        ->add(24)
394
                        ->add(42)
395
                        ->add(66)
396
                )
397
                ->equals($s)
398
        );
399
        $this->assertSame(
400
            [24, 42, 66, 90, 114],
401
            unwrap($s->merge(
402
                Set::of('int')
403
                    ->add(90)
404
                    ->add(114)
405
            )),
406
        );
407
        $this->assertSame([24, 42, 66], unwrap($s));
408
        $this->assertSame($s->type(), $s->merge(Set::of('int'))->type());
409
    }
410
411
    public function testThrowWhenMergingSetsOfDifferentType()
412
    {
413
        $this->expectException(\TypeError::class);
414
        $this->expectExceptionMessage('Argument 1 must be of type Set<int>, Set<float> given');
415
416
        Set::of('int')->merge(Set::of('float'));
417
    }
418
419
    public function testReduce()
420
    {
421
        $s = Set::of('int')
422
            ->add(4)
423
            ->add(3)
424
            ->add(2);
425
426
        $v = $s->reduce(
427
            42,
0 ignored issues
show
42 of type integer is incompatible with the type Innmind\Immutable\R expected by parameter $carry of Innmind\Immutable\Set::reduce(). ( Ignorable by Annotation )

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

427
            /** @scrutinizer ignore-type */ 42,
Loading history...
428
            function (float $carry, int $value): float {
429
                return $carry / $value;
430
            }
431
        );
432
433
        $this->assertSame(1.75, $v);
434
        $this->assertSame([4, 3, 2], unwrap($s));
435
    }
436
437
    public function testVariableSet()
438
    {
439
        $this->assertSame(
440
            ['foo', 42, 42.1, true, []],
441
            unwrap(Set::of('variable')
442
                ->add('foo')
443
                ->add(42)
444
                ->add(42.1)
445
                ->add(true)
446
                ->add([]))
447
        );
448
    }
449
450
    public function testEmpty()
451
    {
452
        $this->assertTrue(Set::of('int')->empty());
453
        $this->assertFalse(Set::of('int', 1)->empty());
454
    }
455
456
    public function testToSequenceOf()
457
    {
458
        $set = Set::ints(1, 2, 3);
459
        $sequence = $set->toSequenceOf('string|int', function($i) {
460
            yield (string) $i;
461
            yield $i;
462
        });
463
464
        $this->assertInstanceOf(Sequence::class, $sequence);
465
        $this->assertSame(
466
            ['1', 1, '2', 2, '3', 3],
467
            unwrap($sequence),
0 ignored issues
show
$sequence of type Innmind\Immutable\Sequence is incompatible with the type Innmind\Immutable\Set expected by parameter $structure of Innmind\Immutable\unwrap(). ( Ignorable by Annotation )

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

467
            unwrap(/** @scrutinizer ignore-type */ $sequence),
Loading history...
468
        );
469
        $this->assertSame(
470
            [1, 2, 3],
471
            unwrap($set->toSequenceOf('int')),
472
        );
473
    }
474
475
    public function testToSetOf()
476
    {
477
        $initial = Set::ints(1, 2, 3);
478
        $set = $initial->toSetOf('string|int', function($i) {
479
            yield (string) $i;
480
            yield $i;
481
        });
482
483
        $this->assertInstanceOf(Set::class, $set);
484
        $this->assertSame(
485
            ['1', 1, '2', 2, '3', 3],
486
            unwrap($set),
487
        );
488
        $this->assertSame(
489
            [1, 2, 3],
490
            unwrap($initial->toSetOf('int')),
491
        );
492
    }
493
494
    public function testToMapOf()
495
    {
496
        $set = Set::ints(1, 2, 3);
497
        $map = $set->toMapOf('string', 'int', fn($i) => yield (string) $i => $i);
498
499
        $this->assertInstanceOf(Map::class, $map);
500
        $this->assertCount(3, $map);
501
        $this->assertSame(1, $map->get('1'));
502
        $this->assertSame(2, $map->get('2'));
503
        $this->assertSame(3, $map->get('3'));
504
    }
505
}
506