Skip to content

Commit 1643362

Browse files
committed
Improved error-handling for extendable attributes & elements
1 parent b5541c7 commit 1643362

4 files changed

Lines changed: 21 additions & 15 deletions

File tree

src/XML/ExtendableAttributesTrait.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use SimpleSAML\XML\Assert\Assert;
1010
use SimpleSAML\XML\Attribute;
1111
use SimpleSAML\XML\Constants as C;
12+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
1213
use SimpleSAML\XMLSchema\Type\StringValue;
1314
use SimpleSAML\XMLSchema\XML\Constants\NS;
1415

@@ -204,7 +205,7 @@ function (Attribute $attr) {
204205

205206
if ($namespace === NS::LOCAL) {
206207
// If ##local then all namespaces must be null
207-
Assert::allNull($actual_namespaces);
208+
Assert::allNull($actual_namespaces, SchemaviolationException::class);
208209
} elseif (is_array($namespace)) {
209210
// Make a local copy of the property that we can edit
210211
$allowed_namespaces = $namespace;
@@ -227,17 +228,18 @@ function (Attribute $attr) {
227228
rtrim(implode(', ', $diff)),
228229
self::NS,
229230
),
231+
SchemaViolationException::class,
230232
);
231233
} else {
232234
if ($namespace === NS::OTHER) {
233235
// All attributes must be namespaced, ergo non-null
234-
Assert::allNotNull($actual_namespaces);
236+
Assert::allNotNull($actual_namespaces, SchemaViolationException::class);
235237

236238
// Must be any namespace other than the parent element
237-
Assert::allNotSame($actual_namespaces, self::NS);
239+
Assert::allNotSame($actual_namespaces, self::NS, SchemaViolationException::class);
238240
} elseif ($namespace === NS::TARGETNAMESPACE) {
239241
// Must be the same namespace as the one of the parent element
240-
Assert::allSame($actual_namespaces, self::NS);
242+
Assert::allSame($actual_namespaces, self::NS, SchemaViolationException::class);
241243
}
242244
}
243245

src/XML/ExtendableElementTrait.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use SimpleSAML\XML\Chunk;
1111
use SimpleSAML\XML\Constants as C;
1212
use SimpleSAML\XML\Registry\ElementRegistry;
13+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
1314
use SimpleSAML\XMLSchema\XML\Constants\NS;
1415

1516
use function array_diff;
@@ -146,7 +147,7 @@ function (AbstractElement|Chunk $elt): ?string {
146147

147148
if ($namespace === NS::LOCAL) {
148149
// If ##local then all namespaces must be null
149-
Assert::allNull($actual_namespaces);
150+
Assert::allNull($actual_namespaces, SchemaViolationException::class);
150151
} elseif (is_array($namespace)) {
151152
// Make a local copy of the property that we can edit
152153
$allowed_namespaces = $namespace;
@@ -169,14 +170,15 @@ function (AbstractElement|Chunk $elt): ?string {
169170
rtrim(implode(', ', $diff)),
170171
self::NS,
171172
),
173+
SchemaViolationException::class,
172174
);
173175
} elseif ($namespace === NS::OTHER) {
174176
// Must be any namespace other than the parent element, excluding elements with no namespace
175-
Assert::notInArray(null, $actual_namespaces);
176-
Assert::allNotSame($actual_namespaces, self::NS);
177+
Assert::notInArray(null, $actual_namespaces, SchemaViolationException::class);
178+
Assert::allNotSame($actual_namespaces, self::NS, SchemaViolationException::class);
177179
} elseif ($namespace === NS::TARGETNAMESPACE) {
178180
// Must be the same namespace as the one of the parent element
179-
Assert::allSame($actual_namespaces, self::NS);
181+
Assert::allSame($actual_namespaces, self::NS, SchemaViolationexception::class);
180182
} else {
181183
// XS_ANY_NS_ANY
182184
}

tests/XML/ExtendableAttributesTraitTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use SimpleSAML\Assert\AssertionFailedException;
99
use SimpleSAML\Test\Helper\ExtendableAttributesElement;
1010
use SimpleSAML\XML\Attribute;
11+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
1112
use SimpleSAML\XMLSchema\Type\StringValue;
1213
use SimpleSAML\XMLSchema\XML\Constants\NS;
1314

@@ -124,7 +125,7 @@ public function getAttributeNamespace(): array|string
124125
*/
125126
public function testOtherNamespacePassingLocalThrowsAnException(): void
126127
{
127-
$this->expectException(AssertionFailedException::class);
128+
$this->expectException(SchemaViolationException::class);
128129
new class ([self::$local]) extends ExtendableAttributesElement {
129130
/**
130131
* @return array<int, string>|string
@@ -199,7 +200,7 @@ public function getAttributeNamespace(): array|string
199200
*/
200201
public function testTargetNamespacePassingOtherThrowsAnException(): void
201202
{
202-
$this->expectException(AssertionFailedException::class);
203+
$this->expectException(SchemaViolationException::class);
203204
new class ([self::$other]) extends ExtendableAttributesElement {
204205
/**
205206
* @return array<int, string>|string
@@ -255,7 +256,7 @@ public function getAttributeNamespace(): array|string
255256
*/
256257
public function testLocalNamespacePassingOtherThrowsAnException(): void
257258
{
258-
$this->expectException(AssertionFailedException::class);
259+
$this->expectException(SchemaViolationException::class);
259260
new class ([self::$other]) extends ExtendableAttributesElement {
260261
/**
261262
* @return array<int, string>|string

tests/XML/ExtendableElementTraitTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use SimpleSAML\XML\Chunk;
1111
use SimpleSAML\XML\DOMDocumentFactory;
1212
use SimpleSAML\XML\ElementInterface;
13+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
1314
use SimpleSAML\XMLSchema\XML\Constants\NS;
1415

1516
/**
@@ -139,7 +140,7 @@ public function getElementNamespace(): array|string
139140
*/
140141
public function testOtherNamespacePassingLocalThrowsAnException(): void
141142
{
142-
$this->expectException(AssertionFailedException::class);
143+
$this->expectException(SchemaViolationException::class);
143144
new class ([self::$local]) extends ExtendableElement {
144145
/**
145146
* @return array<int, string>|string
@@ -214,7 +215,7 @@ public function getElementNamespace(): array|string
214215
*/
215216
public function testTargetNamespacePassingOtherThrowsAnException(): void
216217
{
217-
$this->expectException(AssertionFailedException::class);
218+
$this->expectException(SchemaViolationException::class);
218219
new class ([self::$other]) extends ExtendableElement {
219220
/**
220221
* @return array<int, string>|string
@@ -270,7 +271,7 @@ public function getElementNamespace(): array|string
270271
*/
271272
public function testLocalNamespacePassingTargetThrowsAnException(): void
272273
{
273-
$this->expectException(AssertionFailedException::class);
274+
$this->expectException(SchemaViolationException::class);
274275
new class ([self::$target]) extends ExtendableElement {
275276
/**
276277
* @return array<int, string>|string
@@ -288,7 +289,7 @@ public function getElementNamespace(): array|string
288289
*/
289290
public function testLocalNamespacePassingOtherThrowsAnException(): void
290291
{
291-
$this->expectException(AssertionFailedException::class);
292+
$this->expectException(SchemaViolationException::class);
292293
new class ([self::$other]) extends ExtendableElement {
293294
/**
294295
* @return array<int, string>|string

0 commit comments

Comments
 (0)