NodeTraverserTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace PhpParser;
  3. use PhpParser\Node\Scalar\String_;
  4. use PhpParser\Node\Expr;
  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->getMock('PhpParser\NodeVisitor');
  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->getMock('PhpParser\NodeVisitor');
  31. $visitor2 = $this->getMock('PhpParser\NodeVisitor');
  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->getMock('PhpParser\NodeVisitor');
  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->getMock('PhpParser\NodeVisitor');
  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->getMock('PhpParser\NodeVisitor');
  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->getMock('PhpParser\NodeVisitor');
  107. $visitor2 = $this->getMock('PhpParser\NodeVisitor');
  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 testRemovingVisitor() {
  128. $visitor1 = $this->getMock('PhpParser\NodeVisitor');
  129. $visitor2 = $this->getMock('PhpParser\NodeVisitor');
  130. $visitor3 = $this->getMock('PhpParser\NodeVisitor');
  131. $traverser = new NodeTraverser;
  132. $traverser->addVisitor($visitor1);
  133. $traverser->addVisitor($visitor2);
  134. $traverser->addVisitor($visitor3);
  135. $preExpected = array($visitor1, $visitor2, $visitor3);
  136. $this->assertAttributeSame($preExpected, 'visitors', $traverser, 'The appropriate visitors have not been added');
  137. $traverser->removeVisitor($visitor2);
  138. $postExpected = array(0 => $visitor1, 2 => $visitor3);
  139. $this->assertAttributeSame($postExpected, 'visitors', $traverser, 'The appropriate visitors are not present after removal');
  140. }
  141. public function testCloneNodesByDefault() {
  142. $stmts = array(new Node\Stmt\Echo_(array(new String_('Foo'), new String_('Bar'))));
  143. $traverser = new NodeTraverser;
  144. $this->assertNotSame($stmts, $traverser->traverse($stmts));
  145. }
  146. public function testCloneNodesDisabled() {
  147. $stmts = array(new Node\Stmt\Echo_(array(new String_('Foo'), new String_('Bar'))));
  148. $traverser = new NodeTraverser(false);
  149. $this->assertSame($stmts, $traverser->traverse($stmts));
  150. }
  151. }