Skip to content

Commit

Permalink
Use the DI container in tests (#228)
Browse files Browse the repository at this point in the history
Overlooked these, follow-up to #223
  • Loading branch information
spaze authored Dec 10, 2023
2 parents 4613004 + 965d779 commit bf6aa80
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 109 deletions.
27 changes: 13 additions & 14 deletions tests/Usages/AttributeUsagesAllowParamsMultipleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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' => [
[
Expand All @@ -53,7 +44,7 @@ protected function getRule(): Rule
[
'attribute' => '#[\Attributes\AttributeClass()]',
'allowIn' => [
'../src/disallowed-allow/ClassWithAttributesAllow.php',
__DIR__ . '/../src/disallowed-allow/ClassWithAttributesAllow.php',
],
],
]
Expand Down Expand Up @@ -124,4 +115,12 @@ public function testRule(): void
]);
}


public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../extension.neon',
];
}

}
27 changes: 13 additions & 14 deletions tests/Usages/AttributeUsagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,27 @@
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
{

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' => [
[
Expand All @@ -45,7 +36,7 @@ protected function getRule(): Rule
[
'attribute' => '#[\Attributes\AttributeClass()]',
'allowIn' => [
'../src/disallowed-allow/ClassWithAttributesAllow.php',
__DIR__ . '/../src/disallowed-allow/ClassWithAttributesAllow.php',
],
],
]
Expand Down Expand Up @@ -158,4 +149,12 @@ public function testRule(): void
]);
}


public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../extension.neon',
];
}

}
29 changes: 9 additions & 20 deletions tests/Usages/ClassConstantInvalidUsagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}


Expand Down Expand Up @@ -63,4 +44,12 @@ public function testRule(): void
]);
}


public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../extension.neon',
];
}

}
26 changes: 13 additions & 13 deletions tests/Usages/ClassConstantUsagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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',
Expand Down Expand Up @@ -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',
];
}

}
22 changes: 12 additions & 10 deletions tests/Usages/ConstantUsagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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' => [
Expand All @@ -54,7 +48,7 @@ protected function getRule(): Rule
[
'constant' => 'PHP_EOL',
'allowExceptIn' => [
'../src/disallowed/*.php',
__DIR__ . '/../src/disallowed/*.php',
],
],
]
Expand Down Expand Up @@ -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',
];
}

}
27 changes: 13 additions & 14 deletions tests/Usages/NamespaceUsagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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*',
Expand Down Expand Up @@ -78,7 +69,7 @@ protected function getRule(): Rule
'namespace' => 'ZipArchive',
'message' => 'use clippy instead of zippy',
'disallowIn' => [
'../src/disallowed/*.php',
__DIR__ . '/../src/disallowed/*.php',
],
],
]
Expand Down Expand Up @@ -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',
];
}

}
25 changes: 12 additions & 13 deletions tests/Usages/NamespaceUsagesTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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',
Expand Down Expand Up @@ -118,4 +109,12 @@ public function testRule(): void
]);
}


public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../extension.neon',
];
}

}
Loading

0 comments on commit bf6aa80

Please sign in to comment.