|
5 | 5 | namespace SimpleSAML\XML\TestUtils; |
6 | 6 |
|
7 | 7 | use DOMDocument; |
8 | | -use Exception; |
9 | | -use LibXMLError; // Officially spelled with a lower-case `l`, but that breaks composer-require-checker |
10 | 8 | use PHPUnit\Framework\Attributes\Depends; |
11 | | -use SimpleSAML\Assert\Assert; |
12 | | -use SimpleSAML\XML\Exception\SchemaViolationException; |
13 | | -use XMLReader; |
| 9 | +use SimpleSAML\XML\DOMDocumentFactory; |
14 | 10 |
|
15 | | -use function array_unique; |
16 | 11 | 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; |
21 | 12 |
|
22 | 13 | /** |
23 | 14 | * Test for AbstractElement classes to perform schema validation tests. |
@@ -58,58 +49,20 @@ public function testSchemaValidation(): void |
58 | 49 | . ':$xmlRepresentation to a DOMDocument representing the XML-class being tested', |
59 | 50 | ); |
60 | 51 | } 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); |
66 | 54 |
|
| 55 | + // Perform serialization |
67 | 56 | $class = self::$testedClass::fromXML(self::$xmlRepresentation->documentElement); |
68 | 57 | $serializedClass = $class->toXML(); |
69 | 58 |
|
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); |
92 | 61 |
|
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); |
101 | 64 | } |
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; |
111 | 65 | } |
112 | 66 |
|
113 | | - |
114 | 67 | abstract public function testSerialization(): void; |
115 | 68 | } |
0 commit comments