-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.php
More file actions
153 lines (123 loc) · 4.7 KB
/
Copy pathParser.php
File metadata and controls
153 lines (123 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
namespace PHPCensor\Plugins\Testing\Codeception;
/**
* Codeception Plugin
*
* @package PHP Censor
* @subpackage Plugins
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
* @author Adam Cooper <adam@networkpie.co.uk>
*/
class Parser implements ParserInterface
{
private int $totalTests = 0;
private int $totalTimeTaken = 0;
private int $totalFailures = 0;
private int $totalErrors = 0;
public function __construct(
private readonly string $buildPath,
private readonly string $xmlPath
) {
}
/**
* @return array An array of key/value pairs for storage in the plugins result metadata
*/
public function parse(): array
{
$rtn = [];
$results = $this->loadFromFile($this->xmlPath);
if ($results) {
foreach ($results->testsuite as $testSuite) {
$this->totalTests += (int)$testSuite['tests'];
$this->totalTimeTaken += (int)$testSuite['time'];
$this->totalFailures += (int)$testSuite['failures'];
$this->totalErrors += (int)$testSuite['errors'];
foreach ($testSuite->testcase as $testCase) {
$testResult = [
'suite' => (string)$testSuite['name'],
'file' => \str_replace($this->buildPath, '/', (string)$testCase['file']),
'name' => (string)$testCase['name'],
'feature' => (string)$testCase['feature'],
'assertions' => (int)$testCase['assertions'],
'time' => (float)$testCase['time'],
'class' => 'Unknown',
];
if (isset($testCase['class'])) {
$testResult['class'] = (string)$testCase['class'];
}
// PHPUnit testcases does not have feature field. Use class::method instead
if (!$testResult['feature']) {
$testResult['feature'] = \sprintf('%s::%s', $testResult['class'], $testResult['name']);
}
if (isset($testCase->failure) || isset($testCase->error)) {
$testResult['pass'] = false;
$testResult['message'] = isset($testCase->failure) ? (string)$testCase->failure : (string)$testCase->error;
} else {
$testResult['pass'] = true;
}
$rtn[] = $testResult;
}
}
}
return $rtn;
}
/**
* Get the total number of tests performed.
*/
public function getTotalTests(): int
{
return $this->totalTests;
}
/**
* The time take to complete all tests
*/
public function getTotalTimeTaken(): float
{
return $this->totalTimeTaken;
}
/**
* A count of the test failures
*/
public function getTotalFailures(): int
{
return $this->totalFailures + $this->totalErrors;
}
private function loadFromFile(string $filePath): ?\SimpleXMLElement
{
\stream_filter_register('xml_utf8_clean', 'PHPCensor\Helper\Xml\Utf8CleanFilter');
try {
$xml = \simplexml_load_file('php://filter/read=xml_utf8_clean/resource=' . $filePath);
} catch (\Exception) {
$xml = null;
} catch (\Throwable) { // since php7
$xml = null;
}
if (!$xml) {
// from https://stackoverflow.com/questions/7766455/how-to-handle-invalid-unicode-with-simplexml/8092672#8092672
$oldUse = \libxml_use_internal_errors(true);
\libxml_clear_errors();
$dom = new \DOMDocument("1.0", "UTF-8");
$dom->strictErrorChecking = false;
$dom->validateOnParse = false;
$dom->recover = true;
$dom->loadXML(\strtr(
\file_get_contents($filePath),
['"' => "'"] // " in attribute names may mislead the parser
));
$xmlError = \libxml_get_last_error();
if ($xmlError) {
$warning = \sprintf('L%s C%s: %s', $xmlError->line, $xmlError->column, $xmlError->message);
print 'WARNING: ignored errors while reading phpunit result, '.$warning."\n";
}
if (!$dom->hasChildNodes()) {
return new \SimpleXMLElement('<empty />');
}
$xml = \simplexml_import_dom($dom);
\libxml_clear_errors();
\libxml_use_internal_errors($oldUse);
}
return $xml;
}
}