DOMNodeComparatorTest.php 4.2 KB

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