1 | <?php |
||
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 | 6 | public function __construct(LogInterpreter $interpreter, $name = '') |
|
61 | |||
62 | /** |
||
63 | * Get the name of the root suite being written |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | 1 | public function getName() |
|
71 | |||
72 | /** |
||
73 | * Returns the xml structure the writer |
||
74 | * will use |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | 5 | public function getXml() |
|
79 | { |
||
80 | 5 | $suites = $this->interpreter->flattenCases(); |
|
81 | 5 | $root = $this->getSuiteRoot($suites); |
|
82 | 5 | foreach ($suites as $suite) { |
|
83 | 5 | $snode = $this->appendSuite($root, $suite); |
|
84 | 5 | foreach ($suite->cases as $case) { |
|
85 | 5 | $cnode = $this->appendCase($snode, $case); |
|
|
|||
86 | 5 | } |
|
87 | 5 | } |
|
88 | 5 | 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) |
|
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 | 5 | protected function appendSuite($root, TestSuite $suite) |
|
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 | 5 | protected function appendCase($suiteNode, TestCase $case) |
|
147 | |||
148 | /** |
||
149 | * Append error or failure nodes to the given testcase node |
||
150 | * |
||
151 | * @param $caseNode |
||
152 | * @param $defects |
||
153 | * @param $type |
||
154 | */ |
||
155 | 5 | protected function appendDefects($caseNode, $defects, $type) |
|
163 | |||
164 | /** |
||
165 | * Get the root level testsuite node |
||
166 | * |
||
167 | * @param $suites |
||
168 | * @return \DOMElement |
||
169 | */ |
||
170 | 5 | protected function getSuiteRoot($suites) |
|
185 | |||
186 | /** |
||
187 | * Get the attributes used on the root testsuite |
||
188 | * node |
||
189 | * |
||
190 | * @param $suites |
||
191 | * @return mixed |
||
192 | */ |
||
193 | protected function getSuiteRootAttributes($suites) |
||
194 | { |
||
195 | 3 | return array_reduce($suites, function ($result, $suite) { |
|
196 | 3 | $result['tests'] += $suite->tests; |
|
197 | 3 | $result['assertions'] += $suite->assertions; |
|
198 | 3 | $result['failures'] += $suite->failures; |
|
199 | 3 | $result['errors'] += $suite->errors; |
|
200 | 3 | $result['time'] += $suite->time; |
|
201 | 3 | return $result; |
|
202 | 3 | }, array_merge(array('name' => $this->name), self::$defaultSuite)); |
|
203 | } |
||
204 | |||
205 | /** |
||
206 | * Prevent writing empty "line" XML attributes which could break parsers. |
||
207 | * @param string $name |
||
208 | * @param mixed $value |
||
209 | * @return bool |
||
210 | */ |
||
211 | 5 | private function isEmptyLineAttribute($name, $value) |
|
215 | } |
||
216 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.