forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJUnitOutputFormatter.php
More file actions
115 lines (113 loc) · 4.65 KB
/
Copy pathJUnitOutputFormatter.php
File metadata and controls
115 lines (113 loc) · 4.65 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
<?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 RectorPrefix202602\Symfony\Component\Console\Style\SymfonyStyle;
final class JUnitOutputFormatter implements OutputFormatterInterface
{
/**
* @readonly
*/
private SymfonyStyle $symfonyStyle;
/**
* @var string
*/
private const NAME = 'junit';
/**
* @var string
*/
private const XML_ATTRIBUTE_FILE = 'file';
/**
* @var string
*/
private const XML_ATTRIBUTE_NAME = 'name';
/**
* @var string
*/
private const XML_ATTRIBUTE_TYPE = 'type';
/**
* @var string
*/
private const XML_ELEMENT_TESTSUITES = 'testsuites';
/**
* @var string
*/
private const XML_ELEMENT_TESTSUITE = 'testsuite';
/**
* @var string
*/
private const XML_ELEMENT_TESTCASE = 'testcase';
/**
* @var string
*/
private const XML_ELEMENT_ERROR = 'error';
public function __construct(SymfonyStyle $symfonyStyle)
{
$this->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');
$xmlTestSuite = $domDocument->createElement(self::XML_ELEMENT_TESTSUITE);
$xmlTestSuite->setAttribute(self::XML_ATTRIBUTE_NAME, 'rector');
$xmlTestSuites = $domDocument->createElement(self::XML_ELEMENT_TESTSUITES);
$xmlTestSuites->appendChild($xmlTestSuite);
$domDocument->appendChild($xmlTestSuites);
$this->appendSystemErrors($processResult, $configuration, $domDocument, $xmlTestSuite);
$this->appendFileDiffs($processResult, $configuration, $domDocument, $xmlTestSuite);
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);
}
}
}