Completed
Pull Request — master (#204)
by
unknown
02:08
created

Writer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 18
c 1
b 1
f 0
lcom 1
cbo 2
dl 0
loc 196
ccs 71
cts 71
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A write() 0 4 1
A __construct() 0 7 1
A getXml() 0 12 3
A appendSuite() 0 12 3
A appendCase() 0 14 3
A appendDefects() 0 8 2
A getSuiteRoot() 0 15 3
A getSuiteRootAttributes() 0 11 1
1
<?php
2
namespace ParaTest\Logging\JUnit;
3
4
use ParaTest\Logging\LogInterpreter;
5
6
class Writer
7
{
8
    /**
9
     * The name attribute of the testsuite being
10
     * written
11
     *
12
     * @var string
13
     */
14
    protected $name;
15
16
    /**
17
     * @var \ParaTest\Logging\LogInterpreter
18
     */
19
    protected $interpreter;
20
21
    /**
22
     * @var \DOMDocument
23
     */
24
    protected $document;
25
26
    /**
27
     * A pattern for matching testsuite attributes
28
     *
29
     * @var string
30
     */
31
    protected static $suiteAttrs = '/name|(?:test|assertion|failure|error)s|time|file/';
32
33
    /**
34
     * A pattern for matching testcase attrs
35
     *
36
     * @var string
37
     */
38
    protected static $caseAttrs = '/name|class|file|line|assertions|time/';
39
40
    /**
41
     * A default suite to ease flattening of
42
     * suite structures
43
     *
44
     * @var array
45
     */
46
    protected static $defaultSuite = array(
47
                                        'tests' => 0,
48
                                        'assertions' => 0,
49
                                        'failures' => 0,
50
                                        'errors' => 0,
51
                                        'time' => 0
52
                                    );
53
54 5
    public function __construct(LogInterpreter $interpreter, $name = '')
55
    {
56 5
        $this->name = $name;
57 5
        $this->interpreter = $interpreter;
58 5
        $this->document = new \DOMDocument("1.0", "UTF-8");
59 5
        $this->document->formatOutput = true;
60 5
    }
61
62
    /**
63
     * Get the name of the root suite being written
64
     *
65
     * @return string
66
     */
67 1
    public function getName()
68
    {
69 1
        return $this->name;
70
    }
71
72
    /**
73
     * Returns the xml structure the writer
74
     * will use
75
     *
76
     * @return string
77
     */
78 4
    public function getXml()
79
    {
80 4
        $suites = $this->interpreter->flattenCases();
81 4
        $root = $this->getSuiteRoot($suites);
82 4
        foreach ($suites as $suite) {
83 4
            $snode = $this->appendSuite($root, $suite);
84 4
            foreach ($suite->cases as $case) {
85 4
                $cnode = $this->appendCase($snode, $case);
0 ignored issues
show
Unused Code introduced by
$cnode is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
86 4
            }
87 4
        }
88 4
        return $this->document->saveXML();
89
    }
90
91
    /**
92
     * Write the xml structure to a file path
93
     *
94
     * @param $path
95
     */
96 2
    public function write($path)
97
    {
98 2
        file_put_contents($path, $this->getXml());
99 2
    }
100
101
    /**
102
     * Append a testsuite node to the given
103
     * root element
104
     *
105
     * @param $root
106
     * @param TestSuite $suite
107
     * @return \DOMElement
108
     */
109 4
    protected function appendSuite($root, TestSuite $suite)
110
    {
111 4
        $suiteNode = $this->document->createElement("testsuite");
112 4
        $vars = get_object_vars($suite);
113 4
        foreach ($vars as $name => $value) {
114 4
            if (preg_match(static::$suiteAttrs, $name)) {
115 4
                $suiteNode->setAttribute($name, $value);
116 4
            }
117 4
        }
118 4
        $root->appendChild($suiteNode);
119 4
        return $suiteNode;
120
    }
121
122
    /**
123
     * Append a testcase node to the given testsuite
124
     * node
125
     *
126
     * @param $suiteNode
127
     * @param TestCase $case
128
     * @return \DOMElement
129
     */
130 4
    protected function appendCase($suiteNode, TestCase $case)
131
    {
132 4
        $caseNode = $this->document->createElement("testcase");
133 4
        $vars = get_object_vars($case);
134 4
        foreach ($vars as $name => $value) {
135 4
            if (preg_match(static::$caseAttrs, $name)) {
136 4
                $caseNode->setAttribute($name, $value);
137 4
            }
138 4
        }
139 4
        $suiteNode->appendChild($caseNode);
140 4
        $this->appendDefects($caseNode, $case->failures, 'failure');
141 4
        $this->appendDefects($caseNode, $case->errors, 'error');
142 4
        return $caseNode;
143
    }
144
145
    /**
146
     * Append error or failure nodes to the given testcase node
147
     *
148
     * @param $caseNode
149
     * @param $defects
150
     * @param $type
151
     */
152 4
    protected function appendDefects($caseNode, $defects, $type)
153
    {
154 4
        foreach ($defects as $defect) {
155 2
            $defectNode = $this->document->createElement($type, htmlentities($defect['text']) . "\n");
156 2
            $defectNode->setAttribute('type', $defect['type']);
157 2
            $caseNode->appendChild($defectNode);
158 4
        }
159 4
    }
160
161
    /**
162
     * Get the root level testsuite node
163
     *
164
     * @param $suites
165
     * @return \DOMElement
166
     */
167 4
    protected function getSuiteRoot($suites)
168
    {
169 4
        $testsuites = $this->document->createElement("testsuites");
170 4
        $this->document->appendChild($testsuites);
171 4
        if (sizeof($suites) == 1) {
172 2
            return $testsuites;
173
        }
174 2
        $rootSuite = $this->document->createElement('testsuite');
175 2
        $attrs = $this->getSuiteRootAttributes($suites);
176 2
        foreach ($attrs as $attr => $value) {
177 2
            $rootSuite->setAttribute($attr, $value);
178 2
        }
179 2
        $testsuites->appendChild($rootSuite);
180 2
        return $rootSuite;
181
    }
182
183
    /**
184
     * Get the attributes used on the root testsuite
185
     * node
186
     *
187
     * @param $suites
188
     * @return mixed
189
     */
190 2
    protected function getSuiteRootAttributes($suites)
191
    {
192 2
        return array_reduce($suites, function ($result, $suite) {
193 2
            $result['tests'] += $suite->tests;
194 2
            $result['assertions'] += $suite->assertions;
195 2
            $result['failures'] += $suite->failures;
196 2
            $result['errors'] += $suite->errors;
197 2
            $result['time'] += $suite->time;
198 2
            return $result;
199 2
        }, array_merge(array('name' => $this->name), self::$defaultSuite));
200
    }
201
}
202