From 5b0b27548ce5c5a7fffdceee23207598e7600d3e Mon Sep 17 00:00:00 2001 From: Arne Blankerts Date: Sun, 1 Nov 2020 00:47:48 +0100 Subject: [PATCH 1/2] Reimplement main aspect of assertEqualXMLStructure --- src/Framework/Assert.php | 15 +++++++++++++++ src/Framework/Assert/Functions.php | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/Framework/Assert.php b/src/Framework/Assert.php index 2e78802964e..bb32b9fba26 100644 --- a/src/Framework/Assert.php +++ b/src/Framework/Assert.php @@ -2251,6 +2251,21 @@ public static function assertXmlStringNotEqualsXmlString($expectedXml, $actualXm static::assertNotEquals($expected, $actual, $message); } + public static function assertDOMTreesEqualStructurally(DOMElement $expectedElement, DOMElement $actualElement, bool $keepComments = false, string $message = ''): void + { + $ed = new DOMDocument(); + $ed->preserveWhiteSpace = false; + $ed->loadXML($expectedElement->C14N(false, $keepComments)); + $ed->formatOutput = true; + + $ad = new DOMDocument(); + $ad->preserveWhiteSpace = false; + $ad->loadXML($actualElement->C14N(false, $keepComments)); + $ad->formatOutput = true; + + self::assertEquals($ed->documentElement, $ad->documentElement, $message); + } + /** * Asserts that a hierarchy of DOMElements matches. * diff --git a/src/Framework/Assert/Functions.php b/src/Framework/Assert/Functions.php index 9363f4f7bc9..ee1f93ff3ab 100644 --- a/src/Framework/Assert/Functions.php +++ b/src/Framework/Assert/Functions.php @@ -2422,6 +2422,23 @@ function assertXmlStringNotEqualsXmlString($expectedXml, $actualXml, string $mes } } +if (!function_exists('PHPUnit\Framework\assertDOMTreesEqualStructurally')) { + /** + * Asserts that two DOM trees are structurally identical. + * + * @throws AssertionFailedError + * @throws ExpectationFailedException + * + * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit + * + * @see Assert::assertDOMTreesEqualStructurally + */ + function assertDOMTreesEqualStructurally(DOMElement $expectedElement, DOMElement $actualElement, bool $keepComments = false, string $message = ''): void + { + Assert::assertDOMTreesEqualStructurally(...func_get_args()); + } +} + if (!function_exists('PHPUnit\Framework\assertEqualXMLStructure')) { /** * Asserts that a hierarchy of DOMElements matches. From 85d4c053a8d0f6019bb70979b2d7e43cbddd99a8 Mon Sep 17 00:00:00 2001 From: Arne Blankerts Date: Sun, 1 Nov 2020 22:07:18 +0100 Subject: [PATCH 2/2] FIX: C14N doesn't work for non-rooted elements --- src/Framework/Assert.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Framework/Assert.php b/src/Framework/Assert.php index bb32b9fba26..b08c0b8c845 100644 --- a/src/Framework/Assert.php +++ b/src/Framework/Assert.php @@ -2253,14 +2253,20 @@ public static function assertXmlStringNotEqualsXmlString($expectedXml, $actualXm public static function assertDOMTreesEqualStructurally(DOMElement $expectedElement, DOMElement $actualElement, bool $keepComments = false, string $message = ''): void { - $ed = new DOMDocument(); + $ed = new DOMDocument(); + $ed->appendChild($ed->importNode($expectedElement, true)); + $xmlStr = $ed->C14N(false, $keepComments); + $ed->preserveWhiteSpace = false; - $ed->loadXML($expectedElement->C14N(false, $keepComments)); + $ed->loadXML($xmlStr); $ed->formatOutput = true; - $ad = new DOMDocument(); + $ad = new DOMDocument(); + $ad->appendChild($ad->importNode($actualElement, true)); + $xmlStr = $ad->C14N(false, $keepComments); + $ad->preserveWhiteSpace = false; - $ad->loadXML($actualElement->C14N(false, $keepComments)); + $ad->loadXML($xmlStr); $ad->formatOutput = true; self::assertEquals($ed->documentElement, $ad->documentElement, $message);