-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathLogCommand.php
More file actions
196 lines (169 loc) · 7.3 KB
/
LogCommand.php
File metadata and controls
196 lines (169 loc) · 7.3 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
declare(strict_types=1);
namespace Nimut\PhpunitMerger\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
class LogCommand extends Command
{
/**
* @var \DOMDocument
*/
private $document;
/**
* @var \DOMElement[]
*/
private $domElements = [];
protected function configure()
{
$this->setName('log')
->setDescription('Merges multiple PHPUnit JUnit xml files into one')
->addArgument(
'directory',
InputArgument::REQUIRED,
'The directory containing PHPUnit JUnit xml files'
)
->addArgument(
'file',
InputArgument::REQUIRED,
'The file where to write the merged result'
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$finder = new Finder();
$finder->files()
->in(realpath($input->getArgument('directory')));
$this->document = new \DOMDocument('1.0', 'UTF-8');
$this->document->formatOutput = true;
$root = $this->document->createElement('testsuites');
$this->document->appendChild($root);
foreach ($finder as $file) {
try {
$xml = new \SimpleXMLElement(file_get_contents($file->getRealPath()));
$xmlArray = json_decode(json_encode($xml), true);
if (!empty($xmlArray)) {
$this->addTestSuites($root, $xmlArray);
}
} catch (\Exception $exception) {
// Initial fallthrough
}
}
foreach ($this->domElements as $domElement) {
if ($domElement->hasAttribute('parent')) {
$domElement->removeAttribute('parent');
}
}
$file = $input->getArgument('file');
if (!is_dir(dirname($file))) {
@mkdir(dirname($file), 0777, true);
}
$this->document->save($input->getArgument('file'));
return 0;
}
private function addTestSuites(\DOMElement $parent, array $testSuites)
{
foreach ($testSuites as $testSuite) {
if (empty($testSuite['@attributes']['name'])) {
if (!empty($testSuite['testsuite'])) {
$children = isset($testSuite['testsuite']['@attributes']) ? [$testSuite['testsuite']] : $testSuite['testsuite'];
$this->addTestSuites($parent, $children);
}
continue;
}
$name = $testSuite['@attributes']['name'];
if (isset($this->domElements[$name])) {
$element = $this->domElements[$name];
} else {
$element = $this->document->createElement('testsuite');
$element->setAttribute('parent', $parent->getAttribute('name'));
$attributes = $testSuite['@attributes'] ?? [];
foreach ($attributes as $key => $value) {
$value = $key === 'name' || $key === 'file' ? $value : 0;
$element->setAttribute($key, (string)$value);
}
$parent->appendChild($element);
$this->domElements[$name] = $element;
}
if (!empty($testSuite['testsuite'])) {
$children = isset($testSuite['testsuite']['@attributes']) ? [$testSuite['testsuite']] : $testSuite['testsuite'];
$this->addTestSuites($element, $children);
}
if (!empty($testSuite['testcase'])) {
$children = isset($testSuite['testcase']['@attributes']) ? [$testSuite['testcase']] : $testSuite['testcase'];
$this->addTestCases($element, $children);
}
}
}
private function addTestCases(\DOMElement $parent, array $testCases)
{
$statusTags = [
'error' => 'errors',
'failure' => 'failures',
'skipped' => 'skipped',
'warning' => 'warnings',
];
foreach ($testCases as $testCase) {
$attributes = $testCase['@attributes'] ?? [];
if (empty($testCase['@attributes']['name'])) {
continue;
}
$name = $testCase['@attributes']['name'];
$class = $testCase['@attributes']['class'];
if (isset($this->domElements[$class . '::' . $name])) {
$previusTestCase = $this->domElements[$class . '::' . $name];
$previousTime = (float) ($previusTestCase->getAttribute('time') ?? 0);
$newTime = (float) ($testCase['@attributes']['time'] ?? 0);
$hasActualTestCaseAStatusTag = !empty(array_intersect(array_keys($testCase), array_keys($statusTags)));
$hasPreviusTestCaseAStatusTag = $previusTestCase->childNodes->length > 0;
if ($hasActualTestCaseAStatusTag ||
!$hasPreviusTestCaseAStatusTag &&
$newTime < $previousTime) {
continue;
}
$this->addAttributeValueToTestSuite($parent, 'tests', -1);
foreach ($previusTestCase->childNodes as $child) {
$this->addAttributeValueToTestSuite($parent, $statusTags[$child->nodeName], -1);
}
foreach ($previusTestCase->attributes as $attribute) {
if (!is_numeric($attribute->nodeValue) || $attribute->nodeName === 'line') {
continue;
}
$this->addAttributeValueToTestSuite($parent, $attribute->nodeName, -$attribute->nodeValue);
}
$parent->removeChild($previusTestCase);
unset($this->domElements[$class . '::' . $name]);
}
$element = $this->document->createElement('testcase');
foreach ($attributes as $key => $value) {
$element->setAttribute($key, (string)$value);
if (!is_numeric($value) || $key === 'line') {
continue;
}
$this->addAttributeValueToTestSuite($parent, $key, $value);
}
$this->addAttributeValueToTestSuite($parent, 'tests', 1);
foreach ($statusTags as $key => $value) {
if (isset($testCase[$key])) {
$this->addAttributeValueToTestSuite($parent, $value, 1);
$element->appendChild($this->document->createElement($key));
}
}
$parent->appendChild($element);
$this->domElements[$class . '::' . $name] = $element;
}
}
private function addAttributeValueToTestSuite(\DOMElement $element, $key, $value)
{
$currentValue = $element->hasAttribute($key) ? $element->getAttribute($key) : 0;
$element->setAttribute($key, (string)($currentValue + $value));
if ($element->hasAttribute('parent')) {
$parent = $element->getAttribute('parent');
if (isset($this->domElements[$parent])) {
$this->addAttributeValueToTestSuite($this->domElements[$parent], $key, $value);
}
}
}
}