Skip to content

Commit c395587

Browse files
feat: Correctly set suite attributes and prioritize successful tests when handling duplicates.
Ensure all non-name attributes (tests, errors, failures, etc.) are correctly set in test suites. Mark test cases with proper status tags (error, failure, warning, skipped) when a test fails. Refine duplicate test handling logic: skip duplicates only when the existing test has no failure, ensuring failed tests are replaced by successful ones.
1 parent d0ba7c6 commit c395587

1 file changed

Lines changed: 47 additions & 6 deletions

File tree

src/PhpunitMerger/Command/LogCommand.php

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ private function addTestSuites(\DOMElement $parent, array $testSuites)
8282
foreach ($testSuites as $testSuite) {
8383
if (empty($testSuite['@attributes']['name'])) {
8484
if (!empty($testSuite['testsuite'])) {
85-
$this->addTestSuites($parent, $testSuite['testsuite']);
85+
$children = isset($testSuite['testsuite']['@attributes']) ? [$testSuite['testsuite']] : $testSuite['testsuite'];
86+
$this->addTestSuites($parent, $children);
8687
}
8788
continue;
8889
}
@@ -95,7 +96,7 @@ private function addTestSuites(\DOMElement $parent, array $testSuites)
9596
$element->setAttribute('parent', $parent->getAttribute('name'));
9697
$attributes = $testSuite['@attributes'] ?? [];
9798
foreach ($attributes as $key => $value) {
98-
$value = $key === 'name' ? $value : 0;
99+
$value = $key === 'name' || $key === 'file' ? $value : 0;
99100
$element->setAttribute($key, (string)$value);
100101
}
101102
$parent->appendChild($element);
@@ -116,27 +117,67 @@ private function addTestSuites(\DOMElement $parent, array $testSuites)
116117

117118
private function addTestCases(\DOMElement $parent, array $testCases)
118119
{
120+
$statusTags = [
121+
'error' => 'errors',
122+
'failure' => 'failures',
123+
'skipped' => 'skipped',
124+
'warning' => 'warnings',
125+
];
126+
119127
foreach ($testCases as $testCase) {
120128
$attributes = $testCase['@attributes'] ?? [];
121129
if (empty($testCase['@attributes']['name'])) {
122130
continue;
123131
}
124132
$name = $testCase['@attributes']['name'];
133+
$class = $testCase['@attributes']['class'];
134+
if (isset($this->domElements[$class . '::' . $name])) {
135+
$previusTestCase = $this->domElements[$class . '::' . $name];
136+
$previousTime = (float) ($previusTestCase->getAttribute('time') ?? 0);
137+
$newTime = (float) ($testCase['@attributes']['time'] ?? 0);
138+
$hasActualTestCaseAStatusTag = !empty(array_intersect(array_keys($testCase), array_keys($statusTags)));
139+
$hasPreviusTestCaseAStatusTag = $previusTestCase->childNodes->length > 0;
140+
141+
if ($hasActualTestCaseAStatusTag ||
142+
!$hasPreviusTestCaseAStatusTag &&
143+
$newTime < $previousTime) {
144+
continue;
145+
}
125146

126-
if (isset($this->domElements[$name])) {
127-
continue;
147+
$this->addAttributeValueToTestSuite($parent, 'tests', -1);
148+
foreach ($previusTestCase->childNodes as $child) {
149+
$this->addAttributeValueToTestSuite($parent, $statusTags[$child->nodeName], -1);
150+
}
151+
foreach ($previusTestCase->attributes as $attribute) {
152+
if (!is_numeric($attribute->nodeValue) || $attribute->nodeName === 'line') {
153+
continue;
154+
}
155+
$this->addAttributeValueToTestSuite($parent, $attribute->nodeName, -$attribute->nodeValue);
156+
}
157+
158+
$parent->removeChild($previusTestCase);
159+
unset($this->domElements[$class . '::' . $name]);
128160
}
129161

130162
$element = $this->document->createElement('testcase');
131163
foreach ($attributes as $key => $value) {
132164
$element->setAttribute($key, (string)$value);
133-
if (!is_numeric($value)) {
165+
if (!is_numeric($value) || $key === 'line') {
134166
continue;
135167
}
136168
$this->addAttributeValueToTestSuite($parent, $key, $value);
137169
}
170+
171+
$this->addAttributeValueToTestSuite($parent, 'tests', 1);
172+
foreach ($statusTags as $key => $value) {
173+
if (isset($testCase[$key])) {
174+
$this->addAttributeValueToTestSuite($parent, $value, 1);
175+
$element->appendChild($this->document->createElement($key));
176+
}
177+
}
178+
138179
$parent->appendChild($element);
139-
$this->domElements[$name] = $element;
180+
$this->domElements[$class . '::' . $name] = $element;
140181
}
141182
}
142183

0 commit comments

Comments
 (0)