forked from psecio/iniscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJunit.php
89 lines (70 loc) · 2.63 KB
/
Junit.php
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
<?php
/**
* Created by Andreas Katzig.
* Date: 20/02/2017
* Time: 10:53
*/
namespace Psecio\Iniscan\Command\ScanCommand\Output;
/**
* Creates a Junit compatible XML output.
* XSD based on https://github.com/windyroad/JUnit-Schema/blob/master/JUnit.xsd.
* @package Psecio\Iniscan\Command\ScanCommand\Output
*/
class Junit extends \Psecio\Iniscan\Command\Output
{
/**
* @param \Psecio\Iniscan\Rule[] $results
*/
public function render(/*array*/ $results)
{
$xml = $this->getOutput();
$failCount = 0;
$testCount = 0;
$errorCount = 0;
$startTime = microtime(true);
$errors = "";
$output = "";
$dom = new \DomDocument('1.0', 'UTF-8');
$testSuite = $dom->createElement('testsuite');
$properties = $dom->createElement('properties');
$testSuite->appendChild($properties);
foreach ($results as $result) {
$testCount++;
$currentValue = $result->getValue();
$level = '(' . ucfirst(strtolower($result->getLevel())) . ' level)';
$output .= 'Check ' . (($result->getStatus()) ? 'succeeded! ' : 'failed! ');
$output .= $level . ' ' . $result->getName() . ': ' . $result->getDescription() . '. ';
$output .= 'Current Value: ' . ((isset($currentValue)) ? $currentValue : 'not set.');
$output .= "\n=======================================================================================\n";
if ($result->getStatus() === null) {
$errorCount++;
$errors .= $output;
}
$testcase = $dom->createElement('testcase');
if ($result->getStatus() === false) {
$failCount++;
$elem = $dom->createElement('failure', 'Current Value: ' . $currentValue);
$elem->setAttribute('message', $level. ' ' . $result->getDescription());
$elem->setAttribute('type', $result->getSection());
$testcase->appendChild($elem);
}
$testcase->setAttribute('name', $result->getName());
$testcase->setAttribute('time', 0);
$testcase->setAttribute('classname', $result->getTestKey());
$testSuite->appendChild($testcase);
}
$testSuite->setAttribute('name', 'iniscan - Scanner for PHP.ini');
$testSuite->setAttribute('hostname', gethostname() ?: 'localhost');
$testSuite->setAttribute('timestamp', strftime("%Y-%m-%dT%H:%M:%S"));
$testSuite->setAttribute('failures', $failCount);
$testSuite->setAttribute('errors', $errorCount);
$testSuite->setAttribute('tests', $testCount);
$testSuite->setAttribute('time', round((microtime(true) - $startTime), 6));
$dom->appendChild($testSuite);
$sysout = $dom->createElement('system-out', $output);
$testSuite->appendChild($sysout);
$syserr = $dom->createElement('system-err', $errors);
$testSuite->appendChild($syserr);
$xml->writeLn($dom->saveXML());
}
}