assertEqualXMLStructure()


assertEqualXMLStructure(DOMElement $expectedElement, DOMElement $actualElement[, boolean $checkAttributes = FALSE, string $message = ''])

$actualElement 中 DOMElement 的 XML 结构与 $expectedElement 中 DOMElement的 XML 结构不相同时报告错误,错误讯息由 $message 指定。


例 A.12: assertEqualXMLStructure() 的用法

  1. <?php
  2. class EqualXMLStructureTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function testFailureWithDifferentNodeNames()
  5. {
  6. $expected = new DOMElement('foo');
  7. $actual = new DOMElement('bar');
  8.  
  9. $this->assertEqualXMLStructure($expected, $actual);
  10. }
  11.  
  12. public function testFailureWithDifferentNodeAttributes()
  13. {
  14. $expected = new DOMDocument;
  15. $expected->loadXML('<foo bar="true" />');
  16.  
  17. $actual = new DOMDocument;
  18. $actual->loadXML('<foo/>');
  19.  
  20. $this->assertEqualXMLStructure(
  21. $expected->firstChild, $actual->firstChild, TRUE
  22. );
  23. }
  24.  
  25. public function testFailureWithDifferentChildrenCount()
  26. {
  27. $expected = new DOMDocument;
  28. $expected->loadXML('<foo><bar/><bar/><bar/></foo>');
  29.  
  30. $actual = new DOMDocument;
  31. $actual->loadXML('<foo><bar/></foo>');
  32.  
  33. $this->assertEqualXMLStructure(
  34. $expected->firstChild, $actual->firstChild
  35. );
  36. }
  37.  
  38. public function testFailureWithDifferentChildren()
  39. {
  40. $expected = new DOMDocument;
  41. $expected->loadXML('<foo><bar/><bar/><bar/></foo>');
  42.  
  43. $actual = new DOMDocument;
  44. $actual->loadXML('<foo><baz/><baz/><baz/></foo>');
  45.  
  46. $this->assertEqualXMLStructure(
  47. $expected->firstChild, $actual->firstChild
  48. );
  49. }
  50. }
  51. ?>
  1. phpunit EqualXMLStructureTest
  2. PHPUnit 4.8.0 by Sebastian Bergmann and contributors.
  3.  
  4. FFFF
  5.  
  6. Time: 0 seconds, Memory: 5.75Mb
  7.  
  8. There were 4 failures:
  9.  
  10. 1) EqualXMLStructureTest::testFailureWithDifferentNodeNames
  11. Failed asserting that two strings are equal.
  12. --- Expected
  13. +++ Actual
  14. @@ @@
  15. -'foo'
  16. +'bar'
  17.  
  18. /home/sb/EqualXMLStructureTest.php:9
  19.  
  20. 2) EqualXMLStructureTest::testFailureWithDifferentNodeAttributes
  21. Number of attributes on node "foo" does not match
  22. Failed asserting that 0 matches expected 1.
  23.  
  24. /home/sb/EqualXMLStructureTest.php:22
  25.  
  26. 3) EqualXMLStructureTest::testFailureWithDifferentChildrenCount
  27. Number of child nodes of "foo" differs
  28. Failed asserting that 1 matches expected 3.
  29.  
  30. /home/sb/EqualXMLStructureTest.php:35
  31.  
  32. 4) EqualXMLStructureTest::testFailureWithDifferentChildren
  33. Failed asserting that two strings are equal.
  34. --- Expected
  35. +++ Actual
  36. @@ @@
  37. -'bar'
  38. +'baz'
  39.  
  40. /home/sb/EqualXMLStructureTest.php:48
  41.  
  42. FAILURES!
  43. Tests: 4, Assertions: 8, Failures: 4.