Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions bin/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
40 changes: 40 additions & 0 deletions tests/Bin/RectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Bin;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;

final class RectorTest extends TestCase
{
/**
* @return Iterator<string, array{command: string, expectedOutput: string}>
*/
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()));
}
}
10 changes: 10 additions & 0 deletions tests/Bin/config/incorrect-phpstan-files.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withPhpSets()
->withPaths([__DIR__])
->withPHPStanConfigs([__DIR__ . '/phpstan.neon']);
3 changes: 3 additions & 0 deletions tests/Bin/config/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

parameters:
invalidParameters: true
Loading