DOMNodeComparatorTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /*
  3. * This file is part of the Comparator package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Comparator;
  11. use DOMNode;
  12. use DOMDocument;
  13. /**
  14. * @coversDefaultClass SebastianBergmann\Comparator\DOMNodeComparator
  15. *
  16. */
  17. class DOMNodeComparatorTest extends \PHPUnit_Framework_TestCase
  18. {
  19. private $comparator;
  20. protected function setUp()
  21. {
  22. $this->comparator = new DOMNodeComparator;
  23. }
  24. public function acceptsSucceedsProvider()
  25. {
  26. $document = new DOMDocument;
  27. $node = new DOMNode;
  28. return array(
  29. array($document, $document),
  30. array($node, $node),
  31. array($document, $node),
  32. array($node, $document)
  33. );
  34. }
  35. public function acceptsFailsProvider()
  36. {
  37. $document = new DOMDocument;
  38. return array(
  39. array($document, null),
  40. array(null, $document),
  41. array(null, null)
  42. );
  43. }
  44. public function assertEqualsSucceedsProvider()
  45. {
  46. return array(
  47. array(
  48. $this->createDOMDocument('<root></root>'),
  49. $this->createDOMDocument('<root/>')
  50. ),
  51. array(
  52. $this->createDOMDocument('<root attr="bar"></root>'),
  53. $this->createDOMDocument('<root attr="bar"/>')
  54. ),
  55. array(
  56. $this->createDOMDocument('<root><foo attr="bar"></foo></root>'),
  57. $this->createDOMDocument('<root><foo attr="bar"/></root>')
  58. ),
  59. array(
  60. $this->createDOMDocument("<root>\n <child/>\n</root>"),
  61. $this->createDOMDocument('<root><child/></root>')
  62. ),
  63. );
  64. }
  65. public function assertEqualsFailsProvider()
  66. {
  67. return array(
  68. array(
  69. $this->createDOMDocument('<root></root>'),
  70. $this->createDOMDocument('<bar/>')
  71. ),
  72. array(
  73. $this->createDOMDocument('<foo attr1="bar"/>'),
  74. $this->createDOMDocument('<foo attr1="foobar"/>')
  75. ),
  76. array(
  77. $this->createDOMDocument('<foo> bar </foo>'),
  78. $this->createDOMDocument('<foo />')
  79. ),
  80. array(
  81. $this->createDOMDocument('<foo xmlns="urn:myns:bar"/>'),
  82. $this->createDOMDocument('<foo xmlns="urn:notmyns:bar"/>')
  83. ),
  84. array(
  85. $this->createDOMDocument('<foo> bar </foo>'),
  86. $this->createDOMDocument('<foo> bir </foo>')
  87. )
  88. );
  89. }
  90. private function createDOMDocument($content)
  91. {
  92. $document = new DOMDocument;
  93. $document->preserveWhiteSpace = false;
  94. $document->loadXML($content);
  95. return $document;
  96. }
  97. /**
  98. * @covers ::accepts
  99. * @dataProvider acceptsSucceedsProvider
  100. */
  101. public function testAcceptsSucceeds($expected, $actual)
  102. {
  103. $this->assertTrue(
  104. $this->comparator->accepts($expected, $actual)
  105. );
  106. }
  107. /**
  108. * @covers ::accepts
  109. * @dataProvider acceptsFailsProvider
  110. */
  111. public function testAcceptsFails($expected, $actual)
  112. {
  113. $this->assertFalse(
  114. $this->comparator->accepts($expected, $actual)
  115. );
  116. }
  117. /**
  118. * @covers ::assertEquals
  119. * @dataProvider assertEqualsSucceedsProvider
  120. */
  121. public function testAssertEqualsSucceeds($expected, $actual)
  122. {
  123. $exception = null;
  124. try {
  125. $this->comparator->assertEquals($expected, $actual);
  126. }
  127. catch (ComparisonFailure $exception) {
  128. }
  129. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  130. }
  131. /**
  132. * @covers ::assertEquals
  133. * @dataProvider assertEqualsFailsProvider
  134. */
  135. public function testAssertEqualsFails($expected, $actual)
  136. {
  137. $this->setExpectedException(
  138. 'SebastianBergmann\\Comparator\\ComparisonFailure',
  139. 'Failed asserting that two DOM'
  140. );
  141. $this->comparator->assertEquals($expected, $actual);
  142. }
  143. }