Skip to content

Commit

Permalink
PhpUnitJunit tries to load invalid xml file
Browse files Browse the repository at this point in the history
Work around outup from phpunit versions before 6.5.7/7.0.2 (before sebastianbergmann/phpunit#2974 was fixed).
  • Loading branch information
SimonHeimberg authored and corpsee committed May 1, 2018
1 parent 3034901 commit 4c8398f
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/Plugin/Util/PhpUnitResultJunit.php
Expand Up @@ -139,8 +139,41 @@ private function loadResultFile()
return new \SimpleXMLElement('<empty/>'); // new empty element
}

if (true) {
try {
$suites = simplexml_load_file($this->outputFile);
} catch (\Exception $ex) {
$suites = null;
} catch (\Throwable $ex) { // since php7
$suites = null;
}
if (!$suites) {
// 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($this->outputFile),
array('&quot;' => "'") // &quot; in attribute names may mislead the parser
));

/**
* @var \LibXMLError
*/
$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()) {
$this->internalProblem('xml file with no content');
}
$suites = simplexml_import_dom($dom);

libxml_clear_errors();
libxml_use_internal_errors($oldUse);
}

return $suites;
Expand Down

0 comments on commit 4c8398f

Please sign in to comment.