Skip to content

Commit

Permalink
Add samlp:StatusDetail
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Aug 5, 2024
1 parent cf7cd07 commit d08d3fa
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/SAML11/XML/samlp/AbstractStatusDetailType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\SAML11\XML\samlp;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

/**
* SAML StatusDetail data type.
*
* @package simplesamlphp/saml11
*/
abstract class AbstractStatusDetailType extends AbstractSamlpElement
{
use ExtendableElementTrait;

/** The namespace-attribute for the xs:any element */
public const XS_ANY_ELT_NAMESPACE = NS::ANY;


/**
* Initialize a samlp:StatusDetail
*
* @param \SimpleSAML\XML\Chunk[] $details
*/
public function __construct(array $details = [])
{
$this->setElements($details);
}


/**
* Test if an object, at the state it's in, would produce an empty XML-element
*
* @return bool
*/
public function isEmptyElement(): bool
{
return empty($this->elements);
}


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

$details = [];
foreach ($xml->childNodes as $detail) {
if (!($detail instanceof DOMElement)) {
continue;
}

$details[] = new Chunk($detail);
}

return new static($details);
}


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

foreach ($this->getElements() as $detail) {
$detail->toXML($e);
}

return $e;
}
}
14 changes: 14 additions & 0 deletions src/SAML11/XML/samlp/StatusDetail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\SAML11\XML\samlp;

/**
* Class representing a saml:StatusDetail element.
*
* @package simplesamlphp/saml11
*/
final class StatusDetail extends AbstractStatusDetailType
{
}
3 changes: 3 additions & 0 deletions tests/resources/xml/samlp_StatusDetail.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<samlp:StatusDetail xmlns:samlp="urn:oasis:names:tc:SAML:1.0:protocol">
<samlp:StatusMessage>phpunit</samlp:StatusMessage>
</samlp:StatusDetail>
57 changes: 57 additions & 0 deletions tests/src/SAML11/XML/samlp/StatusDetailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\SAML11\XML\saml;

use DOMDocument;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use SimpleSAML\SAML11\XML\samlp\AbstractStatusDetailType;
use SimpleSAML\SAML11\XML\samlp\StatusDetail;
use SimpleSAML\SAML11\XML\samlp\StatusMessage;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;
use function strval;

/**
* Class \SimpleSAML\SAML11\XML\samlp\StatusDetailTest
*
* @package simplesamlphp/saml11
*/
#[CoversClass(StatusDetail::class)]
#[CoversClass(AbstractStatusDetailType::class)]
#[CoversClass(AbstractSamlpElement::class)]
final class StatusDetailTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;

/**
*/
public static function setUpBeforeClass(): void
{
self::$testedClass = StatusDetail::class;

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 5) . '/resources/xml/samlp_StatusDetail.xml',
);
}


/**
*/
public function testMarshalling(): void
{
$statusDetail = new StatusDetail([new StatusMessage('phpunit')]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($statusDetail),
);
}
}

0 comments on commit d08d3fa

Please sign in to comment.