assertEqualXMLStructure()


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

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


例 A.15: assertEqualXMLStructure() 的用法

  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3.  
  4. class EqualXMLStructureTest extends TestCase
  5. {
  6. public function testFailureWithDifferentNodeNames()
  7. {
  8. $expected = new DOMElement('foo');
  9. $actual = new DOMElement('bar');
  10.  
  11. $this->assertEqualXMLStructure($expected, $actual);
  12. }
  13.  
  14. public function testFailureWithDifferentNodeAttributes()
  15. {
  16. $expected = new DOMDocument;
  17. $expected->loadXML('<foo bar="true" />');
  18.  
  19. $actual = new DOMDocument;
  20. $actual->loadXML('<foo/>');
  21.  
  22. $this->assertEqualXMLStructure(
  23. $expected->firstChild, $actual->firstChild, true
  24. );
  25. }
  26.  
  27. public function testFailureWithDifferentChildrenCount()
  28. {
  29. $expected = new DOMDocument;
  30. $expected->loadXML('<foo><bar/><bar/><bar/></foo>');
  31.  
  32. $actual = new DOMDocument;
  33. $actual->loadXML('<foo><bar/></foo>');
  34.  
  35. $this->assertEqualXMLStructure(
  36. $expected->firstChild, $actual->firstChild
  37. );
  38. }
  39.  
  40. public function testFailureWithDifferentChildren()
  41. {
  42. $expected = new DOMDocument;
  43. $expected->loadXML('<foo><bar/><bar/><bar/></foo>');
  44.  
  45. $actual = new DOMDocument;
  46. $actual->loadXML('<foo><baz/><baz/><baz/></foo>');
  47.  
  48. $this->assertEqualXMLStructure(
  49. $expected->firstChild, $actual->firstChild
  50. );
  51. }
  52. }
  53. ?>
  1. phpunit EqualXMLStructureTest
  2. PHPUnit 6.5.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.