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

MapTest::testPartition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nc 1
nop 0
dl 0
loc 40
rs 9.488
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Tests\Innmind\Immutable;
5
6
use Innmind\Immutable\{
7
    Map,
8
    SizeableInterface,
0 ignored issues
show
Bug introduced by
The type Innmind\Immutable\SizeableInterface 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...
9
    Pair,
10
    Str,
11
    Set,
12
    Sequence,
13
    Exception\LogicException,
14
    Exception\ElementNotFound,
15
    Exception\CannotGroupEmptyStructure,
16
};
17
use function Innmind\Immutable\unwrap;
18
use PHPUnit\Framework\TestCase;
19
20
class MapTest extends TestCase
21
{
22
    public function testInterface()
23
    {
24
        $m = Map::of('int', 'float');
25
26
        $this->assertInstanceOf(\Countable::class, $m);
27
        $this->assertSame('int', $m->keyType());
28
        $this->assertSame('float', $m->valueType());
29
    }
30
31
    public function testOf()
32
    {
33
        $map = Map::of('int', 'float')
34
            (1, 1.1)
35
            (2, 2.1);
36
37
        $this->assertTrue(
38
            $map->equals(
39
                Map::of('int', 'float')
40
                    ->put(1, 1.1)
41
                    ->put(2, 2.1)
42
            )
43
        );
44
    }
45
46
    public function testEmptyOf()
47
    {
48
        $this->assertTrue(Map::of('int', 'int')->equals(Map::of('int', 'int')));
49
    }
50
51
    public function testPut()
52
    {
53
        $m = Map::of('int', 'int');
54
55
        $this->assertSame(0, $m->size());
56
        $m2 = $m->put(42, 42);
57
        $this->assertNotSame($m, $m2);
58
        $this->assertSame(0, $m->size());
59
        $this->assertSame(1, $m2->size());
60
61
        $m = Map::of('int', 'int');
62
        $m = $m
63
            ->put(23, 24)
64
            ->put(41, 42)
65
            ->put(65, 66)
66
            ->put(89, 90)
67
            ->put(65, 1);
68
69
        $this->assertSame(24, $m->get(23));
70
        $this->assertSame(42, $m->get(41));
71
        $this->assertSame(1, $m->get(65));
72
        $this->assertSame(90, $m->get(89));
73
        $this->assertSame(4, $m->size());
74
    }
75
76
    public function testTupleLikeInjection()
77
    {
78
        $map = Map::of('int', 'int')
79
            (1, 2)
80
            (3, 4);
81
        $expected = Map::of('int', 'int')
82
            ->put(1, 2)
83
            ->put(3, 4);
84
85
        $this->assertTrue($map->equals($expected));
86
    }
87
88
    public function testThrowWhenKeyDoesntMatchType()
89
    {
90
        $this->expectException(\TypeError::class);
91
        $this->expectExceptionMessage('Argument 1 must be of type int, string given');
92
93
        $m = Map::of('int', 'int');
94
        $m->put('24', 42);
95
    }
96
97
    public function testThrowWhenValueDoesntMatchType()
98
    {
99
        $this->expectException(\TypeError::class);
100
        $this->expectExceptionMessage('Argument 2 must be of type int, float given');
101
102
        $m = Map::of('int', 'int');
103
        $m->put(24, 42.0);
104
    }
105
106
    public function testGet()
107
    {
108
        $m = Map::of('int', 'int');
109
        $m = $m->put(23, 24);
110
111
        $this->assertSame(24, $m->get(23));
112
    }
113
114
    public function testThrowWhenGettingUnknownKey()
115
    {
116
        $this->expectException(ElementNotFound::class);
117
118
        Map::of('int', 'int')->get(24);
119
    }
120
121
    public function testContains()
122
    {
123
        $m = Map::of('int', 'int');
124
        $m = $m->put(23, 24);
125
126
        $this->assertFalse($m->contains(24));
127
        $this->assertTrue($m->contains(23));
128
    }
129
130
    public function testClear()
131
    {
132
        $m = Map::of('int', 'float');
133
        $m = $m->put(24, 42.0);
134
135
        $m2 = $m->clear();
136
        $this->assertNotSame($m, $m2);
137
        $this->assertInstanceOf(Map::class, $m2);
138
        $this->assertSame(1, $m->size());
139
        $this->assertSame(0, $m2->size());
140
        $this->assertSame('int', $m2->keyType());
141
        $this->assertSame('float', $m2->valueType());
142
    }
143
144
    public function testThrowWhenTryingToCheckIfMapsOfDifferentTypesAreEqual()
145
    {
146
        $this->expectException(\TypeError::class);
147
        $this->expectExceptionMessage('Argument 1 must be of type Map<int, int>');
148
149
        Map::of('int', 'int')->equals(Map::of('float', 'int'));
150
    }
151
152
    public function testEquals()
153
    {
154
        $m = Map::of('int', 'int')->put(24, 42);
155
        $m2 = Map::of('int', 'int')->put(24, 42);
156
157
        $this->assertTrue($m->equals($m2));
158
        $this->assertFalse($m->equals($m2->put(65, 66)));
159
        $this->assertFalse($m->equals($m2->put(24, 24)));
160
        $this->assertFalse(
161
            Map::of('string', 'string')
162
                ->put('foo_res', 'res')
163
                ->put('foo_bar_res', 'res')
164
                ->equals(
165
                    Map::of('string', 'string')
166
                        ->put('foo_res', 'res')
167
                        ->put('bar_res', 'res')
168
                )
169
        );
170
171
        $m = Map::of('int', 'int')
172
            ->put(24, 42)
173
            ->put(42, 24);
174
        $m2 = Map::of('int', 'int')
175
            ->put(42, 24)
176
            ->put(24, 42);
177
178
        $this->assertTrue($m->equals($m2));
179
180
        $this->assertTrue(Map::of('int', 'int')->equals(Map::of('int', 'int')));
181
    }
182
183
    public function testFilter()
184
    {
185
        $m = Map::of('int', 'int')
186
            ->put(0, 1)
187
            ->put(1, 2)
188
            ->put(2, 3)
189
            ->put(4, 5);
190
191
        $m2 = $m->filter(function(int $key, int $value) {
192
            return ($key + $value) % 3 === 0;
193
        });
194
195
        $this->assertNotSame($m, $m2);
196
        $this->assertInstanceOf(Map::class, $m2);
197
        $this->assertSame($m->keyType(), $m2->keyType());
198
        $this->assertSame($m->valueType(), $m2->valueType());
199
        $this->assertSame(4, $m->size());
200
        $this->assertSame(2, $m2->size());
201
        $this->assertTrue($m2->contains(1));
202
        $this->assertTrue($m2->contains(4));
203
        $this->assertFalse($m2->contains(0));
204
        $this->assertFalse($m2->contains(2));
205
    }
206
207
    public function testForeach()
208
    {
209
        $m = Map::of('int', 'int')
210
            ->put(0, 1)
211
            ->put(1, 2)
212
            ->put(2, 3)
213
            ->put(3, 4);
214
        $count = 0;
215
216
        $m->foreach(function(int $key, int $value) use (&$count) {
217
            $this->assertSame($count, $key);
218
            $this->assertSame($value, $key + 1);
219
            ++$count;
220
        });
221
        $this->assertSame(4, $count);
222
    }
223
224
    public function testThrowWhenGroupingAnEmptyMap()
225
    {
226
        $this->expectException(CannotGroupEmptyStructure::class);
227
228
        Map::of('int', 'int')->groupBy(function() {});
229
    }
230
231
    public function testGroupBy()
232
    {
233
        $m = Map::of('int', 'int')
234
            ->put(0, 1)
235
            ->put(1, 2)
236
            ->put(2, 3)
237
            ->put(4, 5);
238
239
        $m2 = $m->groupBy(function(int $key, int $value) {
240
            return ($key + $value) % 3;
241
        });
242
        $this->assertNotSame($m, $m2);
243
        $this->assertInstanceOf(Map::class, $m2);
244
        $this->assertSame('int', $m2->keyType());
245
        $this->assertSame(Map::class, $m2->valueType());
246
        $this->assertTrue($m2->contains(0));
247
        $this->assertTrue($m2->contains(1));
248
        $this->assertTrue($m2->contains(2));
249
        $this->assertSame(2, $m2->get(0)->size());
250
        $this->assertSame(1, $m2->get(1)->size());
251
        $this->assertSame(1, $m2->get(2)->size());
252
        $this->assertSame('int', $m2->get(0)->keyType());
253
        $this->assertSame('int', $m2->get(0)->valueType());
254
        $this->assertSame('int', $m2->get(1)->keyType());
255
        $this->assertSame('int', $m2->get(1)->valueType());
256
        $this->assertSame('int', $m2->get(2)->keyType());
257
        $this->assertSame('int', $m2->get(2)->valueType());
258
        $this->assertSame(1, $m2->get(1)->get(0));
259
        $this->assertSame(2, $m2->get(0)->get(1));
260
        $this->assertSame(3, $m2->get(2)->get(2));
261
        $this->assertSame(5, $m2->get(0)->get(4));
262
    }
263
    public function testKeys()
264
    {
265
        $m = Map::of('int', 'int')
266
            ->put(0, 1)
267
            ->put(1, 2)
268
            ->put(2, 3)
269
            ->put(4, 5);
270
271
        $k = $m->keys();
272
        $this->assertInstanceOf(Set::class, $k);
273
        $this->assertSame('int', $k->type());
274
        $this->assertSame([0, 1, 2, 4], unwrap($k));
275
        $this->assertTrue($k->equals($m->keys()));
276
    }
277
278
    public function testValues()
279
    {
280
        $m = Map::of('int', 'int')
281
            ->put(0, 1)
282
            ->put(1, 2)
283
            ->put(2, 3)
284
            ->put(4, 5)
285
            ->put(5, 5);
286
287
        $v = $m->values();
288
        $this->assertInstanceOf(Sequence::class, $v);
289
        $this->assertSame('int', $v->type());
290
        $this->assertSame([1, 2, 3, 5, 5], unwrap($v));
0 ignored issues
show
Bug introduced by
$v 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

290
        $this->assertSame([1, 2, 3, 5, 5], unwrap(/** @scrutinizer ignore-type */ $v));
Loading history...
291
        $this->assertTrue($v->equals($m->values()));
292
    }
293
294
    public function testMap()
295
    {
296
        $m = Map::of('int', 'int')
297
            ->put(0, 1)
298
            ->put(1, 2)
299
            ->put(2, 3)
300
            ->put(4, 5);
301
302
        $m2 = $m->map(function(int $key, int $value) {
303
            if ($key % 2 === 0) {
304
                return new Pair($key + 10, $value);
305
            }
306
307
            return $value**2;
308
        });
309
        $this->assertNotSame($m, $m2);
310
        $this->assertInstanceOf(Map::class, $m2);
311
        $this->assertSame($m->keyType(), $m2->keyType());
312
        $this->assertSame($m->valueType(), $m2->valueType());
313
        $this->assertSame([0, 1, 2, 4], unwrap($m->keys()));
314
        $this->assertSame([1, 2, 3, 5], unwrap($m->values()));
0 ignored issues
show
Bug introduced by
$m->values() 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

314
        $this->assertSame([1, 2, 3, 5], unwrap(/** @scrutinizer ignore-type */ $m->values()));
Loading history...
315
        $this->assertSame([10, 1, 12, 14], unwrap($m2->keys()));
316
        $this->assertSame([1, 4, 3, 5], unwrap($m2->values()));
317
    }
318
319
    public function testTrhowWhenTryingToModifyValueTypeInTheMap()
320
    {
321
        $this->expectException(\TypeError::class);
322
        $this->expectExceptionMessage('Argument 2 must be of type int, string given');
323
324
        Map::of('int', 'int')
325
            ->put(1, 2)
326
            ->map(function(int $key, int $value) {
327
                return (string) $value;
328
            });
329
    }
330
331
    public function testTrhowWhenTryingToModifyKeyTypeInTheMap()
332
    {
333
        $this->expectException(\TypeError::class);
334
        $this->expectExceptionMessage('Argument 1 must be of type int, string given');
335
336
        Map::of('int', 'int')
337
            ->put(1, 2)
338
            ->map(function(int $key, int $value) {
339
                return new Pair((string) $key, $value);
340
            });
341
    }
342
343
    public function testRemove()
344
    {
345
        $m = Map::of('int', 'int')
346
            ->put(0, 1)
347
            ->put(1, 2)
348
            ->put(2, 3)
349
            ->put(3, 4)
350
            ->put(4, 5);
351
352
        $m2 = $m->remove(12);
353
        $this->assertTrue($m->equals($m2));
354
        $this->assertSame([0, 1, 2, 3, 4], unwrap($m->keys()));
355
        $this->assertSame([1, 2, 3, 4, 5], unwrap($m->values()));
0 ignored issues
show
Bug introduced by
$m->values() 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

355
        $this->assertSame([1, 2, 3, 4, 5], unwrap(/** @scrutinizer ignore-type */ $m->values()));
Loading history...
356
357
        $m2 = $m->remove(3);
358
        $this->assertNotSame($m, $m2);
359
        $this->assertInstanceOf(Map::class, $m2);
360
        $this->assertSame([0, 1, 2, 3, 4], unwrap($m->keys()));
361
        $this->assertSame([1, 2, 3, 4, 5], unwrap($m->values()));
362
        $this->assertSame([0, 1, 2, 4], unwrap($m2->keys()));
363
        $this->assertSame([1, 2, 3, 5], unwrap($m2->values()));
364
365
        $m2 = $m->remove(4);
366
        $this->assertNotSame($m, $m2);
367
        $this->assertInstanceOf(Map::class, $m2);
368
        $this->assertSame([0, 1, 2, 3, 4], unwrap($m->keys()));
369
        $this->assertSame([1, 2, 3, 4, 5], unwrap($m->values()));
370
        $this->assertSame([0, 1, 2, 3], unwrap($m2->keys()));
371
        $this->assertSame([1, 2, 3, 4], unwrap($m2->values()));
372
373
        $m2 = $m->remove(0);
374
        $this->assertNotSame($m, $m2);
375
        $this->assertInstanceOf(Map::class, $m2);
376
        $this->assertSame([0, 1, 2, 3, 4], unwrap($m->keys()));
377
        $this->assertSame([1, 2, 3, 4, 5], unwrap($m->values()));
378
        $this->assertSame([1, 2, 3, 4], unwrap($m2->keys()));
379
        $this->assertSame([2, 3, 4, 5], unwrap($m2->values()));
380
    }
381
382
    public function testMerge()
383
    {
384
        $m = Map::of(\stdClass::class, 'int')
385
            ->put($s = new \stdClass, 24)
386
            ->put($s2 = new \stdClass, 42);
387
        $m2 = Map::of(\stdClass::class, 'int')
388
            ->put($s3 = new \stdClass, 24)
389
            ->put($s2, 66)
390
            ->put($s4 = new \stdClass, 42);
391
392
        $m3 = $m->merge($m2);
393
        $this->assertNotSame($m, $m3);
394
        $this->assertNotSame($m2, $m3);
395
        $this->assertInstanceOf(Map::class, $m3);
396
        $this->assertSame($m->keyType(), $m3->keyType());
397
        $this->assertSame($m->valueType(), $m3->valueType());
398
        $this->assertSame(4, $m3->size());
399
        $this->assertSame([$s, $s2], unwrap($m->keys()));
400
        $this->assertSame([24, 42], unwrap($m->values()));
0 ignored issues
show
Bug introduced by
$m->values() 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

400
        $this->assertSame([24, 42], unwrap(/** @scrutinizer ignore-type */ $m->values()));
Loading history...
401
        $this->assertSame([$s3, $s2, $s4], unwrap($m2->keys()));
402
        $this->assertSame([24, 66, 42], unwrap($m2->values()));
403
        $this->assertSame([$s, $s2, $s3, $s4], unwrap($m3->keys()));
404
        $this->assertSame([24, 66, 24, 42], unwrap($m3->values()));
405
        $this->assertFalse($m3->equals($m2->merge($m)));
406
    }
407
408
    public function testThrowWhenMergingMapsOfDifferentType()
409
    {
410
        $this->expectException(\TypeError::class);
411
        $this->expectExceptionMessage('Argument 1 must be of type Map<int, int>');
412
413
        Map::of('int', 'int')->merge(Map::of('float', 'int'));
414
    }
415
416
    public function testPartition()
417
    {
418
        $m = Map::of('int', 'int')
419
            ->put(0, 1)
420
            ->put(1, 2)
421
            ->put(2, 3)
422
            ->put(3, 4)
423
            ->put(4, 5);
424
425
        $p = $m->partition(function(int $i, int $v) {
426
            return ($i + $v) % 3 === 0;
427
        });
428
429
        $this->assertInstanceOf(Map::class, $p);
430
        $this->assertNotSame($p, $m);
431
        $this->assertSame('bool', $p->keyType());
432
        $this->assertSame(Map::class, $p->valueType());
433
        $this->assertSame(
434
            [true, false],
435
            unwrap($p->keys())
436
        );
437
        $this->assertSame('int', $p->get(true)->keyType());
438
        $this->assertSame('int', $p->get(true)->valueType());
439
        $this->assertSame('int', $p->get(false)->keyType());
440
        $this->assertSame('int', $p->get(false)->valueType());
441
        $this->assertSame(
442
            [1, 4],
443
            unwrap($p->get(true)->keys())
444
        );
445
        $this->assertSame(
446
            [2, 5],
447
            unwrap($p->get(true)->values())
448
        );
449
        $this->assertSame(
450
            [0, 2, 3],
451
            unwrap($p->get(false)->keys())
452
        );
453
        $this->assertSame(
454
            [1, 3, 4],
455
            unwrap($p->get(false)->values())
456
        );
457
    }
458
459
    public function testReduce()
460
    {
461
        $m = Map::of('int', 'int')
462
            ->put(4, 4);
463
464
        $v = $m->reduce(
465
            42,
0 ignored issues
show
Bug introduced by
42 of type integer is incompatible with the type Innmind\Immutable\R expected by parameter $carry of Innmind\Immutable\Map::reduce(). ( Ignorable by Annotation )

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

465
            /** @scrutinizer ignore-type */ 42,
Loading history...
466
            function (float $carry, int $key, int $value): float {
467
                return $carry / ($key * $value);
468
            }
469
        );
470
471
        $this->assertSame(2.625, $v);
472
        $this->assertSame([4], unwrap($m->keys()));
473
        $this->assertSame([4], unwrap($m->values()));
0 ignored issues
show
Bug introduced by
$m->values() 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

473
        $this->assertSame([4], unwrap(/** @scrutinizer ignore-type */ $m->values()));
Loading history...
474
    }
475
476
    public function testToSetOf()
477
    {
478
        $map = Map::of('int', 'int')
479
            (1, 2)
480
            (3, 4);
481
        $set = $map->toSetOf('int', function($k, $v) {
482
            yield $k;
483
            yield $v;
484
        });
485
486
        $this->assertInstanceOf(Set::class, $set);
487
        $this->assertSame(
488
            [1, 2, 3, 4],
489
            unwrap($set),
490
        );
491
    }
492
493
    public function testToMapOf()
494
    {
495
        $map = Map::of('int', 'int')
496
            (1, 2)
497
            (3, 4);
498
        $map = $map->toMapOf('string', 'int', fn($i, $j) => yield (string) $j => $i);
499
500
        $this->assertInstanceOf(Map::class, $map);
501
        $this->assertCount(2, $map);
502
        $this->assertSame(1, $map->get('2'));
503
        $this->assertSame(3, $map->get('4'));
504
505
        $this->assertTrue(
506
            Map::of('object', 'int')
507
                (new \stdClass, 1)
508
                ->toMapOf('stdClass', 'int')
509
                ->isOfType('stdClass', 'int')
510
        );
511
    }
512
}
513