1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\Testing; |
4: | |
5: | use PHPStan\Analyser\ConstantResolver; |
6: | use PHPStan\Analyser\DirectInternalScopeFactory; |
7: | use PHPStan\Analyser\Error; |
8: | use PHPStan\Analyser\NodeScopeResolver; |
9: | use PHPStan\Analyser\RicherScopeGetTypeHelper; |
10: | use PHPStan\Analyser\ScopeFactory; |
11: | use PHPStan\Analyser\TypeSpecifier; |
12: | use PHPStan\BetterReflection\Reflector\Reflector; |
13: | use PHPStan\DependencyInjection\Container; |
14: | use PHPStan\DependencyInjection\ContainerFactory; |
15: | use PHPStan\DependencyInjection\Reflection\ClassReflectionExtensionRegistryProvider; |
16: | use PHPStan\DependencyInjection\Type\DynamicReturnTypeExtensionRegistryProvider; |
17: | use PHPStan\DependencyInjection\Type\ExpressionTypeResolverExtensionRegistryProvider; |
18: | use PHPStan\DependencyInjection\Type\OperatorTypeSpecifyingExtensionRegistryProvider; |
19: | use PHPStan\File\FileHelper; |
20: | use PHPStan\Internal\DirectoryCreator; |
21: | use PHPStan\Internal\DirectoryCreatorException; |
22: | use PHPStan\Node\Printer\ExprPrinter; |
23: | use PHPStan\Parser\Parser; |
24: | use PHPStan\Php\ComposerPhpVersionFactory; |
25: | use PHPStan\Php\PhpVersion; |
26: | use PHPStan\PhpDoc\TypeNodeResolver; |
27: | use PHPStan\PhpDoc\TypeStringResolver; |
28: | use PHPStan\Reflection\InitializerExprTypeResolver; |
29: | use PHPStan\Reflection\ReflectionProvider; |
30: | use PHPStan\Reflection\ReflectionProvider\DirectReflectionProviderProvider; |
31: | use PHPStan\Rules\Properties\PropertyReflectionFinder; |
32: | use PHPStan\Type\Constant\OversizedArrayBuilder; |
33: | use PHPStan\Type\TypeAliasResolver; |
34: | use PHPStan\Type\UsefulTypeAliasResolver; |
35: | use PHPUnit\Framework\ExpectationFailedException; |
36: | use PHPUnit\Framework\TestCase; |
37: | use function array_merge; |
38: | use function count; |
39: | use function implode; |
40: | use function rtrim; |
41: | use function sha1; |
42: | use function sprintf; |
43: | use function sys_get_temp_dir; |
44: | use const DIRECTORY_SEPARATOR; |
45: | use const PHP_VERSION_ID; |
46: | |
47: | |
48: | abstract class PHPStanTestCase extends TestCase |
49: | { |
50: | |
51: | |
52: | private static array $containers = []; |
53: | |
54: | |
55: | public static function getContainer(): Container |
56: | { |
57: | $additionalConfigFiles = static::getAdditionalConfigFiles(); |
58: | $additionalConfigFiles[] = __DIR__ . '/TestCase.neon'; |
59: | $cacheKey = sha1(implode("\n", $additionalConfigFiles)); |
60: | |
61: | if (!isset(self::$containers[$cacheKey])) { |
62: | $tmpDir = sys_get_temp_dir() . '/phpstan-tests'; |
63: | try { |
64: | DirectoryCreator::ensureDirectoryExists($tmpDir, 0777); |
65: | } catch (DirectoryCreatorException $e) { |
66: | self::fail($e->getMessage()); |
67: | } |
68: | |
69: | $rootDir = __DIR__ . '/../..'; |
70: | $fileHelper = new FileHelper($rootDir); |
71: | $rootDir = $fileHelper->normalizePath($rootDir, '/'); |
72: | $containerFactory = new ContainerFactory($rootDir); |
73: | $container = $containerFactory->create($tmpDir, array_merge([ |
74: | $containerFactory->getConfigDirectory() . '/config.level8.neon', |
75: | ], $additionalConfigFiles), []); |
76: | self::$containers[$cacheKey] = $container; |
77: | |
78: | foreach ($container->getParameter('bootstrapFiles') as $bootstrapFile) { |
79: | (static function (string $file) use ($container): void { |
80: | require_once $file; |
81: | })($bootstrapFile); |
82: | } |
83: | |
84: | if (PHP_VERSION_ID >= 80000) { |
85: | require_once __DIR__ . '/../../stubs/runtime/Enum/UnitEnum.php'; |
86: | require_once __DIR__ . '/../../stubs/runtime/Enum/BackedEnum.php'; |
87: | require_once __DIR__ . '/../../stubs/runtime/Enum/ReflectionEnum.php'; |
88: | require_once __DIR__ . '/../../stubs/runtime/Enum/ReflectionEnumUnitCase.php'; |
89: | require_once __DIR__ . '/../../stubs/runtime/Enum/ReflectionEnumBackedCase.php'; |
90: | } |
91: | } else { |
92: | ContainerFactory::postInitializeContainer(self::$containers[$cacheKey]); |
93: | } |
94: | |
95: | return self::$containers[$cacheKey]; |
96: | } |
97: | |
98: | |
99: | |
100: | |
101: | public static function getAdditionalConfigFiles(): array |
102: | { |
103: | return []; |
104: | } |
105: | |
106: | public static function getParser(): Parser |
107: | { |
108: | |
109: | $parser = self::getContainer()->getService('defaultAnalysisParser'); |
110: | return $parser; |
111: | } |
112: | |
113: | |
114: | public static function createReflectionProvider(): ReflectionProvider |
115: | { |
116: | return self::getContainer()->getByType(ReflectionProvider::class); |
117: | } |
118: | |
119: | public static function getReflector(): Reflector |
120: | { |
121: | return self::getContainer()->getService('betterReflectionReflector'); |
122: | } |
123: | |
124: | public static function getClassReflectionExtensionRegistryProvider(): ClassReflectionExtensionRegistryProvider |
125: | { |
126: | return self::getContainer()->getByType(ClassReflectionExtensionRegistryProvider::class); |
127: | } |
128: | |
129: | |
130: | |
131: | |
132: | public static function createScopeFactory(ReflectionProvider $reflectionProvider, TypeSpecifier $typeSpecifier, array $dynamicConstantNames = []): ScopeFactory |
133: | { |
134: | $container = self::getContainer(); |
135: | |
136: | if (count($dynamicConstantNames) === 0) { |
137: | $dynamicConstantNames = $container->getParameter('dynamicConstantNames'); |
138: | } |
139: | |
140: | $reflectionProviderProvider = new DirectReflectionProviderProvider($reflectionProvider); |
141: | $composerPhpVersionFactory = $container->getByType(ComposerPhpVersionFactory::class); |
142: | $constantResolver = new ConstantResolver($reflectionProviderProvider, $dynamicConstantNames, null, $composerPhpVersionFactory); |
143: | |
144: | $initializerExprTypeResolver = new InitializerExprTypeResolver( |
145: | $constantResolver, |
146: | $reflectionProviderProvider, |
147: | $container->getByType(PhpVersion::class), |
148: | $container->getByType(OperatorTypeSpecifyingExtensionRegistryProvider::class), |
149: | new OversizedArrayBuilder(), |
150: | $container->getParameter('usePathConstantsAsConstantString'), |
151: | ); |
152: | |
153: | return new ScopeFactory( |
154: | new DirectInternalScopeFactory( |
155: | $reflectionProvider, |
156: | $initializerExprTypeResolver, |
157: | $container->getByType(DynamicReturnTypeExtensionRegistryProvider::class), |
158: | $container->getByType(ExpressionTypeResolverExtensionRegistryProvider::class), |
159: | $container->getByType(ExprPrinter::class), |
160: | $typeSpecifier, |
161: | new PropertyReflectionFinder(), |
162: | self::getParser(), |
163: | $container->getByType(NodeScopeResolver::class), |
164: | new RicherScopeGetTypeHelper($initializerExprTypeResolver), |
165: | $container->getByType(PhpVersion::class), |
166: | $constantResolver, |
167: | ), |
168: | ); |
169: | } |
170: | |
171: | |
172: | |
173: | |
174: | public static function createTypeAliasResolver(array $globalTypeAliases, ReflectionProvider $reflectionProvider): TypeAliasResolver |
175: | { |
176: | $container = self::getContainer(); |
177: | |
178: | return new UsefulTypeAliasResolver( |
179: | $globalTypeAliases, |
180: | $container->getByType(TypeStringResolver::class), |
181: | $container->getByType(TypeNodeResolver::class), |
182: | $reflectionProvider, |
183: | ); |
184: | } |
185: | |
186: | protected function shouldTreatPhpDocTypesAsCertain(): bool |
187: | { |
188: | return true; |
189: | } |
190: | |
191: | public static function getFileHelper(): FileHelper |
192: | { |
193: | return self::getContainer()->getByType(FileHelper::class); |
194: | } |
195: | |
196: | |
197: | |
198: | |
199: | |
200: | protected function assertSamePaths(string $expected, string $actual, string $message = ''): void |
201: | { |
202: | $expected = $this->getFileHelper()->normalizePath($expected); |
203: | $actual = $this->getFileHelper()->normalizePath($actual); |
204: | |
205: | $this->assertSame($expected, $actual, $message); |
206: | } |
207: | |
208: | |
209: | |
210: | |
211: | protected function assertNoErrors(array $errors): void |
212: | { |
213: | try { |
214: | $this->assertCount(0, $errors); |
215: | } catch (ExpectationFailedException $e) { |
216: | $messages = []; |
217: | foreach ($errors as $error) { |
218: | if ($error instanceof Error) { |
219: | $messages[] = sprintf("- %s\n in %s on line %d\n", rtrim($error->getMessage(), '.'), $error->getFile(), $error->getLine()); |
220: | } else { |
221: | $messages[] = $error; |
222: | } |
223: | } |
224: | |
225: | $this->fail($e->getMessage() . "\n\nEmitted errors:\n" . implode("\n", $messages)); |
226: | } |
227: | } |
228: | |
229: | protected function skipIfNotOnWindows(): void |
230: | { |
231: | if (DIRECTORY_SEPARATOR === '\\') { |
232: | return; |
233: | } |
234: | |
235: | self::markTestSkipped(); |
236: | } |
237: | |
238: | protected function skipIfNotOnUnix(): void |
239: | { |
240: | if (DIRECTORY_SEPARATOR === '/') { |
241: | return; |
242: | } |
243: | |
244: | self::markTestSkipped(); |
245: | } |
246: | |
247: | } |
248: | |