NodeTraverserTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php declare(strict_types=1);
  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_([$str1Node, $str2Node]);
  11. $stmts = [$echoNode];
  12. $visitor = $this->getMockBuilder(NodeVisitor::class)->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(NodeVisitor::class)->getMock();
  31. $visitor2 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  32. // replace empty statements with string1 node
  33. $visitor1->expects($this->at(0))->method('beforeTraverse')->with([])
  34. ->willReturn([$str1Node]);
  35. $visitor2->expects($this->at(0))->method('beforeTraverse')->with([$str1Node]);
  36. // replace string1 node with print node
  37. $visitor1->expects($this->at(1))->method('enterNode')->with($str1Node)
  38. ->willReturn($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. ->willReturn($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. ->willReturn($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. ->willReturn($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([$str1Node])
  54. ->willReturn([]);
  55. $visitor2->expects($this->at(5))->method('afterTraverse')->with([]);
  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([], $traverser->traverse([]));
  61. }
  62. public function testRemove() {
  63. $str1Node = new String_('Foo');
  64. $str2Node = new String_('Bar');
  65. $visitor = $this->getMockBuilder(NodeVisitor::class)->getMock();
  66. // remove the string1 node, leave the string2 node
  67. $visitor->expects($this->at(2))->method('leaveNode')->with($str1Node)
  68. ->willReturn(NodeTraverser::REMOVE_NODE);
  69. $traverser = new NodeTraverser;
  70. $traverser->addVisitor($visitor);
  71. $this->assertEquals([$str2Node], $traverser->traverse([$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(NodeVisitor::class)->getMock();
  80. // replace strMiddle with strR1 and strR2 by merge
  81. $visitor->expects($this->at(4))->method('leaveNode')->with($strMiddle)
  82. ->willReturn([$strR1, $strR2]);
  83. $traverser = new NodeTraverser;
  84. $traverser->addVisitor($visitor);
  85. $this->assertEquals(
  86. [$strStart, $strR1, $strR2, $strEnd],
  87. $traverser->traverse([$strStart, $strMiddle, $strEnd])
  88. );
  89. }
  90. public function testInvalidDeepArray() {
  91. $this->expectException(\LogicException::class);
  92. $this->expectExceptionMessage('Invalid node structure: Contains nested arrays');
  93. $strNode = new String_('Foo');
  94. $stmts = [[[$strNode]]];
  95. $traverser = new NodeTraverser;
  96. $this->assertEquals($stmts, $traverser->traverse($stmts));
  97. }
  98. public function testDontTraverseChildren() {
  99. $strNode = new String_('str');
  100. $printNode = new Expr\Print_($strNode);
  101. $varNode = new Expr\Variable('foo');
  102. $mulNode = new Expr\BinaryOp\Mul($varNode, $varNode);
  103. $negNode = new Expr\UnaryMinus($mulNode);
  104. $stmts = [$printNode, $negNode];
  105. $visitor1 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  106. $visitor2 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  107. $visitor1->expects($this->at(1))->method('enterNode')->with($printNode)
  108. ->willReturn(NodeTraverser::DONT_TRAVERSE_CHILDREN);
  109. $visitor2->expects($this->at(1))->method('enterNode')->with($printNode);
  110. $visitor1->expects($this->at(2))->method('leaveNode')->with($printNode);
  111. $visitor2->expects($this->at(2))->method('leaveNode')->with($printNode);
  112. $visitor1->expects($this->at(3))->method('enterNode')->with($negNode);
  113. $visitor2->expects($this->at(3))->method('enterNode')->with($negNode);
  114. $visitor1->expects($this->at(4))->method('enterNode')->with($mulNode);
  115. $visitor2->expects($this->at(4))->method('enterNode')->with($mulNode)
  116. ->willReturn(NodeTraverser::DONT_TRAVERSE_CHILDREN);
  117. $visitor1->expects($this->at(5))->method('leaveNode')->with($mulNode);
  118. $visitor2->expects($this->at(5))->method('leaveNode')->with($mulNode);
  119. $visitor1->expects($this->at(6))->method('leaveNode')->with($negNode);
  120. $visitor2->expects($this->at(6))->method('leaveNode')->with($negNode);
  121. $traverser = new NodeTraverser;
  122. $traverser->addVisitor($visitor1);
  123. $traverser->addVisitor($visitor2);
  124. $this->assertEquals($stmts, $traverser->traverse($stmts));
  125. }
  126. public function testDontTraverseCurrentAndChildren() {
  127. // print 'str'; -($foo * $foo);
  128. $strNode = new String_('str');
  129. $printNode = new Expr\Print_($strNode);
  130. $varNode = new Expr\Variable('foo');
  131. $mulNode = new Expr\BinaryOp\Mul($varNode, $varNode);
  132. $divNode = new Expr\BinaryOp\Div($varNode, $varNode);
  133. $negNode = new Expr\UnaryMinus($mulNode);
  134. $stmts = [$printNode, $negNode];
  135. $visitor1 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  136. $visitor2 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  137. $visitor1->expects($this->at(1))->method('enterNode')->with($printNode)
  138. ->willReturn(NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN);
  139. $visitor1->expects($this->at(2))->method('leaveNode')->with($printNode);
  140. $visitor1->expects($this->at(3))->method('enterNode')->with($negNode);
  141. $visitor2->expects($this->at(1))->method('enterNode')->with($negNode);
  142. $visitor1->expects($this->at(4))->method('enterNode')->with($mulNode)
  143. ->willReturn(NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN);
  144. $visitor1->expects($this->at(5))->method('leaveNode')->with($mulNode)->willReturn($divNode);
  145. $visitor1->expects($this->at(6))->method('leaveNode')->with($negNode);
  146. $visitor2->expects($this->at(2))->method('leaveNode')->with($negNode);
  147. $traverser = new NodeTraverser;
  148. $traverser->addVisitor($visitor1);
  149. $traverser->addVisitor($visitor2);
  150. $resultStmts = $traverser->traverse($stmts);
  151. $this->assertInstanceOf(Expr\BinaryOp\Div::class, $resultStmts[1]->expr);
  152. }
  153. public function testStopTraversal() {
  154. $varNode1 = new Expr\Variable('a');
  155. $varNode2 = new Expr\Variable('b');
  156. $varNode3 = new Expr\Variable('c');
  157. $mulNode = new Expr\BinaryOp\Mul($varNode1, $varNode2);
  158. $printNode = new Expr\Print_($varNode3);
  159. $stmts = [$mulNode, $printNode];
  160. // From enterNode() with array parent
  161. $visitor = $this->getMockBuilder(NodeVisitor::class)->getMock();
  162. $visitor->expects($this->at(1))->method('enterNode')->with($mulNode)
  163. ->willReturn(NodeTraverser::STOP_TRAVERSAL);
  164. $visitor->expects($this->at(2))->method('afterTraverse');
  165. $traverser = new NodeTraverser;
  166. $traverser->addVisitor($visitor);
  167. $this->assertEquals($stmts, $traverser->traverse($stmts));
  168. // From enterNode with Node parent
  169. $visitor = $this->getMockBuilder(NodeVisitor::class)->getMock();
  170. $visitor->expects($this->at(2))->method('enterNode')->with($varNode1)
  171. ->willReturn(NodeTraverser::STOP_TRAVERSAL);
  172. $visitor->expects($this->at(3))->method('afterTraverse');
  173. $traverser = new NodeTraverser;
  174. $traverser->addVisitor($visitor);
  175. $this->assertEquals($stmts, $traverser->traverse($stmts));
  176. // From leaveNode with Node parent
  177. $visitor = $this->getMockBuilder(NodeVisitor::class)->getMock();
  178. $visitor->expects($this->at(3))->method('leaveNode')->with($varNode1)
  179. ->willReturn(NodeTraverser::STOP_TRAVERSAL);
  180. $visitor->expects($this->at(4))->method('afterTraverse');
  181. $traverser = new NodeTraverser;
  182. $traverser->addVisitor($visitor);
  183. $this->assertEquals($stmts, $traverser->traverse($stmts));
  184. // From leaveNode with array parent
  185. $visitor = $this->getMockBuilder(NodeVisitor::class)->getMock();
  186. $visitor->expects($this->at(6))->method('leaveNode')->with($mulNode)
  187. ->willReturn(NodeTraverser::STOP_TRAVERSAL);
  188. $visitor->expects($this->at(7))->method('afterTraverse');
  189. $traverser = new NodeTraverser;
  190. $traverser->addVisitor($visitor);
  191. $this->assertEquals($stmts, $traverser->traverse($stmts));
  192. // Check that pending array modifications are still carried out
  193. $visitor = $this->getMockBuilder(NodeVisitor::class)->getMock();
  194. $visitor->expects($this->at(6))->method('leaveNode')->with($mulNode)
  195. ->willReturn(NodeTraverser::REMOVE_NODE);
  196. $visitor->expects($this->at(7))->method('enterNode')->with($printNode)
  197. ->willReturn(NodeTraverser::STOP_TRAVERSAL);
  198. $visitor->expects($this->at(8))->method('afterTraverse');
  199. $traverser = new NodeTraverser;
  200. $traverser->addVisitor($visitor);
  201. $this->assertEquals([$printNode], $traverser->traverse($stmts));
  202. }
  203. public function testRemovingVisitor() {
  204. $visitor1 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  205. $visitor2 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  206. $visitor3 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  207. $traverser = new NodeTraverser;
  208. $traverser->addVisitor($visitor1);
  209. $traverser->addVisitor($visitor2);
  210. $traverser->addVisitor($visitor3);
  211. $preExpected = [$visitor1, $visitor2, $visitor3];
  212. $this->assertAttributeSame($preExpected, 'visitors', $traverser, 'The appropriate visitors have not been added');
  213. $traverser->removeVisitor($visitor2);
  214. $postExpected = [0 => $visitor1, 2 => $visitor3];
  215. $this->assertAttributeSame($postExpected, 'visitors', $traverser, 'The appropriate visitors are not present after removal');
  216. }
  217. public function testNoCloneNodes() {
  218. $stmts = [new Node\Stmt\Echo_([new String_('Foo'), new String_('Bar')])];
  219. $traverser = new NodeTraverser;
  220. $this->assertSame($stmts, $traverser->traverse($stmts));
  221. }
  222. /**
  223. * @dataProvider provideTestInvalidReturn
  224. */
  225. public function testInvalidReturn($visitor, $message) {
  226. $this->expectException(\LogicException::class);
  227. $this->expectExceptionMessage($message);
  228. $stmts = [new Node\Stmt\Expression(new Node\Scalar\LNumber(42))];
  229. $traverser = new NodeTraverser();
  230. $traverser->addVisitor($visitor);
  231. $traverser->traverse($stmts);
  232. }
  233. public function provideTestInvalidReturn() {
  234. $visitor1 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  235. $visitor1->expects($this->at(1))->method('enterNode')
  236. ->willReturn('foobar');
  237. $visitor2 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  238. $visitor2->expects($this->at(2))->method('enterNode')
  239. ->willReturn('foobar');
  240. $visitor3 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  241. $visitor3->expects($this->at(3))->method('leaveNode')
  242. ->willReturn('foobar');
  243. $visitor4 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  244. $visitor4->expects($this->at(4))->method('leaveNode')
  245. ->willReturn('foobar');
  246. $visitor5 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  247. $visitor5->expects($this->at(3))->method('leaveNode')
  248. ->willReturn([new Node\Scalar\DNumber(42.0)]);
  249. $visitor6 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  250. $visitor6->expects($this->at(4))->method('leaveNode')
  251. ->willReturn(false);
  252. $visitor7 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  253. $visitor7->expects($this->at(1))->method('enterNode')
  254. ->willReturn(new Node\Scalar\LNumber(42));
  255. $visitor8 = $this->getMockBuilder(NodeVisitor::class)->getMock();
  256. $visitor8->expects($this->at(2))->method('enterNode')
  257. ->willReturn(new Node\Stmt\Return_());
  258. return [
  259. [$visitor1, 'enterNode() returned invalid value of type string'],
  260. [$visitor2, 'enterNode() returned invalid value of type string'],
  261. [$visitor3, 'leaveNode() returned invalid value of type string'],
  262. [$visitor4, 'leaveNode() returned invalid value of type string'],
  263. [$visitor5, 'leaveNode() may only return an array if the parent structure is an array'],
  264. [$visitor6, 'bool(false) return from leaveNode() no longer supported. Return NodeTraverser::REMOVE_NODE instead'],
  265. [$visitor7, 'Trying to replace statement (Stmt_Expression) with expression (Scalar_LNumber). Are you missing a Stmt_Expression wrapper?'],
  266. [$visitor8, 'Trying to replace expression (Scalar_LNumber) with statement (Stmt_Return)'],
  267. ];
  268. }
  269. }