Skip to content

Commit

Permalink
Test cases - make some methods static because of PHPUnit 10 dataProvi…
Browse files Browse the repository at this point in the history
…ders
  • Loading branch information
ondrejmirtes committed Mar 29, 2023
1 parent f932058 commit ea2670a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 56 deletions.
14 changes: 7 additions & 7 deletions src/Testing/PHPStanTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function getAdditionalConfigFiles(): array
return [];
}

public function getParser(): Parser
public static function getParser(): Parser
{
/** @var Parser $parser */
$parser = self::getContainer()->getService('defaultAnalysisParser');
Expand All @@ -123,7 +123,7 @@ public function createBroker(): Broker
}

/** @api */
public function createReflectionProvider(): ReflectionProvider
public static function createReflectionProvider(): ReflectionProvider
{
return self::getContainer()->getByType(ReflectionProvider::class);
}
Expand All @@ -146,15 +146,15 @@ public static function getReflectors(): array
];
}

public function getClassReflectionExtensionRegistryProvider(): ClassReflectionExtensionRegistryProvider
public static function getClassReflectionExtensionRegistryProvider(): ClassReflectionExtensionRegistryProvider
{
return self::getContainer()->getByType(ClassReflectionExtensionRegistryProvider::class);
}

/**
* @param string[] $dynamicConstantNames
*/
public function createScopeFactory(ReflectionProvider $reflectionProvider, TypeSpecifier $typeSpecifier, array $dynamicConstantNames = []): ScopeFactory
public static function createScopeFactory(ReflectionProvider $reflectionProvider, TypeSpecifier $typeSpecifier, array $dynamicConstantNames = []): ScopeFactory
{
$container = self::getContainer();

Expand All @@ -181,7 +181,7 @@ public function createScopeFactory(ReflectionProvider $reflectionProvider, TypeS
$container->getByType(ExprPrinter::class),
$typeSpecifier,
new PropertyReflectionFinder(),
$this->getParser(),
self::getParser(),
$container->getByType(NodeScopeResolver::class),
$container->getByType(PhpVersion::class),
$container->getParameter('featureToggles')['explicitMixedInUnknownGenericNew'],
Expand All @@ -194,7 +194,7 @@ public function createScopeFactory(ReflectionProvider $reflectionProvider, TypeS
/**
* @param array<string, string> $globalTypeAliases
*/
public function createTypeAliasResolver(array $globalTypeAliases, ReflectionProvider $reflectionProvider): TypeAliasResolver
public static function createTypeAliasResolver(array $globalTypeAliases, ReflectionProvider $reflectionProvider): TypeAliasResolver
{
$container = self::getContainer();

Expand All @@ -211,7 +211,7 @@ protected function shouldTreatPhpDocTypesAsCertain(): bool
return true;
}

public function getFileHelper(): FileHelper
public static function getFileHelper(): FileHelper
{
return self::getContainer()->getByType(FileHelper::class);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Testing/RuleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function getAnalyser(): Analyser
$reflectionProvider,
self::getContainer()->getByType(InitializerExprTypeResolver::class),
self::getReflector(),
$this->getClassReflectionExtensionRegistryProvider(),
self::getClassReflectionExtensionRegistryProvider(),
$this->getParser(),
self::getContainer()->getByType(FileTypeMapper::class),
self::getContainer()->getByType(StubPhpDocProvider::class),
Expand Down
46 changes: 23 additions & 23 deletions src/Testing/TypeInferenceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ abstract class TypeInferenceTestCase extends PHPStanTestCase
* @param callable(Node , Scope ): void $callback
* @param string[] $dynamicConstantNames
*/
public function processFile(
public static function processFile(
string $file,
callable $callback,
array $dynamicConstantNames = [],
): void
{
$reflectionProvider = $this->createReflectionProvider();
$reflectionProvider = self::createReflectionProvider();
$typeSpecifier = self::getContainer()->getService('typeSpecifier');
$fileHelper = self::getContainer()->getByType(FileHelper::class);
$resolver = new NodeScopeResolver(
$reflectionProvider,
self::getContainer()->getByType(InitializerExprTypeResolver::class),
self::getReflector(),
$this->getClassReflectionExtensionRegistryProvider(),
$this->getParser(),
self::getClassReflectionExtensionRegistryProvider(),
self::getParser(),
self::getContainer()->getByType(FileTypeMapper::class),
self::getContainer()->getByType(StubPhpDocProvider::class),
self::getContainer()->getByType(PhpVersion::class),
Expand All @@ -58,18 +58,18 @@ public function processFile(
self::getContainer()->getByType(ReadWritePropertiesExtensionProvider::class),
true,
true,
$this->getEarlyTerminatingMethodCalls(),
$this->getEarlyTerminatingFunctionCalls(),
static::getEarlyTerminatingMethodCalls(),
static::getEarlyTerminatingFunctionCalls(),
true,
$this->shouldTreatPhpDocTypesAsCertain(),
self::getContainer()->getParameter('treatPhpDocTypesAsCertain'),
);
$resolver->setAnalysedFiles(array_map(static fn (string $file): string => $fileHelper->normalizePath($file), array_merge([$file], $this->getAdditionalAnalysedFiles())));
$resolver->setAnalysedFiles(array_map(static fn (string $file): string => $fileHelper->normalizePath($file), array_merge([$file], static::getAdditionalAnalysedFiles())));

$scopeFactory = $this->createScopeFactory($reflectionProvider, $typeSpecifier, $dynamicConstantNames);
$scopeFactory = self::createScopeFactory($reflectionProvider, $typeSpecifier, $dynamicConstantNames);
$scope = $scopeFactory->create(ScopeContext::create($file));

$resolver->processNodes(
$this->getParser()->parseFile($file),
self::getParser()->parseFile($file),
$scope,
$callback,
);
Expand Down Expand Up @@ -111,10 +111,10 @@ public function assertFileAsserts(
* @api
* @return array<string, mixed[]>
*/
public function gatherAssertTypes(string $file): array
public static function gatherAssertTypes(string $file): array
{
$asserts = [];
$this->processFile($file, function (Node $node, Scope $scope) use (&$asserts, $file): void {
self::processFile($file, static function (Node $node, Scope $scope) use (&$asserts, $file): void {
if (!$node instanceof Node\Expr\FuncCall) {
return;
}
Expand All @@ -136,28 +136,28 @@ public function gatherAssertTypes(string $file): array
} elseif ($functionName === 'PHPStan\\Testing\\assertVariableCertainty') {
$certainty = $node->getArgs()[0]->value;
if (!$certainty instanceof StaticCall) {
$this->fail(sprintf('First argument of %s() must be TrinaryLogic call', $functionName));
self::fail(sprintf('First argument of %s() must be TrinaryLogic call', $functionName));
}
if (!$certainty->class instanceof Node\Name) {
$this->fail(sprintf('ERROR: Invalid TrinaryLogic call.'));
self::fail(sprintf('ERROR: Invalid TrinaryLogic call.'));
}

if ($certainty->class->toString() !== 'PHPStan\\TrinaryLogic') {
$this->fail(sprintf('ERROR: Invalid TrinaryLogic call.'));
self::fail(sprintf('ERROR: Invalid TrinaryLogic call.'));
}

if (!$certainty->name instanceof Node\Identifier) {
$this->fail(sprintf('ERROR: Invalid TrinaryLogic call.'));
self::fail(sprintf('ERROR: Invalid TrinaryLogic call.'));
}

// @phpstan-ignore-next-line
$expectedertaintyValue = TrinaryLogic::{$certainty->name->toString()}();
$variable = $node->getArgs()[1]->value;
if (!$variable instanceof Node\Expr\Variable) {
$this->fail(sprintf('ERROR: Invalid assertVariableCertainty call.'));
self::fail(sprintf('ERROR: Invalid assertVariableCertainty call.'));
}
if (!is_string($variable->name)) {
$this->fail(sprintf('ERROR: Invalid assertVariableCertainty call.'));
self::fail(sprintf('ERROR: Invalid assertVariableCertainty call.'));
}

$actualCertaintyValue = $scope->hasVariableType($variable->name);
Expand All @@ -167,7 +167,7 @@ public function gatherAssertTypes(string $file): array
}

if (count($node->getArgs()) !== 2) {
$this->fail(sprintf(
self::fail(sprintf(
'ERROR: Wrong %s() call on line %d.',
$functionName,
$node->getLine(),
Expand All @@ -178,26 +178,26 @@ public function gatherAssertTypes(string $file): array
});

if (count($asserts) === 0) {
$this->fail(sprintf('File %s does not contain any asserts', $file));
self::fail(sprintf('File %s does not contain any asserts', $file));
}

return $asserts;
}

/** @return string[] */
protected function getAdditionalAnalysedFiles(): array
protected static function getAdditionalAnalysedFiles(): array
{
return [];
}

/** @return string[][] */
protected function getEarlyTerminatingMethodCalls(): array
protected static function getEarlyTerminatingMethodCalls(): array
{
return [];
}

/** @return string[] */
protected function getEarlyTerminatingFunctionCalls(): array
protected static function getEarlyTerminatingFunctionCalls(): array
{
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ private function createAnalyser(bool $reportUnmatchedIgnoredErrors): Analyser
$reflectionProvider,
self::getContainer()->getByType(InitializerExprTypeResolver::class),
self::getReflector(),
$this->getClassReflectionExtensionRegistryProvider(),
self::getClassReflectionExtensionRegistryProvider(),
$this->getParser(),
$fileTypeMapper,
self::getContainer()->getByType(StubPhpDocProvider::class),
Expand Down
49 changes: 25 additions & 24 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class LegacyNodeScopeResolverTest extends TypeInferenceTestCase

public function testClassMethodScope(): void
{
$this->processFile(__DIR__ . '/data/class.php', function (Node $node, Scope $scope): void {
self::processFile(__DIR__ . '/data/class.php', function (Node $node, Scope $scope): void {
if (!($node instanceof Exit_)) {
return;
}
Expand Down Expand Up @@ -9519,24 +9519,25 @@ private function assertTypes(
$assertType(self::$assertTypesCache[$file][$evaluatedPointExpression]);
return;
}
$this->processFile(
$file,
static function (Node $node, Scope $scope) use ($file, $evaluatedPointExpression, $assertType): void {
if ($node instanceof VirtualNode) {
return;
}
$printer = new Printer();
$printedNode = $printer->prettyPrint([$node]);
if ($printedNode !== $evaluatedPointExpression) {
return;
}

self::$assertTypesCache[$file][$evaluatedPointExpression] = $scope;

$assertType($scope);
},
$dynamicConstantNames,
);

self::processFile(
$file,
static function (Node $node, Scope $scope) use ($file, $evaluatedPointExpression, $assertType): void {
if ($node instanceof VirtualNode) {
return;
}
$printer = new Printer();
$printedNode = $printer->prettyPrint([$node]);
if ($printedNode !== $evaluatedPointExpression) {
return;
}

self::$assertTypesCache[$file][$evaluatedPointExpression] = $scope;

$assertType($scope);
},
$dynamicConstantNames,
);
}

public static function getAdditionalConfigFiles(): array
Expand Down Expand Up @@ -9570,7 +9571,7 @@ public function dataDeclareStrictTypes(): array
*/
public function testDeclareStrictTypes(string $file, bool $result): void
{
$this->processFile($file, function (Node $node, Scope $scope) use ($result): void {
self::processFile($file, function (Node $node, Scope $scope) use ($result): void {
if (!($node instanceof Exit_)) {
return;
}
Expand All @@ -9581,7 +9582,7 @@ public function testDeclareStrictTypes(string $file, bool $result): void

public function testEarlyTermination(): void
{
$this->processFile(__DIR__ . '/data/early-termination.php', function (Node $node, Scope $scope): void {
self::processFile(__DIR__ . '/data/early-termination.php', function (Node $node, Scope $scope): void {
if (!($node instanceof Exit_)) {
return;
}
Expand All @@ -9592,7 +9593,7 @@ public function testEarlyTermination(): void
});
}

protected function getEarlyTerminatingMethodCalls(): array
protected static function getEarlyTerminatingMethodCalls(): array
{
return [
\EarlyTermination\Foo::class => [
Expand All @@ -9602,7 +9603,7 @@ protected function getEarlyTerminatingMethodCalls(): array
];
}

protected function getEarlyTerminatingFunctionCalls(): array
protected static function getEarlyTerminatingFunctionCalls(): array
{
return ['baz'];
}
Expand All @@ -9622,7 +9623,7 @@ private function assertTypeDescribe(
}

/** @return string[] */
protected function getAdditionalAnalysedFiles(): array
protected static function getAdditionalAnalysedFiles(): array
{
return [
__DIR__ . '/data/methodPhpDocs-trait-defined.php',
Expand Down

0 comments on commit ea2670a

Please sign in to comment.