Skip to content

Commit 65a383f

Browse files
committed
Bugfix: use libxml_get_errors instead of looping over libxml_get_last_error
1 parent 7d7dc3d commit 65a383f

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

src/DOMDocumentFactory.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
use XMLReader;
1616

1717
use function array_unique;
18+
use function file_exists;
1819
use function file_get_contents;
1920
use function func_num_args;
2021
use function implode;
2122
use function libxml_clear_errors;
22-
use function libxml_get_last_error;
23+
use function libxml_get_errors;
2324
use function libxml_set_external_entity_loader;
2425
use function libxml_use_internal_errors;
2526
use function sprintf;
@@ -150,29 +151,19 @@ public static function schemaValidation(
150151
string $schemaFile,
151152
int $options = self::DEFAULT_OPTIONS,
152153
): void {
153-
$xmlReader = XMLReader::XML($xml, null, $options);
154-
Assert::notFalse($xmlReader, SchemaViolationException::class);
155-
156-
libxml_use_internal_errors(true);
157-
158-
try {
159-
$xmlReader->setSchema($schemaFile);
160-
} catch (Exception) {
161-
$err = libxml_get_last_error();
162-
throw new SchemaViolationException(trim($err->message) . ' on line ' . $err->line);
154+
if (!file_exists($schemaFile)) {
155+
throw new IOException('File not found.');
163156
}
164157

165-
$msgs = [];
166-
while ($xmlReader->read()) {
167-
if (!$xmlReader->isValid()) {
168-
$err = libxml_get_last_error();
169-
if ($err instanceof LibXMLError) {
170-
$msgs[] = trim($err->message) . ' on line ' . $err->line;
171-
}
158+
$document = DOMDocumentFactory::fromString($xml);
159+
$result = $document->schemaValidate($schemaFile);
160+
161+
if ($result === false) {
162+
$msgs = [];
163+
foreach (libxml_get_errors() as $err) {
164+
$msgs[] = trim($err->message) . ' on line ' . $err->line;
172165
}
173-
}
174166

175-
if ($msgs) {
176167
throw new SchemaViolationException(sprintf(
177168
"XML schema validation errors:\n - %s",
178169
implode("\n - ", array_unique($msgs)),

tests/XML/DOMDocumentFactoryTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,17 @@ public function testSchemaValidationUnknownSchemaFileFails(): void
145145
$file = 'tests/resources/xml/ssp_Chunk.xml';
146146
$schemaFile = 'tests/resources/schemas/doesnotexist.xsd';
147147

148-
// Dirty trick to catch the warning emitted by PHP
149-
set_error_handler(static function (int $errno, string $errstr): never {
150-
restore_error_handler();
151-
throw new Exception($errstr, $errno);
152-
}, E_WARNING);
148+
$this->expectExceptionMessage('File not found.');
149+
DOMDocumentFactory::fromFile($file, $schemaFile);
150+
}
153151

154-
$this->expectExceptionMessage('Failed to locate the main schema resource at');
152+
153+
public function testSchemaValidationInvalidSchemaFileFails(): void
154+
{
155+
$file = 'tests/resources/xml/ssp_Chunk.xml';
156+
$schemaFile = 'resources/schemas/xml.xsd';
157+
158+
$this->expectException(SchemaViolationException::class);
155159
DOMDocumentFactory::fromFile($file, $schemaFile);
156160
}
157161
}

0 commit comments

Comments
 (0)