Skip to content

Commit 1e58d40

Browse files
committed
Reworked generics assertTypes tests to be part of NodeScopeResolverTest
World's simplest code-based type resolver tests!
1 parent e55deb4 commit 1e58d40

15 files changed

+73
-232
lines changed

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace PHPStan\Analyser;
44

5+
use PhpParser\Node;
56
use PhpParser\Node\Expr\Exit_;
67
use PhpParser\Node\Expr\MethodCall;
78
use PhpParser\Node\Expr\StaticCall;
9+
use PhpParser\Node\Name;
810
use PHPStan\Broker\AnonymousClassNameHelper;
911
use PHPStan\Cache\Cache;
1012
use PHPStan\File\FileHelper;
@@ -9165,6 +9167,68 @@ public function testArrayShapesInPhpDoc(
91659167
);
91669168
}
91679169

9170+
public function dataGenerics(): array
9171+
{
9172+
require_once __DIR__ . '/data/generics.php';
9173+
9174+
return $this->gatherAssertTypes(__DIR__ . '/data/generics.php');
9175+
}
9176+
9177+
/**
9178+
* @dataProvider dataGenerics
9179+
* @param ConstantStringType $expectedType
9180+
* @param Type $actualType
9181+
*/
9182+
public function testGenerics(
9183+
ConstantStringType $expectedType,
9184+
Type $actualType,
9185+
int $line
9186+
): void
9187+
{
9188+
$expected = $expectedType->getValue();
9189+
$actual = $actualType->describe(VerbosityLevel::precise());
9190+
$this->assertSame(
9191+
$expected,
9192+
$actual,
9193+
sprintf('Expected type %s, got type %s on line %d.', $expected, $actual, $line)
9194+
);
9195+
}
9196+
9197+
private function gatherAssertTypes(string $file): array
9198+
{
9199+
$types = [];
9200+
$this->processFile($file, function (Node $node, Scope $scope) use (&$types): void {
9201+
if (!$node instanceof Node\Expr\FuncCall) {
9202+
return;
9203+
}
9204+
9205+
$nameNode = $node->name;
9206+
if (!$nameNode instanceof Name) {
9207+
return;
9208+
}
9209+
9210+
$assertTypeFunctionName = 'PHPStan\\Analyser\\assertType';
9211+
if ((string) $nameNode !== $assertTypeFunctionName) {
9212+
return;
9213+
}
9214+
9215+
if (count($node->args) !== 2) {
9216+
$this->fail(sprintf(
9217+
'ERROR: Wrong %s() call on line %d.',
9218+
$assertTypeFunctionName,
9219+
$node->getLine()
9220+
));
9221+
}
9222+
9223+
$expectedTypeString = $scope->getType($node->args[0]->value);
9224+
$actualType = $scope->getType($node->args[1]->value);
9225+
9226+
$types[] = [$expectedTypeString, $actualType, $node->getLine()];
9227+
});
9228+
9229+
return $types;
9230+
}
9231+
91689232
public function dataTryCatchScope(): array
91699233
{
91709234
return [

tests/PHPStan/Generics/data/functionsAssertType.php renamed to tests/PHPStan/Analyser/data/generics.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace PHPStan\Generics\FunctionsAssertType;
44

5-
use function PHPStan\Generics\assertType;
5+
use function PHPStan\Analyser\assertType;
66

77
/**
88
* @template T

tests/PHPStan/Generics/functions.php renamed to tests/PHPStan/Analyser/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types = 1);
22

3-
namespace PHPStan\Generics;
3+
namespace PHPStan\Analyser;
44

55
/**
66
* Asserts the static type of a value.

tests/PHPStan/Generics/AssertTypeRule.php

Lines changed: 0 additions & 67 deletions
This file was deleted.

tests/PHPStan/Generics/GenericsIntegrationTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public function dataTopics(): array
99
{
1010
return [
1111
['functions'],
12-
['functionsAssertType'],
1312
['invalidReturn'],
1413
['pick'],
1514
['varyingAcceptor'],

tests/PHPStan/Generics/data/functionsAssertType-0.json

Lines changed: 0 additions & 127 deletions
This file was deleted.

tests/PHPStan/Generics/data/functionsAssertType-5.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/PHPStan/Generics/data/functionsAssertType-7.json

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"message": "Parameter #1 $c of function PHPStan\\Generics\\Pick\\foo expects PHPStan\\Generics\\Pick\\A|PHPStan\\Generics\\Pick\\B, PHPStan\\Generics\\Pick\\A|PHPStan\\Generics\\Pick\\C given.",
4-
"line": 26,
4+
"line": 24,
55
"ignorable": true
66
}
77
]

tests/PHPStan/Generics/data/pick.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace PHPStan\Generics\Pick;
44

5-
use function PHPStan\Generics\assertType;
6-
75
class A {}
86
class B {}
97
class C {}

0 commit comments

Comments
 (0)