Skip to content

Commit a80b5d7

Browse files
committed
Add Calculations based on first level nodes.
We should assume that the calculation from PHPUnit is correct so we have to recalculate the base node only. Add Subnodes of testcase node also beacuse it contains important error messages
1 parent ff0c4e8 commit a80b5d7

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

src/PhpunitMerger/Command/LogCommand.php

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class LogCommand extends Command
2222
*/
2323
private $domElements = [];
2424

25+
private $keysToCalculate = ["assertions", "time", "tests", "errors", "failures", "skipped"];
26+
2527
protected function configure()
2628
{
2729
$this->setName('log')
@@ -49,26 +51,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
4951

5052
$root = $this->document->createElement('testsuites');
5153
$baseSuite = $this->document->createElement('testsuite');
52-
$baseSuite->setAttribute('name', "");
54+
$baseSuite->setAttribute('name', "All Suites");
5355
$baseSuite->setAttribute('tests', "0");
5456
$baseSuite->setAttribute('assertions', "0");
5557
$baseSuite->setAttribute('errors', "0");
5658
$baseSuite->setAttribute('failures', "0");
5759
$baseSuite->setAttribute('skipped', "0");
5860
$baseSuite->setAttribute('time', "0");
5961

62+
$this->domElements["All Suites"] = $baseSuite;
63+
6064
$root->appendChild($baseSuite);
6165
$this->document->appendChild($root);
6266

6367
foreach ($finder as $file) {
6468
try {
6569
$xml = new \SimpleXMLElement(file_get_contents($file->getRealPath()));
70+
$code = json_encode($xml);
6671
$xmlArray = json_decode(json_encode($xml), true);
6772
if (!empty($xmlArray)) {
6873
$this->addTestSuites($baseSuite, $xmlArray);
6974
}
7075
} catch (\Exception $exception) {
71-
// Initial fallthrough
76+
$output->writeln(sprintf("<error>Error in file %s: %s</error>", $file->getRealPath(), $exception->getMessage()));
7277
}
7378
}
7479

@@ -77,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7782
$domElement->removeAttribute('parent');
7883
}
7984
}
80-
85+
$this->calculateTopLevelStats();
8186
$file = $input->getArgument('file');
8287
if (!is_dir(dirname($file))) {
8388
@mkdir(dirname($file), 0777, true);
@@ -135,24 +140,31 @@ private function addTestCases(\DOMElement $parent, array $testCases)
135140
if (isset($this->domElements[$name])) {
136141
continue;
137142
}
138-
139143
$element = $this->document->createElement('testcase');
140144
foreach ($attributes as $key => $value) {
141145
$element->setAttribute($key, (string)$value);
142-
if (!is_numeric($value)) {
143-
continue;
144-
}
145-
$this->addAttributeValueToTestSuite($parent, $key, $value);
146+
}
147+
if (isset($testCase['failure']) || isset($testCase['warning']) || isset($testCase['error'])) {
148+
$this->addChildElements($testCase, $element);
146149
}
147150
$parent->appendChild($element);
148151
$this->domElements[$name] = $element;
149152
}
150153
}
151-
154+
private function addChildElements(array $tree, \DOMElement $element)
155+
{
156+
foreach ($tree as $key => $value) {
157+
if ($key == "@attributes") {
158+
continue;
159+
}
160+
$child = $this->document->createElement($key);
161+
$child->nodeValue = $value;
162+
$element->appendChild($child);
163+
}
164+
}
152165
private function addAttributeValueToTestSuite(\DOMElement $element, $key, $value)
153166
{
154-
$keysToCalculate = ["assertions", "time", "tests", "errors", "failures", "skipped"];
155-
if (in_array($key, $keysToCalculate)) {
167+
if (in_array($key, $this->keysToCalculate)) {
156168
$currentValue = $element->hasAttribute($key) ? $element->getAttribute($key) : 0;
157169
$element->setAttribute($key, (string)($currentValue + $value));
158170

@@ -164,4 +176,26 @@ private function addAttributeValueToTestSuite(\DOMElement $element, $key, $value
164176
}
165177
}
166178
}
179+
180+
private function calculateTopLevelStats()
181+
{
182+
/** @var \DOMElement $topNode */
183+
$suites = $this->document->getElementsByTagName("testsuites")->item(0);
184+
$topNode = $suites->firstChild;
185+
if ($topNode->hasChildNodes()) {
186+
$stats = array_flip($this->keysToCalculate);
187+
$stats = array_map(function ($_value) { return 0; }, $stats);
188+
foreach($topNode->childNodes as $child) {
189+
$attributes = $child->attributes;
190+
foreach ($attributes as $key => $value) {
191+
if (in_array($key, $this->keysToCalculate)) {
192+
$stats[$key] += $value->nodeValue;
193+
}
194+
}
195+
}
196+
foreach ($stats as $key => $value) {
197+
$topNode->setAttribute($key, (string)$value);
198+
}
199+
}
200+
}
167201
}

0 commit comments

Comments
 (0)