|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\XML; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use SimpleSAML\XML\DOMDocumentFactory; |
| 10 | +use SimpleSAML\XML\Exception\{InvalidValueTypeException, SchemaViolationException}; |
| 11 | +use SimpleSAML\XML\TypedTextContentTrait; |
| 12 | + |
| 13 | +/** |
| 14 | + * Class \SimpleSAML\XML\TypedTextContentTraitTest |
| 15 | + * |
| 16 | + * @package simplesamlphp\xml-common |
| 17 | + */ |
| 18 | +#[CoversClass(TypedTextContentTrait::class)] |
| 19 | +final class TypedTextContentTraitTest extends TestCase |
| 20 | +{ |
| 21 | + public function testTypedContentPassesForString(): void |
| 22 | + { |
| 23 | + $file = 'tests/resources/xml/ssp_StringElement.xml'; |
| 24 | + $doc = DOMDocumentFactory::fromFile($file); |
| 25 | + /** @var \DOMElement $elt */ |
| 26 | + $elt = $doc->documentElement; |
| 27 | + |
| 28 | + $stringElt = StringElement::fromXML($elt); |
| 29 | + $this->assertInstanceOf(StringElement::class, $stringElt); |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + public function testTypedContentPassesForBoolean(): void |
| 34 | + { |
| 35 | + $file = 'tests/resources/xml/ssp_BooleanElement.xml'; |
| 36 | + $doc = DOMDocumentFactory::fromFile($file); |
| 37 | + /** @var \DOMElement $elt */ |
| 38 | + $elt = $doc->documentElement; |
| 39 | + |
| 40 | + $stringElt = BooleanElement::fromXML($elt); |
| 41 | + $this->assertInstanceOf(BooleanElement::class, $stringElt); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + public function testTypedContentFailsForWrongType(): void |
| 46 | + { |
| 47 | + $file = 'tests/resources/xml/ssp_BooleanElement.xml'; |
| 48 | + $doc = DOMDocumentFactory::fromFile($file); |
| 49 | + /** @var \DOMElement $elt */ |
| 50 | + $elt = $doc->documentElement; |
| 51 | + $elt->textContent = 'not-a-boolean'; |
| 52 | + |
| 53 | + $this->expectException(SchemaViolationException::class); |
| 54 | + BooleanElement::fromXML($elt); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + public function testTypedContentFailsForWrongClass(): void |
| 59 | + { |
| 60 | + // Base64Binary has a TEXTCONTENT_TYPE that makes no sense |
| 61 | + $file = 'tests/resources/xml/ssp_Base64BinaryElement.xml'; |
| 62 | + $doc = DOMDocumentFactory::fromFile($file); |
| 63 | + /** @var \DOMElement $elt */ |
| 64 | + $elt = $doc->documentElement; |
| 65 | + |
| 66 | + $this->expectException(InvalidValueTypeException::class); |
| 67 | + Base64BinaryElement::fromXML($elt); |
| 68 | + } |
| 69 | +} |
0 commit comments