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 9a0ccd0 commit 4ba3c7e
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/XML/fed/AutomaticPseudonyms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\fed;

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

use function in_array;
use function sprintf;

/**
* A AutomaticPseudonyms element
*
* @package tvdijen/ws-security
*/
final class AutomaticPseudonyms extends AbstractFedElement
{
use StringElementTrait;


/**
* @param bool $content
*/
public function __construct(bool $content)
{
$this->setContent($content ? 'true' : 'false');
}


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

Assert::oneOf(
$xml->textContent,
['0', '1', 'false', 'true'],
sprintf(
'The value \'%s\' of an %s:%s element must be a boolean.',
$xml->textContent,
static::NS_PREFIX,
static::getLocalName(),
),
);

return new static(in_array($xml->textContent, ['1', 'true'], true));
}
}
58 changes: 58 additions & 0 deletions tests/WSSecurity/XML/fed/AutomaticPseudonymsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\sp;

use PHPUnit\Framework\TestCase;
use SimpleSAML\WSSecurity\XML\fed\AutomaticPseudonyms;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;

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


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

self::$testedClass = AutomaticPseudonyms::class;

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


// test marshalling


/**
* Test creating a AutomaticPseudonyms object from scratch.
*/
public function testMarshalling(): void
{
$automaticPseudonyms = new AutomaticPseudonyms(true);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($automaticPseudonyms),
);
}
}
1 change: 1 addition & 0 deletions tests/resources/xml/fed_AutomaticPseudonyms.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<fed:AutomaticPseudonyms xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706">true</fed:AutomaticPseudonyms>

0 comments on commit 4ba3c7e

Please sign in to comment.