|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Command; |
| 4 | + |
| 5 | +use LogicException; |
| 6 | +use PHPStan\Command\Symfony\SymfonyOutput; |
| 7 | +use PHPStan\Command\Symfony\SymfonyStyle; |
| 8 | +use PHPStan\Reflection\BetterReflection\SourceLocator\BootstrapAutoloaderExceptionReporter; |
| 9 | +use PHPStan\Testing\PHPStanTestCase; |
| 10 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 11 | +use Symfony\Component\Console\Input\InputInterface; |
| 12 | +use Symfony\Component\Console\Output\StreamOutput; |
| 13 | +use function fopen; |
| 14 | +use function rewind; |
| 15 | +use function stream_get_contents; |
| 16 | + |
| 17 | +class BootstrapFilesRunnerTest extends PHPStanTestCase |
| 18 | +{ |
| 19 | + |
| 20 | + /** |
| 21 | + * @return iterable<string, array{bool, bool}> |
| 22 | + */ |
| 23 | + public static function dataReporterIsEnabledOnlyWithDebug(): iterable |
| 24 | + { |
| 25 | + yield 'debug' => [true, true]; |
| 26 | + yield 'no debug' => [false, false]; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @throws InceptionNotSuccessfulException |
| 31 | + */ |
| 32 | + #[DataProvider('dataReporterIsEnabledOnlyWithDebug')] |
| 33 | + public function testReporterIsEnabledOnlyWithDebug(bool $debugEnabled, bool $expectPrinted): void |
| 34 | + { |
| 35 | + $stream = fopen('php://memory', 'w+'); |
| 36 | + $this->assertNotFalse($stream); |
| 37 | + $output = new StreamOutput($stream); |
| 38 | + $errorOutput = new SymfonyOutput($output, new SymfonyStyle(new \Symfony\Component\Console\Style\SymfonyStyle($this->createStub(InputInterface::class), $output))); |
| 39 | + |
| 40 | + $reporter = new BootstrapAutoloaderExceptionReporter(); |
| 41 | + $runner = new BootstrapFilesRunner(self::getContainer(), $reporter); |
| 42 | + $runner->run($errorOutput, $debugEnabled); |
| 43 | + $reporter->report('SomeClass', new LogicException('Cannot read the class map.')); |
| 44 | + |
| 45 | + rewind($stream); |
| 46 | + $printed = stream_get_contents($stream); |
| 47 | + $this->assertIsString($printed); |
| 48 | + if ($expectPrinted) { |
| 49 | + $this->assertStringContainsString('by a bootstrap autoloader for class SomeClass: Cannot read the class map.', $printed); |
| 50 | + } else { |
| 51 | + $this->assertSame('', $printed); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @throws InceptionNotSuccessfulException |
| 57 | + */ |
| 58 | + public function testReporterIsEnabledWhenTheFilesAlreadyRan(): void |
| 59 | + { |
| 60 | + $stream = fopen('php://memory', 'w+'); |
| 61 | + $this->assertNotFalse($stream); |
| 62 | + $output = new StreamOutput($stream); |
| 63 | + $errorOutput = new SymfonyOutput($output, new SymfonyStyle(new \Symfony\Component\Console\Style\SymfonyStyle($this->createStub(InputInterface::class), $output))); |
| 64 | + |
| 65 | + $reporter = new BootstrapAutoloaderExceptionReporter(); |
| 66 | + $runner = new BootstrapFilesRunner(self::getContainer(), $reporter); |
| 67 | + $runner->run($errorOutput, false); |
| 68 | + $runner->run($errorOutput, true); |
| 69 | + $reporter->report('SomeClass', new LogicException('Cannot read the class map.')); |
| 70 | + |
| 71 | + rewind($stream); |
| 72 | + $printed = stream_get_contents($stream); |
| 73 | + $this->assertIsString($printed); |
| 74 | + $this->assertStringContainsString('by a bootstrap autoloader for class SomeClass', $printed); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments