Skip to content

Commit

Permalink
Implement AssertionIDRef element (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 31, 2023
1 parent 988860c commit 278e0e0
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/AssertionIDRef.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:AssertionIDRef element.
*
* @package simplesaml/saml2
*/
final class AssertionIDRef 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::validNCName($content, SchemaViolationException::class); // Covers the empty string
}


/**
* Convert XML into an AssertionIDRef
*
* @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, 'AssertionIDRef', InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, AssertionIDRef::NS, InvalidDOMElementException::class);

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


/**
* Convert this AssertionIDRef to XML.
*
* @param \DOMElement $parent The element we are converting to XML.
* @return \DOMElement The XML element after adding the data corresponding to this AssertionIDRef.
*/
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/AssertionIDRefTest.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\AssertionIDRef;
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\AssertionIDRefTest
*
* @covers \SimpleSAML\SAML2\XML\saml\AssertionIDRef
* @covers \SimpleSAML\SAML2\XML\saml\AbstractSamlElement
*
* @package simplesamlphp/saml2
*/
final class AssertionIDRefTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;

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

$this->testedClass = AssertionIDRef::class;

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


/**
*/
public function testMarshalling(): void
{
$assertionIDRef = new AssertionIDRef('_Test');

$assertionIDRefElement = $assertionIDRef->toXML();
$this->assertEquals('_Test', $assertionIDRefElement->textContent);

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


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

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

0 comments on commit 278e0e0

Please sign in to comment.