From 965d779d1f91f8cc394a4f0bf18457ec353d8691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Sun, 10 Dec 2023 06:09:43 +0100 Subject: [PATCH] Use the DI container in tests Overlooked these, follow-up to #223 --- ...AttributeUsagesAllowParamsMultipleTest.php | 27 +++++++++-------- tests/Usages/AttributeUsagesTest.php | 27 +++++++++-------- .../Usages/ClassConstantInvalidUsagesTest.php | 29 ++++++------------- tests/Usages/ClassConstantUsagesTest.php | 26 ++++++++--------- tests/Usages/ConstantUsagesTest.php | 22 +++++++------- tests/Usages/NamespaceUsagesTest.php | 27 +++++++++-------- tests/Usages/NamespaceUsagesTypesTest.php | 25 ++++++++-------- tests/Usages/SuperglobalUsagesTest.php | 23 ++++++++------- 8 files changed, 97 insertions(+), 109 deletions(-) diff --git a/tests/Usages/AttributeUsagesAllowParamsMultipleTest.php b/tests/Usages/AttributeUsagesAllowParamsMultipleTest.php index 0539bca..8328459 100644 --- a/tests/Usages/AttributeUsagesAllowParamsMultipleTest.php +++ b/tests/Usages/AttributeUsagesAllowParamsMultipleTest.php @@ -4,16 +4,9 @@ namespace Spaze\PHPStan\Rules\Disallowed\Usages; use Attributes\AttributeEntity; -use PHPStan\File\FileHelper; use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; -use Spaze\PHPStan\Rules\Disallowed\Allowed\Allowed; -use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath; use Spaze\PHPStan\Rules\Disallowed\DisallowedAttributeFactory; -use Spaze\PHPStan\Rules\Disallowed\File\FilePath; -use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter; -use Spaze\PHPStan\Rules\Disallowed\Identifier\Identifier; -use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedAttributeRuleErrors; use Waldo\Quux\Blade; @@ -22,19 +15,17 @@ class AttributeUsagesAllowParamsMultipleTest extends RuleTestCase protected function getRule(): Rule { - $normalizer = new Normalizer(); - $formatter = new Formatter($normalizer); - $allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FilePath(new FileHelper(__DIR__)))); + $container = self::getContainer(); return new AttributeUsages( - new DisallowedAttributeRuleErrors($allowed, new Identifier(), $formatter), - new DisallowedAttributeFactory($allowed, $normalizer), + $container->getByType(DisallowedAttributeRuleErrors::class), + $container->getByType(DisallowedAttributeFactory::class), [ [ 'attribute' => [ AttributeEntity::class, ], 'allowIn' => [ - '../src/disallowed-allow/ClassWithAttributesAllow.php', + __DIR__ . '/../src/disallowed-allow/ClassWithAttributesAllow.php', ], 'allowParamsAnywhereAnyValue' => [ [ @@ -53,7 +44,7 @@ protected function getRule(): Rule [ 'attribute' => '#[\Attributes\AttributeClass()]', 'allowIn' => [ - '../src/disallowed-allow/ClassWithAttributesAllow.php', + __DIR__ . '/../src/disallowed-allow/ClassWithAttributesAllow.php', ], ], ] @@ -124,4 +115,12 @@ public function testRule(): void ]); } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/../../extension.neon', + ]; + } + } diff --git a/tests/Usages/AttributeUsagesTest.php b/tests/Usages/AttributeUsagesTest.php index 55769c9..cc163a9 100644 --- a/tests/Usages/AttributeUsagesTest.php +++ b/tests/Usages/AttributeUsagesTest.php @@ -4,16 +4,9 @@ namespace Spaze\PHPStan\Rules\Disallowed\Usages; use Attributes\AttributeEntity; -use PHPStan\File\FileHelper; use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; -use Spaze\PHPStan\Rules\Disallowed\Allowed\Allowed; -use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath; use Spaze\PHPStan\Rules\Disallowed\DisallowedAttributeFactory; -use Spaze\PHPStan\Rules\Disallowed\File\FilePath; -use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter; -use Spaze\PHPStan\Rules\Disallowed\Identifier\Identifier; -use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedAttributeRuleErrors; class AttributeUsagesTest extends RuleTestCase @@ -21,19 +14,17 @@ class AttributeUsagesTest extends RuleTestCase protected function getRule(): Rule { - $normalizer = new Normalizer(); - $formatter = new Formatter($normalizer); - $allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FilePath(new FileHelper(__DIR__)))); + $container = self::getContainer(); return new AttributeUsages( - new DisallowedAttributeRuleErrors($allowed, new Identifier(), $formatter), - new DisallowedAttributeFactory($allowed, $normalizer), + $container->getByType(DisallowedAttributeRuleErrors::class), + $container->getByType(DisallowedAttributeFactory::class), [ [ 'attribute' => [ AttributeEntity::class, ], 'allowIn' => [ - '../src/disallowed-allow/ClassWithAttributesAllow.php', + __DIR__ . '/../src/disallowed-allow/ClassWithAttributesAllow.php', ], 'allowParamsAnywhereAnyValue' => [ [ @@ -45,7 +36,7 @@ protected function getRule(): Rule [ 'attribute' => '#[\Attributes\AttributeClass()]', 'allowIn' => [ - '../src/disallowed-allow/ClassWithAttributesAllow.php', + __DIR__ . '/../src/disallowed-allow/ClassWithAttributesAllow.php', ], ], ] @@ -158,4 +149,12 @@ public function testRule(): void ]); } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/../../extension.neon', + ]; + } + } diff --git a/tests/Usages/ClassConstantInvalidUsagesTest.php b/tests/Usages/ClassConstantInvalidUsagesTest.php index 9dafff1..f7ca026 100644 --- a/tests/Usages/ClassConstantInvalidUsagesTest.php +++ b/tests/Usages/ClassConstantInvalidUsagesTest.php @@ -3,17 +3,9 @@ namespace Spaze\PHPStan\Rules\Disallowed\Usages; -use PHPStan\File\FileHelper; use PHPStan\Rules\Rule; use PHPStan\ShouldNotHappenException; use PHPStan\Testing\RuleTestCase; -use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath; -use Spaze\PHPStan\Rules\Disallowed\DisallowedConstantFactory; -use Spaze\PHPStan\Rules\Disallowed\File\FilePath; -use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter; -use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer; -use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedConstantRuleErrors; -use Spaze\PHPStan\Rules\Disallowed\Type\TypeResolver; class ClassConstantInvalidUsagesTest extends RuleTestCase { @@ -23,18 +15,7 @@ class ClassConstantInvalidUsagesTest extends RuleTestCase */ protected function getRule(): Rule { - $normalizer = new Normalizer(); - $formatter = new Formatter($normalizer); - return new ClassConstantUsages( - new DisallowedConstantRuleErrors( - new AllowedPath(new FilePath(new FileHelper(__DIR__))), - $formatter - ), - new DisallowedConstantFactory($normalizer), - new TypeResolver(), - $formatter, - [] - ); + return self::getContainer()->getByType(ClassConstantUsages::class); } @@ -63,4 +44,12 @@ public function testRule(): void ]); } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/../../extension.neon', + ]; + } + } diff --git a/tests/Usages/ClassConstantUsagesTest.php b/tests/Usages/ClassConstantUsagesTest.php index 06a2513..34d3752 100644 --- a/tests/Usages/ClassConstantUsagesTest.php +++ b/tests/Usages/ClassConstantUsagesTest.php @@ -3,15 +3,11 @@ namespace Spaze\PHPStan\Rules\Disallowed\Usages; -use PHPStan\File\FileHelper; use PHPStan\Rules\Rule; use PHPStan\ShouldNotHappenException; use PHPStan\Testing\RuleTestCase; -use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath; use Spaze\PHPStan\Rules\Disallowed\DisallowedConstantFactory; -use Spaze\PHPStan\Rules\Disallowed\File\FilePath; use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter; -use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedConstantRuleErrors; use Spaze\PHPStan\Rules\Disallowed\Type\TypeResolver; @@ -23,16 +19,12 @@ class ClassConstantUsagesTest extends RuleTestCase */ protected function getRule(): Rule { - $normalizer = new Normalizer(); - $formatter = new Formatter($normalizer); + $container = self::getContainer(); return new ClassConstantUsages( - new DisallowedConstantRuleErrors( - new AllowedPath(new FilePath(new FileHelper(__DIR__))), - $formatter - ), - new DisallowedConstantFactory($normalizer), - new TypeResolver(), - $formatter, + $container->getByType(DisallowedConstantRuleErrors::class), + $container->getByType(DisallowedConstantFactory::class), + $container->getByType(TypeResolver::class), + $container->getByType(Formatter::class), [ [ 'class' => '\Inheritance\Base', @@ -148,4 +140,12 @@ public function testRule(): void $this->analyse([__DIR__ . '/../src/disallowed-allow/constantUsages.php'], []); } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/../../extension.neon', + ]; + } + } diff --git a/tests/Usages/ConstantUsagesTest.php b/tests/Usages/ConstantUsagesTest.php index 052f5a4..5f81177 100644 --- a/tests/Usages/ConstantUsagesTest.php +++ b/tests/Usages/ConstantUsagesTest.php @@ -3,14 +3,10 @@ namespace Spaze\PHPStan\Rules\Disallowed\Usages; -use PHPStan\File\FileHelper; use PHPStan\Rules\Rule; use PHPStan\ShouldNotHappenException; use PHPStan\Testing\RuleTestCase; -use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath; use Spaze\PHPStan\Rules\Disallowed\DisallowedConstantFactory; -use Spaze\PHPStan\Rules\Disallowed\File\FilePath; -use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter; use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedConstantRuleErrors; @@ -23,12 +19,10 @@ class ConstantUsagesTest extends RuleTestCase protected function getRule(): Rule { $normalizer = new Normalizer(); + $container = self::getContainer(); return new ConstantUsages( - new DisallowedConstantRuleErrors( - new AllowedPath(new FilePath(new FileHelper(__DIR__))), - new Formatter($normalizer) - ), - new DisallowedConstantFactory($normalizer), + $container->getByType(DisallowedConstantRuleErrors::class), + $container->getByType(DisallowedConstantFactory::class), [ [ 'constant' => [ @@ -54,7 +48,7 @@ protected function getRule(): Rule [ 'constant' => 'PHP_EOL', 'allowExceptIn' => [ - '../src/disallowed/*.php', + __DIR__ . '/../src/disallowed/*.php', ], ], ] @@ -90,4 +84,12 @@ public function testRule(): void $this->analyse([__DIR__ . '/../src/disallowed-allow/constantUsages.php'], []); } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/../../extension.neon', + ]; + } + } diff --git a/tests/Usages/NamespaceUsagesTest.php b/tests/Usages/NamespaceUsagesTest.php index 8bbfcf5..1e8c8da 100644 --- a/tests/Usages/NamespaceUsagesTest.php +++ b/tests/Usages/NamespaceUsagesTest.php @@ -3,14 +3,9 @@ namespace Spaze\PHPStan\Rules\Disallowed\Usages; -use PHPStan\File\FileHelper; use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; -use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath; use Spaze\PHPStan\Rules\Disallowed\DisallowedNamespaceFactory; -use Spaze\PHPStan\Rules\Disallowed\File\FilePath; -use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter; -use Spaze\PHPStan\Rules\Disallowed\Identifier\Identifier; use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedNamespaceRuleErrors; @@ -19,15 +14,11 @@ class NamespaceUsagesTest extends RuleTestCase protected function getRule(): Rule { - $normalizer = new Normalizer(); + $container = self::getContainer(); return new NamespaceUsages( - new DisallowedNamespaceRuleErrors( - new AllowedPath(new FilePath(new FileHelper(__DIR__))), - new Identifier(), - new Formatter($normalizer) - ), - new DisallowedNamespaceFactory($normalizer), - $normalizer, + $container->getByType(DisallowedNamespaceRuleErrors::class), + $container->getByType(DisallowedNamespaceFactory::class), + $container->getByType(Normalizer::class), [ [ 'namespace' => 'Framew*rk\Some*', @@ -78,7 +69,7 @@ protected function getRule(): Rule 'namespace' => 'ZipArchive', 'message' => 'use clippy instead of zippy', 'disallowIn' => [ - '../src/disallowed/*.php', + __DIR__ . '/../src/disallowed/*.php', ], ], ] @@ -158,4 +149,12 @@ public function testRule(): void $this->analyse([__DIR__ . '/../src/disallowed-allow/namespaceUsages.php'], []); } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/../../extension.neon', + ]; + } + } diff --git a/tests/Usages/NamespaceUsagesTypesTest.php b/tests/Usages/NamespaceUsagesTypesTest.php index 12a4d81..eb97e7b 100644 --- a/tests/Usages/NamespaceUsagesTypesTest.php +++ b/tests/Usages/NamespaceUsagesTypesTest.php @@ -3,14 +3,9 @@ namespace Spaze\PHPStan\Rules\Disallowed\Usages; -use PHPStan\File\FileHelper; use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; -use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath; use Spaze\PHPStan\Rules\Disallowed\DisallowedNamespaceFactory; -use Spaze\PHPStan\Rules\Disallowed\File\FilePath; -use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter; -use Spaze\PHPStan\Rules\Disallowed\Identifier\Identifier; use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedNamespaceRuleErrors; @@ -22,15 +17,11 @@ class NamespaceUsagesTypesTest extends RuleTestCase protected function getRule(): Rule { - $normalizer = new Normalizer(); + $container = self::getContainer(); return new NamespaceUsages( - new DisallowedNamespaceRuleErrors( - new AllowedPath(new FilePath(new FileHelper(__DIR__))), - new Identifier(), - new Formatter($normalizer) - ), - new DisallowedNamespaceFactory($normalizer), - $normalizer, + $container->getByType(DisallowedNamespaceRuleErrors::class), + $container->getByType(DisallowedNamespaceFactory::class), + $container->getByType(Normalizer::class), [ [ 'class' => 'Waldo\Quux\Blade', @@ -118,4 +109,12 @@ public function testRule(): void ]); } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/../../extension.neon', + ]; + } + } diff --git a/tests/Usages/SuperglobalUsagesTest.php b/tests/Usages/SuperglobalUsagesTest.php index 6a93f62..8caca8a 100644 --- a/tests/Usages/SuperglobalUsagesTest.php +++ b/tests/Usages/SuperglobalUsagesTest.php @@ -3,15 +3,10 @@ namespace Spaze\PHPStan\Rules\Disallowed\Usages; -use PHPStan\File\FileHelper; use PHPStan\Rules\Rule; use PHPStan\ShouldNotHappenException; use PHPStan\Testing\RuleTestCase; -use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath; use Spaze\PHPStan\Rules\Disallowed\DisallowedSuperglobalFactory; -use Spaze\PHPStan\Rules\Disallowed\File\FilePath; -use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter; -use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedVariableRuleErrors; class SuperglobalUsagesTest extends RuleTestCase @@ -22,12 +17,10 @@ class SuperglobalUsagesTest extends RuleTestCase */ protected function getRule(): Rule { + $container = self::getContainer(); return new VariableUsages( - new DisallowedVariableRuleErrors( - new AllowedPath(new FilePath(new FileHelper(__DIR__))), - new Formatter(new Normalizer()) - ), - (new DisallowedSuperglobalFactory())->getDisallowedVariables([ + $container->getByType(DisallowedVariableRuleErrors::class), + $container->getByType(DisallowedSuperglobalFactory::class)->getDisallowedVariables([ [ 'superglobal' => '$GLOBALS', 'message' => 'the cake is a lie', @@ -53,7 +46,7 @@ protected function getRule(): Rule 'superglobal' => '$_REQUEST', 'message' => 'so $_GET or $_POST?', 'disallowIn' => [ - '../src/disallowed/*.php', + __DIR__ . '/../src/disallowed/*.php', ], ], ]) @@ -92,4 +85,12 @@ public function testRule(): void $this->analyse([__DIR__ . '/../src/disallowed-allow/superglobalUsages.php'], []); } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/../../extension.neon', + ]; + } + }