From c49509200852c562368a4d831fcf0e98f85c646c Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Mon, 25 Dec 2023 12:03:11 +0100 Subject: [PATCH] Implement generic version of testUnmarshalling --- .../SerializableElementTestTrait.php | 38 ++++++++++++++++--- tests/XML/AbstractElementTest.php | 1 + tests/XML/Base64ElementTraitTest.php | 1 + tests/XML/ChunkTest.php | 1 + tests/XML/ExtendableAttributesTest.php | 13 ------- tests/XML/ExtendableElementTest.php | 13 ------- tests/XML/LocalizedStringElementTraitTest.php | 11 ------ tests/XML/QNameElementTraitTest.php | 11 ------ tests/XML/StringElementTraitTest.php | 10 ----- 9 files changed, 35 insertions(+), 64 deletions(-) diff --git a/src/TestUtils/SerializableElementTestTrait.php b/src/TestUtils/SerializableElementTestTrait.php index 4fb7d9c5..27736a14 100644 --- a/src/TestUtils/SerializableElementTestTrait.php +++ b/src/TestUtils/SerializableElementTestTrait.php @@ -22,6 +22,38 @@ trait SerializableElementTestTrait protected static DOMDocument $xmlRepresentation; + /** + * Test creating XML from a class. + */ + abstract public function testMarshalling(): void; + + + /** + * Test creating a class from XML. + */ + public function testUnmarshalling(): void + { + if (!class_exists(self::$testedClass)) { + $this->markTestSkipped( + 'Unable to run ' . self::class . '::testUnmarshalling(). Please set ' . self::class + . ':$testedClass to a class-string representing the XML-class being tested', + ); + } elseif (empty(self::$xmlRepresentation)) { + $this->markTestSkipped( + 'Unable to run ' . self::class . '::testUnmarshalling(). Please set ' . self::class + . ':$xmlRepresentation to a DOMDocument representing the XML-class being tested', + ); + } else { + $elt = self::$testedClass::fromXML(self::$xmlRepresentation->documentElement); + + $this->assertEquals( + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), + strval($elt), + ); + } + } + + /** * Test serialization / unserialization. * @@ -47,10 +79,4 @@ public function testSerialization(): void ); } } - - - abstract public function testMarshalling(): void; - - - abstract public function testUnmarshalling(): void; } diff --git a/tests/XML/AbstractElementTest.php b/tests/XML/AbstractElementTest.php index cf6a3300..97906ec0 100644 --- a/tests/XML/AbstractElementTest.php +++ b/tests/XML/AbstractElementTest.php @@ -54,6 +54,7 @@ public function testMarshalling(): void /** */ + #[Override] public function testUnmarshalling(): void { $element = Element::fromXML(self::$xmlRepresentation->documentElement); diff --git a/tests/XML/Base64ElementTraitTest.php b/tests/XML/Base64ElementTraitTest.php index 8b5e140c..0640fbe5 100644 --- a/tests/XML/Base64ElementTraitTest.php +++ b/tests/XML/Base64ElementTraitTest.php @@ -57,6 +57,7 @@ public function testMarshalling(): void /** */ + #[Override] public function testUnmarshalling(): void { $base64Element = Base64Element::fromXML(self::$xmlRepresentation->documentElement); diff --git a/tests/XML/ChunkTest.php b/tests/XML/ChunkTest.php index 97fd7f68..153c78ff 100644 --- a/tests/XML/ChunkTest.php +++ b/tests/XML/ChunkTest.php @@ -53,6 +53,7 @@ public function testMarshalling(): void /** */ + #[Override] public function testUnmarshalling(): void { $element = Chunk::fromXML(self::$xmlRepresentation->documentElement); diff --git a/tests/XML/ExtendableAttributesTest.php b/tests/XML/ExtendableAttributesTest.php index 833d174b..2cfe8205 100644 --- a/tests/XML/ExtendableAttributesTest.php +++ b/tests/XML/ExtendableAttributesTest.php @@ -57,17 +57,4 @@ public function testMarshalling(): void strval($extendableElement), ); } - - - /** - */ - public function testUnmarshalling(): void - { - $extendableElement = ExtendableAttributesElement::fromXML(self::$xmlRepresentation->documentElement); - - $this->assertEquals( - self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), - strval($extendableElement), - ); - } } diff --git a/tests/XML/ExtendableElementTest.php b/tests/XML/ExtendableElementTest.php index 73f32b94..8e5238c9 100644 --- a/tests/XML/ExtendableElementTest.php +++ b/tests/XML/ExtendableElementTest.php @@ -58,17 +58,4 @@ public function testMarshalling(): void strval($extendableElement), ); } - - - /** - */ - public function testUnmarshalling(): void - { - $extendableElement = ExtendableElement::fromXML(self::$xmlRepresentation->documentElement); - - $this->assertEquals( - self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), - strval($extendableElement), - ); - } } diff --git a/tests/XML/LocalizedStringElementTraitTest.php b/tests/XML/LocalizedStringElementTraitTest.php index c7bba0d2..968b8e5c 100644 --- a/tests/XML/LocalizedStringElementTraitTest.php +++ b/tests/XML/LocalizedStringElementTraitTest.php @@ -50,15 +50,4 @@ public function testMarshalling(): void strval($localizedStringElement), ); } - - - /** - */ - public function testUnmarshalling(): void - { - $localizedStringElement = LocalizedStringElement::fromXML(self::$xmlRepresentation->documentElement); - - $this->assertEquals('en', $localizedStringElement->getLanguage()); - $this->assertEquals('test', $localizedStringElement->getContent()); - } } diff --git a/tests/XML/QNameElementTraitTest.php b/tests/XML/QNameElementTraitTest.php index 96075659..35f2fa2c 100644 --- a/tests/XML/QNameElementTraitTest.php +++ b/tests/XML/QNameElementTraitTest.php @@ -76,17 +76,6 @@ public function testMarshallingInvalidQualifiedNameThrowsException(): void } - /** - */ - public function testUnmarshalling(): void - { - $qnameElement = QNameElement::fromXML(self::$xmlRepresentation->documentElement); - - $this->assertEquals('env:Sender', $qnameElement->getContent()); - $this->assertEquals('http://www.w3.org/2003/05/soap-envelope', $qnameElement->getContentNamespaceUri()); - } - - /** */ public function testUnmarshallingNonNamepacedQualifiedName(): void diff --git a/tests/XML/StringElementTraitTest.php b/tests/XML/StringElementTraitTest.php index e8c17deb..14893ab3 100644 --- a/tests/XML/StringElementTraitTest.php +++ b/tests/XML/StringElementTraitTest.php @@ -50,14 +50,4 @@ public function testMarshalling(): void strval($stringElement), ); } - - - /** - */ - public function testUnmarshalling(): void - { - $stringElement = StringElement::fromXML(self::$xmlRepresentation->documentElement); - - $this->assertEquals('test', $stringElement->getContent()); - } }