Skip to content

Commit

Permalink
Add mex classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 28, 2024
1 parent ce91037 commit f3dcfdb
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/XML/mex/MetadataReference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\mex;

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

use function array_pop;

/**
* Class defining the MetadataReference element
*
* @package tvdijen/ws-security
*/
final class MetadataReference extends AbstractMexElement
{
use ExtendableElementTrait;

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


/**
* MetadataReference constructor
*
* @param \SimpleSAML\XML\SerializableElementInterface[] $children
*/
final public function __construct(
array $children = []
) {
Assert::minCount($children, 1, MissingElementException::class);
$this->setElements($children);
}


/**
* Create an instance of this object from its XML representation.
*
* @param \DOMElement $xml
* @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, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

$children = [];
foreach ($xml->childNodes as $child) {
if (!($child instanceof DOMElement)) {
continue;
} elseif ($child->namespaceURI === static::NS) {
continue;
}

$children[] = new Chunk($child);
}

return new static($children);
}


/**
* Add this MetadataReference to an XML element.
*
* @param \DOMElement $parent The element we should append this MetadataReference to.
* @return \DOMElement
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = parent::instantiateParentElement($parent);

foreach ($this->getElements() as $child) {
if (!$child->isEmptyElement()) {
$child->toXML($e);
}
}

return $e;
}
}
89 changes: 89 additions & 0 deletions tests/WSSecurity/XML/mex/MetadataReferenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\mex;

use DOMElement;
use PHPUnit\Framework\TestCase;
use SimpleSAML\WSSecurity\XML\mex\MetadataReference;
use SimpleSAML\WSSecurity\XML\wsa\Address;
use SimpleSAML\WSSecurity\XML\wsa\EndpointReference;
use SimpleSAML\XML\Attribute;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;
use function strval;

/**
* Tests for mex:MetadataReference.
*
* @covers \SimpleSAML\WSSecurity\XML\mex\MetadataReference
* @covers \SimpleSAML\WSSecurity\XML\wsa\AbstractMexElement
* @package tvdijen/ws-security
*/
final class MetadataReferenceTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;

/** @var \DOMElement $referenceParametersContent */
protected static DOMElement $referenceParametersContent;

/** @var \DOMElement $metadataContent */
protected static DOMElement $metadataContent;

/** @var \DOMElement $customContent */
protected static DOMElement $customContent;


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

self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/MetadataExchange.xsd';

self::$xmlRepresentation = DOMDocumentFactory::FromFile(
dirname(__FILE__, 4) . '/resources/xml/mex_MetadataReference.xml'
);

self::$customContent = DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">SomeChunk</ssp:Chunk>'
)->documentElement;
}


// test marshalling


/**
* Test creating an MetadataReference object from scratch.
*/
public function testMarshalling(): void
{
$attr1 = new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'test1', 'value1');
$attr2 = new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'test2', 'value2');

$chunk = new Chunk(self::$customContent);

$endpointReference = new EndpointReference(
new Address('https://login.microsoftonline.com/login.srf', [$attr2]),
[],
[],
[$chunk],
[$attr1],
);

$metadataReference = new MetadataReference([$endpointReference]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($metadataReference)
);
}
}
6 changes: 6 additions & 0 deletions tests/resources/xml/mex_MetadataReference.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<mex:MetadataReference xmlns:mex="http://schemas.xmlsoap.org/ws/2004/09/mex">
<wsa:EndpointReference xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:test1="value1">
<wsa:Address ssp:test2="value2">https://login.microsoftonline.com/login.srf</wsa:Address>
<ssp:Chunk>SomeChunk</ssp:Chunk>
</wsa:EndpointReference>
</mex:MetadataReference>

0 comments on commit f3dcfdb

Please sign in to comment.