3 * This file is part of the Comparator package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace SebastianBergmann\Comparator;
17 * Compares DOMNode instances for equality.
19 class DOMNodeComparator extends ObjectComparator
22 * Returns whether the comparator can compare two values.
24 * @param mixed $expected The first value to compare
25 * @param mixed $actual The second value to compare
28 public function accepts($expected, $actual)
30 return $expected instanceof DOMNode && $actual instanceof DOMNode;
34 * Asserts that two values are equal.
36 * @param mixed $expected First value to compare
37 * @param mixed $actual Second value to compare
38 * @param float $delta Allowed numerical distance between two values to consider them equal
39 * @param bool $canonicalize Arrays are sorted before comparison when set to true
40 * @param bool $ignoreCase Case is ignored when set to true
41 * @param array $processed List of already processed elements (used to prevent infinite recursion)
43 * @throws ComparisonFailure
45 public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = array())
47 $expectedAsString = $this->nodeToText($expected, true, $ignoreCase);
48 $actualAsString = $this->nodeToText($actual, true, $ignoreCase);
50 if ($expectedAsString !== $actualAsString) {
51 if ($expected instanceof DOMDocument) {
57 throw new ComparisonFailure(
63 sprintf("Failed asserting that two DOM %s are equal.\n", $type)
69 * Returns the normalized, whitespace-cleaned, and indented textual
70 * representation of a DOMNode.
72 * @param DOMNode $node
73 * @param bool $canonicalize
74 * @param bool $ignoreCase
77 private function nodeToText(DOMNode $node, $canonicalize, $ignoreCase)
80 $document = new DOMDocument;
81 $document->loadXML($node->C14N());
86 if ($node instanceof DOMDocument) {
89 $document = $node->ownerDocument;
92 $document->formatOutput = true;
93 $document->normalizeDocument();
95 if ($node instanceof DOMDocument) {
96 $text = $node->saveXML();
98 $text = $document->saveXML($node);
102 $text = strtolower($text);