diff --git a/src/Framework/Assert.php b/src/Framework/Assert.php index 2e78802964e..b08c0b8c845 100644 --- a/src/Framework/Assert.php +++ b/src/Framework/Assert.php @@ -2251,6 +2251,27 @@ 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->appendChild($ed->importNode($expectedElement, true)); + $xmlStr = $ed->C14N(false, $keepComments); + + $ed->preserveWhiteSpace = false; + $ed->loadXML($xmlStr); + $ed->formatOutput = true; + + $ad = new DOMDocument(); + $ad->appendChild($ad->importNode($actualElement, true)); + $xmlStr = $ad->C14N(false, $keepComments); + + $ad->preserveWhiteSpace = false; + $ad->loadXML($xmlStr); + $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.