diff --git a/src/ChangesReporting/Output/JsonOutputFormatter.php b/src/ChangesReporting/Output/JsonOutputFormatter.php index 65062a4bb8e..f37835ba859 100644 --- a/src/ChangesReporting/Output/JsonOutputFormatter.php +++ b/src/ChangesReporting/Output/JsonOutputFormatter.php @@ -31,7 +31,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() @@ -39,11 +40,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..6a11a5f4487 --- /dev/null +++ b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php @@ -0,0 +1,66 @@ +jsonOutputFormatter = new JsonOutputFormatter(); + + parent::setUp(); + } + + public function testGetName(): void + { + $this->assertSame('json', $this->jsonOutputFormatter->getName()); + } + + public function testReportShouldShowNumberOfChangesWithNoDiffs(): void + { + $expectedOutput = (string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json'); + $this->expectOutputString(rtrim($expectedOutput) . PHP_EOL); + + $this->jsonOutputFormatter->report( + new ProcessResult( + [new SystemError('Some error message', 'some/file.php', 1)], + [ + new FileDiff( + 'some/file.php', + <<<'DIFF' + --- Original + +++ New + @@ -38,5 +39,6 @@ + return true; + } + + DIFF, + 'diff console formatted', + [new RectorWithLineChange(StrStartsWithRector::class, 38)] + ), + new FileDiff( + 'some/file_foo.php', + '', + '', + [new RectorWithLineChange(StrStartsWithRector::class, 38)] + ), + ], + 2 + ), + new Configuration(showDiffs: false) + ); + } +}