Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare similar XMLs with PHPUnit?

So let's say I want to compare two DOMDocument objects. They have the same content but order and formatting might be off. For example, first one outputs this XML:

<responses>
    <response id="12">
        <foo>bar</foo>


 <lorem>ipsum</lorem>
           <sit>dolor</sit>

    </response></responses>

Other one outputs:

<responses>
<response id="12">

            <lorem>ipsum</lorem><sit>dolor</sit>
        <foo>bar</foo>
                            </response>
</responses>

As you can see, they contain the same XML structure but some elements might be in different order and formatting is completely random.

If I do:

$this->assertEquals();

The test will of course fail. I don't want to test just XML structure but also contents.

Any ideas?

like image 866
Richard Knop Avatar asked Apr 25 '12 16:04

Richard Knop


1 Answers

This seems to have solved the problem:

https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertXmlStringEqualsXmlString

like image 145
Richard Knop Avatar answered Sep 30 '22 21:09

Richard Knop