Skip to content

Commit

Permalink
Raise coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Sep 2, 2024
1 parent 8d45363 commit 8c055a9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/SAML2/XML/saml/AttributeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,23 @@ final public function __construct(
*/
public function getXsiType(): string
{
$type = gettype($this->value);
$value = $this->getValue();
$type = gettype($value);

switch ($type) {
case "integer":
return "xs:integer";
case "NULL":
return "xs:nil";
case "object":
if ($this->value instanceof DateTimeInterface) {
if ($value instanceof DateTimeInterface) {
return 'xs:dateTime';
}

return sprintf(
'%s:%s',
$this->value::getNamespacePrefix(),
AbstractElement::getClassName(get_class($this->value)),
$value::getNamespacePrefix(),
AbstractElement::getClassName(get_class($value)),
);
default:
return "xs:string";
Expand Down Expand Up @@ -138,6 +139,7 @@ public static function fromXML(DOMElement $xml): static
$xml->getAttributeNS(C::NS_XSI, "nil") === "true")
) {
Assert::isEmpty($xml->textContent);

$value = null;
} else {
$value = $xml->textContent;
Expand All @@ -158,33 +160,34 @@ public function toXML(DOMElement $parent = null): DOMElement
{
$e = parent::instantiateParentElement($parent);

$type = gettype($this->value);
$value = $this->getValue();
$type = gettype($value);

switch ($type) {
case "integer":
// make sure that the xs namespace is available in the AttributeValue
$e->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', C::NS_XSI);
$e->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xs', C::NS_XS);
$e->setAttributeNS(C::NS_XSI, 'xsi:type', 'xs:integer');
$e->textContent = strval($this->getValue());
$e->textContent = strval($value);
break;
case "NULL":
$e->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', C::NS_XSI);
$e->setAttributeNS(C::NS_XSI, 'xsi:nil', '1');
$e->textContent = '';
break;
case "object":
if ($this->value instanceof DateTimeInterface) {
if ($value instanceof DateTimeInterface) {
$e->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', C::NS_XSI);
$e->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xs', C::NS_XS);
$e->setAttributeNS(C::NS_XSI, 'xsi:type', 'xs:dateTime');
$e->textContent = $this->value->format(C::DATETIME_FORMAT);
$e->textContent = $value->format(C::DATETIME_FORMAT);
} else {
$this->getValue()->toXML($e);
$value->toXML($e);
}
break;
default: // string
$e->textContent = $this->getValue();
$e->textContent = $value;
break;
}

Expand Down
23 changes: 23 additions & 0 deletions tests/SAML2/XML/saml/AttributeValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ public function testMarshallingInteger(): void

$this->assertEquals(3, $av->getValue());
$this->assertEquals('xs:integer', $av->getXsiType());

$nssaml = C::NS_SAML;
$nsxs = C::NS_XS;
$nsxsi = C::NS_XSI;
$xml = <<<XML
<saml:AttributeValue xmlns:saml="{$nssaml}" xmlns:xsi="{$nsxsi}" xmlns:xs="{$nsxs}" xsi:type="xs:integer">3</saml:AttributeValue>
XML;
$this->assertEquals(
$xml,
strval($av),
);
}


Expand All @@ -101,6 +112,17 @@ public function testMarshallingDateTime(): void
$value = $av->getValue();
$this->assertEquals('2024-04-04T04:44:44Z', $value->format(C::DATETIME_FORMAT));
$this->assertEquals('xs:dateTime', $av->getXsiType());

$nssaml = C::NS_SAML;
$nsxs = C::NS_XS;
$nsxsi = C::NS_XSI;
$xml = <<<XML
<saml:AttributeValue xmlns:saml="{$nssaml}" xmlns:xsi="{$nsxsi}" xmlns:xs="{$nsxs}" xsi:type="xs:dateTime">2024-04-04T04:44:44Z</saml:AttributeValue>
XML;
$this->assertEquals(
$xml,
strval($av),
);
}


Expand All @@ -111,6 +133,7 @@ public function testMarshallingNull(): void
$av = new AttributeValue(null);
$this->assertNull($av->getValue());
$this->assertEquals('xs:nil', $av->getXsiType());

$nssaml = C::NS_SAML;
$nsxsi = C::NS_XSI;
$xml = <<<XML
Expand Down

0 comments on commit 8c055a9

Please sign in to comment.