|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Domain\Insights; |
| 6 | + |
| 7 | +use NunoMaduro\PhpInsights\Application\Console\Formatters\PathShortener; |
| 8 | +use NunoMaduro\PhpInsights\Domain\Analyser; |
| 9 | +use NunoMaduro\PhpInsights\Domain\Insights\ForbiddenNormalClasses; |
| 10 | +use NunoMaduro\PhpInsights\Domain\Metrics\Architecture\Classes; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +final class ReadonlyClassTest extends TestCase |
| 14 | +{ |
| 15 | + public function testHasIssue(): void |
| 16 | + { |
| 17 | + if (PHP_VERSION_ID < 80200) { |
| 18 | + self::markTestSkipped('Readonly classes are only available in PHP 8.2+'); |
| 19 | + } |
| 20 | + |
| 21 | + $files = [ |
| 22 | + __DIR__ . '/Fixtures/ReadonlyClass.php', |
| 23 | + ]; |
| 24 | + |
| 25 | + $analyzer = new Analyser(); |
| 26 | + $collector = $analyzer->analyse([__DIR__ . '/Fixtures/'], $files, PathShortener::extractCommonPath($files)); |
| 27 | + $insight = new ForbiddenNormalClasses($collector, []); |
| 28 | + |
| 29 | + self::assertTrue($insight->hasIssue()); |
| 30 | + self::assertIsArray($insight->getDetails()); |
| 31 | + self::assertNotEmpty($insight->getDetails()); |
| 32 | + } |
| 33 | + |
| 34 | + public function testSkipFile(): void |
| 35 | + { |
| 36 | + $fileLocation = __DIR__ . '/Fixtures/ReadonlyClass.php'; |
| 37 | + |
| 38 | + $collection = $this->runAnalyserOnConfig( |
| 39 | + [ |
| 40 | + 'add' => [ |
| 41 | + Classes::class => [ |
| 42 | + ForbiddenNormalClasses::class, |
| 43 | + ], |
| 44 | + ], |
| 45 | + 'config' => [ |
| 46 | + ForbiddenNormalClasses::class => [ |
| 47 | + 'exclude' => [$fileLocation], |
| 48 | + ], |
| 49 | + ], |
| 50 | + ], |
| 51 | + [$fileLocation] |
| 52 | + ); |
| 53 | + |
| 54 | + $classErrors = 0; |
| 55 | + |
| 56 | + foreach ($collection->allFrom(new Classes()) as $insight) { |
| 57 | + if ($insight->hasIssue() && $insight->getInsightClass() === ForbiddenNormalClasses::class) { |
| 58 | + $classErrors++; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + self::assertEquals(0, $classErrors); |
| 63 | + } |
| 64 | +} |
0 commit comments