NodeTraverserTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace PhpParser;
  3. use PhpParser\Node\Expr;
  4. use PhpParser\Node\Scalar\String_;
  5. class NodeTraverserTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testNonModifying() {
  8. $str1Node = new String_('Foo');
  9. $str2Node = new String_('Bar');
  10. $echoNode = new Node\Stmt\Echo_(array($str1Node, $str2Node));
  11. $stmts = array($echoNode);
  12. $visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  13. $visitor->expects($this->at(0))->method('beforeTraverse')->with($stmts);
  14. $visitor->expects($this->at(1))->method('enterNode')->with($echoNode);
  15. $visitor->expects($this->at(2))->method('enterNode')->with($str1Node);
  16. $visitor->expects($this->at(3))->method('leaveNode')->with($str1Node);
  17. $visitor->expects($this->at(4))->method('enterNode')->with($str2Node);
  18. $visitor->expects($this->at(5))->method('leaveNode')->with($str2Node);
  19. $visitor->expects($this->at(6))->method('leaveNode')->with($echoNode);
  20. $visitor->expects($this->at(7))->method('afterTraverse')->with($stmts);
  21. $traverser = new NodeTraverser;
  22. $traverser->addVisitor($visitor);
  23. $this->assertEquals($stmts, $traverser->traverse($stmts));
  24. }
  25. public function testModifying() {
  26. $str1Node = new String_('Foo');
  27. $str2Node = new String_('Bar');
  28. $printNode = new Expr\Print_($str1Node);
  29. // first visitor changes the node, second verifies the change
  30. $visitor1 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  31. $visitor2 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  32. // replace empty statements with string1 node
  33. $visitor1->expects($this->at(0))->method('beforeTraverse')->with(array())
  34. ->will($this->returnValue(array($str1Node)));
  35. $visitor2->expects($this->at(0))->method('beforeTraverse')->with(array($str1Node));
  36. // replace string1 node with print node
  37. $visitor1->expects($this->at(1))->method('enterNode')->with($str1Node)
  38. ->will($this->returnValue($printNode));
  39. $visitor2->expects($this->at(1))->method('enterNode')->with($printNode);
  40. // replace string1 node with string2 node
  41. $visitor1->expects($this->at(2))->method('enterNode')->with($str1Node)
  42. ->will($this->returnValue($str2Node));
  43. $visitor2->expects($this->at(2))->method('enterNode')->with($str2Node);
  44. // replace string2 node with string1 node again
  45. $visitor1->expects($this->at(3))->method('leaveNode')->with($str2Node)
  46. ->will($this->returnValue($str1Node));
  47. $visitor2->expects($this->at(3))->method('leaveNode')->with($str1Node);
  48. // replace print node with string1 node again
  49. $visitor1->expects($this->at(4))->method('leaveNode')->with($printNode)
  50. ->will($this->returnValue($str1Node));
  51. $visitor2->expects($this->at(4))->method('leaveNode')->with($str1Node);
  52. // replace string1 node with empty statements again
  53. $visitor1->expects($this->at(5))->method('afterTraverse')->with(array($str1Node))
  54. ->will($this->returnValue(array()));
  55. $visitor2->expects($this->at(5))->method('afterTraverse')->with(array());
  56. $traverser = new NodeTraverser;
  57. $traverser->addVisitor($visitor1);
  58. $traverser->addVisitor($visitor2);
  59. // as all operations are reversed we end where we start
  60. $this->assertEquals(array(), $traverser->traverse(array()));
  61. }
  62. public function testRemove() {
  63. $str1Node = new String_('Foo');
  64. $str2Node = new String_('Bar');
  65. $visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  66. // remove the string1 node, leave the string2 node
  67. $visitor->expects($this->at(2))->method('leaveNode')->with($str1Node)
  68. ->will($this->returnValue(false));
  69. $traverser = new NodeTraverser;
  70. $traverser->addVisitor($visitor);
  71. $this->assertEquals(array($str2Node), $traverser->traverse(array($str1Node, $str2Node)));
  72. }
  73. public function testMerge() {
  74. $strStart = new String_('Start');
  75. $strMiddle = new String_('End');
  76. $strEnd = new String_('Middle');
  77. $strR1 = new String_('Replacement 1');
  78. $strR2 = new String_('Replacement 2');
  79. $visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  80. // replace strMiddle with strR1 and strR2 by merge
  81. $visitor->expects($this->at(4))->method('leaveNode')->with($strMiddle)
  82. ->will($this->returnValue(array($strR1, $strR2)));
  83. $traverser = new NodeTraverser;
  84. $traverser->addVisitor($visitor);
  85. $this->assertEquals(
  86. array($strStart, $strR1, $strR2, $strEnd),
  87. $traverser->traverse(array($strStart, $strMiddle, $strEnd))
  88. );
  89. }
  90. public function testDeepArray() {
  91. $strNode = new String_('Foo');
  92. $stmts = array(array(array($strNode)));
  93. $visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  94. $visitor->expects($this->at(1))->method('enterNode')->with($strNode);
  95. $traverser = new NodeTraverser;
  96. $traverser->addVisitor($visitor);
  97. $this->assertEquals($stmts, $traverser->traverse($stmts));
  98. }
  99. public function testDontTraverseChildren() {
  100. $strNode = new String_('str');
  101. $printNode = new Expr\Print_($strNode);
  102. $varNode = new Expr\Variable('foo');
  103. $mulNode = new Expr\BinaryOp\Mul($varNode, $varNode);
  104. $negNode = new Expr\UnaryMinus($mulNode);
  105. $stmts = array($printNode, $negNode);
  106. $visitor1 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  107. $visitor2 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  108. $visitor1->expects($this->at(1))->method('enterNode')->with($printNode)
  109. ->will($this->returnValue(NodeTraverser::DONT_TRAVERSE_CHILDREN));
  110. $visitor2->expects($this->at(1))->method('enterNode')->with($printNode);
  111. $visitor1->expects($this->at(2))->method('leaveNode')->with($printNode);
  112. $visitor2->expects($this->at(2))->method('leaveNode')->with($printNode);
  113. $visitor1->expects($this->at(3))->method('enterNode')->with($negNode);
  114. $visitor2->expects($this->at(3))->method('enterNode')->with($negNode);
  115. $visitor1->expects($this->at(4))->method('enterNode')->with($mulNode);
  116. $visitor2->expects($this->at(4))->method('enterNode')->with($mulNode)
  117. ->will($this->returnValue(NodeTraverser::DONT_TRAVERSE_CHILDREN));
  118. $visitor1->expects($this->at(5))->method('leaveNode')->with($mulNode);
  119. $visitor2->expects($this->at(5))->method('leaveNode')->with($mulNode);
  120. $visitor1->expects($this->at(6))->method('leaveNode')->with($negNode);
  121. $visitor2->expects($this->at(6))->method('leaveNode')->with($negNode);
  122. $traverser = new NodeTraverser;
  123. $traverser->addVisitor($visitor1);
  124. $traverser->addVisitor($visitor2);
  125. $this->assertEquals($stmts, $traverser->traverse($stmts));
  126. }
  127. public function testStopTraversal() {
  128. $varNode1 = new Expr\Variable('a');
  129. $varNode2 = new Expr\Variable('b');
  130. $varNode3 = new Expr\Variable('c');
  131. $mulNode = new Expr\BinaryOp\Mul($varNode1, $varNode2);
  132. $printNode = new Expr\Print_($varNode3);
  133. $stmts = [$mulNode, $printNode];
  134. // From enterNode() with array parent
  135. $visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  136. $visitor->expects($this->at(1))->method('enterNode')->with($mulNode)
  137. ->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL));
  138. $visitor->expects($this->at(2))->method('afterTraversal');
  139. $traverser = new NodeTraverser;
  140. $traverser->addVisitor($visitor);
  141. $this->assertEquals($stmts, $traverser->traverse($stmts));
  142. // From enterNode with Node parent
  143. $visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  144. $visitor->expects($this->at(2))->method('enterNode')->with($varNode1)
  145. ->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL));
  146. $visitor->expects($this->at(3))->method('afterTraversal');
  147. $traverser = new NodeTraverser;
  148. $traverser->addVisitor($visitor);
  149. $this->assertEquals($stmts, $traverser->traverse($stmts));
  150. // From leaveNode with Node parent
  151. $visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  152. $visitor->expects($this->at(3))->method('leaveNode')->with($varNode1)
  153. ->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL));
  154. $visitor->expects($this->at(4))->method('afterTraversal');
  155. $traverser = new NodeTraverser;
  156. $traverser->addVisitor($visitor);
  157. $this->assertEquals($stmts, $traverser->traverse($stmts));
  158. // From leaveNode with array parent
  159. $visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  160. $visitor->expects($this->at(6))->method('leaveNode')->with($mulNode)
  161. ->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL));
  162. $visitor->expects($this->at(7))->method('afterTraversal');
  163. $traverser = new NodeTraverser;
  164. $traverser->addVisitor($visitor);
  165. $this->assertEquals($stmts, $traverser->traverse($stmts));
  166. // Check that pending array modifications are still carried out
  167. $visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  168. $visitor->expects($this->at(6))->method('leaveNode')->with($mulNode)
  169. ->will($this->returnValue(NodeTraverser::REMOVE_NODE));
  170. $visitor->expects($this->at(7))->method('enterNode')->with($printNode)
  171. ->will($this->returnValue(NodeTraverser::STOP_TRAVERSAL));
  172. $visitor->expects($this->at(8))->method('afterTraversal');
  173. $traverser = new NodeTraverser;
  174. $traverser->addVisitor($visitor);
  175. $this->assertEquals([$printNode], $traverser->traverse($stmts));
  176. }
  177. public function testRemovingVisitor() {
  178. $visitor1 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  179. $visitor2 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  180. $visitor3 = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  181. $traverser = new NodeTraverser;
  182. $traverser->addVisitor($visitor1);
  183. $traverser->addVisitor($visitor2);
  184. $traverser->addVisitor($visitor3);
  185. $preExpected = array($visitor1, $visitor2, $visitor3);
  186. $this->assertAttributeSame($preExpected, 'visitors', $traverser, 'The appropriate visitors have not been added');
  187. $traverser->removeVisitor($visitor2);
  188. $postExpected = array(0 => $visitor1, 2 => $visitor3);
  189. $this->assertAttributeSame($postExpected, 'visitors', $traverser, 'The appropriate visitors are not present after removal');
  190. }
  191. public function testNoCloneNodes() {
  192. $stmts = array(new Node\Stmt\Echo_(array(new String_('Foo'), new String_('Bar'))));
  193. $traverser = new NodeTraverser;
  194. $this->assertSame($stmts, $traverser->traverse($stmts));
  195. }
  196. /**
  197. * @expectedException \LogicException
  198. * @expectedExceptionMessage leaveNode() may only return an array if the parent structure is an array
  199. */
  200. public function testReplaceByArrayOnlyAllowedIfParentIsArray() {
  201. $stmts = array(new Node\Expr\UnaryMinus(new Node\Scalar\LNumber(42)));
  202. $visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
  203. $visitor->method('leaveNode')->willReturn(array(new Node\Scalar\DNumber(42.0)));
  204. $traverser = new NodeTraverser();
  205. $traverser->addVisitor($visitor);
  206. $traverser->traverse($stmts);
  207. }
  208. }