Skip to content

Commit 4d07c91

Browse files
committed
Raise coverage
1 parent 0aaddc1 commit 4d07c91

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

src/SchemaValidatableElementTrait.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
use function file_exists;
1515
use function implode;
1616
use function libxml_get_errors;
17-
use function restore_error_handler;
18-
use function set_error_handler;
1917
use function sprintf;
2018
use function trim;
2119

@@ -35,22 +33,11 @@ trait SchemaValidatableElementTrait
3533
public static function schemaValidate(DOMDocument $document): DOMDocument
3634
{
3735
$schemaFile = self::getSchemaFile();
36+
// Must suppress the warnings here in order to throw them as an error below.
37+
$result = @$document->schemaValidate($schemaFile);
3838

39-
// Dirty trick to catch the warnings emitted by XML-DOMs schemaValidate
40-
// This will turn the warning into an exception
41-
set_error_handler(static function (int $errno, string $errstr): never {
42-
throw new SchemaViolationException($errstr, $errno);
43-
}, E_WARNING);
44-
45-
try {
46-
$result = $document->schemaValidate($schemaFile);
47-
} finally {
48-
// Restore the error handler, whether we throw an exception or not
49-
restore_error_handler();
50-
}
51-
52-
$msgs = [];
5339
if ($result === false) {
40+
$msgs = [];
5441
foreach (libxml_get_errors() as $err) {
5542
$msgs[] = trim($err->message) . ' on line ' . $err->line;
5643
}

tests/Utils/URIElement.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66

77
use SimpleSAML\XML\AbstractElement;
88
use SimpleSAML\XML\URIElementTrait;
9+
use SimpleSAML\XML\SchemaValidatableElementInterface;
10+
use SimpleSAML\XML\SchemaValidatableElementTrait;
911

1012
/**
1113
* Empty shell class for testing URIElement.
1214
*
1315
* @package simplesaml/xml-common
1416
*/
15-
final class URIElement extends AbstractElement
17+
final class URIElement extends AbstractElement implements SchemaValidatableElementInterface
1618
{
19+
use SchemaValidatableElementTrait;
1720
use URIElementTrait;
1821

1922
/** @var string */
@@ -22,6 +25,9 @@ final class URIElement extends AbstractElement
2225
/** @var string */
2326
public const NS_PREFIX = 'ssp';
2427

28+
/** @var string */
29+
public const SCHEMA = 'tests/resources/schemas/simplesamlphp.xsd';
30+
2531

2632
/**
2733
* @param string $content

tests/XML/SchemaValidatableElementTraitTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public function testSchemaValidationPasses(): void
3232
}
3333

3434

35+
public function testSchemaValidationFails(): void
36+
{
37+
$file = 'tests/resources/xml/invalid_ExtendableElement.xml';
38+
$chunk = DOMDocumentFactory::fromFile($file);
39+
40+
$this->expectException(SchemaViolationException::class);
41+
$document = StringElement::schemaValidate($chunk);
42+
}
43+
44+
3545
public function testSchemaValidationWrongElementFails(): void
3646
{
3747
$file = 'tests/resources/xml/ssp_Base64Element.xml';

tests/resources/schemas/simplesamlphp.xsd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747

4848
<!-- End StringElement -->
4949

50+
<!-- Start URIElement -->
51+
52+
<element name="URIElement" type="anyURI"/>
53+
54+
<!-- End URIElement -->
55+
5056
<!-- Start ExtendableAttributesElement -->
5157

5258
<element name="ExtendableAttributesElement" type="ssp:ExtendableAttributesElementType"/>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<ssp:ExtendableElement xmlns:ssp="urn:x-simplesamlphp:namespace">
2+
<dummy:Chunk xmlns:dummy="urn:custom:dummy">some</dummy:Chunk>
3+
</ssp:ExtendableElement>

0 commit comments

Comments
 (0)