What version of PHPUnit is this? I am sure the latest versions support DomDocument comparison.
Short version: use the $doc->preserveWhiteSpace
option to remove spaces, and then use $doc->C14N()
to strip the comments and get a line that you can compare.
OK, here is a script that you can play with, note that the lines are EOD;
cannot have any trailing or leading spaces.
$x1 = <<<EOD <responses> <response id="12"> <foo>bar</foo> <lorem>ipsum</lorem> <sit>dolor</sit> <!--This is a comment --> </response></responses> EOD; $x2 = <<<EOD <responses> <response id="12"> <lorem>ipsum</lorem><sit>dolor</sit> <foo>bar</foo> <!--This is another comment --> </response> </responses> EOD;
// The next block is part of the same file, I just make this formatting gap so that the StackOverflow syntax highlighting system does not suffocate.
$USE_C14N = true; // Try false, just to see the difference. $d1 = new DOMDocument(1.0); $d2 = new DOMDocument(1.0); $d1->preserveWhiteSpace = false; $d2->preserveWhiteSpace = false; $d1->formatOutput = false; // Only useful for "pretty" output with saveXML() $d2->formatOutput = false; // Only useful for "pretty" output with saveXML() $d1->loadXML($x1); // Must be done AFTER preserveWhiteSpace and formatOutput are set $d2->loadXML($x2); // Must be done AFTER preserveWhiteSpace and formatOutput are set if($USE_C14N){ $s1 = $d1->C14N(true, false); $s2 = $d2->C14N(true, false); } else { $s1 = $d1->saveXML(); $s2 = $d2->saveXML(); } echo $s1 . "\n"; echo $s2 . "\n";
Exit with $USE_C14N=true;
<responses><response id="12"><foo>bar</foo><lorem>ipsum</lorem><sit>dolor</sit></response></responses> <responses><response id="12"><lorem>ipsum</lorem><sit>dolor</sit><foo>bar</foo></response></responses>
Exit with $USE_C14N=false;
<?xml version="1.0"?> <responses><response id="12"><foo>bar</foo><lorem>ipsum</lorem><sit>dolor</sit></response></responses> <?xml version="1.0"?> <responses><response id="12"><lorem>ipsum</lorem><sit>dolor</sit><foo>bar</foo></response></responses>
Note that $doc->C14N()
may be slower, but I think it seems likely that it is advisable to remove comments. Note that all of this also assumes that spaces in your XML are not important, and there are probably some use cases when this assumption is wrong ...