|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of project-quality-inspector. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Alexandre GESLIN <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace ProjectQualityInspector\Application\Output; |
|
12
|
|
|
|
|
13
|
|
|
use ProjectQualityInspector\Exception\RuleViolationException; |
|
14
|
|
|
use ProjectQualityInspector\Iterator\RuleFilterIterator; |
|
15
|
|
|
|
|
16
|
|
|
class JunitHelper |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param RuleInterface[] $rules |
|
20
|
|
|
* @param string $junitFile |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function generateJunitFile(RuleFilterIterator $rules, $junitFile) |
|
23
|
|
|
{ |
|
24
|
|
|
$xml = new \DOMDocument('1.0', 'utf-8'); |
|
25
|
|
|
$testSuites = $xml->createElement("testsuites"); |
|
26
|
|
|
|
|
27
|
|
|
foreach ($rules as $rule) { |
|
28
|
|
|
$testSuites->appendChild(static::createTestSuite($rule::getRuleName(), $rule->getAssertions(), $xml)); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$testSuites->setAttribute('name', 'pqi'); |
|
32
|
|
|
$testSuites->setAttribute('errors', static::sumDomChildsAttribute($testSuites, 'errors')); |
|
|
|
|
|
|
33
|
|
|
$testSuites->setAttribute('failures', static::sumDomChildsAttribute($testSuites, 'failures')); |
|
|
|
|
|
|
34
|
|
|
$testSuites->setAttribute('time', static::sumDomChildsAttribute($testSuites, 'time')); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
$xml->appendChild($testSuites); |
|
37
|
|
|
|
|
38
|
|
|
file_put_contents($junitFile, $xml->saveXML()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $tests |
|
43
|
|
|
* @param DOMDocument $xml |
|
44
|
|
|
* @return DOMDocument |
|
45
|
|
|
*/ |
|
46
|
|
|
private static function createTestSuite($name, array $tests, \DOMDocument $xml) |
|
47
|
|
|
{ |
|
48
|
|
|
$testSuite = static::createElement('testsuite', [ |
|
|
|
|
|
|
49
|
|
|
'name' => sprintf('pqi.%s', $name), |
|
50
|
|
|
'tests' => count($tests), |
|
51
|
|
|
'failures' => static::sumArraysKey('failures', $tests, 'sum'), |
|
|
|
|
|
|
52
|
|
|
'errors' => static::sumArraysKey('errors', $tests, 'sum'), |
|
|
|
|
|
|
53
|
|
|
'time' => static::sumArraysKey('time', $tests) |
|
|
|
|
|
|
54
|
|
|
], $xml); |
|
55
|
|
|
|
|
56
|
|
|
foreach ($tests as $test) { |
|
57
|
|
|
$testSuite->appendChild(static::createTestCase($test, $xml)); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $testSuite; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private static function createTestCase(array $test, \DOMDocument $xml) |
|
64
|
|
|
{ |
|
65
|
|
|
$testCase = $xml->createElement('testcase'); |
|
66
|
|
|
$testCase->setAttribute('name', $test['name']); |
|
67
|
|
|
$testCase->setAttribute('assertions', $test['assertions']); |
|
68
|
|
|
$testCase->setAttribute('classname', $test['classname']); |
|
69
|
|
|
$testCase->setAttribute('status', $test['status']); |
|
70
|
|
|
$testCase->setAttribute('time', $test['time']); |
|
71
|
|
|
|
|
72
|
|
View Code Duplication |
if ($test['failures']['sum'] > 0) { |
|
|
|
|
|
|
73
|
|
|
foreach ($test['failures']['list'] as $failure) { |
|
74
|
|
|
$testCase->appendChild(static::createElement('failure', ['type' => $failure['type']], $xml, $failure['message'])); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
View Code Duplication |
if ($test['errors']['sum'] > 0) { |
|
|
|
|
|
|
79
|
|
|
foreach ($test['errors']['list'] as $error) { |
|
80
|
|
|
$testCase->appendChild(static::createElement('error', ['type' => $error['type']], $xml, $failure['message'])); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $testCase; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $tagName |
|
89
|
|
|
* @param array $attributes |
|
90
|
|
|
* @param \DOMDocument $xml |
|
91
|
|
|
* @param string $value |
|
92
|
|
|
* @return \DOMDocument |
|
93
|
|
|
*/ |
|
94
|
|
|
private static function createElement($tagName, array $attributes, \DOMDocument $xml, $value = null) |
|
95
|
|
|
{ |
|
96
|
|
|
$element = $xml->createElement($tagName, $value); |
|
97
|
|
|
|
|
98
|
|
|
foreach ($attributes as $key => $value) { |
|
99
|
|
|
$element->setAttribute($key, $value); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $element; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $key |
|
107
|
|
|
* @param array $arrays |
|
108
|
|
|
* @param string $subKey |
|
109
|
|
|
* @return integer |
|
110
|
|
|
*/ |
|
111
|
|
View Code Duplication |
private static function sumArraysKey($key, array $arrays, $subKey = null) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
$sum = 0; |
|
114
|
|
|
|
|
115
|
|
|
foreach ($arrays as $array) { |
|
116
|
|
|
$sum += ($subKey) ? $array[$key][$subKey] : $array[$key]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $sum; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param \DOMElement $element |
|
124
|
|
|
* @param string $attribute |
|
125
|
|
|
* @return integer |
|
126
|
|
|
*/ |
|
127
|
|
|
private static function sumDomChildsAttribute(\DOMElement $element, $attribute) |
|
128
|
|
|
{ |
|
129
|
|
|
$sum = 0; |
|
130
|
|
|
foreach ($element->childNodes as $childElement) { |
|
131
|
|
|
$sum += (int) $childElement->getAttribute($attribute); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $sum; |
|
135
|
|
|
} |
|
136
|
|
|
} |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: