DOMNodeComparatorTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /*
  3. * This file is part of sebastian/comparator.
  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 DOMDocument;
  12. use DOMNode;
  13. use PHPUnit\Framework\TestCase;
  14. /**
  15. * @covers \SebastianBergmann\Comparator\DOMNodeComparator<extended>
  16. *
  17. * @uses \SebastianBergmann\Comparator\Comparator
  18. * @uses \SebastianBergmann\Comparator\Factory
  19. * @uses \SebastianBergmann\Comparator\ComparisonFailure
  20. */
  21. final class DOMNodeComparatorTest extends TestCase
  22. {
  23. /**
  24. * @var DOMNodeComparator
  25. */
  26. private $comparator;
  27. protected function setUp(): void
  28. {
  29. $this->comparator = new DOMNodeComparator;
  30. }
  31. public function acceptsSucceedsProvider()
  32. {
  33. $document = new DOMDocument;
  34. $node = new DOMNode;
  35. return [
  36. [$document, $document],
  37. [$node, $node],
  38. [$document, $node],
  39. [$node, $document]
  40. ];
  41. }
  42. public function acceptsFailsProvider()
  43. {
  44. $document = new DOMDocument;
  45. return [
  46. [$document, null],
  47. [null, $document],
  48. [null, null]
  49. ];
  50. }
  51. public function assertEqualsSucceedsProvider()
  52. {
  53. return [
  54. [
  55. $this->createDOMDocument('<root></root>'),
  56. $this->createDOMDocument('<root/>')
  57. ],
  58. [
  59. $this->createDOMDocument('<root attr="bar"></root>'),
  60. $this->createDOMDocument('<root attr="bar"/>')
  61. ],
  62. [
  63. $this->createDOMDocument('<root><foo attr="bar"></foo></root>'),
  64. $this->createDOMDocument('<root><foo attr="bar"/></root>')
  65. ],
  66. [
  67. $this->createDOMDocument("<root>\n <child/>\n</root>"),
  68. $this->createDOMDocument('<root><child/></root>')
  69. ],
  70. [
  71. $this->createDOMDocument('<Root></Root>'),
  72. $this->createDOMDocument('<root></root>'),
  73. $ignoreCase = true
  74. ],
  75. [
  76. $this->createDOMDocument("<a x='' a=''/>"),
  77. $this->createDOMDocument("<a a='' x=''/>"),
  78. ],
  79. ];
  80. }
  81. public function assertEqualsFailsProvider()
  82. {
  83. return [
  84. [
  85. $this->createDOMDocument('<root></root>'),
  86. $this->createDOMDocument('<bar/>')
  87. ],
  88. [
  89. $this->createDOMDocument('<foo attr1="bar"/>'),
  90. $this->createDOMDocument('<foo attr1="foobar"/>')
  91. ],
  92. [
  93. $this->createDOMDocument('<foo> bar </foo>'),
  94. $this->createDOMDocument('<foo />')
  95. ],
  96. [
  97. $this->createDOMDocument('<foo xmlns="urn:myns:bar"/>'),
  98. $this->createDOMDocument('<foo xmlns="urn:notmyns:bar"/>')
  99. ],
  100. [
  101. $this->createDOMDocument('<foo> bar </foo>'),
  102. $this->createDOMDocument('<foo> bir </foo>')
  103. ],
  104. [
  105. $this->createDOMDocument('<Root></Root>'),
  106. $this->createDOMDocument('<root></root>')
  107. ],
  108. [
  109. $this->createDOMDocument('<root> bar </root>'),
  110. $this->createDOMDocument('<root> BAR </root>')
  111. ]
  112. ];
  113. }
  114. /**
  115. * @dataProvider acceptsSucceedsProvider
  116. */
  117. public function testAcceptsSucceeds($expected, $actual): void
  118. {
  119. $this->assertTrue(
  120. $this->comparator->accepts($expected, $actual)
  121. );
  122. }
  123. /**
  124. * @dataProvider acceptsFailsProvider
  125. */
  126. public function testAcceptsFails($expected, $actual): void
  127. {
  128. $this->assertFalse(
  129. $this->comparator->accepts($expected, $actual)
  130. );
  131. }
  132. /**
  133. * @dataProvider assertEqualsSucceedsProvider
  134. */
  135. public function testAssertEqualsSucceeds($expected, $actual, $ignoreCase = false): void
  136. {
  137. $exception = null;
  138. try {
  139. $delta = 0.0;
  140. $canonicalize = false;
  141. $this->comparator->assertEquals($expected, $actual, $delta, $canonicalize, $ignoreCase);
  142. } catch (ComparisonFailure $exception) {
  143. }
  144. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  145. }
  146. /**
  147. * @dataProvider assertEqualsFailsProvider
  148. */
  149. public function testAssertEqualsFails($expected, $actual): void
  150. {
  151. $this->expectException(ComparisonFailure::class);
  152. $this->expectExceptionMessage('Failed asserting that two DOM');
  153. $this->comparator->assertEquals($expected, $actual);
  154. }
  155. private function createDOMDocument($content)
  156. {
  157. $document = new DOMDocument;
  158. $document->preserveWhiteSpace = false;
  159. $document->loadXML($content);
  160. return $document;
  161. }
  162. }