From 2bd6ae96cb720892d28b8391e655254b9cfdd48d Mon Sep 17 00:00:00 2001 From: SimonHeimberg Date: Mon, 17 Jul 2017 21:26:32 +0200 Subject: [PATCH] add junit format for PhpUnitResult --- src/PHPCensor/Plugin/Util/PhpUnitResult.php | 2 + .../Plugin/Util/PhpUnitResultJunit.php | 130 ++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 src/PHPCensor/Plugin/Util/PhpUnitResultJunit.php diff --git a/src/PHPCensor/Plugin/Util/PhpUnitResult.php b/src/PHPCensor/Plugin/Util/PhpUnitResult.php index 10ef2fa1..d412cda5 100644 --- a/src/PHPCensor/Plugin/Util/PhpUnitResult.php +++ b/src/PHPCensor/Plugin/Util/PhpUnitResult.php @@ -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; diff --git a/src/PHPCensor/Plugin/Util/PhpUnitResultJunit.php b/src/PHPCensor/Plugin/Util/PhpUnitResultJunit.php new file mode 100644 index 00000000..3f41cb8f --- /dev/null +++ b/src/PHPCensor/Plugin/Util/PhpUnitResultJunit.php @@ -0,0 +1,130 @@ + + */ +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; + } +}