assertEqualXMLStructure()
assertEqualXMLStructure(DOMElement $expectedElement, DOMElement $actualElement[, boolean $checkAttributes = FALSE, string $message = ''])
当 $actualElement
中 DOMElement 的 XML 结构与 $expectedElement
中 DOMElement的 XML 结构不相同时报告错误,错误讯息由 $message
指定。
例 A.12: assertEqualXMLStructure() 的用法
- <?php
- class EqualXMLStructureTest extends PHPUnit_Framework_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 4.8.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.