|
4 | 4 |
|
5 | 5 | use PHPStan; |
6 | 6 | use PHPUnit; |
| 7 | +use PhpParser; |
7 | 8 |
|
8 | 9 |
|
9 | 10 | final class DatabaseSpecificTypeInferenceTest extends PHPStan\Testing\TypeInferenceTestCase |
@@ -38,4 +39,130 @@ public static function getAdditionalConfigFiles(): array |
38 | 39 | return [__DIR__ . '/DatabaseSpecificTypeInferenceTest.extension.neon']; |
39 | 40 | } |
40 | 41 |
|
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * @api |
| 46 | + * @param mixed ...$args |
| 47 | + */ |
| 48 | + public function assertFileAsserts( |
| 49 | + string $assertType, |
| 50 | + string $file, |
| 51 | + ...$args, |
| 52 | + ): void |
| 53 | + { |
| 54 | + parent::assertFileAsserts($assertType, $file, ...$args); |
| 55 | + |
| 56 | + if ($assertType === 'superType') { |
| 57 | + $expected = $args[0]; |
| 58 | + $actual = $args[1]; |
| 59 | + $isCorrect = $args[2]; |
| 60 | + |
| 61 | + $failureMessage = sprintf('Expected subtype of %s, got type %s in %s on line %d.', $expected, $actual, $file, $args[3]); |
| 62 | + |
| 63 | + self::assertTrue( |
| 64 | + $isCorrect, |
| 65 | + $failureMessage, |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * @api |
| 74 | + * @return array<string, mixed[]> |
| 75 | + */ |
| 76 | + public static function gatherAssertTypes(string $file): array |
| 77 | + { |
| 78 | + $parentAsserts = parent::gatherAssertTypes($file); |
| 79 | + |
| 80 | + $fileHelper = self::getContainer()->getByType(PHPStan\File\FileHelper::class); |
| 81 | + |
| 82 | + $relativePathHelper = new PHPStan\File\SystemAgnosticSimpleRelativePathHelper($fileHelper); |
| 83 | + $typeStringResolver = self::getContainer()->getByType(PHPStan\PhpDoc\TypeStringResolver::class); |
| 84 | + |
| 85 | + $file = $fileHelper->normalizePath($file); |
| 86 | + |
| 87 | + $asserts = []; |
| 88 | + self::processFile($file, static function (PhpParser\Node $node, PHPStan\Analyser\Scope $scope) use (& $asserts, $file, $relativePathHelper, $typeStringResolver): void { |
| 89 | + if (!$node instanceof PhpParser\Node\Expr\FuncCall) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + $nameNode = $node->name; |
| 94 | + |
| 95 | + if (!$nameNode instanceof PhpParser\Node\Name) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + $functionName = $nameNode->toString(); |
| 100 | + |
| 101 | + if (in_array(strtolower($functionName), ['assertsupertype'], true)) { |
| 102 | + self::fail(sprintf( |
| 103 | + 'Missing use statement for %s() in %s on line %d.', |
| 104 | + $functionName, |
| 105 | + $relativePathHelper->getRelativePath($file), |
| 106 | + $node->getStartLine(), |
| 107 | + )); |
| 108 | + } elseif ($functionName === 'PHPStan\\Testing\\assertSuperType') { |
| 109 | + $expectedType = $scope->getType($node->getArgs()[0]->value); |
| 110 | + $expectedTypeStrings = $expectedType->getConstantStrings(); |
| 111 | + |
| 112 | + if (count($expectedTypeStrings) !== 1) { |
| 113 | + self::fail(sprintf( |
| 114 | + 'Expected super type must be a literal string, %s given in %s on line %d.', |
| 115 | + $expectedType->describe(PHPStan\Type\VerbosityLevel::precise()), |
| 116 | + $relativePathHelper->getRelativePath($file), |
| 117 | + $node->getStartLine(), |
| 118 | + )); |
| 119 | + } |
| 120 | + |
| 121 | + $actualType = $scope->getType($node->getArgs()[1]->value); |
| 122 | + $isCorrect = $typeStringResolver->resolve($expectedTypeStrings[0]->getValue())->isSuperTypeOf($actualType)->yes(); |
| 123 | + |
| 124 | + $assert = ['superType', $file, $expectedTypeStrings[0]->getValue(), $actualType->describe(PHPStan\Type\VerbosityLevel::precise()), $isCorrect, $node->getStartLine()]; |
| 125 | + } else { |
| 126 | + $correctFunction = null; |
| 127 | + |
| 128 | + $assertFunctions = [ |
| 129 | + 'assertSuperType' => 'PHPStan\\Testing\\assertSuperType', |
| 130 | + ]; |
| 131 | + |
| 132 | + foreach ($assertFunctions as $assertFn => $fqFunctionName) { |
| 133 | + if (stripos($functionName, $assertFn) === false) { |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + $correctFunction = $fqFunctionName; |
| 138 | + } |
| 139 | + |
| 140 | + if ($correctFunction === null) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + self::fail(sprintf( |
| 145 | + 'Function %s imported with wrong namespace %s called in %s on line %d.', |
| 146 | + $correctFunction, |
| 147 | + $functionName, |
| 148 | + $relativePathHelper->getRelativePath($file), |
| 149 | + $node->getStartLine(), |
| 150 | + )); |
| 151 | + } |
| 152 | + |
| 153 | + if (count($node->getArgs()) !== 2) { |
| 154 | + self::fail(sprintf( |
| 155 | + 'ERROR: Wrong %s() call in %s on line %d.', |
| 156 | + $functionName, |
| 157 | + $relativePathHelper->getRelativePath($file), |
| 158 | + $node->getStartLine(), |
| 159 | + )); |
| 160 | + } |
| 161 | + |
| 162 | + $asserts[$file . ':' . $node->getStartLine()] = $assert; |
| 163 | + }); |
| 164 | + |
| 165 | + return array_merge($parentAsserts, $asserts); |
| 166 | + } |
| 167 | + |
41 | 168 | } |
0 commit comments