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

ObjectKeysTest::testPartition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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