Skip to content

Commit 994de22

Browse files
committed
Re-use new functionality in test-trait
1 parent 8718623 commit 994de22

File tree

1 file changed

+8
-55
lines changed

1 file changed

+8
-55
lines changed

src/TestUtils/SchemaValidationTestTrait.php

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,10 @@
55
namespace SimpleSAML\XML\TestUtils;
66

77
use DOMDocument;
8-
use Exception;
9-
use LibXMLError; // Officially spelled with a lower-case `l`, but that breaks composer-require-checker
108
use PHPUnit\Framework\Attributes\Depends;
11-
use SimpleSAML\Assert\Assert;
12-
use SimpleSAML\XML\Exception\SchemaViolationException;
13-
use XMLReader;
9+
use SimpleSAML\XML\DOMDocumentFactory;
1410

15-
use function array_unique;
1611
use function class_exists;
17-
use function implode;
18-
use function libxml_get_last_error;
19-
use function libxml_use_internal_errors;
20-
use function trim;
2112

2213
/**
2314
* Test for AbstractElement classes to perform schema validation tests.
@@ -58,58 +49,20 @@ public function testSchemaValidation(): void
5849
. ':$xmlRepresentation to a DOMDocument representing the XML-class being tested',
5950
);
6051
} else {
61-
$predoc = XMLReader::XML(self::$xmlRepresentation->saveXML());
62-
Assert::notFalse($predoc);
63-
64-
$pre = $this->validateDocument($predoc);
65-
$this->assertTrue($pre);
52+
// Validate before serialization
53+
DOMDocumentFactory::schemaValidation(self::$xmlRepresentation->saveXML(), self::$schemaFile);
6654

55+
// Perform serialization
6756
$class = self::$testedClass::fromXML(self::$xmlRepresentation->documentElement);
6857
$serializedClass = $class->toXML();
6958

70-
$postdoc = XMLReader::XML($serializedClass->ownerDocument->saveXML());
71-
Assert::notFalse($postdoc);
72-
$post = $this->validateDocument($postdoc);
73-
$this->assertTrue($post);
74-
}
75-
}
76-
77-
78-
/**
79-
* @param \XMLReader $doc
80-
* @return boolean
81-
*/
82-
private function validateDocument(XMLReader $xmlReader): bool
83-
{
84-
libxml_use_internal_errors(true);
85-
86-
try {
87-
$xmlReader->setSchema(self::$schemaFile);
88-
} catch (Exception) {
89-
$err = libxml_get_last_error();
90-
throw new SchemaViolationException(trim($err->message) . ' on line ' . $err->line);
91-
}
59+
// Validate after serialization
60+
DOMDocumentFactory::schemaValidation($serializedClass->ownerDocument->saveXML(), self::$schemaFile);
9261

93-
$msgs = [];
94-
while ($xmlReader->read()) {
95-
if (!$xmlReader->isValid()) {
96-
$err = libxml_get_last_error();
97-
if ($err instanceof LibXMLError) {
98-
$msgs[] = trim($err->message) . ' on line ' . $err->line;
99-
}
100-
}
62+
// If we got this far and no exceptions were thrown, consider this test passed!
63+
$this->addToAssertionCount(1);
10164
}
102-
103-
if ($msgs) {
104-
throw new SchemaViolationException(sprintf(
105-
"XML schema validation errors:\n - %s",
106-
implode("\n - ", array_unique($msgs)),
107-
));
108-
}
109-
110-
return true;
11165
}
11266

113-
11467
abstract public function testSerialization(): void;
11568
}

0 commit comments

Comments
 (0)