Skip to content

Commit

Permalink
Add IntegerElementTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Nov 29, 2024
1 parent 3ace522 commit 93595fa
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/Base64ElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\StringElementTrait;

use function str_replace;

Expand Down
37 changes: 37 additions & 0 deletions src/IntegerElementTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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 elements with just some textContent
*
* @package simplesamlphp/xml-common
*/
trait IntegerElementTrait
{
use StringElementTrait;


/**
* Validate the content of the element.
*
* @param int $content The value to go in the XML textContent
* @throws \Exception on failure
* @return void
*/
protected function validateContent(/** @scrutinizer ignore-unused */ string $content): void
{
/**
* Perform no validation by default.
* Override this method on the implementing class to perform content validation.
*/
Assert::numeric($content, SchemaViolationException::class);
}
}
33 changes: 33 additions & 0 deletions tests/Utils/IntegerElement.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\IntegerElementTrait;

/**
* Empty shell class for testing Integer elements.
*
* @package simplesaml/xml-common
*/
final class IntegerElement extends AbstractElement
{
use IntegerElementTrait;

/** @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);
}
}
53 changes: 53 additions & 0 deletions tests/XML/IntegerElementTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\XML;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Test\XML\IntegerElement;
use SimpleSAML\XML\AbstractElement;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\IntegerElementTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;
use function strval;

/**
* Class \SimpleSAML\XML\IntegerElementTraitTest
*
* @package simplesamlphp\xml-common
*/
#[CoversClass(SerializableElementTestTrait::class)]
#[CoversClass(IntegerElementTrait::class)]
#[CoversClass(AbstractElement::class)]
final class IntegerElementTraitTest extends TestCase
{
use SerializableElementTestTrait;


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

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

/**
*/
public function testMarshalling(): void
{
$integerElement = new IntegerElement('-001');

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($integerElement),
);
}
}
1 change: 1 addition & 0 deletions tests/resources/xml/ssp_IntegerElement.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ssp:IntegerElement xmlns:ssp="urn:x-simplesamlphp:namespace">-001</ssp:IntegerElement>

0 comments on commit 93595fa

Please sign in to comment.