Skip to content

Commit

Permalink
WIP: create fed classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 6, 2024
1 parent 7a5f72f commit 3fe7246
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/XML/fed/AbstractAttributeExtensibleURIType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\fed;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\StringElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

/**
* An AbstractAttributeExtensibleURIType element
*
* @package tvdijen/ws-security
*/
abstract class AbstractAttributeExtensibleURIType extends AbstractFedElement
{
use ExtendableAttributesTrait;
use StringElementTrait;

/** The namespace-attribute for the xs:anyAttribute element */
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;


/**
* @param string $content
* @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
*/
public function __construct(string $content, array $namespacedAttributes)
{
$this->setContent($content);
$this->setAttributesNS($namespacedAttributes);
}


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


/**
* Create a class from XML
*
* @param \DOMElement $xml
* @return static
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

return new static(
$xml->textContent,
self::getAttributesNSFromXML($xml),
);
}


/**
* Create XML from this class
*
* @param \DOMElement|null $parent
* @return \DOMElement
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);
$e->textContent = $this->getContent();

foreach ($this->getAttributesNS() as $attr) {
$attr->toXML($e);
}

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

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\fed;

/**
* A FederationID element
*
* @package tvdijen/ws-security
*/
final class FederationID extends AbstractAttributeExtensibleURIType
{
}
62 changes: 62 additions & 0 deletions tests/WSSecurity/XML/fed/FederationIDTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\sp;

use PHPUnit\Framework\TestCase;
use SimpleSAML\Test\WSSecurity\Constants as C;
use SimpleSAML\WSSecurity\XML\fed\FederationID;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;

/**
* Class \SimpleSAML\WSSecurity\XML\fed\FederationIDTest
*
* @covers \SimpleSAML\WSSecurity\XML\fed\FederationID
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractAttributeExtensibleURIType
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractFedElement
*
* @package tvdijen/ws-security
*/
final class FederationIDTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;


/**
*/
public static function setUpBeforeClass(): void
{
self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/ws-federation.xsd';

self::$testedClass = FederationID::class;

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


// test marshalling


/**
* Test creating a FederationID object from scratch.
*/
public function testMarshalling(): void
{
$attr1 = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'testval1');
$federationID = new FederationID(C::NAMESPACE, [$attr1]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($federationID),
);
}
}
1 change: 1 addition & 0 deletions tests/resources/xml/fed_FederationID.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<fed:FederationID xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">urn:x-simplesamlphp:namespace</fed:FederationID>

0 comments on commit 3fe7246

Please sign in to comment.