-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathLogCommand.php
More file actions
155 lines (133 loc) · 5.22 KB
/
LogCommand.php
File metadata and controls
155 lines (133 loc) · 5.22 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
<?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)
{
$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'])) {
$this->addTestSuites($parent, $testSuite['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) {
$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)
{
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 (!is_numeric($value)) {
continue;
}
$this->addAttributeValueToTestSuite($parent, $key, $value);
}
$parent->appendChild($element);
$this->domElements[$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);
}
}
}
}