|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPNomad\PhpstanRules\Tests\Rules\Di; |
| 4 | + |
| 5 | +use PHPNomad\PhpstanRules\Rules\Di\NoServiceLocatorRule; |
| 6 | +use PHPNomad\PhpstanRules\Services\ClassTypeResolver; |
| 7 | +use PHPStan\Rules\Rule; |
| 8 | +use PHPStan\Testing\RuleTestCase; |
| 9 | + |
| 10 | +/** |
| 11 | + * @extends RuleTestCase<NoServiceLocatorRule> |
| 12 | + */ |
| 13 | +class NoServiceLocatorRuleTest extends RuleTestCase |
| 14 | +{ |
| 15 | + protected function getRule(): Rule |
| 16 | + { |
| 17 | + return new NoServiceLocatorRule(new ClassTypeResolver()); |
| 18 | + } |
| 19 | + |
| 20 | + public static function getAdditionalConfigFiles(): array |
| 21 | + { |
| 22 | + return [__DIR__ . '/../../phpstan.neon']; |
| 23 | + } |
| 24 | + |
| 25 | + public function testBusinessClassWithInstanceProviderViolation(): void |
| 26 | + { |
| 27 | + $this->analyse([__DIR__ . '/data/BusinessClassWithInstanceProvider.php'], [ |
| 28 | + ['Avoid using Container::get() as a service locator in business classes. Use constructor injection instead.', 15], |
| 29 | + ]); |
| 30 | + } |
| 31 | + |
| 32 | + public function testBusinessClassWithCanSetContainerViolation(): void |
| 33 | + { |
| 34 | + $this->analyse([__DIR__ . '/data/BusinessClassWithCanSetContainer.php'], [ |
| 35 | + ['Avoid using Container::get() as a service locator in business classes. Use constructor injection instead.', 14], |
| 36 | + ]); |
| 37 | + } |
| 38 | + |
| 39 | + public function testInitializerExempt(): void |
| 40 | + { |
| 41 | + $this->analyse([__DIR__ . '/data/InitializerWithInstanceProvider.php'], []); |
| 42 | + } |
| 43 | + |
| 44 | + public function testFacadeWithContainerGet(): void |
| 45 | + { |
| 46 | + $this->analyse([__DIR__ . '/data/FacadeWithContainerGet.php'], []); |
| 47 | + } |
| 48 | + |
| 49 | + public function testRegularClassNoContainerPasses(): void |
| 50 | + { |
| 51 | + $this->analyse([__DIR__ . '/data/RegularClassNoContainer.php'], []); |
| 52 | + } |
| 53 | +} |
0 commit comments