Skip to content

Commit 00f4f5a

Browse files
committed
[ChangesReporting][Alternative] Use JsonOutputFactory to make easy test for cross OS usage
1 parent 47d556b commit 00f4f5a

File tree

4 files changed

+90
-96
lines changed

4 files changed

+90
-96
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\ChangesReporting\Output\Factory;
6+
7+
use Nette\Utils\Json;
8+
use Rector\Parallel\ValueObject\Bridge;
9+
use Rector\ValueObject\Configuration;
10+
use Rector\ValueObject\Error\SystemError;
11+
use Rector\ValueObject\ProcessResult;
12+
13+
final class JsonOutputFactory
14+
{
15+
public static function create(ProcessResult $processResult, Configuration $configuration): string
16+
{
17+
$errorsJson = [
18+
'totals' => [
19+
'changed_files' => $processResult->getTotalChanged(),
20+
],
21+
];
22+
23+
// We need onlyWithChanges: false to include all file diffs
24+
$fileDiffs = $processResult->getFileDiffs(onlyWithChanges: false);
25+
ksort($fileDiffs);
26+
foreach ($fileDiffs as $fileDiff) {
27+
$filePath = $configuration->isReportingWithRealPath()
28+
? ($fileDiff->getAbsoluteFilePath() ?? '')
29+
: $fileDiff->getRelativeFilePath()
30+
;
31+
32+
if ($configuration->shouldShowDiffs() && $fileDiff->getDiff() !== '') {
33+
$errorsJson[Bridge::FILE_DIFFS][] = [
34+
'file' => $filePath,
35+
'diff' => $fileDiff->getDiff(),
36+
'applied_rectors' => $fileDiff->getRectorClasses(),
37+
];
38+
}
39+
40+
// for Rector CI
41+
$errorsJson['changed_files'][] = $filePath;
42+
}
43+
44+
$systemErrors = $processResult->getSystemErrors();
45+
$errorsJson['totals']['errors'] = count($systemErrors);
46+
47+
$errorsData = self::createErrorsData($systemErrors, $configuration->isReportingWithRealPath());
48+
if ($errorsData !== []) {
49+
$errorsJson['errors'] = $errorsData;
50+
}
51+
52+
return Json::encode($errorsJson, pretty: true);
53+
}
54+
55+
/**
56+
* @param SystemError[] $errors
57+
* @return mixed[]
58+
*/
59+
private static function createErrorsData(array $errors, bool $absoluteFilePath): array
60+
{
61+
$errorsData = [];
62+
63+
foreach ($errors as $error) {
64+
$errorDataJson = [
65+
'message' => $error->getMessage(),
66+
'file' => $absoluteFilePath ? $error->getAbsoluteFilePath() : $error->getRelativeFilePath(),
67+
];
68+
69+
if ($error->getRectorClass() !== null) {
70+
$errorDataJson['caused_by'] = $error->getRectorClass();
71+
}
72+
73+
if ($error->getLine() !== null) {
74+
$errorDataJson['line'] = $error->getLine();
75+
}
76+
77+
$errorsData[] = $errorDataJson;
78+
}
79+
80+
return $errorsData;
81+
}
82+
}

src/ChangesReporting/Output/JsonOutputFormatter.php

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
namespace Rector\ChangesReporting\Output;
66

7-
use Nette\Utils\Json;
87
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
9-
use Rector\Parallel\ValueObject\Bridge;
8+
use Rector\ChangesReporting\Output\Factory\JsonOutputFactory;
109
use Rector\ValueObject\Configuration;
11-
use Rector\ValueObject\Error\SystemError;
1210
use Rector\ValueObject\ProcessResult;
1311

1412
final readonly class JsonOutputFormatter implements OutputFormatterInterface
@@ -25,70 +23,6 @@ public function getName(): string
2523

2624
public function report(ProcessResult $processResult, Configuration $configuration): void
2725
{
28-
$errorsJson = [
29-
'totals' => [
30-
'changed_files' => $processResult->getTotalChanged(),
31-
],
32-
];
33-
34-
// We need onlyWithChanges: false to include all file diffs
35-
$fileDiffs = $processResult->getFileDiffs(onlyWithChanges: false);
36-
ksort($fileDiffs);
37-
foreach ($fileDiffs as $fileDiff) {
38-
$filePath = $configuration->isReportingWithRealPath()
39-
? ($fileDiff->getAbsoluteFilePath() ?? '')
40-
: $fileDiff->getRelativeFilePath()
41-
;
42-
43-
if ($configuration->shouldShowDiffs() && $fileDiff->getDiff() !== '') {
44-
$errorsJson[Bridge::FILE_DIFFS][] = [
45-
'file' => $filePath,
46-
'diff' => $fileDiff->getDiff(),
47-
'applied_rectors' => $fileDiff->getRectorClasses(),
48-
];
49-
}
50-
51-
// for Rector CI
52-
$errorsJson['changed_files'][] = $filePath;
53-
}
54-
55-
$systemErrors = $processResult->getSystemErrors();
56-
$errorsJson['totals']['errors'] = count($systemErrors);
57-
58-
$errorsData = $this->createErrorsData($systemErrors, $configuration->isReportingWithRealPath());
59-
if ($errorsData !== []) {
60-
$errorsJson['errors'] = $errorsData;
61-
}
62-
63-
$json = Json::encode($errorsJson, pretty: true);
64-
echo $json . PHP_EOL;
65-
}
66-
67-
/**
68-
* @param SystemError[] $errors
69-
* @return mixed[]
70-
*/
71-
private function createErrorsData(array $errors, bool $absoluteFilePath): array
72-
{
73-
$errorsData = [];
74-
75-
foreach ($errors as $error) {
76-
$errorDataJson = [
77-
'message' => $error->getMessage(),
78-
'file' => $absoluteFilePath ? $error->getAbsoluteFilePath() : $error->getRelativeFilePath(),
79-
];
80-
81-
if ($error->getRectorClass() !== null) {
82-
$errorDataJson['caused_by'] = $error->getRectorClass();
83-
}
84-
85-
if ($error->getLine() !== null) {
86-
$errorDataJson['line'] = $error->getLine();
87-
}
88-
89-
$errorsData[] = $errorDataJson;
90-
}
91-
92-
return $errorsData;
26+
echo JsonOutputFactory::create($processResult, $configuration) . PHP_EOL;
9327
}
9428
}

tests/ChangesReporting/Output/JsonOutputFormatterTest.php renamed to tests/ChangesReporting/Output/Factory/JsonOutputFactoryTest.php

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,22 @@
22

33
declare(strict_types=1);
44

5-
namespace ChangesReporting\Output;
5+
namespace Rector\Tests\ChangesReporting\Output\Factory;
66

77
use PHPUnit\Framework\TestCase;
8-
use Rector\ChangesReporting\Output\JsonOutputFormatter;
8+
use Rector\ChangesReporting\Output\Factory\JsonOutputFactory;
99
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
1010
use Rector\Php80\Rector\Identical\StrStartsWithRector;
1111
use Rector\ValueObject\Configuration;
1212
use Rector\ValueObject\Error\SystemError;
1313
use Rector\ValueObject\ProcessResult;
1414
use Rector\ValueObject\Reporting\FileDiff;
1515

16-
final class JsonOutputFormatterTest extends TestCase
16+
final class JsonOutputFactoryTest extends TestCase
1717
{
18-
private readonly JsonOutputFormatter $jsonOutputFormatter;
19-
20-
protected function setUp(): void
21-
{
22-
$this->jsonOutputFormatter = new JsonOutputFormatter();
23-
24-
parent::setUp();
25-
}
26-
27-
public function testGetName(): void
28-
{
29-
$this->assertSame('json', $this->jsonOutputFormatter->getName());
30-
}
31-
3218
public function testReportShouldShowNumberOfChangesWithNoDiffs(): void
3319
{
34-
ob_start();
35-
36-
$this->jsonOutputFormatter->report(
20+
$actualOutput = JsonOutputFactory::create(
3721
new ProcessResult(
3822
[new SystemError('Some error message', 'some/file.php', 1)],
3923
[
@@ -57,13 +41,7 @@ public function testReportShouldShowNumberOfChangesWithNoDiffs(): void
5741
new Configuration(showDiffs: false)
5842
);
5943

60-
$actualOutput = (string) ob_get_clean();
61-
$expectedOutput = (string) file_get_contents(__DIR__ . '/Fixtures/without_diffs.json');
62-
63-
// Normalize line endings for comparison
64-
$actualOutput = str_replace("\r\n", "\n", $actualOutput);
65-
$expectedOutput = str_replace("\r\n", "\n", $expectedOutput);
66-
44+
$expectedOutput = (string) file_get_contents(__DIR__ . '/../Fixtures/without_diffs.json');
6745
$this->assertSame($expectedOutput, $actualOutput);
6846
}
6947
}

tests/ChangesReporting/Output/Fixtures/without_diffs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
"line": 1
1515
}
1616
]
17-
}
17+
}

0 commit comments

Comments
 (0)