/** * Make sure that the errors are properly formatted and consumed. */ public function testCreateUsingErrors() { $internal = libxml_use_internal_errors(true); $file = tempnam(sys_get_temp_dir(), 'country'); file_put_contents($file, '<'); $doc = new DOMDocument(); @$doc->load($file); $errors = libxml_get_errors(); libxml_use_internal_errors($internal); $this->assertEquals(<<<MESSAGE An error was encountered reading an XML document: [Fatal] [line 1] [column 2] StartTag: invalid element name in "{$file}". MESSAGE , XmlException::createUsingErrors($errors)->getMessage(), 'All errors should be present and properly formatted.'); }
/** * Loads and validates an XML document from a file for querying. * * @param string $file The file path. * * @return DOMXPath The XPath object for querying. * * @throws XmlException If the file could not be loaded. */ private function loadXml($file) { $internal = libxml_use_internal_errors(true); $doc = new DOMDocument(); $doc->preserveWhiteSpace = false; $doc->validateOnParse = true; if (!@$doc->load($file)) { $errors = libxml_get_errors(); libxml_use_internal_errors($internal); throw XmlException::createUsingErrors($errors); } libxml_use_internal_errors($internal); return new DOMXPath($doc); }