assertEqualXMLStructure()
assertEqualXMLStructure(DOMElement $expectedElement, DOMElement $actualElement[, boolean $checkAttributes = false, string $message = ''])
当 $actualElement
中 DOMElement 的 XML 结构与 $expectedElement
中 DOMElement的 XML 结构不相同时报告错误,错误讯息由 $message
指定。
例 A.15: assertEqualXMLStructure() 的用法
- <?php
- use PHPUnit\Framework\TestCase;
- class EqualXMLStructureTest extends TestCase
- {
- public function testFailureWithDifferentNodeNames()
- {
- $expected = new DOMElement('foo');
- $actual = new DOMElement('bar');
- $this->assertEqualXMLStructure($expected, $actual);
- }
- public function testFailureWithDifferentNodeAttributes()
- {
- $expected = new DOMDocument;
- $expected->loadXML('<foo bar="true" />');
- $actual = new DOMDocument;
- $actual->loadXML('<foo/>');
- $this->assertEqualXMLStructure(
- $expected->firstChild, $actual->firstChild, true
- );
- }
- public function testFailureWithDifferentChildrenCount()
- {
- $expected = new DOMDocument;
- $expected->loadXML('<foo><bar/><bar/><bar/></foo>');
- $actual = new DOMDocument;
- $actual->loadXML('<foo><bar/></foo>');
- $this->assertEqualXMLStructure(
- $expected->firstChild, $actual->firstChild
- );
- }
- public function testFailureWithDifferentChildren()
- {
- $expected = new DOMDocument;
- $expected->loadXML('<foo><bar/><bar/><bar/></foo>');
- $actual = new DOMDocument;
- $actual->loadXML('<foo><baz/><baz/><baz/></foo>');
- $this->assertEqualXMLStructure(
- $expected->firstChild, $actual->firstChild
- );
- }
- }
- ?>
phpunit EqualXMLStructureTest
- PHPUnit 6.5.0 by Sebastian Bergmann and contributors.
- FFFF
- Time: 0 seconds, Memory: 5.75Mb
- There were 4 failures:
- 1) EqualXMLStructureTest::testFailureWithDifferentNodeNames
- Failed asserting that two strings are equal.
- --- Expected
- +++ Actual
- @@ @@
- -'foo'
- +'bar'
- /home/sb/EqualXMLStructureTest.php:9
- 2) EqualXMLStructureTest::testFailureWithDifferentNodeAttributes
- Number of attributes on node "foo" does not match
- Failed asserting that 0 matches expected 1.
- /home/sb/EqualXMLStructureTest.php:22
- 3) EqualXMLStructureTest::testFailureWithDifferentChildrenCount
- Number of child nodes of "foo" differs
- Failed asserting that 1 matches expected 3.
- /home/sb/EqualXMLStructureTest.php:35
- 4) EqualXMLStructureTest::testFailureWithDifferentChildren
- Failed asserting that two strings are equal.
- --- Expected
- +++ Actual
- @@ @@
- -'bar'
- +'baz'
- /home/sb/EqualXMLStructureTest.php:48
- FAILURES!
- Tests: 4, Assertions: 8, Failures: 4.