exceptions
1 year ago
ArrayComparator.php
1 year ago
Comparator.php
1 year ago
ComparisonFailure.php
1 year ago
DOMNodeComparator.php
1 year ago
DateTimeComparator.php
1 year ago
DoubleComparator.php
1 year ago
ExceptionComparator.php
1 year ago
Factory.php
1 year ago
MockObjectComparator.php
1 year ago
NumericComparator.php
1 year ago
ObjectComparator.php
1 year ago
ResourceComparator.php
1 year ago
ScalarComparator.php
1 year ago
SplObjectStorageComparator.php
1 year ago
TypeComparator.php
1 year ago
DOMNodeComparator.php
94 lines
| 1 | <?php declare(strict_types=1); |
| 2 | /* |
| 3 | * This file is part of sebastian/comparator. |
| 4 | * |
| 5 | * (c) Sebastian Bergmann <sebastian@phpunit.de> |
| 6 | * |
| 7 | * For the full copyright and license information, please view the LICENSE |
| 8 | * file that was distributed with this source code. |
| 9 | */ |
| 10 | namespace SebastianBergmann\Comparator; |
| 11 | |
| 12 | use function sprintf; |
| 13 | use function strtolower; |
| 14 | use DOMDocument; |
| 15 | use DOMNode; |
| 16 | use ValueError; |
| 17 | |
| 18 | /** |
| 19 | * Compares DOMNode instances for equality. |
| 20 | */ |
| 21 | class DOMNodeComparator extends ObjectComparator |
| 22 | { |
| 23 | /** |
| 24 | * Returns whether the comparator can compare two values. |
| 25 | * |
| 26 | * @param mixed $expected The first value to compare |
| 27 | * @param mixed $actual The second value to compare |
| 28 | * |
| 29 | * @return bool |
| 30 | */ |
| 31 | public function accepts($expected, $actual) |
| 32 | { |
| 33 | return $expected instanceof DOMNode && $actual instanceof DOMNode; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Asserts that two values are equal. |
| 38 | * |
| 39 | * @param mixed $expected First value to compare |
| 40 | * @param mixed $actual Second value to compare |
| 41 | * @param float $delta Allowed numerical distance between two values to consider them equal |
| 42 | * @param bool $canonicalize Arrays are sorted before comparison when set to true |
| 43 | * @param bool $ignoreCase Case is ignored when set to true |
| 44 | * @param array $processed List of already processed elements (used to prevent infinite recursion) |
| 45 | * |
| 46 | * @throws ComparisonFailure |
| 47 | */ |
| 48 | public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = [])/*: void*/ |
| 49 | { |
| 50 | $expectedAsString = $this->nodeToText($expected, true, $ignoreCase); |
| 51 | $actualAsString = $this->nodeToText($actual, true, $ignoreCase); |
| 52 | |
| 53 | if ($expectedAsString !== $actualAsString) { |
| 54 | $type = $expected instanceof DOMDocument ? 'documents' : 'nodes'; |
| 55 | |
| 56 | throw new ComparisonFailure( |
| 57 | $expected, |
| 58 | $actual, |
| 59 | $expectedAsString, |
| 60 | $actualAsString, |
| 61 | false, |
| 62 | sprintf("Failed asserting that two DOM %s are equal.\n", $type) |
| 63 | ); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the normalized, whitespace-cleaned, and indented textual |
| 69 | * representation of a DOMNode. |
| 70 | */ |
| 71 | private function nodeToText(DOMNode $node, bool $canonicalize, bool $ignoreCase): string |
| 72 | { |
| 73 | if ($canonicalize) { |
| 74 | $document = new DOMDocument; |
| 75 | |
| 76 | try { |
| 77 | @$document->loadXML($node->C14N()); |
| 78 | } catch (ValueError $e) { |
| 79 | } |
| 80 | |
| 81 | $node = $document; |
| 82 | } |
| 83 | |
| 84 | $document = $node instanceof DOMDocument ? $node : $node->ownerDocument; |
| 85 | |
| 86 | $document->formatOutput = true; |
| 87 | $document->normalizeDocument(); |
| 88 | |
| 89 | $text = $node instanceof DOMDocument ? $node->saveXML() : $document->saveXML($node); |
| 90 | |
| 91 | return $ignoreCase ? strtolower($text) : $text; |
| 92 | } |
| 93 | } |
| 94 |