Skip to content

Commit

Permalink
Fix DOMDocument::schemaValidateSource when create warning
Browse files Browse the repository at this point in the history
  • Loading branch information
eclipxe13 committed Sep 4, 2018
1 parent e48fdd7 commit 81ab965
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/XmlSchemaValidator/LibXmlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -47,6 +49,7 @@ public static function useInternalErrors(callable $callable)
try {
static::throwFromLibXml();
} finally {
error_reporting($previousErrorReporting);
libxml_use_internal_errors($previousLibXmlUseInternalErrors);
}
return $return;
Expand Down
1 change: 1 addition & 0 deletions src/XmlSchemaValidator/SchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function getLastError(): string
*
* @param Schemas $schemas
* @return void
*
* @throws LibXmlException if schema validation fails
*/
public function validateWithSchemas(Schemas $schemas)
Expand Down
14 changes: 14 additions & 0 deletions tests/XmlSchemaValidatorTests/SchemaValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Empty file added tests/assets/empty.xsd
Empty file.

0 comments on commit 81ab965

Please sign in to comment.