2013-01-26 02:06:20 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PHPUnit Result Parsing utility
|
|
|
|
*
|
|
|
|
* Intended to enable custom unit engines derived
|
|
|
|
* from phpunit to reuse common business logic related
|
|
|
|
* to parsing phpunit test results and reports
|
|
|
|
*
|
|
|
|
* For an example on how to integrate with your test
|
|
|
|
* engine, see PhpunitTestEngine.
|
|
|
|
*
|
|
|
|
*/
|
2013-01-28 02:16:50 +01:00
|
|
|
final class PhpunitResultParser extends ArcanistBaseTestResultParser {
|
2013-01-26 02:06:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse test results from phpunit json report
|
|
|
|
*
|
|
|
|
* @param string $path Path to test
|
2013-01-28 02:16:50 +01:00
|
|
|
* @param string $test_results String containing phpunit json report
|
2013-01-26 02:06:20 +01:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-01-28 02:16:50 +01:00
|
|
|
public function parseTestResults($path, $test_results) {
|
2013-01-26 02:06:20 +01:00
|
|
|
|
2013-01-28 02:16:50 +01:00
|
|
|
$report = $this->getJsonReport($test_results);
|
2013-01-26 02:06:20 +01:00
|
|
|
|
2013-01-28 02:16:50 +01:00
|
|
|
// coverage is for all testcases in the executed $path
|
|
|
|
$coverage = array();
|
|
|
|
if ($this->enableCoverage !== false) {
|
|
|
|
$coverage = $this->readCoverage();
|
|
|
|
}
|
2013-01-26 02:06:20 +01:00
|
|
|
|
2013-01-28 02:16:50 +01:00
|
|
|
$results = array();
|
|
|
|
foreach ($report as $event) {
|
|
|
|
if ('test' != $event->event) {
|
|
|
|
continue;
|
2013-01-26 02:06:20 +01:00
|
|
|
}
|
|
|
|
|
2013-01-28 02:16:50 +01:00
|
|
|
$status = ArcanistUnitTestResult::RESULT_PASS;
|
|
|
|
$user_data = '';
|
2013-01-26 02:06:20 +01:00
|
|
|
|
2013-01-28 02:16:50 +01:00
|
|
|
if ('fail' == $event->status) {
|
|
|
|
$status = ArcanistUnitTestResult::RESULT_FAIL;
|
|
|
|
$user_data .= $event->message . "\n";
|
|
|
|
foreach ($event->trace as $trace) {
|
|
|
|
$user_data .= sprintf("\n%s:%s", $trace->file, $trace->line);
|
|
|
|
}
|
|
|
|
} else if ('error' == $event->status) {
|
|
|
|
if (strpos($event->message, 'Skipped Test') !== false) {
|
|
|
|
$status = ArcanistUnitTestResult::RESULT_SKIP;
|
|
|
|
$user_data .= $event->message;
|
|
|
|
} else if (strpos($event->message, 'Incomplete Test') !== false) {
|
|
|
|
$status = ArcanistUnitTestResult::RESULT_SKIP;
|
|
|
|
$user_data .= $event->message;
|
|
|
|
} else {
|
|
|
|
$status = ArcanistUnitTestResult::RESULT_BROKEN;
|
|
|
|
$user_data .= $event->message;
|
2013-01-26 02:06:20 +01:00
|
|
|
foreach ($event->trace as $trace) {
|
|
|
|
$user_data .= sprintf("\n%s:%s", $trace->file, $trace->line);
|
|
|
|
}
|
|
|
|
}
|
2013-01-28 02:16:50 +01:00
|
|
|
}
|
2013-01-26 02:06:20 +01:00
|
|
|
|
2013-01-28 02:16:50 +01:00
|
|
|
$name = preg_replace('/ \(.*\)/', '', $event->test);
|
2013-01-26 02:06:20 +01:00
|
|
|
|
2013-01-28 02:16:50 +01:00
|
|
|
$result = new ArcanistUnitTestResult();
|
|
|
|
$result->setName($name);
|
|
|
|
$result->setResult($status);
|
|
|
|
$result->setDuration($event->time);
|
|
|
|
$result->setCoverage($coverage);
|
|
|
|
$result->setUserData($user_data);
|
2013-01-26 02:06:20 +01:00
|
|
|
|
2013-01-28 02:16:50 +01:00
|
|
|
$results[] = $result;
|
2013-01-26 02:06:20 +01:00
|
|
|
}
|
|
|
|
|
2013-01-28 02:16:50 +01:00
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2013-01-26 02:06:20 +01:00
|
|
|
/**
|
|
|
|
* Read the coverage from phpunit generated clover report
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-01-28 02:16:50 +01:00
|
|
|
private function readCoverage() {
|
|
|
|
$test_results = Filesystem::readFile($this->coverageFile);
|
2013-01-26 02:06:20 +01:00
|
|
|
if (empty($test_results)) {
|
|
|
|
throw new Exception('Clover coverage XML report file is empty, '
|
|
|
|
. 'it probably means that phpunit failed to run tests. '
|
|
|
|
. 'Try running arc unit with --trace option and then run '
|
|
|
|
. 'generated phpunit command yourself, you might get the '
|
|
|
|
. 'answer.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$coverage_dom = new DOMDocument();
|
|
|
|
$coverage_dom->loadXML($test_results);
|
|
|
|
|
|
|
|
$reports = array();
|
|
|
|
$files = $coverage_dom->getElementsByTagName('file');
|
|
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$class_path = $file->getAttribute('name');
|
2013-01-28 02:16:50 +01:00
|
|
|
if (empty($this->affectedTests[$class_path])) {
|
2013-01-26 02:06:20 +01:00
|
|
|
continue;
|
|
|
|
}
|
2013-01-28 02:16:50 +01:00
|
|
|
$test_path = $this->affectedTests[$file->getAttribute('name')];
|
2013-01-26 02:06:20 +01:00
|
|
|
// get total line count in file
|
|
|
|
$line_count = count(file($class_path));
|
|
|
|
|
|
|
|
$coverage = '';
|
|
|
|
$start_line = 1;
|
|
|
|
$lines = $file->getElementsByTagName('line');
|
|
|
|
for ($ii = 0; $ii < $lines->length; $ii++) {
|
|
|
|
$line = $lines->item($ii);
|
|
|
|
for (; $start_line < $line->getAttribute('num'); $start_line++) {
|
|
|
|
$coverage .= 'N';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($line->getAttribute('type') != 'stmt') {
|
|
|
|
$coverage .= 'N';
|
|
|
|
} else {
|
|
|
|
if ((int) $line->getAttribute('count') == 0) {
|
|
|
|
$coverage .= 'U';
|
|
|
|
} else if ((int) $line->getAttribute('count') > 0) {
|
|
|
|
$coverage .= 'C';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$start_line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; $start_line <= $line_count; $start_line++) {
|
|
|
|
$coverage .= 'N';
|
|
|
|
}
|
|
|
|
|
|
|
|
$len = strlen($this->projectRoot . DIRECTORY_SEPARATOR);
|
|
|
|
$class_path = substr($class_path, $len);
|
|
|
|
$reports[$class_path] = $coverage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $reports;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We need this non-sense to make json generated by phpunit
|
|
|
|
* valid.
|
|
|
|
*
|
2013-01-28 02:16:50 +01:00
|
|
|
* @param string $json String containing JSON report
|
2013-01-26 02:06:20 +01:00
|
|
|
*
|
|
|
|
* @return array JSON decoded array
|
|
|
|
*/
|
2013-01-28 02:16:50 +01:00
|
|
|
private function getJsonReport($json) {
|
2013-01-26 02:06:20 +01:00
|
|
|
|
|
|
|
if (empty($json)) {
|
|
|
|
throw new Exception('JSON report file is empty, '
|
|
|
|
. 'it probably means that phpunit failed to run tests. '
|
|
|
|
. 'Try running arc unit with --trace option and then run '
|
|
|
|
. 'generated phpunit command yourself, you might get the '
|
|
|
|
. 'answer.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$json = preg_replace('/}{\s*"/', '},{"', $json);
|
|
|
|
$json = '[' . $json . ']';
|
|
|
|
$json = json_decode($json);
|
|
|
|
if (!is_array($json)) {
|
|
|
|
throw new Exception('JSON could not be decoded');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $json;
|
|
|
|
}
|
|
|
|
}
|