Skip to content

Commit 8573d63

Browse files
committed
Add some more assertions based on specifications
1 parent 8e5416e commit 8573d63

File tree

9 files changed

+36
-15
lines changed

9 files changed

+36
-15
lines changed

src/XMLSchema/XML/xs/AbstractAttribute.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,20 @@ public function __construct(
5151
?IDValue $id = null,
5252
array $namespacedAttributes = [],
5353
) {
54-
Assert::true(is_null($type) || is_null($reference), ProtocolViolationException::class);
55-
Assert::true(is_null($name) || is_null($reference), ProtocolViolationException::class);
54+
Assert::oneOf(null, [$type, $reference], ProtocolViolationException::class);
55+
Assert::oneOf(null, [$name, $reference], ProtocolViolationException::class);
5656
Assert::false(is_null($name) && is_null($reference), ProtocolViolationException::class);
5757

58+
/**
59+
* default and fixed are mutually exclusive
60+
*/
61+
Assert::oneOf(null, [$default, $fixed], ProtocolViolationException::class);
62+
63+
// simpleType only if no type|ref attribute
64+
if ($simpleType !== null) {
65+
Assert::true(is_null($type) && is_null($reference), ProtocolViolationException::class);
66+
}
67+
5868
parent::__construct($annotation, $id, $namespacedAttributes);
5969

6070
$this->setName($name);

src/XMLSchema/XML/xs/AbstractComplexType.php

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

77
use DOMElement;
88
use SimpleSAML\XML\Assert\Assert;
9-
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
9+
use SimpleSAML\XMLSchema\Exception\{ProtocolViolationException, SchemaViolationException};
1010
use SimpleSAML\XMLSchema\Type\Builtin\{BooleanValue, IDValue, NCNameValue};
1111
use SimpleSAML\XMLSchema\Type\DerivationSetValue;
1212

@@ -60,6 +60,10 @@ public function __construct(
6060
Assert::null($anyAttribute, SchemaViolationException::class);
6161

6262
$this->setContent($content);
63+
64+
if ($content instanceof SimpleContent) {
65+
Assert::null($mixed, ProtocolViolationException::class, 'mixed is disallowed if simpleContent');
66+
}
6367
} else {
6468
Assert::null($content, SchemaViolationException::class);
6569

src/XMLSchema/XML/xs/AbstractElement.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,21 @@ public function __construct(
7777
* type and ref are mutually exclusive.
7878
* name and ref are mutually exclusive, one is required
7979
*/
80-
Assert::true(is_null($type) || is_null($reference), ProtocolViolationException::class);
81-
Assert::true(is_null($name) || is_null($reference), ProtocolViolationException::class);
80+
Assert::oneOf(null, [$type, $reference], ProtocolViolationException::class);
81+
Assert::oneOf(null, [$name, $reference], ProtocolViolationException::class);
8282
Assert::false(is_null($name) && is_null($reference), ProtocolViolationException::class);
8383

8484
/**
8585
* default and fixed are mutually exclusive
8686
*/
87-
Assert::true(is_null($default) || is_null($fixed), ProtocolViolationException::class);
87+
Assert::oneOf(null, [$default, $fixed], ProtocolViolationException::class);
88+
89+
/**
90+
* simpleType or complexType only if no type|ref attribute
91+
*/
92+
if ($localType !== null) {
93+
Assert::true(is_null($type) || is_null($reference), ProtocolViolationException::class);
94+
}
8895

8996
parent::__construct($annotation, $id, $namespacedAttributes);
9097

tests/XMLSchema/XML/xs/LocalAttributeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ public function testMarshalling(): void
133133

134134
$attribute = new LocalAttribute(
135135
null,
136+
NCNameValue::fromString('myAttr'),
136137
null,
137-
QNameValue::fromString('{http://www.w3.org/XML/1998/namespace}xml:lang'),
138138
UseValue::fromEnum(UseEnum::Required),
139-
StringValue::fromString('en'),
139+
null,
140140
StringValue::fromString('en'),
141141
FormChoiceValue::fromEnum(FormChoiceEnum::Unqualified),
142142
$simpleType,

tests/XMLSchema/XML/xs/SchemaTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ public function testMarshalling(): void
208208

209209
$attribute = new TopLevelAttribute(
210210
NCNameValue::fromString('number'),
211-
QNameValue::fromString('{http://www.w3.org/2001/XMLSchema}xs:integer'),
212-
StringValue::fromString('1'),
211+
null,
213212
StringValue::fromString('1'),
213+
null,
214214
$simpleType,
215215
null,
216216
IDValue::fromString('phpunit_attribute'),

tests/XMLSchema/XML/xs/TopLevelAttributeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ public function testMarshalling(): void
133133

134134
$attribute = new TopLevelAttribute(
135135
NCNameValue::fromString('number'),
136-
QNameValue::fromString('{http://www.w3.org/2001/XMLSchema}xs:integer'),
137-
StringValue::fromString('1'),
136+
null,
138137
StringValue::fromString('1'),
138+
null,
139139
$simpleType,
140140
$annotation,
141141
IDValue::fromString('phpunit_attribute'),

tests/resources/xml/xs/localAttribute.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<xs:attribute xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr4="value4" id="phpunit_attribute" ref="xml:lang" use="required" default="en" fixed="en" form="unqualified">
1+
<xs:attribute xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr4="value4" id="phpunit_attribute" name="myAttr" use="required" fixed="en" form="unqualified">
22
<xs:annotation ssp:attr3="value3" id="phpunit_annotation">
33
<xs:appinfo source="urn:x-simplesamlphp:source" ssp:attr1="value1">Application Information</xs:appinfo>
44
<xs:appinfo source="urn:x-simplesamlphp:source" ssp:attr2="value2">Other Application Information</xs:appinfo>

tests/resources/xml/xs/schema.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<xs:annotation ssp:attr1="value1" id="phpunit_annotation3">
1919
<xs:documentation source="urn:x-simplesamlphp:source" xml:lang="nl" ssp:attr2="value2">Attribute</xs:documentation>
2020
</xs:annotation>
21-
<xs:attribute ssp:attr4="value4" id="phpunit_attribute" name="number" type="xs:integer" default="1" fixed="1">
21+
<xs:attribute ssp:attr4="value4" id="phpunit_attribute" name="number" default="1">
2222
<xs:simpleType ssp:attr4="value4" id="phpunit_simpleType">
2323
<xs:restriction base="xs:nonNegativeInteger"/>
2424
</xs:simpleType>

tests/resources/xml/xs/topLevelAttribute.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<xs:attribute xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr4="value4" id="phpunit_attribute" name="number" type="xs:integer" default="1" fixed="1">
1+
<xs:attribute xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr4="value4" id="phpunit_attribute" name="number" default="1">
22
<xs:annotation ssp:attr3="value3" id="phpunit_annotation">
33
<xs:appinfo source="urn:x-simplesamlphp:source" ssp:attr1="value1">Application Information</xs:appinfo>
44
<xs:appinfo source="urn:x-simplesamlphp:source" ssp:attr2="value2">Other Application Information</xs:appinfo>

0 commit comments

Comments
 (0)