Passed
Push — master ( d4832b...fcf6a8 )
by Stéphane
21:50
created

drillDownTestSuite()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 29
rs 9.3222
1
<?php
2
3
$report = file_get_contents(dirname(__DIR__) . "/test-report.xml");
4
5
$doc = new DOMDocument($report);
6
$doc->loadXML($report);
7
8
function hasDataSetTestCase(DomNode $node) {
9
    foreach ($node->childNodes as $child) {
10
        if ($child->nodeName === "testcase" && strpos($child->attributes->getNamedItem("name")->textContent, "with data set #" ) !== false) {
11
            return $child;
12
        }
13
    }
14
15
    return false;
16
}
17
18
function drillDownTestSuite(DomDocument $document, DomNode $node) {
19
    if ($dataset = hasDataSetTestCase($node)) {
20
        $childAttributes =  $dataset->attributes;
21
        $nodeAttributes= $node->attributes;
22
23
        $case = $document->createElement('testcase');
24
        $case->setAttribute('name', $childAttributes->getNamedItem('name')->textContent);
25
        $case->setAttribute('class', $childAttributes->getNamedItem('class')->textContent);
26
        $case->setAttribute('classname', $childAttributes->getNamedItem('classname')->textContent);
27
        $case->setAttribute('file', $childAttributes->getNamedItem('file')->textContent);
28
        $case->setAttribute('line', $childAttributes->getNamedItem('line')->textContent);
29
        $case->setAttribute('assertions', $nodeAttributes->getNamedItem('assertions')->textContent);
30
        $case->setAttribute('time', $nodeAttributes->getNamedItem('time')->textContent);
31
32
        $node->parentNode->replaceChild($case, $node);
33
        return true;
34
    }
35
36
    /** @var DomNode $child */
37
    for ($i=0; $i< $node->childNodes->length; $i++) {
38
        $child = $node->childNodes->item($i);
39
        if ($child->localName === "testsuite") {
40
            if (drillDownTestSuite($document, $child)) {
41
                $i--;
42
            }
43
        }
44
    }
45
46
    return false;
47
}
48
49
drillDownTestSuite($doc, $doc->firstChild);
50
51
file_put_contents(dirname(__DIR__) . "/test-report.xml", $doc->saveXML());
52
echo "Done\n";
53