add junit format for PhpUnitResult

This commit is contained in:
SimonHeimberg 2017-07-17 21:26:32 +02:00 committed by Dmitry Khomutov
parent f87cb40e6f
commit 2bd6ae96cb
No known key found for this signature in database
GPG key ID: 7EB36C9576F9ECB9
2 changed files with 132 additions and 0 deletions

View file

@ -13,6 +13,8 @@ abstract class PhpUnitResult
const SEVERITY_FAIL = 'fail';
const SEVERITY_ERROR = 'error';
const SEVERITY_SKIPPED = 'skipped';
const SEVERITY_WARN = self::SEVERITY_PASS;
const SEVERITY_RISKY = self::SEVERITY_PASS;
protected $outputFile;
protected $buildPath;

View file

@ -0,0 +1,130 @@
<?php
namespace PHPCensor\Plugin\Util;
/**
* Class PhpUnitResultJunit parses the results for the PhpUnitV2 plugin
*
* @author Simon Heimberg <simon.heimberg@heimberg-ea.ch>
*/
class PhpUnitResultJunit extends PhpUnitResult
{
/**
* Parse the results
*
* @return $this
* @throws \Exception If fails to parse the output
*/
public function parse()
{
$suites = simplexml_load_file($this->outputFile);
// Reset the parsing variables
$this->results = [];
$this->errors = [];
$this->failures = 0;
foreach ($suites->xpath('//testcase') as $testCase) {
$this->parseTestcase($testCase);
}
$suites['failures'];
$suites['errors'];
return $this;
}
protected function getSeverity($testCase)
{
$severity = self::SEVERITY_PASS;
foreach($testCase as $child) {
switch ($child->getName()) {
case 'failure':
$severity = self::SEVERITY_FAIL;
break 2;
case 'error':
if ('PHPUnit\Framework\RiskyTestError' == $child['type']) { // == because convertion to string is desired
$severity = self::SEVERITY_RISKY;
} else {
$severity = self::SEVERITY_ERROR;
}
break 2;
case 'skipped':
// skipped and ignored, can not distinguish
$severity = self::SEVERITY_SKIPPED;
break 2;
case 'warning':
$severity = self::SEVERITY_WARN;
break 2;
case 'system-out':
case 'system-err':
// not results
continue;
default:
$severity = 'UNKNOWN RESULT TYPE: '.$child->getName();
break 2;
}
}
return $severity;
}
protected function buildMessage($testCase)
{
$tracePos = -1;
$msg = $this->getMessageTrace($testCase);
if ('' !== $msg) {
//strip trace
$trPos = strrpos($msg, "\n\n");
if (false !== $trPos) {
$tracePos = $trPos;
$msg = substr($msg, 0, $trPos);
}
}
if ('' === $msg) {
$msg = $testCase['class'].'::'.$testCase['name'];
};
$testCase['_tracePos'] = $tracePos; // will be converted to string
return $msg;
}
protected function getOutput($testCase) {
return (string)$testCase->{'system-out'};
}
protected function buildTrace($testCase)
{
if (!is_int($testCase['_tracePos'])) {
$this->buildMessage($testCase);
}
if ($testCase['_tracePos'] >= 0) {
$stackStr = substr($this->getMessageTrace($testCase), (int)$testCase['_tracePos'] + 2, -1);
$trace = explode("\n", str_replace($this->buildPath, '.', $stackStr));
} else {
$trace = array();
}
return $trace;
}
private function getMessageTrace($testCase) {
$msg = '';
foreach($testCase as $child) {
switch ($child->getName()) {
case 'system-out':
case 'system-err':
// not results
continue;
default:
$msg = (string)$child['message']; // according to xsd
if ('' === $msg) {
$msg = (string)$child;
}
break 2;
}
}
return $msg;
}
}