Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOMNodeComparator::nodeToText Refactoring #56

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions src/DOMNodeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ 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)
*
* @throws ComparisonFailure
*/
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';
Expand All @@ -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;
}
Expand Down