Skip to content

Commit

Permalink
Merge pull request #5 from vemaeg/feature/childnode-error-and-skipped…
Browse files Browse the repository at this point in the history
…-handling

Added support for childnodes (errors and skipped) plus skipped-attribute on testsuites.
  • Loading branch information
thirsch authored Sep 27, 2021
2 parents 0f7453d + 3cb3a75 commit 9047b33
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/PhpunitMerger/Command/LogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ private function addTestSuites(\DOMElement $parent, array $testSuites)
$element->setAttribute('parent', $parent->getAttribute('name'));
$attributes = $testSuite['@attributes'] ?? [];
foreach ($attributes as $key => $value) {
$value = $key === 'name' ? $value : 0;
// As skipped is not a value of a testcase, but testsuite, we have to retain the value... Maybe
// this is not fully correct, as in case of an merge within a testsuite, this approach does not
// calculate the new skipped value...
$value = $key === 'name' || $key === 'skipped' ? $value : 0;
$element->setAttribute($key, (string)$value);
}
$parent->appendChild($element);
Expand Down Expand Up @@ -136,6 +139,7 @@ private function addTestCases(\DOMElement $parent, array $testCases)
}
$this->addAttributeValueToTestSuite($parent, $key, $value);
}
$this->addChildElements($element, $testCase);
$parent->appendChild($element);
$this->domElements[$name] = $element;
}
Expand All @@ -158,4 +162,31 @@ private function addAttributeValueToTestSuite(\DOMElement $element, $key, $value
}
}
}

private function addChildElements(\DOMElement $element, array $testCase)
{
// A testcase might contain further nodes, an error for example. But the json_encode/decode-stuff does
// not contain both, attributes and values of the nodes. Example:
//
// <testcase name="testMyTest" class="TestClass" classname="Tests.TestClass" file="src/Tests/TestClass.php" line="26" assertions="0" time="0.151037">
// <error type="TypeError">error with stack trace...</error>
// </testcase>
//
// Remove the @attributes collection (name, class, classname, file, ...)
unset($testCase['@attributes']);

// Iterate over any child elements.
foreach ($testCase as $key => $value) {
// Unfortunately, the json_encode/decode used with simplexml does not contain both, attributes and values
// of child elements.
$childNode = $element->ownerDocument->createElement($key);

// Skipped tests contain an empty tag <skipped />, but the son_encode/decode produces an empty array.
if ($value) {
$childNode->nodeValue = is_array($value) ? implode(', ', $value) : $value;
}

$element->appendChild($childNode);
}
}
}

0 comments on commit 9047b33

Please sign in to comment.