Skip to content

Commit 0225645

Browse files
authored
Implement AssertionURIRef element (#319)
1 parent 278e0e0 commit 0225645

3 files changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\SAML2\XML\saml;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\Exception\SchemaViolationException;
11+
use SimpleSAML\XML\StringElementTrait;
12+
13+
/**
14+
* Class representing a saml:AssertionURIRef element.
15+
*
16+
* @package simplesaml/saml2
17+
*/
18+
final class AssertionURIRef extends AbstractSamlElement
19+
{
20+
use StringElementTrait;
21+
22+
23+
/**
24+
* @param string $content
25+
*/
26+
public function __construct(string $content)
27+
{
28+
$this->setContent($content);
29+
}
30+
31+
32+
/**
33+
* Validate the content of the element.
34+
*
35+
* @param string $content The value to go in the XML textContent
36+
* @throws \Exception on failure
37+
* @return void
38+
*/
39+
protected function validateContent(string $content): void
40+
{
41+
Assert::validURI($content, SchemaViolationException::class); // Covers the empty string
42+
}
43+
44+
45+
/**
46+
* Convert XML into an AssertionURIRef
47+
*
48+
* @param \DOMElement $xml The XML element we should load
49+
* @return static
50+
*
51+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
52+
* If the qualified name of the supplied element is wrong
53+
*/
54+
public static function fromXML(DOMElement $xml): static
55+
{
56+
Assert::same($xml->localName, 'AssertionURIRef', InvalidDOMElementException::class);
57+
Assert::same($xml->namespaceURI, AssertionURIRef::NS, InvalidDOMElementException::class);
58+
59+
return new static($xml->textContent);
60+
}
61+
62+
63+
/**
64+
* Convert this AssertionURIRef to XML.
65+
*
66+
* @param \DOMElement $parent The element we are converting to XML.
67+
* @return \DOMElement The XML element after adding the data corresponding to this Condition.
68+
*/
69+
public function toXML(DOMElement $parent = null): DOMElement
70+
{
71+
$element = $this->instantiateParentElement($parent);
72+
$element->textContent = $this->getContent();
73+
74+
return $element;
75+
}
76+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\SAML2\Test\SAML2\XML\saml;
6+
7+
use DOMDocument;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\SAML2\XML\saml\AssertionURIRef;
10+
use SimpleSAML\Test\XML\SchemaValidationTestTrait;
11+
use SimpleSAML\Test\XML\SerializableElementTestTrait;
12+
use SimpleSAML\XML\DOMDocumentFactory;
13+
14+
use function dirname;
15+
use function strval;
16+
17+
/**
18+
* Class \SimpleSAML\SAML2\XML\saml\AssertionURIRefTest
19+
*
20+
* @covers \SimpleSAML\SAML2\XML\saml\AssertionURIRef
21+
* @covers \SimpleSAML\SAML2\XML\saml\AbstractSamlElement
22+
*
23+
* @package simplesamlphp/saml2
24+
*/
25+
final class AssertionURIRefTest extends TestCase
26+
{
27+
use SchemaValidationTestTrait;
28+
use SerializableElementTestTrait;
29+
30+
/**
31+
*/
32+
protected function setUp(): void
33+
{
34+
$this->schema = dirname(__FILE__, 5) . '/schemas/saml-schema-assertion-2.0.xsd';
35+
36+
$this->testedClass = AssertionURIRef::class;
37+
38+
$this->xmlRepresentation = DOMDocumentFactory::fromFile(
39+
dirname(__FILE__, 4) . '/resources/xml/saml_AssertionURIRef.xml',
40+
);
41+
}
42+
43+
44+
/**
45+
*/
46+
public function testMarshalling(): void
47+
{
48+
$assertionURIRef = new AssertionURIRef('urn:x-simplesamlphp:reference');
49+
50+
$assertionURIRefElement = $assertionURIRef->toXML();
51+
$this->assertEquals('urn:x-simplesamlphp:reference', $assertionURIRefElement->textContent);
52+
53+
$this->assertEquals(
54+
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement),
55+
strval($assertionURIRef),
56+
);
57+
}
58+
59+
60+
/**
61+
*/
62+
public function testUnmarshalling(): void
63+
{
64+
$assertionURIRef = AssertionURIRef::fromXML($this->xmlRepresentation->documentElement);
65+
66+
$this->assertEquals(
67+
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement),
68+
strval($assertionURIRef),
69+
);
70+
}
71+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<saml:AssertionURIRef xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">urn:x-simplesamlphp:reference</saml:AssertionURIRef>

0 commit comments

Comments
 (0)