Skip to content

Commit

Permalink
Implement AssertionURIRef element (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 31, 2023
1 parent 278e0e0 commit 0225645
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/SAML2/XML/saml/AssertionURIRef.php
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\SAML2\XML\saml;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\StringElementTrait;

/**
* Class representing a saml:AssertionURIRef element.
*
* @package simplesaml/saml2
*/
final class AssertionURIRef extends AbstractSamlElement
{
use StringElementTrait;


/**
* @param string $content
*/
public function __construct(string $content)
{
$this->setContent($content);
}


/**
* Validate the content of the element.
*
* @param string $content The value to go in the XML textContent
* @throws \Exception on failure
* @return void
*/
protected function validateContent(string $content): void
{
Assert::validURI($content, SchemaViolationException::class); // Covers the empty string
}


/**
* Convert XML into an AssertionURIRef
*
* @param \DOMElement $xml The XML element we should load
* @return static
*
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
* If the qualified name of the supplied element is wrong
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, 'AssertionURIRef', InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, AssertionURIRef::NS, InvalidDOMElementException::class);

return new static($xml->textContent);
}


/**
* Convert this AssertionURIRef to XML.
*
* @param \DOMElement $parent The element we are converting to XML.
* @return \DOMElement The XML element after adding the data corresponding to this Condition.
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$element = $this->instantiateParentElement($parent);
$element->textContent = $this->getContent();

return $element;
}
}
71 changes: 71 additions & 0 deletions tests/SAML2/XML/saml/AssertionURIRefTest.php
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\SAML2\Test\SAML2\XML\saml;

use DOMDocument;
use PHPUnit\Framework\TestCase;
use SimpleSAML\SAML2\XML\saml\AssertionURIRef;
use SimpleSAML\Test\XML\SchemaValidationTestTrait;
use SimpleSAML\Test\XML\SerializableElementTestTrait;
use SimpleSAML\XML\DOMDocumentFactory;

use function dirname;
use function strval;

/**
* Class \SimpleSAML\SAML2\XML\saml\AssertionURIRefTest
*
* @covers \SimpleSAML\SAML2\XML\saml\AssertionURIRef
* @covers \SimpleSAML\SAML2\XML\saml\AbstractSamlElement
*
* @package simplesamlphp/saml2
*/
final class AssertionURIRefTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;

/**
*/
protected function setUp(): void
{
$this->schema = dirname(__FILE__, 5) . '/schemas/saml-schema-assertion-2.0.xsd';

$this->testedClass = AssertionURIRef::class;

$this->xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/saml_AssertionURIRef.xml',
);
}


/**
*/
public function testMarshalling(): void
{
$assertionURIRef = new AssertionURIRef('urn:x-simplesamlphp:reference');

$assertionURIRefElement = $assertionURIRef->toXML();
$this->assertEquals('urn:x-simplesamlphp:reference', $assertionURIRefElement->textContent);

$this->assertEquals(
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement),
strval($assertionURIRef),
);
}


/**
*/
public function testUnmarshalling(): void
{
$assertionURIRef = AssertionURIRef::fromXML($this->xmlRepresentation->documentElement);

$this->assertEquals(
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement),
strval($assertionURIRef),
);
}
}
1 change: 1 addition & 0 deletions tests/resources/xml/saml_AssertionURIRef.xml
@@ -0,0 +1 @@
<saml:AssertionURIRef xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">urn:x-simplesamlphp:reference</saml:AssertionURIRef>

0 comments on commit 0225645

Please sign in to comment.