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 — master ( fd060d...258593 )
by Baptiste
03:01 queued 10s
created

PrimitiveTest::testRemove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 26
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 37
rs 9.504
1
<?php
2
declare(strict_types = 1);
3
4
namespace Tests\Innmind\Immutable\Map;
5
6
use Innmind\Immutable\{
7
    Map\Primitive,
0 ignored issues
show
Bug introduced by
The type Innmind\Immutable\Map\Primitive 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
    Map\Implementation,
9
    Map,
10
    Pair,
11
    Str,
12
    Set,
13
    Sequence,
14
    Exception\LogicException,
15
    Exception\ElementNotFound,
16
    Exception\CannotGroupEmptyStructure,
17
};
18
use function Innmind\Immutable\unwrap;
19
use PHPUnit\Framework\TestCase;
20
21
class PrimitiveTest extends TestCase
22
{
23
    public function testInterface()
24
    {
25
        $m = new Primitive('int', 'float');
26
27
        $this->assertInstanceOf(Map\Implementation::class, $m);
28
        $this->assertInstanceOf(\Countable::class, $m);
29
        $this->assertSame('int', $m->keyType());
30
        $this->assertSame('float', $m->valueType());
31
    }
32
33
    public function testAcceptKeyTypes()
34
    {
35
        foreach (['int', 'integer', 'string'] as $type) {
36
            $this->assertSame($type, (new Primitive($type, 'stdClass'))->keyType());
37
        }
38
    }
39
40
    public function testThrowWhenInvalidKeyType()
41
    {
42
        $this->expectException(LogicException::class);
43
44
        new Primitive('float', 'stdClass');
45
    }
46
47
    public function testPut()
48
    {
49
        $m = new Primitive('int', 'int');
50
51
        $this->assertSame(0, $m->size());
52
        $m2 = ($m)(42, 42);
53
        $this->assertNotSame($m, $m2);
54
        $this->assertSame(0, $m->size());
55
        $this->assertSame(1, $m2->size());
56
57
        $m = new Primitive('int', 'int');
58
        $m = $m
59
            (23, 24)
60
            (41, 42)
61
            (65, 66)
62
            (89, 90)
63
            (65, 1);
64
65
        $this->assertSame(24, $m->get(23));
66
        $this->assertSame(42, $m->get(41));
67
        $this->assertSame(1, $m->get(65));
68
        $this->assertSame(90, $m->get(89));
69
        $this->assertSame(4, $m->size());
70
    }
71
72
    public function testThrowWhenKeyDoesntMatchType()
73
    {
74
        $this->expectException(\TypeError::class);
75
        $this->expectExceptionMessage('Argument 1 must be of type int, string given');
76
77
        $m = new Primitive('int', 'int');
78
        ($m)('24', 42);
79
    }
80
81
    public function testThrowWhenValueDoesntMatchType()
82
    {
83
        $this->expectException(\TypeError::class);
84
        $this->expectExceptionMessage('Argument 2 must be of type int, float given');
85
86
        $m = new Primitive('int', 'int');
87
        ($m)(24, 42.0);
88
    }
89
90
    public function testGet()
91
    {
92
        $m = new Primitive('int', 'int');
93
        $m = ($m)(23, 24);
94
95
        $this->assertSame(24, $m->get(23));
96
    }
97
98
    public function testThrowWhenGettingUnknownKey()
99
    {
100
        $this->expectException(ElementNotFound::class);
101
102
        (new Primitive('int', 'int'))->get(24);
103
    }
104
105
    public function testContains()
106
    {
107
        $m = new Primitive('int', 'int');
108
        $m = ($m)(23, 24);
109
110
        $this->assertFalse($m->contains(24));
111
        $this->assertTrue($m->contains(23));
112
    }
113
114
    public function testClear()
115
    {
116
        $m = new Primitive('int', 'float');
117
        $m = ($m)(24, 42.0);
118
119
        $m2 = $m->clear();
120
        $this->assertNotSame($m, $m2);
121
        $this->assertInstanceOf(Primitive::class, $m2);
122
        $this->assertSame(1, $m->size());
123
        $this->assertSame(0, $m2->size());
124
        $this->assertSame('int', $m2->keyType());
125
        $this->assertSame('float', $m2->valueType());
126
    }
127
128
    public function testEquals()
129
    {
130
        $m = (new Primitive('int', 'int'))(24, 42);
131
        $m2 = (new Primitive('int', 'int'))(24, 42);
132
133
        $this->assertTrue($m->equals($m2));
134
        $this->assertFalse($m->equals(($m2)(65, 66)));
135
        $this->assertFalse($m->equals(($m2)(24, 24)));
136
        $this->assertFalse(
137
            (new Primitive('string', 'string'))
138
                ('foo_res', 'res')
139
                ('foo_bar_res', 'res')
140
                ->equals(
141
                    (new Primitive('string', 'string'))
142
                        ('foo_res', 'res')
143
                        ('bar_res', 'res')
144
                )
145
        );
146
147
        $m = (new Primitive('int', 'int'))
148
            (24, 42)
149
            (42, 24);
150
        $m2 = (new Primitive('int', 'int'))
151
            (42, 24)
152
            (24, 42);
153
154
        $this->assertTrue($m->equals($m2));
155
156
        $this->assertTrue((new Primitive('int', 'int'))->equals(new Primitive('int', 'int')));
157
    }
158
159
    public function testFilter()
160
    {
161
        $m = (new Primitive('int', 'int'))
162
            (0, 1)
163
            (1, 2)
164
            (2, 3)
165
            (4, 5);
166
167
        $m2 = $m->filter(function(int $key, int $value) {
168
            return ($key + $value) % 3 === 0;
169
        });
170
171
        $this->assertNotSame($m, $m2);
172
        $this->assertInstanceOf(Primitive::class, $m2);
173
        $this->assertSame($m->keyType(), $m2->keyType());
174
        $this->assertSame($m->valueType(), $m2->valueType());
175
        $this->assertSame(4, $m->size());
176
        $this->assertSame(2, $m2->size());
177
        $this->assertTrue($m2->contains(1));
178
        $this->assertTrue($m2->contains(4));
179
        $this->assertFalse($m2->contains(0));
180
        $this->assertFalse($m2->contains(2));
181
    }
182
183
    public function testForeach()
184
    {
185
        $m = (new Primitive('int', 'int'))
186
            (0, 1)
187
            (1, 2)
188
            (2, 3)
189
            (3, 4);
190
        $count = 0;
191
192
        $m->foreach(function(int $key, int $value) use (&$count) {
193
            $this->assertSame($count, $key);
194
            $this->assertSame($value, $key + 1);
195
            ++$count;
196
        });
197
        $this->assertSame(4, $count);
198
    }
199
200
    public function testThrowWhenGroupingAnEmptyMap()
201
    {
202
        $this->expectException(CannotGroupEmptyStructure::class);
203
204
        (new Primitive('int', 'int'))->groupBy(function() {});
205
    }
206
207
    public function testGroupBy()
208
    {
209
        $m = (new Primitive('int', 'int'))
210
            (0, 1)
211
            (1, 2)
212
            (2, 3)
213
            (4, 5);
214
215
        $m2 = $m->groupBy(function(int $key, int $value) {
216
            return ($key + $value) % 3;
217
        });
218
        $this->assertNotSame($m, $m2);
219
        $this->assertInstanceOf(Map::class, $m2);
220
        $this->assertSame('int', $m2->keyType());
221
        $this->assertSame(Map::class, $m2->valueType());
222
        $this->assertTrue($m2->contains(0));
223
        $this->assertTrue($m2->contains(1));
224
        $this->assertTrue($m2->contains(2));
225
        $this->assertSame(2, $m2->get(0)->size());
226
        $this->assertSame(1, $m2->get(1)->size());
227
        $this->assertSame(1, $m2->get(2)->size());
228
        $this->assertSame('int', $m2->get(0)->keyType());
229
        $this->assertSame('int', $m2->get(0)->valueType());
230
        $this->assertSame('int', $m2->get(1)->keyType());
231
        $this->assertSame('int', $m2->get(1)->valueType());
232
        $this->assertSame('int', $m2->get(2)->keyType());
233
        $this->assertSame('int', $m2->get(2)->valueType());
234
        $this->assertSame(1, $m2->get(1)->get(0));
235
        $this->assertSame(2, $m2->get(0)->get(1));
236
        $this->assertSame(3, $m2->get(2)->get(2));
237
        $this->assertSame(5, $m2->get(0)->get(4));
238
    }
239
    public function testKeys()
240
    {
241
        $m = (new Primitive('int', 'int'))
242
            (0, 1)
243
            (1, 2)
244
            (2, 3)
245
            (4, 5);
246
247
        $k = $m->keys();
248
        $this->assertInstanceOf(Set::class, $k);
249
        $this->assertSame('int', $k->type());
250
        $this->assertSame([0, 1, 2, 4], unwrap($k));
251
        $this->assertTrue($k->equals($m->keys()));
252
    }
253
254
    public function testValues()
255
    {
256
        $m = (new Primitive('int', 'int'))
257
            (0, 1)
258
            (1, 2)
259
            (2, 3)
260
            (4, 5)
261
            (5, 5);
262
263
        $v = $m->values();
264
        $this->assertInstanceOf(Sequence::class, $v);
265
        $this->assertSame('int', $v->type());
266
        $this->assertSame([1, 2, 3, 5, 5], unwrap($v));
267
        $this->assertTrue($v->equals($m->values()));
268
    }
269
270
    public function testMap()
271
    {
272
        $m = (new Primitive('int', 'int'))
273
            (0, 1)
274
            (1, 2)
275
            (2, 3)
276
            (4, 5);
277
278
        $m2 = $m->map(function(int $key, int $value) {
279
            if ($key % 2 === 0) {
280
                return new Pair($key + 10, $value);
281
            }
282
283
            return $value**2;
284
        });
285
        $this->assertNotSame($m, $m2);
286
        $this->assertInstanceOf(Primitive::class, $m2);
287
        $this->assertSame($m->keyType(), $m2->keyType());
288
        $this->assertSame($m->valueType(), $m2->valueType());
289
        $this->assertSame([0, 1, 2, 4], unwrap($m->keys()));
290
        $this->assertSame([1, 2, 3, 5], unwrap($m->values()));
291
        $this->assertSame([10, 1, 12, 14], unwrap($m2->keys()));
292
        $this->assertSame([1, 4, 3, 5], unwrap($m2->values()));
293
    }
294
295
    public function testThrowWhenTryingToModifyValueTypeInTheMap()
296
    {
297
        $this->expectException(\TypeError::class);
298
        $this->expectExceptionMessage('Argument 2 must be of type int, string given');
299
300
        (new Primitive('int', 'int'))
301
            (1, 2)
302
            ->map(function(int $key, int $value) {
303
                return (string) $value;
304
            });
305
    }
306
307
    public function testThrowWhenTryingToModifyKeyTypeInTheMap()
308
    {
309
        $this->expectException(\TypeError::class);
310
        $this->expectExceptionMessage('Argument 1 must be of type int, string given');
311
312
        (new Primitive('int', 'int'))
313
            (1, 2)
314
            ->map(function(int $key, int $value) {
315
                return new Pair((string) $key, $value);
316
            });
317
    }
318
319
    public function testRemove()
320
    {
321
        $m = (new Primitive('int', 'int'))
322
            (0, 1)
323
            (1, 2)
324
            (2, 3)
325
            (3, 4)
326
            (4, 5);
327
328
        $m2 = $m->remove(12);
329
        $this->assertSame($m, $m2);
330
        $this->assertSame([0, 1, 2, 3, 4], unwrap($m->keys()));
331
        $this->assertSame([1, 2, 3, 4, 5], unwrap($m->values()));
332
333
        $m2 = $m->remove(3);
334
        $this->assertNotSame($m, $m2);
335
        $this->assertInstanceOf(Primitive::class, $m2);
336
        $this->assertSame([0, 1, 2, 3, 4], unwrap($m->keys()));
337
        $this->assertSame([1, 2, 3, 4, 5], unwrap($m->values()));
338
        $this->assertSame([0, 1, 2, 4], unwrap($m2->keys()));
339
        $this->assertSame([1, 2, 3, 5], unwrap($m2->values()));
340
341
        $m2 = $m->remove(4);
342
        $this->assertNotSame($m, $m2);
343
        $this->assertInstanceOf(Primitive::class, $m2);
344
        $this->assertSame([0, 1, 2, 3, 4], unwrap($m->keys()));
345
        $this->assertSame([1, 2, 3, 4, 5], unwrap($m->values()));
346
        $this->assertSame([0, 1, 2, 3], unwrap($m2->keys()));
347
        $this->assertSame([1, 2, 3, 4], unwrap($m2->values()));
348
349
        $m2 = $m->remove(0);
350
        $this->assertNotSame($m, $m2);
351
        $this->assertInstanceOf(Primitive::class, $m2);
352
        $this->assertSame([0, 1, 2, 3, 4], unwrap($m->keys()));
353
        $this->assertSame([1, 2, 3, 4, 5], unwrap($m->values()));
354
        $this->assertSame([1, 2, 3, 4], unwrap($m2->keys()));
355
        $this->assertSame([2, 3, 4, 5], unwrap($m2->values()));
356
    }
357
358
    public function testMerge()
359
    {
360
        $m = (new Primitive('int', 'int'))
361
            ($s = 90, 24)
362
            ($s2 = 91, 42);
363
        $m2 = (new Primitive('int', 'int'))
364
            ($s3 = 92, 24)
365
            ($s2, 66)
366
            ($s4 = 93, 42);
367
368
        $m3 = $m->merge($m2);
369
        $this->assertNotSame($m, $m3);
370
        $this->assertNotSame($m2, $m3);
371
        $this->assertInstanceOf(Primitive::class, $m3);
372
        $this->assertSame($m->keyType(), $m3->keyType());
373
        $this->assertSame($m->valueType(), $m3->valueType());
374
        $this->assertSame(4, $m3->size());
375
        $this->assertSame([$s, $s2], unwrap($m->keys()));
376
        $this->assertSame([24, 42], unwrap($m->values()));
377
        $this->assertSame([$s3, $s2, $s4], unwrap($m2->keys()));
378
        $this->assertSame([24, 66, 42], unwrap($m2->values()));
379
        $this->assertSame([$s, $s2, $s3, $s4], unwrap($m3->keys()));
380
        $this->assertSame([24, 66, 24, 42], unwrap($m3->values()));
381
        $this->assertFalse($m3->equals($m2->merge($m)));
382
    }
383
384
    public function testPartition()
385
    {
386
        $m = (new Primitive('int', 'int'))
387
            (0, 1)
388
            (1, 2)
389
            (2, 3)
390
            (3, 4)
391
            (4, 5);
392
393
        $p = $m->partition(function(int $i, int $v) {
394
            return ($i + $v) % 3 === 0;
395
        });
396
397
        $this->assertInstanceOf(Map::class, $p);
398
        $this->assertNotSame($p, $m);
399
        $this->assertSame('bool', $p->keyType());
400
        $this->assertSame(Map::class, $p->valueType());
401
        $this->assertSame(
402
            [true, false],
403
            unwrap($p->keys())
404
        );
405
        $this->assertSame('int', $p->get(true)->keyType());
406
        $this->assertSame('int', $p->get(true)->valueType());
407
        $this->assertSame('int', $p->get(false)->keyType());
408
        $this->assertSame('int', $p->get(false)->valueType());
409
        $this->assertSame(
410
            [1, 4],
411
            unwrap($p->get(true)->keys())
412
        );
413
        $this->assertSame(
414
            [2, 5],
415
            unwrap($p->get(true)->values())
416
        );
417
        $this->assertSame(
418
            [0, 2, 3],
419
            unwrap($p->get(false)->keys())
420
        );
421
        $this->assertSame(
422
            [1, 3, 4],
423
            unwrap($p->get(false)->values())
424
        );
425
    }
426
427
    public function testReduce()
428
    {
429
        $m = (new Primitive('int', 'int'))
430
            (4, 4);
431
432
        $v = $m->reduce(
433
            42,
434
            function (float $carry, int $key, int $value): float {
435
                return $carry / ($key * $value);
436
            }
437
        );
438
439
        $this->assertSame(2.625, $v);
440
        $this->assertSame([4], unwrap($m->keys()));
441
        $this->assertSame([4], unwrap($m->values()));
442
    }
443
444
    public function testEmpty()
445
    {
446
        $this->assertTrue((new Primitive('int', 'int'))->empty());
447
        $this->assertFalse((new Primitive('int', 'int'))(1, 2)->empty());
448
    }
449
450
    public function testWorkAroundPhpImplicitCast()
451
    {
452
        $map = (new Primitive('string', 'string'))('1', 'foo');
453
454
        $this->assertTrue($map->contains('1'));
455
        $map->foreach(function($key, $value) {
456
            $this->assertSame('1', $key);
457
            $this->assertSame('foo', $value);
458
        });
459
        $this->assertSame('foo', $map->get('1'));
460
        $this->assertSame(['1'], unwrap($map->keys()));
461
    }
462
463
    public function testToSequenceOf()
464
    {
465
        $map = (new Primitive('int', 'int'))
466
            (1, 2)
467
            (3, 4);
468
        $sequence = $map->toSequenceOf('int', function($k, $v) {
469
            yield $k;
470
            yield $v;
471
        });
472
473
        $this->assertInstanceOf(Sequence::class, $sequence);
474
        $this->assertSame(
475
            [1, 2, 3, 4],
476
            unwrap($sequence),
477
        );
478
    }
479
480
    public function testToSetOf()
481
    {
482
        $map = (new Primitive('int', 'int'))
483
            (1, 2)
484
            (3, 4);
485
        $set = $map->toSetOf('int', function($k, $v) {
486
            yield $k;
487
            yield $v;
488
        });
489
490
        $this->assertInstanceOf(Set::class, $set);
491
        $this->assertSame(
492
            [1, 2, 3, 4],
493
            unwrap($set),
494
        );
495
    }
496
497
    public function testToMapOf()
498
    {
499
        $map = (new Primitive('int', 'int'))
500
            (1, 2)
501
            (3, 4);
502
        $map = $map->toMapOf('string', 'int', fn($i, $j) => yield (string) $j => $i);
503
504
        $this->assertInstanceOf(Map::class, $map);
505
        $this->assertCount(2, $map);
506
        $this->assertSame(1, $map->get('2'));
507
        $this->assertSame(3, $map->get('4'));
508
    }
509
}
510