NodeTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the league/commonmark package.
  5. *
  6. * (c) Colin O'Dell <colinodell@gmail.com>
  7. *
  8. * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
  9. * - (c) John MacFarlane
  10. *
  11. * For the full copyright and license information, please view the LICENSE
  12. * file that was distributed with this source code.
  13. */
  14. namespace League\CommonMark\Tests\Unit\Node;
  15. use PHPUnit\Framework\TestCase;
  16. final class NodeTest extends TestCase
  17. {
  18. public function testInsertBeforeElementWhichDoesNotHaveAPreviousOne(): void
  19. {
  20. $root = new SimpleNode();
  21. $root->appendChild($targetNode = new SimpleNode());
  22. $newNode = new SimpleNode();
  23. $targetNode->insertBefore($newNode);
  24. $this->assertNull($newNode->previous());
  25. $this->assertSame($targetNode, $newNode->next());
  26. $this->assertSame($newNode, $targetNode->previous());
  27. }
  28. public function testInsertBeforeElementWhichAlreadyHasPrevious(): void
  29. {
  30. $root = new SimpleNode();
  31. $root->appendChild($firstNode = new SimpleNode());
  32. $root->appendChild($targetNode = new SimpleNode());
  33. $newNode = new SimpleNode();
  34. $targetNode->insertBefore($newNode);
  35. $this->assertSame($firstNode, $newNode->previous());
  36. $this->assertSame($targetNode, $newNode->next());
  37. $this->assertSame($newNode, $targetNode->previous());
  38. }
  39. public function testPrependChildToChildlessParent(): void
  40. {
  41. $root = new SimpleNode();
  42. $root->prependChild($newNode = new SimpleNode());
  43. $this->assertSame($newNode, $root->firstChild());
  44. $this->assertSame($root, $newNode->parent());
  45. $this->assertNull($newNode->previous());
  46. }
  47. public function testPrependChildToParentWhichAlreadyHasChildren(): void
  48. {
  49. $root = new SimpleNode();
  50. $root->prependChild($existingChild = new SimpleNode());
  51. $this->assertSame($existingChild, $root->firstChild());
  52. $newNode = new SimpleNode();
  53. $root->prependChild($newNode);
  54. $this->assertCount(2, $root->children());
  55. $this->assertSame($newNode, $root->firstChild());
  56. $this->assertSame($root, $newNode->parent());
  57. $this->assertNull($newNode->previous());
  58. $this->assertSame($existingChild, $newNode->next());
  59. $this->assertSame($newNode, $existingChild->previous());
  60. }
  61. public function testDetachChildren(): void
  62. {
  63. $root = new SimpleNode();
  64. $root->appendChild($child1 = new SimpleNode());
  65. $root->appendChild($child2 = new SimpleNode());
  66. $root->detachChildren();
  67. $this->assertCount(0, $root->children());
  68. $this->assertNull($root->firstChild());
  69. $this->assertNull($root->lastChild());
  70. $this->assertNull($child1->parent());
  71. $this->assertNull($child2->parent());
  72. $this->assertSame($child2, $child1->next());
  73. $this->assertSame($child1, $child2->previous());
  74. }
  75. public function testReplaceChildren(): void
  76. {
  77. $root = new SimpleNode();
  78. $root->appendChild($oldChild = new SimpleNode());
  79. $newChildren = [
  80. $newChild1 = new SimpleNode(),
  81. $newChild2 = new SimpleNode(),
  82. ];
  83. $root->replaceChildren($newChildren);
  84. $this->assertCount(2, $root->children());
  85. $this->assertSame($newChild1, $root->firstChild());
  86. $this->assertSame($newChild2, $root->lastChild());
  87. $this->assertSame($root, $newChild1->parent());
  88. $this->assertSame($root, $newChild2->parent());
  89. $this->assertNull($oldChild->parent());
  90. }
  91. public function testInsertAfterWithParent(): void
  92. {
  93. $root = new SimpleNode();
  94. $root->appendChild($child1 = new SimpleNode());
  95. $root->appendChild($child2 = new SimpleNode());
  96. $otherRoot = new SimpleNode();
  97. $otherRoot->appendChild($child3 = new SimpleNode());
  98. $otherRoot->appendChild($child4 = new SimpleNode());
  99. $child1->insertAfter($child3);
  100. $this->assertCount(3, $root->children());
  101. $this->assertCount(1, $otherRoot->children());
  102. $this->assertSame($child1, $root->firstChild());
  103. $this->assertSame($child2, $root->lastChild());
  104. $this->assertSame($root, $child2->parent());
  105. }
  106. public function testInsertAfterWithoutParent(): void
  107. {
  108. $node1 = new SimpleNode();
  109. $node2 = new SimpleNode();
  110. $node1->insertAfter($node2);
  111. $this->assertSame($node2, $node1->next());
  112. $this->assertSame($node1, $node2->previous());
  113. }
  114. public function testInsertBeforeWithParent(): void
  115. {
  116. $root = new SimpleNode();
  117. $root->appendChild($child1 = new SimpleNode());
  118. $root->appendChild($child2 = new SimpleNode());
  119. $root->appendChild($child3 = new SimpleNode());
  120. $otherRoot = new SimpleNode();
  121. $otherRoot->appendChild($child4 = new SimpleNode());
  122. $otherRoot->appendChild($child5 = new SimpleNode());
  123. $child2->insertBefore($child4);
  124. $this->assertCount(4, $root->children());
  125. $this->assertCount(1, $otherRoot->children());
  126. $this->assertSame($child1, $root->children()[0]);
  127. $this->assertSame($child4, $root->children()[1]);
  128. $this->assertSame($child2, $root->children()[2]);
  129. $this->assertSame($child3, $root->children()[3]);
  130. $this->assertSame($root, $child4->parent());
  131. }
  132. public function testInsertBeforeWithoutParent(): void
  133. {
  134. $node1 = new SimpleNode();
  135. $node2 = new SimpleNode();
  136. $node1->insertBefore($node2);
  137. $this->assertSame($node1, $node2->next());
  138. $this->assertSame($node2, $node1->previous());
  139. }
  140. public function testClone(): void
  141. {
  142. // Build our intial AST
  143. $root = new SimpleNode();
  144. $root->appendChild($child1 = new SimpleNode());
  145. $root->appendChild($child2 = new SimpleNode());
  146. $child1->appendChild($grandChild1 = new SimpleNode());
  147. $child1->appendChild($grandChild2 = new SimpleNode());
  148. $grandChild2->appendChild($greatGrandChild1 = new SimpleNode());
  149. // Set values on each node to indicate they are originals
  150. $walker = $root->walker();
  151. while ($event = $walker->next()) {
  152. $event->getNode()->value = 'original';
  153. }
  154. // Clone one of the children
  155. $cloneOfChild1 = clone $child1;
  156. // Set values throught the cloned node to indicate they are clones
  157. $walker = $cloneOfChild1->walker();
  158. while ($event = $walker->next()) {
  159. $event->getNode()->value = 'cloned';
  160. }
  161. // Now check the original to ensure nothing changed there
  162. $walker = $root->walker();
  163. while ($event = $walker->next()) {
  164. $this->assertSame('original', $event->getNode()->value);
  165. }
  166. }
  167. }