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
15 changes: 9 additions & 6 deletions src/ChangesReporting/Output/JsonOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ 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()
? ($fileDiff->getAbsoluteFilePath() ?? '')
: $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;
Expand Down
17 changes: 17 additions & 0 deletions tests/ChangesReporting/Output/Fixtures/without_diffs.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
66 changes: 66 additions & 0 deletions tests/ChangesReporting/Output/JsonOutputFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace ChangesReporting\Output;

use PHPUnit\Framework\TestCase;
use Rector\ChangesReporting\Output\JsonOutputFormatter;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Php80\Rector\Identical\StrStartsWithRector;
use Rector\ValueObject\Configuration;
use Rector\ValueObject\Error\SystemError;
use Rector\ValueObject\ProcessResult;
use Rector\ValueObject\Reporting\FileDiff;

final class JsonOutputFormatterTest extends TestCase
{
private readonly JsonOutputFormatter $jsonOutputFormatter;

protected function setUp(): void
{
$this->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)
);
}
}