No Description

DOMNodeComparatorTest.php 4.4KB

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