Repository navigation
Expand file tree
/
Copy pathJUnitOutputFormatter.php
More file actions
136 lines (101 loc) · 4.54 KB
/
Copy pathJUnitOutputFormatter.php
File metadata and controls
136 lines (101 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* JUnit specification:
* - https://github.com/junit-team/junit5/blob/main/platform-tests/src/test/resources/jenkins-junit.xsda
*/
declare(strict_types=1);
namespace Rector\ChangesReporting\Output;
use DOMDocument;
use DOMElement;
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
use Rector\ValueObject\Configuration;
use Rector\ValueObject\ProcessResult;
use Symfony\Component\Console\Style\SymfonyStyle;
final readonly class JUnitOutputFormatter implements OutputFormatterInterface
{
private const string NAME = 'junit';
private const string XML_ATTRIBUTE_FILE = 'file';
private const string XML_ATTRIBUTE_NAME = 'name';
private const string XML_ATTRIBUTE_TYPE = 'type';
private const string XML_ELEMENT_TESTSUITES = 'testsuites';
private const string XML_ELEMENT_TESTSUITE = 'testsuite';
private const string XML_ELEMENT_TESTCASE = 'testcase';
private const string XML_ELEMENT_ERROR = 'error';
public function __construct(
private SymfonyStyle $symfonyStyle,
) {
}
public function getName(): string
{
return self::NAME;
}
public function report(ProcessResult $processResult, Configuration $configuration): void
{
if (! extension_loaded('dom')) {
$this->symfonyStyle->warning(
'The "dom" extension is not loaded. The rector could not generate a response in the JUnit format',
);
return;
}
$domDocument = new DOMDocument('1.0', 'UTF-8');
$domElement = $domDocument->createElement(self::XML_ELEMENT_TESTSUITE);
$domElement->setAttribute(self::XML_ATTRIBUTE_NAME, 'rector');
$xmlTestSuites = $domDocument->createElement(self::XML_ELEMENT_TESTSUITES);
$xmlTestSuites->appendChild($domElement);
$domDocument->appendChild($xmlTestSuites);
$this->appendSystemErrors($processResult, $configuration, $domDocument, $domElement);
$this->appendFileDiffs($processResult, $configuration, $domDocument, $domElement);
echo $domDocument->saveXML() . PHP_EOL;
}
private function appendSystemErrors(
ProcessResult $processResult,
Configuration $configuration,
DOMDocument $domDocument,
DOMElement $domElement,
): void {
if ($processResult->getSystemErrors() === []) {
return;
}
foreach ($processResult->getSystemErrors() as $systemError) {
$filePath = $configuration->isReportingWithRealPath()
? ($systemError->getAbsoluteFilePath() ?? '')
: ($systemError->getRelativeFilePath() ?? '')
;
$xmlError = $domDocument->createElement(self::XML_ELEMENT_ERROR);
$xmlError->setAttribute(self::XML_ATTRIBUTE_TYPE, 'Error');
$xmlError->appendChild($domDocument->createTextNode($systemError->getMessage()));
$xmlTestCase = $domDocument->createElement(self::XML_ELEMENT_TESTCASE);
$xmlTestCase->setAttribute(self::XML_ATTRIBUTE_FILE, $filePath);
$xmlTestCase->setAttribute(self::XML_ATTRIBUTE_NAME, $filePath . ':' . $systemError->getLine());
$xmlTestCase->appendChild($xmlError);
$domElement->appendChild($xmlTestCase);
}
}
private function appendFileDiffs(
ProcessResult $processResult,
Configuration $configuration,
DOMDocument $domDocument,
DOMElement $domElement,
): void {
if ($processResult->getFileDiffs() === []) {
return;
}
$fileDiffs = $processResult->getFileDiffs();
ksort($fileDiffs);
foreach ($fileDiffs as $fileDiff) {
$filePath = $configuration->isReportingWithRealPath()
? ($fileDiff->getAbsoluteFilePath() ?? '')
: ($fileDiff->getRelativeFilePath() ?? '')
;
$rectorClasses = implode(' / ', $fileDiff->getRectorShortClasses());
$xmlError = $domDocument->createElement(self::XML_ELEMENT_ERROR);
$xmlError->setAttribute(self::XML_ATTRIBUTE_TYPE, $rectorClasses);
$xmlError->appendChild($domDocument->createTextNode($fileDiff->getDiff()));
$xmlTestCase = $domDocument->createElement(self::XML_ELEMENT_TESTCASE);
$xmlTestCase->setAttribute(self::XML_ATTRIBUTE_FILE, $filePath);
$xmlTestCase->setAttribute(self::XML_ATTRIBUTE_NAME, $filePath . ':' . $fileDiff->getFirstLineNumber());
$xmlTestCase->appendChild($xmlError);
$domElement->appendChild($xmlTestCase);
}
}
}