diff --git a/src/ChangesReporting/Output/JsonOutputFormatter.php b/src/ChangesReporting/Output/JsonOutputFormatter.php index 521208a7ba2..5ec742933c3 100644 --- a/src/ChangesReporting/Output/JsonOutputFormatter.php +++ b/src/ChangesReporting/Output/JsonOutputFormatter.php @@ -28,7 +28,8 @@ public function report(ProcessResult $processResult, Configuration $configuratio ], ]; - $fileDiffs = $processResult->getFileDiffs(); + // We need onlyWithChanges: false to include all file diffs + $fileDiffs = $processResult->getFileDiffs(onlyWithChanges: false); ksort($fileDiffs); foreach ($fileDiffs as $fileDiff) { $filePath = $configuration->isReportingWithRealPath() @@ -36,11 +37,13 @@ public function report(ProcessResult $processResult, Configuration $configuratio : $fileDiff->getRelativeFilePath() ; - $errorsJson[Bridge::FILE_DIFFS][] = [ - 'file' => $filePath, - 'diff' => $fileDiff->getDiff(), - 'applied_rectors' => $fileDiff->getRectorClasses(), - ]; + if ($configuration->shouldShowDiffs() && $fileDiff->getDiff() !== '') { + $errorsJson[Bridge::FILE_DIFFS][] = [ + 'file' => $filePath, + 'diff' => $fileDiff->getDiff(), + 'applied_rectors' => $fileDiff->getRectorClasses(), + ]; + } // for Rector CI $errorsJson['changed_files'][] = $filePath; diff --git a/tests/ChangesReporting/Output/Fixtures/without_diffs.json b/tests/ChangesReporting/Output/Fixtures/without_diffs.json new file mode 100644 index 00000000000..19629ae13dc --- /dev/null +++ b/tests/ChangesReporting/Output/Fixtures/without_diffs.json @@ -0,0 +1,17 @@ +{ + "totals": { + "changed_files": 2, + "errors": 1 + }, + "changed_files": [ + "some/file.php", + "some/file_foo.php" + ], + "errors": [ + { + "message": "Some error message", + "file": "some/file.php", + "line": 1 + } + ] +} diff --git a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php new file mode 100644 index 00000000000..8fe6f194346 --- /dev/null +++ b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php @@ -0,0 +1,70 @@ +jsonOutputFormatter = new JsonOutputFormatter(); + + parent::setUp(); + } + + public function testGetName(): void + { + $this->assertSame('json', $this->jsonOutputFormatter->getName()); + } + + public function testReportShouldShowNumberOfChangesWithNoDiffs(): void + { + $this->expectOsOutputString((string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json')); + + $this->jsonOutputFormatter->report( + new ProcessResult( + [new SystemError('Some error message', 'some/file.php', 1)], + [ + new FileDiff( + 'some/file.php', + '--- Original' . PHP_EOL . '+++ New' . PHP_EOL . + '@@ -38,5 +39,6 @@' . PHP_EOL . + 'return true;' . PHP_EOL . '}' . PHP_EOL, + 'diff console formatted', + [new RectorWithLineChange(StrStartsWithRector::class, 38)] + ), + new FileDiff( + 'some/file_foo.php', + '', + '', + [new RectorWithLineChange(StrStartsWithRector::class, 38)] + ), + ], + 2 + ), + new Configuration(showDiffs: false) + ); + } + + protected function expectOsOutputString(string $expectedOutput): void + { + $isWindows = strncasecmp(PHP_OS, 'WIN', 3) === 0; + if ($isWindows) { + $expectedOutput = str_replace('%0A', '%0D%0A', $expectedOutput); + } + + parent::expectOutputString($expectedOutput); + } +}