From 81ab9654af5e7abd0c3926932a63977cb3c00ed4 Mon Sep 17 00:00:00 2001 From: Carlos C Soto Date: Tue, 4 Sep 2018 14:25:19 -0500 Subject: [PATCH] Fix DOMDocument::schemaValidateSource when create warning --- CHANGELOG.md | 6 ++++++ src/XmlSchemaValidator/LibXmlException.php | 3 +++ src/XmlSchemaValidator/SchemaValidator.php | 1 + .../SchemaValidatorTest.php | 14 ++++++++++++++ tests/assets/empty.xsd | 0 5 files changed, 24 insertions(+) create mode 100644 tests/assets/empty.xsd diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cdb278..a887525 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# Version 2.0.2 +- Fix bug when running on PHP >= 7.1 and warning was raised when call `DOMDocument::schemaValidateSource` + making impossible to obtain errors from `libxml_clear_errors` and throw a new `LibXmlException` +- Add a new test `SchemaValidatorTest::testValidateWithEmptySchema` to make sure that + a `LibXmlException` exception is raised + # Version 2.0.1 - Fix bug when using windows path (backslashs), it does not validate - Add docblock to buildSchemas diff --git a/src/XmlSchemaValidator/LibXmlException.php b/src/XmlSchemaValidator/LibXmlException.php index cc72f83..56c583a 100644 --- a/src/XmlSchemaValidator/LibXmlException.php +++ b/src/XmlSchemaValidator/LibXmlException.php @@ -39,6 +39,8 @@ public static function throwFromLibXml() */ public static function useInternalErrors(callable $callable) { + $previousErrorReporting = error_reporting(); + error_reporting(0); $previousLibXmlUseInternalErrors = libxml_use_internal_errors(true); if ($previousLibXmlUseInternalErrors) { libxml_clear_errors(); @@ -47,6 +49,7 @@ public static function useInternalErrors(callable $callable) try { static::throwFromLibXml(); } finally { + error_reporting($previousErrorReporting); libxml_use_internal_errors($previousLibXmlUseInternalErrors); } return $return; diff --git a/src/XmlSchemaValidator/SchemaValidator.php b/src/XmlSchemaValidator/SchemaValidator.php index a87a822..4a4d4cc 100644 --- a/src/XmlSchemaValidator/SchemaValidator.php +++ b/src/XmlSchemaValidator/SchemaValidator.php @@ -73,6 +73,7 @@ public function getLastError(): string * * @param Schemas $schemas * @return void + * * @throws LibXmlException if schema validation fails */ public function validateWithSchemas(Schemas $schemas) diff --git a/tests/XmlSchemaValidatorTests/SchemaValidatorTest.php b/tests/XmlSchemaValidatorTests/SchemaValidatorTest.php index 6e4305a..e1a7705 100644 --- a/tests/XmlSchemaValidatorTests/SchemaValidatorTest.php +++ b/tests/XmlSchemaValidatorTests/SchemaValidatorTest.php @@ -116,4 +116,18 @@ public function testValidateWithSchemasUsingLocal() $validator->validateWithSchemas($schemas); $this->assertTrue(true, 'validateWithSchemas did not throw any exception'); } + + public function testValidateWithEmptySchema() + { + $validator = $this->utilCreateValidator('books-valid.xml'); + $schemas = new Schemas(); + $schemas->create( + 'http://test.org/schemas/books', + $this->utilAssetLocation('empty.xsd') + ); + + $this->expectException(SchemaValidatorException::class); + $this->expectExceptionMessage('Failed to parse the XML resource'); + $validator->validateWithSchemas($schemas); + } } diff --git a/tests/assets/empty.xsd b/tests/assets/empty.xsd new file mode 100644 index 0000000..e69de29