Skip to content

Commit e3230c1

Browse files
committed
[ChangesReporting][Alternative] Use JsonOutputFactory to make easy test for cross OS usage
1 parent 5527842 commit e3230c1

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
@@ -22,70 +20,6 @@ public function getName(): string
2220

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

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)