diff --git a/bin/rector.php b/bin/rector.php index 07eea9a09cc..ef8161e1ffa 100755 --- a/bin/rector.php +++ b/bin/rector.php @@ -143,14 +143,20 @@ public function loadIfExistsAndNotLoadedYet(string $filePath): void // report fatal error in json format if ($outputFormat === JsonOutputFormatter::NAME) { + $errors = []; + do { + $errors[] = $throwable->getMessage(); + } while ($throwable = $throwable->getPrevious()); echo Json::encode([ - 'fatal_errors' => [$throwable->getMessage()], + 'fatal_errors' => $errors, ]); } else { // report fatal errors in console format $symfonyStyleFactory = new SymfonyStyleFactory(new PrivatesAccessor()); $symfonyStyle = $symfonyStyleFactory->create(); - $symfonyStyle->error(str_replace("\r\n", "\n", $throwable->getMessage())); + do { + $symfonyStyle->error(str_replace("\r\n", "\n", $throwable->getMessage())); + } while ($throwable = $throwable->getPrevious()); } exit(Command::FAILURE); diff --git a/tests/Bin/RectorTest.php b/tests/Bin/RectorTest.php new file mode 100644 index 00000000000..11f37ff522e --- /dev/null +++ b/tests/Bin/RectorTest.php @@ -0,0 +1,40 @@ + + */ + public static function outputProvider(): Iterator + { + yield 'Version' => [ + 'command' => PHP_BINARY . ' bin/rector --version', + 'expectedOutput' => "Rector @package_version@" . PHP_EOL, + ]; + yield 'Exception with previous console output' => [ + 'command' => PHP_BINARY . ' bin/rector -c tests/Bin/config/incorrect-phpstan-files.php', + 'expectedOutput' => PHP_EOL . " [ERROR] Rector\\NodeTypeResolver\\DependencyInjection\\PHPStanServicesFactory " . PHP_EOL . PHP_EOL . " [ERROR] Unexpected item 'parameters › invalidParameters'. " . PHP_EOL . PHP_EOL, + ]; + yield 'Exception with previous console output in JSON format' => [ + 'command' => PHP_BINARY . ' bin/rector -c tests/Bin/config/incorrect-phpstan-files.php --output-format json', + 'expectedOutput' => '{"fatal_errors":["Rector\\\\NodeTypeResolver\\\\DependencyInjection\\\\PHPStanServicesFactory","Unexpected item \'parameters › invalidParameters\'."]}', + ]; + } + + #[DataProvider('outputProvider')] + public function testConsoleOutput(string $command, string $expectedOutput): void + { + $process = Process::fromShellCommandline($command); + $process->run(); + $this->assertSame($expectedOutput, preg_replace("/ +/", " ", $process->getOutput())); + } +} diff --git a/tests/Bin/config/incorrect-phpstan-files.php b/tests/Bin/config/incorrect-phpstan-files.php new file mode 100644 index 00000000000..3d570489773 --- /dev/null +++ b/tests/Bin/config/incorrect-phpstan-files.php @@ -0,0 +1,10 @@ +withPhpSets() + ->withPaths([__DIR__]) + ->withPHPStanConfigs([__DIR__ . '/phpstan.neon']); diff --git a/tests/Bin/config/phpstan.neon b/tests/Bin/config/phpstan.neon new file mode 100644 index 00000000000..efd970b5f28 --- /dev/null +++ b/tests/Bin/config/phpstan.neon @@ -0,0 +1,3 @@ + +parameters: + invalidParameters: true