Skip to content

Commit

Permalink
Introduce new BooleanElementTrait and URIElementTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 22, 2024
1 parent b764f1e commit 4f1257d
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 2 deletions.
68 changes: 68 additions & 0 deletions src/BooleanElementTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML;

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

/**
* Trait grouping common functionality for simple boolean elements
*
* @package simplesamlphp/xml-common
*/
trait BooleanElementTrait
{
use StringElementTrait;

/**
* 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::oneOf(
$content,
['0', '1', 'false', 'true'],
sprintf('The value of %s must be a boolean, "%s" given.', $this->getQualifiedName(), $content),
SchemaViolationException::class,
);
}


/**
* 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);

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


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

return $e;
}
}
2 changes: 0 additions & 2 deletions src/LocalizedStringElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ public static function fromXML(DOMElement $xml): static
'Missing xml:lang from ' . static::getLocalName(),
MissingAttributeException::class,
);
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

return new static($xml->getAttributeNS(C::NS_XML, 'lang'), $xml->textContent);
}
Expand Down
63 changes: 63 additions & 0 deletions src/URIElementTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML;

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

/**
* Trait grouping common functionality for simple URI string elements
*
* @package simplesamlphp/xml-common
*/
trait URIElementTrait
{
use StringElementTrait;

/**
* 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::validURI($content, SchemaViolationException::class);
}


/**
* 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);

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


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

return $e;
}
}
33 changes: 33 additions & 0 deletions tests/Utils/BooleanElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\XML;

use SimpleSAML\XML\AbstractElement;
use SimpleSAML\XML\BooleanElementTrait;

/**
* Empty shell class for testing BooleanElement.
*
* @package simplesaml/xml-common
*/
final class BooleanElement extends AbstractElement
{
use BooleanElementTrait;

/** @var string */
public const NS = 'urn:x-simplesamlphp:namespace';

/** @var string */
public const NS_PREFIX = 'ssp';


/**
* @param string $content
*/
public function __construct(string $content)
{
$this->setContent($content);
}
}
33 changes: 33 additions & 0 deletions tests/Utils/URIElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\XML;

use SimpleSAML\XML\AbstractElement;
use SimpleSAML\XML\URIElementTrait;

/**
* Empty shell class for testing URIElement.
*
* @package simplesaml/xml-common
*/
final class URIElement extends AbstractElement
{
use URIElementTrait;

/** @var string */
public const NS = 'urn:x-simplesamlphp:namespace';

/** @var string */
public const NS_PREFIX = 'ssp';


/**
* @param string $content
*/
public function __construct(string $content)
{
$this->setContent($content);
}
}
51 changes: 51 additions & 0 deletions tests/XML/BooleanElementTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\XML;

use PHPUnit\Framework\TestCase;
use SimpleSAML\Test\XML\BooleanElement;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;
use function strval;

/**
* Class \SimpleSAML\XML\BooleanElementTraitTest
*
* @covers \SimpleSAML\XML\TestUtils\SerializableElementTestTrait
* @covers \SimpleSAML\XML\BooleanElementTrait
* @covers \SimpleSAML\XML\AbstractElement
*
* @package simplesamlphp\xml-common
*/
final class BooleanElementTraitTest extends TestCase
{
use SerializableElementTestTrait;


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

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

/**
*/
public function testMarshalling(): void
{
$booleanElement = new BooleanElement('true');

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($booleanElement),
);
}
}
51 changes: 51 additions & 0 deletions tests/XML/URIElementTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\XML;

use PHPUnit\Framework\TestCase;
use SimpleSAML\Test\XML\URIElement;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;
use function strval;

/**
* Class \SimpleSAML\XML\URIElementTraitTest
*
* @covers \SimpleSAML\XML\TestUtils\SerializableElementTestTrait
* @covers \SimpleSAML\XML\URIElementTrait
* @covers \SimpleSAML\XML\AbstractElement
*
* @package simplesamlphp\xml-common
*/
final class URIElementTraitTest extends TestCase
{
use SerializableElementTestTrait;


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

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

/**
*/
public function testMarshalling(): void
{
$URIElement = new URIElement('https://example.org');

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($URIElement),
);
}
}
1 change: 1 addition & 0 deletions tests/resources/xml/ssp_BooleanElement.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ssp:BooleanElement xmlns:ssp="urn:x-simplesamlphp:namespace">true</ssp:BooleanElement>
1 change: 1 addition & 0 deletions tests/resources/xml/ssp_URIElement.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ssp:URIElement xmlns:ssp="urn:x-simplesamlphp:namespace">https://example.org</ssp:URIElement>

0 comments on commit 4f1257d

Please sign in to comment.