From 20b568d67b1a997c8a1d9fafed1b75a837a18f19 Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Mon, 5 Jan 2026 14:27:03 +0000 Subject: [PATCH 1/8] Fixes showing files changed in JSON formatter --- .../Output/JsonOutputFormatter.php | 15 ++-- .../Output/Fixtures/without_diffs.json | 17 +++++ .../Output/JsonOutputFormatterTest.php | 70 +++++++++++++++++++ 3 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 tests/ChangesReporting/Output/Fixtures/without_diffs.json create mode 100644 tests/ChangesReporting/Output/JsonOutputFormatterTest.php 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..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); + } +} From e9b5a0f13addb2c4d9c5faf8b72cc51b48ec3088 Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Mon, 5 Jan 2026 16:29:12 +0000 Subject: [PATCH 2/8] test change --- .../Output/JsonOutputFormatterTest.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php index 8fe6f194346..b5c2fc2dbbc 100644 --- a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php +++ b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php @@ -31,7 +31,7 @@ public function testGetName(): void public function testReportShouldShowNumberOfChangesWithNoDiffs(): void { - $this->expectOsOutputString((string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json')); + $this->expectOutputString((string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json')); $this->jsonOutputFormatter->report( new ProcessResult( @@ -57,14 +57,4 @@ public function testReportShouldShowNumberOfChangesWithNoDiffs(): void 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); - } } From 908372afd0433f96c9fbbefd6a46ecec7f797f1d Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Mon, 5 Jan 2026 16:34:49 +0000 Subject: [PATCH 3/8] Revert "test change" This reverts commit e9b5a0f13addb2c4d9c5faf8b72cc51b48ec3088. --- .../Output/JsonOutputFormatterTest.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php index b5c2fc2dbbc..8fe6f194346 100644 --- a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php +++ b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php @@ -31,7 +31,7 @@ public function testGetName(): void public function testReportShouldShowNumberOfChangesWithNoDiffs(): void { - $this->expectOutputString((string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json')); + $this->expectOsOutputString((string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json')); $this->jsonOutputFormatter->report( new ProcessResult( @@ -57,4 +57,14 @@ public function testReportShouldShowNumberOfChangesWithNoDiffs(): void 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); + } } From 8bd51e05dcb6e9486f98de2b8cda7d8489f763f6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 7 Jan 2026 13:53:27 +0700 Subject: [PATCH 4/8] normalized line ending --- .../Output/JsonOutputFormatterTest.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php index 8fe6f194346..e177a49c4d2 100644 --- a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php +++ b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php @@ -31,7 +31,7 @@ public function testGetName(): void public function testReportShouldShowNumberOfChangesWithNoDiffs(): void { - $this->expectOsOutputString((string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json')); + ob_start(); $this->jsonOutputFormatter->report( new ProcessResult( @@ -56,15 +56,14 @@ public function testReportShouldShowNumberOfChangesWithNoDiffs(): void ), 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); - } + $actualOutput = ob_get_clean(); + $expectedOutput = (string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json'); + + // Normalize line endings for comparison + $actualOutput = str_replace("\r\n", "\n", $actualOutput); + $expectedOutput = str_replace("\r\n", "\n", $expectedOutput); - parent::expectOutputString($expectedOutput); + $this->assertSame($expectedOutput, $actualOutput); } } From a544397e350f841bfe4548e4273f3c4a74d39a6f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 7 Jan 2026 14:30:59 +0700 Subject: [PATCH 5/8] fix phpstan --- tests/ChangesReporting/Output/JsonOutputFormatterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php index e177a49c4d2..36a295894a7 100644 --- a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php +++ b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php @@ -57,7 +57,7 @@ public function testReportShouldShowNumberOfChangesWithNoDiffs(): void new Configuration(showDiffs: false) ); - $actualOutput = ob_get_clean(); + $actualOutput = (string) ob_get_clean(); $expectedOutput = (string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json'); // Normalize line endings for comparison From d698b97085203aadc1e0649c597c526d2f1af392 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 7 Jan 2026 17:12:33 +0700 Subject: [PATCH 6/8] update using heredoc --- .../Output/JsonOutputFormatterTest.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php index 36a295894a7..6da2077e982 100644 --- a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php +++ b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php @@ -39,9 +39,14 @@ public function testReportShouldShowNumberOfChangesWithNoDiffs(): void [ new FileDiff( 'some/file.php', - '--- Original' . PHP_EOL . '+++ New' . PHP_EOL . - '@@ -38,5 +39,6 @@' . PHP_EOL . - 'return true;' . PHP_EOL . '}' . PHP_EOL, + <<<'DIFF' + --- Original + +++ New + @@ -38,5 +39,6 @@ + return true; + } + + DIFF, 'diff console formatted', [new RectorWithLineChange(StrStartsWithRector::class, 38)] ), From e1d3d99c4a89183f0e60000e34b9ef4195be98d7 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 7 Jan 2026 17:36:26 +0700 Subject: [PATCH 7/8] Fix on use of parent::expectOutputString() --- .../Output/JsonOutputFormatterTest.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php index 6da2077e982..836e7de02d6 100644 --- a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php +++ b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php @@ -31,7 +31,7 @@ public function testGetName(): void public function testReportShouldShowNumberOfChangesWithNoDiffs(): void { - ob_start(); + $this->expectOsOutputString((string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json')); $this->jsonOutputFormatter->report( new ProcessResult( @@ -61,14 +61,16 @@ public function testReportShouldShowNumberOfChangesWithNoDiffs(): void ), new Configuration(showDiffs: false) ); + } - $actualOutput = (string) ob_get_clean(); - $expectedOutput = (string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json'); - - // Normalize line endings for comparison - $actualOutput = str_replace("\r\n", "\n", $actualOutput); - $expectedOutput = str_replace("\r\n", "\n", $expectedOutput); + protected function expectOsOutputString(string $expectedOutput): void + { + $isWindows = strncasecmp(PHP_OS, 'WIN', 3) === 0; + if ($isWindows) { + $expectedOutput = str_replace('%0A', '%0D%0A', $expectedOutput); + $expectedOutput = trim($expectedOutput) . PHP_EOL; + } - $this->assertSame($expectedOutput, $actualOutput); + parent::expectOutputString($expectedOutput); } } From 9cc1408131a847ac16978dbe5ade0666e202e26e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 7 Jan 2026 19:02:06 +0700 Subject: [PATCH 8/8] clean up unnecessary override lookalike --- .../Output/JsonOutputFormatterTest.php | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php index 836e7de02d6..6a11a5f4487 100644 --- a/tests/ChangesReporting/Output/JsonOutputFormatterTest.php +++ b/tests/ChangesReporting/Output/JsonOutputFormatterTest.php @@ -31,7 +31,8 @@ public function testGetName(): void public function testReportShouldShowNumberOfChangesWithNoDiffs(): void { - $this->expectOsOutputString((string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json')); + $expectedOutput = (string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json'); + $this->expectOutputString(rtrim($expectedOutput) . PHP_EOL); $this->jsonOutputFormatter->report( new ProcessResult( @@ -62,15 +63,4 @@ public function testReportShouldShowNumberOfChangesWithNoDiffs(): void 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); - $expectedOutput = trim($expectedOutput) . PHP_EOL; - } - - parent::expectOutputString($expectedOutput); - } }