|
| 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\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use SimpleSAML\Test\XML\XMLDumper; |
| 11 | +use SimpleSAML\XML\AbstractElement; |
| 12 | +use SimpleSAML\XML\DOMDocumentFactory; |
| 13 | +use SimpleSAML\XML\HexBinaryElementTrait; |
| 14 | +use SimpleSAML\XML\StringElementTrait; |
| 15 | +use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; |
| 16 | + |
| 17 | +use function dirname; |
| 18 | +use function strval; |
| 19 | + |
| 20 | +/** |
| 21 | + * Class \SimpleSAML\XML\HexBinaryElementTraitTest |
| 22 | + * |
| 23 | + * @package simplesamlphp\xml-common |
| 24 | + */ |
| 25 | +#[CoversClass(SerializableElementTestTrait::class)] |
| 26 | +#[CoversClass(HexBinaryElementTrait::class)] |
| 27 | +#[CoversClass(StringElementTrait::class)] |
| 28 | +#[CoversClass(AbstractElement::class)] |
| 29 | +final class HexBinaryElementTraitTest extends TestCase |
| 30 | +{ |
| 31 | + use SerializableElementTestTrait; |
| 32 | + |
| 33 | + |
| 34 | + /** |
| 35 | + */ |
| 36 | + public static function setUpBeforeClass(): void |
| 37 | + { |
| 38 | + self::$testedClass = HexBinaryElement::class; |
| 39 | + |
| 40 | + self::$xmlRepresentation = DOMDocumentFactory::fromFile( |
| 41 | + dirname(__FILE__, 2) . '/resources/xml/ssp_HexBinaryElement.xml', |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + */ |
| 47 | + public function testMarshalling(): void |
| 48 | + { |
| 49 | + $hexBinaryElement = new HexBinaryElement( |
| 50 | + '3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f', |
| 51 | + ); |
| 52 | + |
| 53 | + $this->assertEquals( |
| 54 | + XMLDumper::dumpDOMDocumentXMLWithBase64Content(self::$xmlRepresentation), |
| 55 | + strval($hexBinaryElement), |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + /** |
| 61 | + */ |
| 62 | + public function testUnmarshalling(): void |
| 63 | + { |
| 64 | + /** @var \DOMElement $xml */ |
| 65 | + $xml = self::$xmlRepresentation->documentElement; |
| 66 | + $hexBinaryElement = HexBinaryElement::fromXML($xml); |
| 67 | + |
| 68 | + $this->assertEquals( |
| 69 | + '3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f', |
| 70 | + $hexBinaryElement->getContent(), |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + /** |
| 76 | + * @param non-empty-string $xml |
| 77 | + */ |
| 78 | + #[DataProvider('provideHexBinaryCases')] |
| 79 | + public function testHexBinaryCases(string $xml): void |
| 80 | + { |
| 81 | + $xmlRepresentation = DOMDocumentFactory::fromString($xml); |
| 82 | + /** @var \DOMElement $xmlElement */ |
| 83 | + $xmlElement = $xmlRepresentation->documentElement; |
| 84 | + |
| 85 | + $hexBinary = HexBinaryElement::fromXML($xmlElement); |
| 86 | + |
| 87 | + $this->assertStringContainsString($hexBinary->getRawContent(), $xml); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @return array<string, array{0: string}> |
| 92 | + */ |
| 93 | + public static function provideHexBinaryCases(): array |
| 94 | + { |
| 95 | + return [ |
| 96 | + 'inline' => [ |
| 97 | + <<<XML |
| 98 | +<ssp:HexBinaryElement xmlns:ssp="urn:x-simplesamlphp:namespace">3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f</ssp:HexBinaryElement> |
| 99 | +XML |
| 100 | + , |
| 101 | + ], |
| 102 | + 'multiline' => [ |
| 103 | + <<<XML |
| 104 | +<ssp:HexBinaryElement xmlns:ssp="urn:x-simplesamlphp:namespace"> |
| 105 | +3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f |
| 106 | +</ssp:HexBinaryElement> |
| 107 | +XML |
| 108 | + , |
| 109 | + ], |
| 110 | + ]; |
| 111 | + } |
| 112 | +} |
0 commit comments