Skip to content

Commit 646fa62

Browse files
authored
Merge pull request #525 from phparkitect/refactor/reduce-test-duplication
2 parents bfeca55 + 84f991b commit 646fa62

File tree

2 files changed

+60
-113
lines changed

2 files changed

+60
-113
lines changed

.github/workflows/build.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444

4545
- name: Coding Standard Checks
4646
if: ${{ matrix.php-versions == '7.4' }}
47-
run: PHP_CS_FIXER_IGNORE_ENV=1 ./bin/php-cs-fixer fix --dry-run -v
47+
run: PHP_CS_FIXER_IGNORE_ENV=1 ./bin/php-cs-fixer fix --dry-run --diff
4848

4949
- name: Static Analysis
5050
if: ${{ matrix.php-versions == '7.4' }}
@@ -84,7 +84,7 @@ jobs:
8484

8585
- name: "Rename phar to avoid conflicts"
8686
run: mv ./phparkitect.phar phparkitect-${{ github.sha }}.phar
87-
87+
8888
- name: "Upload phar file artifact"
8989
uses: actions/upload-artifact@v4
9090
with:
@@ -127,27 +127,27 @@ jobs:
127127
with:
128128
# will download phar in project root
129129
name: phar-artifact
130-
130+
131131
- name: Import GPG key
132132
uses: crazy-max/ghaction-import-gpg@v6
133133
with:
134134
gpg_private_key: ${{ secrets.GPG_KEY_47CD54B6398FE21B3709D0A4D9C905CED1932CA2 }}
135135
passphrase: ${{ secrets.GPG_KEY_47CD54B6398FE21B3709D0A4D9C905CED1932CA2_PASSPHRASE }}
136136

137137
- name: Rename phar
138-
run: mv ./phparkitect-${{ github.sha }}.phar phparkitect.phar
138+
run: mv ./phparkitect-${{ github.sha }}.phar phparkitect.phar
139139

140140
- name: Sign the PHAR
141-
run: |
141+
run: |
142142
gpg --local-user 47CD54B6398FE21B3709D0A4D9C905CED1932CA2 \
143143
--batch \
144144
--yes \
145145
--passphrase="${{ secrets.GPG_KEY_47CD54B6398FE21B3709D0A4D9C905CED1932CA2_PASSPHRASE }}" \
146146
--detach-sign \
147147
--output ./phparkitect.phar.asc \
148148
./phparkitect.phar
149-
150-
- name: Add phar to the release
149+
150+
- name: Add phar to the release
151151
uses: softprops/action-gh-release@v2
152152
with:
153153
token: ${{ secrets.GITHUB_TOKEN }}

tests/Unit/Analyzer/FileParser/CanParseClassTest.php

Lines changed: 53 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Arkitect\Tests\Unit\Analyzer;
5+
namespace Arkitect\Tests\Unit\Analyzer\FileParser;
66

77
use Arkitect\Analyzer\ClassDependency;
88
use Arkitect\Analyzer\ClassDescription;
@@ -38,17 +38,14 @@ public function __construct(Request $request)
3838
}
3939
EOF;
4040

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);
4542

4643
$dependsOnTheseNamespaces = new DependsOnlyOnTheseNamespaces(['Foo']);
47-
$dependsOnTheseNamespaces->evaluate($fp->getClassDescriptions()[0], $violations, 'because');
44+
$violations = $this->evaluateRule($dependsOnTheseNamespaces, $classDescriptions[0]);
4845

4946
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());
5249
}
5350

5451
public function test_should_parse_instanceof(): void
@@ -69,9 +66,7 @@ public function bar($a, $b)
6966
}
7067
EOF;
7168

72-
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_7_4);
73-
$fp->parse($code, 'relativePathName');
74-
$cd = $fp->getClassDescriptions();
69+
$cd = $this->parseCode($code);
7570

7671
self::assertCount(1, $cd);
7772
self::assertCount(1, $cd[0]->getDependencies());
@@ -97,9 +92,7 @@ class Cat implements AnInterface
9792
}
9893
EOF;
9994

100-
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_7_4);
101-
$fp->parse($code, 'relativePathName');
102-
$cd = $fp->getClassDescriptions();
95+
$cd = $this->parseCode($code);
10396

10497
self::assertCount(2, $cd);
10598
self::assertInstanceOf(ClassDescription::class, $cd[0]);
@@ -141,9 +134,7 @@ class Cat implements AnInterface
141134
}
142135
EOF;
143136

144-
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_7_4);
145-
$fp->parse($code, 'relativePathName');
146-
$cd = $fp->getClassDescriptions();
137+
$cd = $this->parseCode($code);
147138

148139
self::assertCount(2, $cd);
149140
self::assertInstanceOf(ClassDescription::class, $cd[0]);
@@ -183,9 +174,7 @@ class Cat implements AnInterface
183174
}
184175
EOF;
185176

186-
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_7_4);
187-
$fp->parse($code, 'relativePathName');
188-
$cd = $fp->getClassDescriptions();
177+
$cd = $this->parseCode($code);
189178

190179
self::assertCount(2, $cd);
191180
self::assertInstanceOf(ClassDescription::class, $cd[0]);
@@ -217,10 +206,8 @@ class Cat extends Animal
217206
}
218207
EOF;
219208

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];
224211

225212
self::assertEquals('Root\Animals\Animal', $cd->getExtends()[0]->toString());
226213
}
@@ -245,10 +232,8 @@ public function methodWithAnonymous(): void
245232
}
246233
EOF;
247234

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];
252237

253238
self::assertEquals('Root\Animals\Animal', $cd->getExtends()[0]->toString());
254239
}
@@ -272,14 +257,10 @@ public function __construct(Request $request)
272257
}
273258
EOF;
274259

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);
280261

281262
$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]);
283264

284265
self::assertCount(0, $violations);
285266
}
@@ -308,9 +289,7 @@ public function __construct(Request $request, ?Nullable $nullable)
308289
}
309290
EOF;
310291

311-
$fp = FileParserFactory::forPhpVersion(TargetPhpVersion::PHP_7_4);
312-
$fp->parse($code, 'relativePathName');
313-
$cd = $fp->getClassDescriptions();
292+
$cd = $this->parseCode($code);
314293

315294
$expectedDependencies = [
316295
new ClassDependency('Foo\Baz\Baz', 10),
@@ -340,15 +319,10 @@ public function __construct()
340319
}
341320
EOF;
342321

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);
349323

350324
$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]);
352326

353327
self::assertCount(0, $violations);
354328
}
@@ -379,15 +353,10 @@ public function doSomething(self $self, static $static)
379353
}
380354
EOF;
381355

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);
388357

389358
$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]);
391360

392361
self::assertCount(0, $violations);
393362
}
@@ -411,15 +380,10 @@ public function foo()
411380
}
412381
EOF;
413382

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);
420384

421385
$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]);
423387

424388
self::assertCount(1, $violations);
425389
}
@@ -452,15 +416,10 @@ public function getStatic(): self
452416
}
453417
EOF;
454418

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);
461420

462421
$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]);
464423

465424
self::assertCount(1, $violations);
466425
}
@@ -484,15 +443,11 @@ class Test implements Order
484443

485444
EOF;
486445

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
493448

494449
$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);
496451

497452
self::assertCount(0, $violations);
498453
}
@@ -512,15 +467,10 @@ public function getBookList(): QueryBuilder;
512467
}
513468
EOF;
514469

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);
521471

522472
$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]);
524474

525475
self::assertCount(1, $violations);
526476
}
@@ -549,10 +499,7 @@ public function foobar();
549499
}
550500
EOF;
551501

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);
556503

557504
self::assertCount(3, $cd);
558505
self::assertEquals('MyProject\AppBundle\Application\FooAble', $cd[2]->getExtends()[0]->toString());
@@ -583,14 +530,10 @@ public function getRequest(): Request //the violations is reported here
583530
}
584531
EOF;
585532

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);
591534

592535
$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]);
594537

595538
self::assertCount(0, $violations);
596539
}
@@ -608,13 +551,9 @@ public function __construct() {
608551
}
609552
EOF;
610553

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);
616555
$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]);
618557

619558
self::assertCount(0, $violations);
620559
}
@@ -634,13 +573,9 @@ abstract public function foo() {}
634573
}
635574
EOF;
636575

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);
642577
$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]);
644579

645580
self::assertCount(0, $violations);
646581
}
@@ -658,14 +593,26 @@ public function __construct() {
658593
}
659594
EOF;
660595

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);
662606
$fp->parse($code, 'relativePathName');
663607

664-
$cd = $fp->getClassDescriptions();
608+
return $fp->getClassDescriptions();
609+
}
610+
611+
private function evaluateRule($rule, ClassDescription $classDescription): Violations
612+
{
665613
$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');
668615

669-
self::assertCount(0, $violations);
616+
return $violations;
670617
}
671618
}

0 commit comments

Comments
 (0)