diff --git a/src/Plugin/Util/PhpUnitResultJunit.php b/src/Plugin/Util/PhpUnitResultJunit.php index 776e65136..c16a4ebc5 100644 --- a/src/Plugin/Util/PhpUnitResultJunit.php +++ b/src/Plugin/Util/PhpUnitResultJunit.php @@ -139,8 +139,41 @@ private function loadResultFile() return new \SimpleXMLElement(''); // 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('"' => "'") // " 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;