2
2
3
3
declare (strict_types=1 );
4
4
5
- namespace Arkitect \Tests \Unit \Analyzer ;
5
+ namespace Arkitect \Tests \Unit \Analyzer \ FileParser ;
6
6
7
7
use Arkitect \Analyzer \ClassDependency ;
8
8
use Arkitect \Analyzer \ClassDescription ;
@@ -38,17 +38,14 @@ public function __construct(Request $request)
38
38
}
39
39
EOF;
40
40
41
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
42
- $ fp ->parse ($ code , 'path/to/class.php ' );
43
-
44
- $ violations = new Violations ();
41
+ $ classDescriptions = $ this ->parseCode ($ code );
45
42
46
43
$ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['Foo ' ]);
47
- $ dependsOnTheseNamespaces -> evaluate ( $ fp -> getClassDescriptions ()[ 0 ], $ violations , ' because ' );
44
+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ classDescriptions [ 0 ] );
48
45
49
46
self ::assertCount (2 , $ violations );
50
- self ::assertEquals ('path/to/class.php ' , $ violations ->get (0 )->getFilePath ());
51
- self ::assertEquals ('path/to/class.php ' , $ violations ->get (1 )->getFilePath ());
47
+ self ::assertEquals ('relativePathName ' , $ violations ->get (0 )->getFilePath ());
48
+ self ::assertEquals ('relativePathName ' , $ violations ->get (1 )->getFilePath ());
52
49
}
53
50
54
51
public function test_should_parse_instanceof (): void
@@ -69,9 +66,7 @@ public function bar($a, $b)
69
66
}
70
67
EOF;
71
68
72
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
73
- $ fp ->parse ($ code , 'relativePathName ' );
74
- $ cd = $ fp ->getClassDescriptions ();
69
+ $ cd = $ this ->parseCode ($ code );
75
70
76
71
self ::assertCount (1 , $ cd );
77
72
self ::assertCount (1 , $ cd [0 ]->getDependencies ());
@@ -97,9 +92,7 @@ class Cat implements AnInterface
97
92
}
98
93
EOF;
99
94
100
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
101
- $ fp ->parse ($ code , 'relativePathName ' );
102
- $ cd = $ fp ->getClassDescriptions ();
95
+ $ cd = $ this ->parseCode ($ code );
103
96
104
97
self ::assertCount (2 , $ cd );
105
98
self ::assertInstanceOf (ClassDescription::class, $ cd [0 ]);
@@ -141,9 +134,7 @@ class Cat implements AnInterface
141
134
}
142
135
EOF;
143
136
144
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
145
- $ fp ->parse ($ code , 'relativePathName ' );
146
- $ cd = $ fp ->getClassDescriptions ();
137
+ $ cd = $ this ->parseCode ($ code );
147
138
148
139
self ::assertCount (2 , $ cd );
149
140
self ::assertInstanceOf (ClassDescription::class, $ cd [0 ]);
@@ -183,9 +174,7 @@ class Cat implements AnInterface
183
174
}
184
175
EOF;
185
176
186
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
187
- $ fp ->parse ($ code , 'relativePathName ' );
188
- $ cd = $ fp ->getClassDescriptions ();
177
+ $ cd = $ this ->parseCode ($ code );
189
178
190
179
self ::assertCount (2 , $ cd );
191
180
self ::assertInstanceOf (ClassDescription::class, $ cd [0 ]);
@@ -217,10 +206,8 @@ class Cat extends Animal
217
206
}
218
207
EOF;
219
208
220
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
221
- $ fp ->parse ($ code , 'relativePathName ' );
222
-
223
- $ cd = $ fp ->getClassDescriptions ()[1 ];
209
+ $ cd = $ this ->parseCode ($ code );
210
+ $ cd = $ cd [1 ];
224
211
225
212
self ::assertEquals ('Root\Animals\Animal ' , $ cd ->getExtends ()[0 ]->toString ());
226
213
}
@@ -245,10 +232,8 @@ public function methodWithAnonymous(): void
245
232
}
246
233
EOF;
247
234
248
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
249
- $ fp ->parse ($ code , 'relativePathName ' );
250
-
251
- $ cd = $ fp ->getClassDescriptions ()[1 ];
235
+ $ cd = $ this ->parseCode ($ code );
236
+ $ cd = $ cd [1 ];
252
237
253
238
self ::assertEquals ('Root\Animals\Animal ' , $ cd ->getExtends ()[0 ]->toString ());
254
239
}
@@ -272,14 +257,10 @@ public function __construct(Request $request)
272
257
}
273
258
EOF;
274
259
275
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
276
- $ fp ->parse ($ code , 'relativePathName ' );
277
- $ cd = $ fp ->getClassDescriptions ();
278
-
279
- $ violations = new Violations ();
260
+ $ cd = $ this ->parseCode ($ code );
280
261
281
262
$ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['Foo ' , 'Symfony ' , 'Doctrine ' ]);
282
- $ dependsOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , ' we want to add this rule for our software ' );
263
+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ cd [0 ]);
283
264
284
265
self ::assertCount (0 , $ violations );
285
266
}
@@ -308,9 +289,7 @@ public function __construct(Request $request, ?Nullable $nullable)
308
289
}
309
290
EOF;
310
291
311
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
312
- $ fp ->parse ($ code , 'relativePathName ' );
313
- $ cd = $ fp ->getClassDescriptions ();
292
+ $ cd = $ this ->parseCode ($ code );
314
293
315
294
$ expectedDependencies = [
316
295
new ClassDependency ('Foo\Baz\Baz ' , 10 ),
@@ -340,15 +319,10 @@ public function __construct()
340
319
}
341
320
EOF;
342
321
343
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
344
- $ fp ->parse ($ code , 'relativePathName ' );
345
-
346
- $ cd = $ fp ->getClassDescriptions ();
347
-
348
- $ violations = new Violations ();
322
+ $ cd = $ this ->parseCode ($ code );
349
323
350
324
$ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['Foo ' , 'Symfony ' , 'Doctrine ' ]);
351
- $ dependsOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , ' we want to add this rule for our software ' );
325
+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ cd [0 ]);
352
326
353
327
self ::assertCount (0 , $ violations );
354
328
}
@@ -379,15 +353,10 @@ public function doSomething(self $self, static $static)
379
353
}
380
354
EOF;
381
355
382
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
383
- $ fp ->parse ($ code , 'relativePathName ' );
384
-
385
- $ cd = $ fp ->getClassDescriptions ();
386
-
387
- $ violations = new Violations ();
356
+ $ cd = $ this ->parseCode ($ code );
388
357
389
358
$ notHaveDependencyOutsideNamespace = new NotHaveDependencyOutsideNamespace ('Root\Animals ' );
390
- $ notHaveDependencyOutsideNamespace -> evaluate ( $ cd [0 ], $ violations , ' we want to add this rule for our software ' );
359
+ $ violations = $ this -> evaluateRule ( $ notHaveDependencyOutsideNamespace , $ cd [0 ]);
391
360
392
361
self ::assertCount (0 , $ violations );
393
362
}
@@ -411,15 +380,10 @@ public function foo()
411
380
}
412
381
EOF;
413
382
414
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
415
- $ fp ->parse ($ code , 'relativePathName ' );
416
-
417
- $ cd = $ fp ->getClassDescriptions ();
418
-
419
- $ violations = new Violations ();
383
+ $ cd = $ this ->parseCode ($ code );
420
384
421
385
$ dependsOnlyOnTheseNamespaces = new DependsOnlyOnTheseNamespaces ();
422
- $ dependsOnlyOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , ' we want to add this rule for our software ' );
386
+ $ violations = $ this -> evaluateRule ( $ dependsOnlyOnTheseNamespaces , $ cd [0 ]);
423
387
424
388
self ::assertCount (1 , $ violations );
425
389
}
@@ -452,15 +416,10 @@ public function getStatic(): self
452
416
}
453
417
EOF;
454
418
455
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
456
- $ fp ->parse ($ code , 'relativePathName ' );
457
-
458
- $ cd = $ fp ->getClassDescriptions ();
459
-
460
- $ violations = new Violations ();
419
+ $ cd = $ this ->parseCode ($ code );
461
420
462
421
$ notHaveDependencyOutsideNamespace = new NotHaveDependencyOutsideNamespace ('Root\Cars ' );
463
- $ notHaveDependencyOutsideNamespace -> evaluate ( $ cd [0 ], $ violations , ' we want to add this rule for our software ' );
422
+ $ violations = $ this -> evaluateRule ( $ notHaveDependencyOutsideNamespace , $ cd [0 ]);
464
423
465
424
self ::assertCount (1 , $ violations );
466
425
}
@@ -484,15 +443,11 @@ class Test implements Order
484
443
485
444
EOF;
486
445
487
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_1 );
488
- $ fp ->parse ($ code , 'relativePathName ' );
489
-
490
- $ cd = $ fp ->getClassDescriptions ()[2 ]; // class Test
491
-
492
- $ violations = new Violations ();
446
+ $ cd = $ this ->parseCode ($ code , TargetPhpVersion::PHP_8_1 );
447
+ $ cd = $ cd [2 ]; // class Test
493
448
494
449
$ implement = new Implement ('Foo\Order ' );
495
- $ implement -> evaluate ( $ cd , $ violations , ' we want to add this rule for our software ' );
450
+ $ violations = $ this -> evaluateRule ( $ implement , $ cd );
496
451
497
452
self ::assertCount (0 , $ violations );
498
453
}
@@ -512,15 +467,10 @@ public function getBookList(): QueryBuilder;
512
467
}
513
468
EOF;
514
469
515
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_1 );
516
- $ fp ->parse ($ code , 'relativePathName ' );
517
-
518
- $ cd = $ fp ->getClassDescriptions ();
519
-
520
- $ violations = new Violations ();
470
+ $ cd = $ this ->parseCode ($ code , TargetPhpVersion::PHP_8_1 );
521
471
522
472
$ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['MyProject\AppBundle\Application ' ]);
523
- $ dependsOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , ' we want to add this rule for our software ' );
473
+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ cd [0 ]);
524
474
525
475
self ::assertCount (1 , $ violations );
526
476
}
@@ -549,10 +499,7 @@ public function foobar();
549
499
}
550
500
EOF;
551
501
552
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_1 );
553
- $ fp ->parse ($ code , 'relativePathName ' );
554
-
555
- $ cd = $ fp ->getClassDescriptions ();
502
+ $ cd = $ this ->parseCode ($ code , TargetPhpVersion::PHP_8_1 );
556
503
557
504
self ::assertCount (3 , $ cd );
558
505
self ::assertEquals ('MyProject\AppBundle\Application\FooAble ' , $ cd [2 ]->getExtends ()[0 ]->toString ());
@@ -583,14 +530,10 @@ public function getRequest(): Request //the violations is reported here
583
530
}
584
531
EOF;
585
532
586
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_7_4 );
587
- $ fp ->parse ($ code , 'relativePathName ' );
588
- $ cd = $ fp ->getClassDescriptions ();
589
-
590
- $ violations = new Violations ();
533
+ $ cd = $ this ->parseCode ($ code );
591
534
592
535
$ dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces (['Foo ' , 'Symfony ' , 'Doctrine ' ]);
593
- $ dependsOnTheseNamespaces -> evaluate ( $ cd [0 ], $ violations , ' we want to add this rule for our software ' );
536
+ $ violations = $ this -> evaluateRule ( $ dependsOnTheseNamespaces , $ cd [0 ]);
594
537
595
538
self ::assertCount (0 , $ violations );
596
539
}
@@ -608,13 +551,9 @@ public function __construct() {
608
551
}
609
552
EOF;
610
553
611
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_4 );
612
- $ fp ->parse ($ code , 'relativePathName ' );
613
-
614
- $ cd = $ fp ->getClassDescriptions ();
615
- $ violations = new Violations ();
554
+ $ cd = $ this ->parseCode ($ code , TargetPhpVersion::PHP_8_4 );
616
555
$ isFinal = new IsFinal ();
617
- $ isFinal -> evaluate ( $ cd [0 ], $ violations , ' we want to add this rule for our software ' );
556
+ $ violations = $ this -> evaluateRule ( $ isFinal , $ cd [0 ]);
618
557
619
558
self ::assertCount (0 , $ violations );
620
559
}
@@ -634,13 +573,9 @@ abstract public function foo() {}
634
573
}
635
574
EOF;
636
575
637
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_4 );
638
- $ fp ->parse ($ code , 'relativePathName ' );
639
-
640
- $ cd = $ fp ->getClassDescriptions ();
641
- $ violations = new Violations ();
576
+ $ cd = $ this ->parseCode ($ code , TargetPhpVersion::PHP_8_4 );
642
577
$ isAbstract = new IsAbstract ();
643
- $ isAbstract -> evaluate ( $ cd [0 ], $ violations , ' we want to add this rule for our software ' );
578
+ $ violations = $ this -> evaluateRule ( $ isAbstract , $ cd [0 ]);
644
579
645
580
self ::assertCount (0 , $ violations );
646
581
}
@@ -658,14 +593,26 @@ public function __construct() {
658
593
}
659
594
EOF;
660
595
661
- $ fp = FileParserFactory::forPhpVersion (TargetPhpVersion::PHP_8_4 );
596
+ $ cd = $ this ->parseCode ($ code , TargetPhpVersion::PHP_8_4 );
597
+ $ isReadOnly = new IsReadonly ();
598
+ $ violations = $ this ->evaluateRule ($ isReadOnly , $ cd [0 ]);
599
+
600
+ self ::assertCount (0 , $ violations );
601
+ }
602
+
603
+ private function parseCode (string $ code , ?string $ version = null ): array
604
+ {
605
+ $ fp = FileParserFactory::forPhpVersion ($ version ?? TargetPhpVersion::PHP_7_4 );
662
606
$ fp ->parse ($ code , 'relativePathName ' );
663
607
664
- $ cd = $ fp ->getClassDescriptions ();
608
+ return $ fp ->getClassDescriptions ();
609
+ }
610
+
611
+ private function evaluateRule ($ rule , ClassDescription $ classDescription ): Violations
612
+ {
665
613
$ violations = new Violations ();
666
- $ isReadOnly = new IsReadonly ();
667
- $ isReadOnly ->evaluate ($ cd [0 ], $ violations , 'we want to add this rule for our software ' );
614
+ $ rule ->evaluate ($ classDescription , $ violations , 'we want to add this rule for our software ' );
668
615
669
- self :: assertCount ( 0 , $ violations) ;
616
+ return $ violations ;
670
617
}
671
618
}
0 commit comments