-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathLogCommand.php
More file actions
189 lines (166 loc) · 6.6 KB
/
LogCommand.php
File metadata and controls
189 lines (166 loc) · 6.6 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
<?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 = [];
private $keysToCalculate = ['assertions', 'time', 'tests', 'errors', 'failures', 'skipped'];
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)
{
$finder = new Finder();
$finder->files()
->in(realpath($input->getArgument('directory')))->sortByName(true);
$this->document = new \DOMDocument('1.0', 'UTF-8');
$this->document->formatOutput = true;
$root = $this->document->createElement('testsuites');
$baseSuite = $this->document->createElement('testsuite');
$baseSuite->setAttribute('name', 'All Suites');
$baseSuite->setAttribute('tests', '0');
$baseSuite->setAttribute('assertions', '0');
$baseSuite->setAttribute('errors', '0');
$baseSuite->setAttribute('failures', '0');
$baseSuite->setAttribute('skipped', '0');
$baseSuite->setAttribute('time', '0');
$this->domElements['All Suites'] = $baseSuite;
$root->appendChild($baseSuite);
$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($baseSuite, $xmlArray);
}
} catch (\Exception $exception) {
$output->writeln(sprintf('<error>Error in file %s: %s</error>', $file->getRealPath(), $exception->getMessage()));
}
}
foreach ($this->domElements as $domElement) {
if ($domElement->hasAttribute('parent')) {
$domElement->removeAttribute('parent');
}
}
$this->calculateTopLevelStats();
$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'])) {
$this->addTestSuites($parent, $testSuite);
}
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) {
$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)
{
foreach ($testCases as $testCase) {
$attributes = $testCase['@attributes'] ?? [];
if (empty($testCase['@attributes']['name'])) {
continue;
}
$name = $testCase['@attributes']['name'];
if (isset($this->domElements[$name])) {
continue;
}
$element = $this->document->createElement('testcase');
foreach ($attributes as $key => $value) {
$element->setAttribute($key, (string)$value);
}
if (isset($testCase['failure']) || isset($testCase['warning']) || isset($testCase['error'])) {
$this->addChildElements($testCase, $element);
}
$parent->appendChild($element);
$this->domElements[$name] = $element;
}
}
private function addChildElements(array $tree, \DOMElement $element)
{
foreach ($tree as $key => $value) {
if ($key == '@attributes') {
continue;
}
$child = $this->document->createElement($key);
$child->nodeValue = $value;
$element->appendChild($child);
}
}
private function calculateTopLevelStats()
{
/** @var \DOMElement $topNode */
$suites = $this->document->getElementsByTagName('testsuites')->item(0);
$topNode = $suites->firstChild;
if ($topNode->hasChildNodes()) {
$stats = array_flip($this->keysToCalculate);
$stats = array_map(function ($_value) {
return 0;
}, $stats);
foreach ($topNode->childNodes as $child) {
$attributes = $child->attributes;
foreach ($attributes as $key => $value) {
if (in_array($key, $this->keysToCalculate)) {
$stats[$key] += $value->nodeValue;
}
}
}
foreach ($stats as $key => $value) {
$topNode->setAttribute($key, (string)$value);
}
}
}
}