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
Pull Request — 2.2 (#99)
by Simone
04:14 queued 01:10
created

JsonPathFinderTest::testLookForPathFromStartEntityToDestination()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 0
dl 0
loc 40
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
use Mado\QueryBundle\Component\Meta\JsonPathFinder;
4
use PHPUnit\Framework\TestCase as TestCase;
5
6
class JsonPathFinderTest extends TestCase
7
{
8
    private $samepleJson;
9
10
    public function testRecognizeFirstChildOfAnEntity()
11
    {
12
        $this->samepleJson = [
13
            "AppBundle\\Entity\\Bar" => [
14
                "relations" => [
15
                    "fizz" => "AppBundle\\Entity\\Fizz",
16
                ]
17
            ],
18
        ];
19
20
        $this->mapper = $this
21
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
22
            ->disableOriginalConstructor()
23
            ->getMock();
24
25
        $this->mapper->expects($this->once())
26
            ->method('getMap')
27
            ->will($this->returnValue(
28
                $this->samepleJson
29
            ));
30
31
        $this->pathFinder = new JsonPathFinder(
32
            $this->mapper
33
        );
34
35
        $this->assertEquals(
36
            "AppBundle\\Entity\\Fizz",
37
            $this->pathFinder->getFirstChildOf("AppBundle\\Entity\\Bar")
38
        );
39
    }
40
41
    public function testCatchRootEntityOfInnerOne()
42
    {
43
        $this->samepleJson = [
44
            "AppBundle\\Entity\\Bar" => [
45
                "relations" => [
46
                    "fizz" => "AppBundle\\Entity\\Fizz",
47
                ]
48
            ],
49
        ];
50
51
        $this->mapper = $this
52
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
53
            ->disableOriginalConstructor()
54
            ->getMock();
55
56
        $this->mapper->expects($this->once())
57
            ->method('getMap')
58
            ->will($this->returnValue(
59
                $this->samepleJson
60
            ));
61
62
        $this->pathFinder = new JsonPathFinder(
63
            $this->mapper
64
        );
65
66
        $this->assertEquals(
67
            "AppBundle\\Entity\\Bar",
68
            $this->pathFinder->getFirstParentOf("AppBundle\\Entity\\Fizz")
69
        );
70
    }
71
72
    public function testCatchRelationNameToInnerEntity()
73
    {
74
        $this->samepleJson = [
75
            "AppBundle\\Entity\\Bar" => [
76
                "relations" => [
77
                    "fizz" => "AppBundle\\Entity\\Fizz",
78
                ]
79
            ],
80
        ];
81
82
        $this->mapper = $this
83
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
84
            ->disableOriginalConstructor()
85
            ->getMock();
86
87
        $this->mapper->expects($this->once())
88
            ->method('getMap')
89
            ->will($this->returnValue(
90
                $this->samepleJson
91
            ));
92
93
        $this->pathFinder = new JsonPathFinder(
94
            $this->mapper
95
        );
96
97
        $this->assertEquals(
98
            "fizz",
99
            $this->pathFinder->getSourceRelation("AppBundle\\Entity\\Fizz")
100
        );
101
    }
102
103
    public function testLookForPathFromStartEntityToDestination()
104
    {
105
        $this->samepleJson = [
106
            "AppBundle\\Entity\\Root" => [
107
                "relations" => [
108
                    "foo" => "AppBundle\\Entity\\Foo",
109
                ]
110
            ],
111
            "AppBundle\\Entity\\Foo" => [
112
                "relations" => [
113
                    "bar" => "AppBundle\\Entity\\Bar",
114
                ]
115
            ],
116
            "AppBundle\\Entity\\Bar" => [
117
                "relations" => [
118
                    "fizz" => "AppBundle\\Entity\\Fizz",
119
                ]
120
            ],
121
        ];
122
123
        $this->mapper = $this
124
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
125
            ->disableOriginalConstructor()
126
            ->getMock();
127
128
        $this->mapper->expects($this->once())
129
            ->method('getMap')
130
            ->will($this->returnValue(
131
                $this->samepleJson
132
            ));
133
134
        $this->pathFinder = new JsonPathFinder(
135
            $this->mapper
136
        );
137
138
        $this->pathFinder->setEntity("AppBundle\\Entity\\Root");
139
140
        $this->assertEquals(
141
            "foo.bar.fizz",
142
            $this->pathFinder->getPathTo("AppBundle\\Entity\\Fizz")
143
        );
144
    }
145
146
    public function testBuildPathBetweenTwoEntities()
147
    {
148
        $this->samepleJson = [
149
            "FooBundle\\Entity\\Item" => [
150
                "relations" => [
151
                    "items" => "AppBundle\\Entity\\Foo",
152
                ]
153
            ],
154
            "AppBundle\\Entity\\Foo" => [
155
                "relations" => [
156
                    "item" => "ZarroBundle\\Entity\\Item",
157
                ]
158
            ],
159
            "ZarroBundle\\Entity\\Item" => [
160
                "relations" => [
161
                    "family" => "AppBundle\\Entity\\Family",
162
                ]
163
            ],
164
        ];
165
166
        $this->mapper = $this
167
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
168
            ->disableOriginalConstructor()
169
            ->getMock();
170
171
        $this->mapper->expects($this->once())
172
            ->method('getMap')
173
            ->will($this->returnValue(
174
                $this->samepleJson
175
            ));
176
177
        $this->pathFinder = new JsonPathFinder(
178
            $this->mapper
179
        );
180
181
        $this->pathFinder->setQueryStartEntity("FooBundle\\Entity\\Item");
182
183
        $this->assertEquals(
184
            "_embedded.items.item.family",
185
            $this->pathFinder->getPathToEntity("AppBundle\\Entity\\Family")
186
        );
187
    }
188
189
    /**
190
     * @expectedException \Mado\QueryBundle\Component\Meta\Exceptions\UnexpectedValueException
191
     */
192
    public function testThrowExceptionIfPathNotExists()
193
    {
194
        $this->samepleJson = [
195
            "FooBundle\\Entity\\Merenghe" => [
196
                "relations" => [
197
                    "items" => "AppBundle\\Entity\\Foo",
198
                ]
199
            ],
200
            "AppBundle\\Entity\\Foo" => [
201
                "relations" => [
202
                    "item" => "ZarroBundle\\Entity\\Item",
203
                ]
204
            ],
205
            "ZarroBundle\\Entity\\Item" => [
206
                "relations" => [
207
                    "family" => "AppBundle\\Entity\\Family",
208
                ]
209
            ],
210
        ];
211
212
        $this->mapper = $this
213
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
214
            ->disableOriginalConstructor()
215
            ->getMock();
216
217
        $this->mapper->expects($this->once())
218
            ->method('getMap')
219
            ->will($this->returnValue(
220
                $this->samepleJson
221
            ));
222
223
        $this->pathFinder = new JsonPathFinder(
224
            $this->mapper
225
        );
226
227
        $this->pathFinder->setQueryStartEntity("FooBundle\\Entity\\Item");
228
        $this->pathFinder->getPathToEntity("AppBundle\\Entity\\Family");
229
    }
230
231
    public function testCountNumberOfParentOfRelationEntity()
232
    {
233
        $this->samepleJson = [
234
            "FooBundle\\Entity\\Zzz" => [
235
                "relations" => [
236
                    "items" => "AppBundle\\Entity\\Foo",
237
                ]
238
            ],
239
            "FooBundle\\Entity\\Item" => [
240
                "relations" => [
241
                    "items" => "AppBundle\\Entity\\Foo",
242
                ]
243
            ],
244
        ];
245
246
        $this->mapper = $this
247
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
248
            ->disableOriginalConstructor()
249
            ->getMock();
250
251
        $this->mapper->expects($this->once())
252
            ->method('getMap')
253
            ->will($this->returnValue(
254
                $this->samepleJson
255
            ));
256
257
        $this->pathFinder = new JsonPathFinder(
258
            $this->mapper
259
        );
260
261
        $this->assertEquals(
262
            2,
263
            $this->pathFinder->numberOfRelationsToEntity("AppBundle\\Entity\\Foo")
264
        );
265
    }
266
267
    public function testListParentOfInnerEntity()
268
    {
269
        $this->samepleJson = [
270
            "FooBundle\\Entity\\Zzz" => [
271
                "relations" => [
272
                    "items" => "AppBundle\\Entity\\Foo",
273
                ]
274
            ],
275
            "FooBundle\\Entity\\Item" => [
276
                "relations" => [
277
                    "items" => "AppBundle\\Entity\\Foo",
278
                ]
279
            ],
280
        ];
281
282
        $this->mapper = $this
283
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
284
            ->disableOriginalConstructor()
285
            ->getMock();
286
287
        $this->mapper->expects($this->once())
288
            ->method('getMap')
289
            ->will($this->returnValue(
290
                $this->samepleJson
291
            ));
292
293
        $this->pathFinder = new JsonPathFinder(
294
            $this->mapper
295
        );
296
297
        $this->assertEquals(
298
            [
299
                "FooBundle\\Entity\\Zzz",
300
                "FooBundle\\Entity\\Item",
301
            ],
302
            $this->pathFinder->listOfParentsOf("AppBundle\\Entity\\Foo")
303
        );
304
    }
305
306
    public function testBuildListOfEntityReachedDuringTheWalk()
307
    {
308
        $this->samepleJson = [
309
            "FooBundle\\Entity\\Zzz" => [
310
                "relations" => [
311
                    "items" => "AppBundle\\Entity\\Foo",
312
                ]
313
            ],
314
            "FooBundle\\Entity\\Item" => [
315
                "relations" => [
316
                    "items" => "AppBundle\\Entity\\Foo",
317
                ]
318
            ],
319
            "AppBundle\\Entity\\Foo" => [
320
                "relations" => [
321
                    "item" => "ZarroBundle\\Entity\\Item",
322
                ]
323
            ],
324
            "ZarroBundle\\Entity\\Item" => [
325
                "relations" => [
326
                    "family" => "AppBundle\\Entity\\Family",
327
                ]
328
            ],
329
        ];
330
331
        $this->mapper = $this
332
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
333
            ->disableOriginalConstructor()
334
            ->getMock();
335
336
        $this->mapper->expects($this->once())
337
            ->method('getMap')
338
            ->will($this->returnValue(
339
                $this->samepleJson
340
            ));
341
342
        $this->pathFinder = new JsonPathFinder(
343
            $this->mapper
344
        );
345
346
        $this->pathFinder->setQueryStartEntity("FooBundle\\Entity\\Item");
347
348
        $this->assertEquals(
349
            "_embedded.items.item.family",
350
            $this->pathFinder->getPathToEntity("AppBundle\\Entity\\Family")
351
        );
352
353
        $this->assertEquals(
354
            [
355
                "AppBundle\\Entity\\Family",
356
                "ZarroBundle\\Entity\\Item",
357
                "AppBundle\\Entity\\Foo",
358
            ],
359
            $this->pathFinder->getEntitiesPath("AppBundle\\Entity\\Family")
360
        );
361
    }
362
363
    public function testBuildRightPathAlsoWhenAtTheEndThereIsAFork()
364
    {
365
        $this->samepleJson = [
366
            "FooBundle\\Entity\\Zzz" => [
367
                "relations" => [
368
                    "items" => "AppBundle\\Entity\\Foo",
369
                ]
370
            ],
371
            "FooBundle\\Entity\\Item" => [
372
                "relations" => [
373
                    "items" => "AppBundle\\Entity\\Foo",
374
                ]
375
            ],
376
            "AppBundle\\Entity\\Foo" => [
377
                "relations" => [
378
                    "item" => "ZarroBundle\\Entity\\Item",
379
                ]
380
            ],
381
            "ZarroBundle\\Entity\\Item" => [
382
                "relations" => [
383
                    "family" => "AppBundle\\Entity\\Family",
384
                ]
385
            ],
386
        ];
387
388
        $this->mapper = $this
389
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
390
            ->disableOriginalConstructor()
391
            ->getMock();
392
393
        $this->mapper->expects($this->once())
394
            ->method('getMap')
395
            ->will($this->returnValue(
396
                $this->samepleJson
397
            ));
398
399
        $this->pathFinder = new JsonPathFinder(
400
            $this->mapper
401
        );
402
403
        $this->pathFinder->setQueryStartEntity("FooBundle\\Entity\\Item");
404
405
        $this->assertEquals(
406
            "_embedded.items.item.family",
407
            $this->pathFinder->getPathToEntity("AppBundle\\Entity\\Family")
408
        );
409
    }
410
411
    public function testBuildRightPathAlsoWithForksIntPath()
412
    {
413
        $this->samepleJson = [
414
            "FooBundle\\Entity\\Item" => [
415
                "relations" => [
416
                    "items" => "AppBundle\\Entity\\Foo",
417
                ]
418
            ],
419
            "AppBundle\\Entity\\Wrong" => [
420
                "relations" => [
421
                    "item" => "ZarroBundle\\Entity\\Item",
422
                ]
423
            ],
424
            "AppBundle\\Entity\\Sbagliato" => [
425
                "relations" => [
426
                    "item" => "ZarroBundle\\Entity\\Item",
427
                ]
428
            ],
429
            "AppBundle\\Entity\\Foo" => [
430
                "relations" => [
431
                    "item" => "ZarroBundle\\Entity\\Item",
432
                ]
433
            ],
434
            "ZarroBundle\\Entity\\Item" => [
435
                "relations" => [
436
                    "family" => "AppBundle\\Entity\\Family",
437
                ]
438
            ],
439
        ];
440
441
        $this->mapper = $this
442
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
443
            ->disableOriginalConstructor()
444
            ->getMock();
445
446
        $this->mapper->expects($this->once())
447
            ->method('getMap')
448
            ->will($this->returnValue(
449
                $this->samepleJson
450
            ));
451
452
        $this->pathFinder = new JsonPathFinder(
453
            $this->mapper
454
        );
455
456
        $this->pathFinder->setQueryStartEntity("FooBundle\\Entity\\Item");
457
458
        $this->pathFinder->removeStep("AppBundle\\Entity\\Wrong");
459
        $this->pathFinder->removeStep("AppBundle\\Entity\\Sbagliato");
460
461
        $this->assertEquals(
462
            "_embedded.items.item.family",
463
            $this->pathFinder->getPathToEntity("AppBundle\\Entity\\Family")
464
        );
465
    }
466
467
    public function testGenerateHasKeyFoRequest()
468
    {
469
        $this->mapper = $this
470
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
471
            ->disableOriginalConstructor()
472
            ->getMock();
473
474
        $this->mapper->expects($this->never())
475
            ->method('getMap');
476
477
        $this->pathFinder = new JsonPathFinder(
478
            $this->mapper
479
        );
480
481
        $startEntity = "FooBundle\\Entity\\Item";
482
        $endEntity = "AppBundle\\Entity\\Family";
483
484
        $this->pathFinder->setQueryStartEntity($startEntity);
485
        $hash = $this->pathFinder->getHashKeyForDestination($endEntity);
486
487
        $this->assertEquals(
488
            md5($startEntity . $endEntity),
489
            $hash
490
        );
491
    }
492
493
    public function testIncrementEntities()
494
    {
495
        $this->mapper = $this
496
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
497
            ->disableOriginalConstructor()
498
            ->getMock();
499
500
        $this->mapper->expects($this->never())
501
            ->method('getMap');
502
503
        $this->pathFinder = new JsonPathFinder($this->mapper);
504
505
        $startingCollection = [];
506
507
        $endCollection = $this->pathFinder->addEntity(
508
            $startingCollection,
509
            'ciaone'
510
        );
511
512
        $this->assertEquals(
513
            ['ciaone'],
514
            $endCollection
515
        );
516
    }
517
518
    /**
519
     * @expectedException \Mado\QueryBundle\Component\Meta\Exceptions\UndefinedPathException
520
     */
521
    public function testEntitiesPathCantExistsIfAnyPathWasLoaded()
522
    {
523
        $this->mapper = $this
524
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
525
            ->disableOriginalConstructor()
526
            ->getMock();
527
528
        $this->pathFinder = new JsonPathFinder($this->mapper);
529
530
        $this->pathFinder->getEntitiesPath("AppBundle\\Entity\\Family");
531
    }
532
533
    /**
534
     * @expectedException \Mado\QueryBundle\Component\Meta\Exceptions\UnreachablePathException
535
     */
536
    public function testUnreachablePathExceptionIsThrownWheneverEntityIsMissed()
537
    {
538
        $this->samepleJson = [
539
            "" => [
540
                "relations" => [
541
                    "foo" => "AppBundle\\Entity\\Foo",
542
                ]
543
            ],
544
            "AppBundle\\Entity\\Foo" => [
545
                "relations" => [
546
                    "bar" => "AppBundle\\Entity\\Bar",
547
                ]
548
            ],
549
            "AppBundle\\Entity\\Bar" => [
550
                "relations" => [
551
                    "fizz" => "AppBundle\\Entity\\Fizz",
552
                ]
553
            ],
554
        ];
555
556
        $this->mapper = $this
557
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
558
            ->disableOriginalConstructor()
559
            ->getMock();
560
561
        $this->mapper->expects($this->once())
562
            ->method('getMap')
563
            ->will($this->returnValue(
564
                $this->samepleJson
565
            ));
566
567
        $this->pathFinder = new JsonPathFinder(
568
            $this->mapper
569
        );
570
571
        $this->pathFinder->setEntity("AppBundle\\Entity\\Root");
572
        $this->pathFinder->getPathTo("AppBundle\\Entity\\Fizz");
573
    }
574
575
    /**
576
     * @expectedException \Mado\QueryBundle\Component\Meta\Exceptions\NestingException
577
     */
578
    public function testCatchNesting()
579
    {
580
        $this->samepleJson = [
581
            "AppBundle\\Entity\\Fizz" => [
582
                "relations" => [
583
                    "fizz" => "AppBundle\\Entity\\Fizz",
584
                ]
585
            ],
586
        ];
587
588
        $this->mapper = $this
589
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
590
            ->disableOriginalConstructor()
591
            ->getMock();
592
593
        $this->mapper->expects($this->once())
594
            ->method('getMap')
595
            ->will($this->returnValue(
596
                $this->samepleJson
597
            ));
598
599
        $this->pathFinder = new JsonPathFinder(
600
            $this->mapper
601
        );
602
603
        $this->pathFinder->setEntity("AppBundle\\Entity\\Root");
604
        $this->pathFinder->getPathTo("AppBundle\\Entity\\Fizz");
605
    }
606
607
    public function testReloadMapWheneverPathIsRequestedTwice()
608
    {
609
        $this->samepleJson = [
610
            "AppBundle\\Entity\\Foo" => [
611
                "relations" => [
612
                    "item" => "ZarroBundle\\Entity\\Item",
613
                ]
614
            ],
615
            "ZarroBundle\\Entity\\Item" => [
616
                "relations" => [
617
                    "family" => "AppBundle\\Entity\\Family",
618
                ]
619
            ],
620
        ];
621
622
        $this->mapper = $this
623
            ->getMockBuilder('Mado\QueryBundle\Component\Meta\DataMapper')
624
            ->disableOriginalConstructor()
625
            ->getMock();
626
627
        $this->mapper->expects($this->exactly(2))
628
            ->method('getMap')
629
            ->will($this->returnValue(
630
                $this->samepleJson
631
            ));
632
633
        $this->pathFinder = new JsonPathFinder(
634
            $this->mapper
635
        );
636
637
        $this->pathFinder->setQueryStartEntity("ZarroBundle\\Entity\\Item");
638
        $firstPath = $this->pathFinder->getPathToEntity("AppBundle\\Entity\\Family");
639
        $this->assertEquals('_embedded.family', $firstPath);
640
641
        $this->pathFinder->setQueryStartEntity("AppBundle\\Entity\\Foo");
642
        $secondPath = $this->pathFinder->getPathToEntity("ZarroBundle\\Entity\\Item", true);
643
        $this->assertEquals('_embedded.item', $secondPath);
644
    }
645
}
646