diff --git a/src/DOMNodeComparator.php b/src/DOMNodeComparator.php index a19b6ee..9cc706b 100644 --- a/src/DOMNodeComparator.php +++ b/src/DOMNodeComparator.php @@ -37,7 +37,7 @@ public function accepts($expected, $actual) * @param mixed $expected First value to compare * @param mixed $actual Second value to compare * @param float $delta Allowed numerical distance between two values to consider them equal - * @param bool $canonicalize Arrays are sorted before comparison when set to true + * @param bool $canonicalize The value of this argument ignored and always considered as true * @param bool $ignoreCase Case is ignored when set to true * @param array $processed List of already processed elements (used to prevent infinite recursion) * @@ -45,8 +45,8 @@ public function accepts($expected, $actual) */ public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = []) { - $expectedAsString = $this->nodeToText($expected, true, $ignoreCase); - $actualAsString = $this->nodeToText($actual, true, $ignoreCase); + $expectedAsString = $this->nodeToText($expected, $ignoreCase); + $actualAsString = $this->nodeToText($actual, $ignoreCase); if ($expectedAsString !== $actualAsString) { $type = $expected instanceof DOMDocument ? 'documents' : 'nodes'; @@ -63,24 +63,28 @@ public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = f } /** - * Returns the normalized, whitespace-cleaned, and indented textual - * representation of a DOMNode. + * Returns the normalized, whitespace-cleaned, and indented textual representation of a DOMNode. + * + * @param DOMNode $node + * @param bool $ignoreCase If true - whole xml (tags and inner text) will be converted to lowercase during + * comparison + * + * @return string Text representation of DOMNode */ - private function nodeToText(DOMNode $node, bool $canonicalize, bool $ignoreCase): string + private function nodeToText(DOMNode $node, bool $ignoreCase): string { - if ($canonicalize) { - $document = new DOMDocument; - @$document->loadXML($node->C14N()); + $document = new DOMDocument(); - $node = $document; - } + $nodeString = $node->C14N(); - $document = $node instanceof DOMDocument ? $node : $node->ownerDocument; + // If an empty string is passed as the source, a warning will be generated. We prevent it here. + if ($nodeString !== "") { + $document->loadXML($nodeString); + } - $document->formatOutput = true; - $document->normalizeDocument(); + $node = $document; - $text = $node instanceof DOMDocument ? $node->saveXML() : $document->saveXML($node); + $text = $node->saveXML(); return $ignoreCase ? \strtolower($text) : $text; }