|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\File; |
| 4 | + |
| 5 | +use PHPStan\Analyser\Scope; |
| 6 | +use PHPStan\Testing\PHPStanTestCase; |
| 7 | +use function array_map; |
| 8 | +use function str_replace; |
| 9 | + |
| 10 | +final class IncludedFilePathResolverTest extends PHPStanTestCase |
| 11 | +{ |
| 12 | + |
| 13 | + private function createResolver(): IncludedFilePathResolver |
| 14 | + { |
| 15 | + return new IncludedFilePathResolver(__DIR__, self::getContainer()->getByType(FileHelper::class)); |
| 16 | + } |
| 17 | + |
| 18 | + private function createScope(): Scope |
| 19 | + { |
| 20 | + $scope = $this->createMock(Scope::class); |
| 21 | + $scope->method('isInTrait')->willReturn(false); |
| 22 | + $scope->method('getFile')->willReturn(__DIR__ . '/test/lorem.php'); |
| 23 | + |
| 24 | + return $scope; |
| 25 | + } |
| 26 | + |
| 27 | + public function testPathWithAnUnregisteredStreamWrapperHasNoCandidates(): void |
| 28 | + { |
| 29 | + // is_file() on it raises "Unable to find the wrapper" instead of answering, and the path |
| 30 | + // cannot become readable later either - vfsStream registers its wrapper from a test's |
| 31 | + // setUp(), which never runs inside PHPStan. |
| 32 | + $this->assertSame([], $this->createResolver()->resolve('vfs://drupal/sites/default/x.php', $this->createScope())); |
| 33 | + } |
| 34 | + |
| 35 | + public function testPathWithARegisteredStreamWrapperIsKept(): void |
| 36 | + { |
| 37 | + $this->assertSame(['php://memory'], $this->createResolver()->resolve('php://memory', $this->createScope())); |
| 38 | + } |
| 39 | + |
| 40 | + public function testRelativePathIsResolvedAgainstEveryDirectory(): void |
| 41 | + { |
| 42 | + // Compared with forward slashes so the expectations hold on Windows too, where |
| 43 | + // absolutizePath() joins with a backslash. |
| 44 | + $paths = array_map( |
| 45 | + static fn (string $path): string => str_replace('\\', '/', $path), |
| 46 | + $this->createResolver()->resolve('lorem.php', $this->createScope()), |
| 47 | + ); |
| 48 | + $directory = str_replace('\\', '/', __DIR__); |
| 49 | + |
| 50 | + $this->assertContains($directory . '/lorem.php', $paths); |
| 51 | + $this->assertContains($directory . '/test/lorem.php', $paths); |
| 52 | + } |
| 53 | + |
| 54 | +} |
0 commit comments