Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the DI container in tests #223

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions tests/Allowed/AllowedPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Generator;
use PHPStan\Analyser\ScopeContext;
use PHPStan\Analyser\ScopeFactory;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\File\FileHelper;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
Expand All @@ -32,10 +33,11 @@ class AllowedPathTest extends PHPStanTestCase

protected function setUp(): void
{
$this->allowedPath = new AllowedPath(new FilePath(new FileHelper(__DIR__)));
$this->allowedPathWithRootDir = new AllowedPath(new FilePath(new FileHelper(__DIR__), '/foo/bar'));
$fileHelper = new FileHelper(__DIR__);
$this->allowedPath = new AllowedPath(new FilePath($fileHelper));
$this->allowedPathWithRootDir = new AllowedPath(new FilePath($fileHelper, '/foo/bar'));
$this->reflectionProvider = $this->createReflectionProvider();
$this->scopeFactory = $this->createScopeFactory($this->reflectionProvider, self::getContainer()->getService('typeSpecifier'));
$this->scopeFactory = $this->createScopeFactory($this->reflectionProvider, self::getContainer()->getByType(TypeSpecifier::class));
}


Expand Down
28 changes: 13 additions & 15 deletions tests/Calls/EchoCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@

namespace Spaze\PHPStan\Rules\Disallowed\Calls;

use PHPStan\File\FileHelper;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\Allowed\Allowed;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
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\DisallowedCallsRuleErrors;

class EchoCallsTest extends RuleTestCase
Expand All @@ -24,19 +17,16 @@ class EchoCallsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$filePath = new FilePath(new FileHelper(__DIR__));
$allowed = new Allowed($formatter, $normalizer, new AllowedPath($filePath));
$container = self::getContainer();
return new EchoCalls(
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath, $formatter),
new DisallowedCallFactory($formatter, $normalizer, $allowed),
$container->getByType(DisallowedCallsRuleErrors::class),
$container->getByType(DisallowedCallFactory::class),
[
[
'function' => 'echo()',
'allowIn' => [
'../src/disallowed-allow/*.php',
'../src/*-allow/*.*',
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
],
]
Expand All @@ -57,4 +47,12 @@ public function testRule(): void
$this->analyse([__DIR__ . '/../src/disallowed-allow/functionCalls.php'], []);
}


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

}
28 changes: 13 additions & 15 deletions tests/Calls/EmptyCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@

namespace Spaze\PHPStan\Rules\Disallowed\Calls;

use PHPStan\File\FileHelper;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\Allowed\Allowed;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
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\DisallowedCallsRuleErrors;

class EmptyCallsTest extends RuleTestCase
Expand All @@ -24,19 +17,16 @@ class EmptyCallsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$filePath = new FilePath(new FileHelper(__DIR__));
$allowed = new Allowed($formatter, $normalizer, new AllowedPath($filePath));
$container = self::getContainer();
return new EmptyCalls(
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath, $formatter),
new DisallowedCallFactory($formatter, $normalizer, $allowed),
$container->getByType(DisallowedCallsRuleErrors::class),
$container->getByType(DisallowedCallFactory::class),
[
[
'function' => 'empty()',
'allowIn' => [
'../src/disallowed-allow/*.php',
'../src/*-allow/*.*',
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
],
]
Expand All @@ -57,4 +47,12 @@ public function testRule(): void
$this->analyse([__DIR__ . '/../src/disallowed-allow/functionCalls.php'], []);
}


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

}
28 changes: 13 additions & 15 deletions tests/Calls/EvalCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@

namespace Spaze\PHPStan\Rules\Disallowed\Calls;

use PHPStan\File\FileHelper;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\Allowed\Allowed;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
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\DisallowedCallsRuleErrors;

class EvalCallsTest extends RuleTestCase
Expand All @@ -24,19 +17,16 @@ class EvalCallsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$filePath = new FilePath(new FileHelper(__DIR__));
$allowed = new Allowed($formatter, $normalizer, new AllowedPath($filePath));
$container = self::getContainer();
return new EvalCalls(
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath, $formatter),
new DisallowedCallFactory($formatter, $normalizer, $allowed),
$container->getByType(DisallowedCallsRuleErrors::class),
$container->getByType(DisallowedCallFactory::class),
[
[
'function' => 'eval()',
'allowIn' => [
'../src/disallowed-allow/*.php',
'../src/*-allow/*.*',
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
],
]
Expand All @@ -57,4 +47,12 @@ public function testRule(): void
$this->analyse([__DIR__ . '/../src/disallowed-allow/functionCalls.php'], []);
}


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

}
28 changes: 13 additions & 15 deletions tests/Calls/ExitDieCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@

namespace Spaze\PHPStan\Rules\Disallowed\Calls;

use PHPStan\File\FileHelper;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\Allowed\Allowed;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
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\DisallowedCallsRuleErrors;

class ExitDieCallsTest extends RuleTestCase
Expand All @@ -24,22 +17,19 @@ class ExitDieCallsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$filePath = new FilePath(new FileHelper(__DIR__));
$allowed = new Allowed($formatter, $normalizer, new AllowedPath($filePath));
$container = self::getContainer();
return new ExitDieCalls(
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath, $formatter),
new DisallowedCallFactory($formatter, $normalizer, $allowed),
$container->getByType(DisallowedCallsRuleErrors::class),
$container->getByType(DisallowedCallFactory::class),
[
[
'function' => [
'die()',
'exit()',
],
'allowIn' => [
'../src/disallowed-allow/*.php',
'../src/*-allow/*.*',
__DIR__ . '/../src/disallowed-allow/*.php',
__DIR__ . '/../src/*-allow/*.*',
],
],
]
Expand Down Expand Up @@ -72,4 +62,12 @@ public function testRule(): void
$this->analyse([__DIR__ . '/../src/disallowed-allow/functionCalls.php'], []);
}


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

}
24 changes: 11 additions & 13 deletions tests/Calls/FunctionCallsAllowInFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@

namespace Spaze\PHPStan\Rules\Disallowed\Calls;

use PHPStan\File\FileHelper;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\Allowed\Allowed;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
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\DisallowedCallsRuleErrors;

class FunctionCallsAllowInFunctionsTest extends RuleTestCase
Expand All @@ -24,13 +17,10 @@ class FunctionCallsAllowInFunctionsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$filePath = new FilePath(new FileHelper(__DIR__));
$allowed = new Allowed($formatter, $normalizer, new AllowedPath($filePath));
$container = self::getContainer();
return new FunctionCalls(
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath, $formatter),
new DisallowedCallFactory($formatter, $normalizer, $allowed),
$container->getByType(DisallowedCallsRuleErrors::class),
$container->getByType(DisallowedCallFactory::class),
$this->createReflectionProvider(),
[
[
Expand Down Expand Up @@ -63,4 +53,12 @@ public function testRule(): void
]);
}


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

}
24 changes: 11 additions & 13 deletions tests/Calls/FunctionCallsAllowInMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@

namespace Spaze\PHPStan\Rules\Disallowed\Calls;

use PHPStan\File\FileHelper;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\Allowed\Allowed;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
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\DisallowedCallsRuleErrors;

class FunctionCallsAllowInMethodsTest extends RuleTestCase
Expand All @@ -24,13 +17,10 @@ class FunctionCallsAllowInMethodsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$filePath = new FilePath(new FileHelper(__DIR__));
$allowed = new Allowed($formatter, $normalizer, new AllowedPath($filePath));
$container = self::getContainer();
return new FunctionCalls(
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath, $formatter),
new DisallowedCallFactory($formatter, $normalizer, $allowed),
$container->getByType(DisallowedCallsRuleErrors::class),
$container->getByType(DisallowedCallFactory::class),
$this->createReflectionProvider(),
[
[
Expand Down Expand Up @@ -72,4 +62,12 @@ public function testRule(): void
]);
}


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

}
Loading