NodeTraverserTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. class PHPParser_Tests_NodeTraverserTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function testNonModifying() {
  5. $str1Node = new PHPParser_Node_Scalar_String('Foo');
  6. $str2Node = new PHPParser_Node_Scalar_String('Bar');
  7. $echoNode = new PHPParser_Node_Stmt_Echo(array($str1Node, $str2Node));
  8. $stmts = array($echoNode);
  9. $visitor = $this->getMock('PHPParser_NodeVisitor');
  10. $visitor->expects($this->at(0))->method('beforeTraverse')->with($stmts);
  11. $visitor->expects($this->at(1))->method('enterNode')->with($echoNode);
  12. $visitor->expects($this->at(2))->method('enterNode')->with($str1Node);
  13. $visitor->expects($this->at(3))->method('leaveNode')->with($str1Node);
  14. $visitor->expects($this->at(4))->method('enterNode')->with($str2Node);
  15. $visitor->expects($this->at(5))->method('leaveNode')->with($str2Node);
  16. $visitor->expects($this->at(6))->method('leaveNode')->with($echoNode);
  17. $visitor->expects($this->at(7))->method('afterTraverse')->with($stmts);
  18. $traverser = new PHPParser_NodeTraverser;
  19. $traverser->addVisitor($visitor);
  20. $this->assertEquals($stmts, $traverser->traverse($stmts));
  21. }
  22. public function testModifying() {
  23. $str1Node = new PHPParser_Node_Scalar_String('Foo');
  24. $str2Node = new PHPParser_Node_Scalar_String('Bar');
  25. $printNode = new PHPParser_Node_Expr_Print($str1Node);
  26. // first visitor changes the node, second verifies the change
  27. $visitor1 = $this->getMock('PHPParser_NodeVisitor');
  28. $visitor2 = $this->getMock('PHPParser_NodeVisitor');
  29. // replace empty statements with string1 node
  30. $visitor1->expects($this->at(0))->method('beforeTraverse')->with(array())
  31. ->will($this->returnValue(array($str1Node)));
  32. $visitor2->expects($this->at(0))->method('beforeTraverse')->with(array($str1Node));
  33. // replace string1 node with print node
  34. $visitor1->expects($this->at(1))->method('enterNode')->with($str1Node)
  35. ->will($this->returnValue($printNode));
  36. $visitor2->expects($this->at(1))->method('enterNode')->with($printNode);
  37. // replace string1 node with string2 node
  38. $visitor1->expects($this->at(2))->method('enterNode')->with($str1Node)
  39. ->will($this->returnValue($str2Node));
  40. $visitor2->expects($this->at(2))->method('enterNode')->with($str2Node);
  41. // replace string2 node with string1 node again
  42. $visitor1->expects($this->at(3))->method('leaveNode')->with($str2Node)
  43. ->will($this->returnValue($str1Node));
  44. $visitor2->expects($this->at(3))->method('leaveNode')->with($str1Node);
  45. // replace print node with string1 node again
  46. $visitor1->expects($this->at(4))->method('leaveNode')->with($printNode)
  47. ->will($this->returnValue($str1Node));
  48. $visitor2->expects($this->at(4))->method('leaveNode')->with($str1Node);
  49. // replace string1 node with empty statements again
  50. $visitor1->expects($this->at(5))->method('afterTraverse')->with(array($str1Node))
  51. ->will($this->returnValue(array()));
  52. $visitor2->expects($this->at(5))->method('afterTraverse')->with(array());
  53. $traverser = new PHPParser_NodeTraverser;
  54. $traverser->addVisitor($visitor1);
  55. $traverser->addVisitor($visitor2);
  56. // as all operations are reversed we end where we start
  57. $this->assertEquals(array(), $traverser->traverse(array()));
  58. }
  59. public function testRemove() {
  60. $str1Node = new PHPParser_Node_Scalar_String('Foo');
  61. $str2Node = new PHPParser_Node_Scalar_String('Bar');
  62. $visitor = $this->getMock('PHPParser_NodeVisitor');
  63. // remove the string1 node, leave the string2 node
  64. $visitor->expects($this->at(2))->method('leaveNode')->with($str1Node)
  65. ->will($this->returnValue(false));
  66. $traverser = new PHPParser_NodeTraverser;
  67. $traverser->addVisitor($visitor);
  68. $this->assertEquals(array($str2Node), $traverser->traverse(array($str1Node, $str2Node)));
  69. }
  70. public function testMerge() {
  71. $strStart = new PHPParser_Node_Scalar_String('Start');
  72. $strMiddle = new PHPParser_Node_Scalar_String('End');
  73. $strEnd = new PHPParser_Node_Scalar_String('Middle');
  74. $strR1 = new PHPParser_Node_Scalar_String('Replacement 1');
  75. $strR2 = new PHPParser_Node_Scalar_String('Replacement 2');
  76. $visitor = $this->getMock('PHPParser_NodeVisitor');
  77. // replace strMiddle with strR1 and strR2 by merge
  78. $visitor->expects($this->at(4))->method('leaveNode')->with($strMiddle)
  79. ->will($this->returnValue(array($strR1, $strR2)));
  80. $traverser = new PHPParser_NodeTraverser;
  81. $traverser->addVisitor($visitor);
  82. $this->assertEquals(
  83. array($strStart, $strR1, $strR2, $strEnd),
  84. $traverser->traverse(array($strStart, $strMiddle, $strEnd))
  85. );
  86. }
  87. public function testDeepArray() {
  88. $strNode = new PHPParser_Node_Scalar_String('Foo');
  89. $stmts = array(array(array($strNode)));
  90. $visitor = $this->getMock('PHPParser_NodeVisitor');
  91. $visitor->expects($this->at(1))->method('enterNode')->with($strNode);
  92. $traverser = new PHPParser_NodeTraverser;
  93. $traverser->addVisitor($visitor);
  94. $this->assertEquals($stmts, $traverser->traverse($stmts));
  95. }
  96. }