2011-07-13 23:07:05 +02:00
|
|
|
<?php
|
|
|
|
|
2011-08-10 11:13:37 +02:00
|
|
|
class PHPParser_Tests_NodeTraverserTest extends PHPUnit_Framework_TestCase
|
2011-07-13 23:07:05 +02:00
|
|
|
{
|
2011-08-19 17:19:27 +02:00
|
|
|
public function getTestNode() {
|
2011-08-09 12:42:12 +02:00
|
|
|
return array(
|
2011-07-13 23:07:05 +02:00
|
|
|
new PHPParser_Node_Stmt_Namespace(array(
|
2011-08-09 14:55:45 +02:00
|
|
|
'name' => new PHPParser_Node_Name(array('Foo', 'Bar')),
|
2011-07-13 23:07:05 +02:00
|
|
|
'stmts' => array(
|
|
|
|
new PHPParser_Node_Stmt_Echo(array(
|
2011-08-11 10:25:51 +02:00
|
|
|
new PHPParser_Node_Scalar_String('Hallo World')
|
2011-08-09 12:42:12 +02:00
|
|
|
)),
|
2011-08-11 10:25:51 +02:00
|
|
|
new PHPParser_Node_Expr_Print(
|
|
|
|
new PHPParser_Node_Scalar_String('Hallo World, again!')
|
|
|
|
),
|
2011-07-13 23:07:05 +02:00
|
|
|
)
|
|
|
|
)),
|
|
|
|
);
|
2011-08-09 12:42:12 +02:00
|
|
|
}
|
|
|
|
|
2011-08-19 17:19:27 +02:00
|
|
|
public function testTraverse() {
|
2011-08-09 12:42:12 +02:00
|
|
|
$node = $this->getTestNode();
|
|
|
|
|
2011-08-10 11:13:37 +02:00
|
|
|
$visitor = new PHPParser_Tests_NodeVisitor;
|
2011-08-09 12:42:12 +02:00
|
|
|
$traverser = new PHPParser_NodeTraverser;
|
|
|
|
|
|
|
|
$traverser->addVisitor($visitor);
|
|
|
|
$traverser->traverse($node);
|
|
|
|
|
|
|
|
$this->assertEquals($node, $visitor->beforeTraverseNode);
|
2011-07-13 23:07:05 +02:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Stmt_Namespace',
|
|
|
|
'Name',
|
|
|
|
'Stmt_Echo',
|
|
|
|
'Scalar_String',
|
|
|
|
'Expr_Print',
|
|
|
|
'Scalar_String',
|
|
|
|
),
|
2011-08-09 12:42:12 +02:00
|
|
|
$visitor->enteredNodes
|
2011-07-13 23:07:05 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Name',
|
|
|
|
'Scalar_String',
|
|
|
|
'Stmt_Echo',
|
|
|
|
'Scalar_String',
|
|
|
|
'Expr_Print',
|
|
|
|
'Stmt_Namespace',
|
|
|
|
),
|
2011-08-09 12:42:12 +02:00
|
|
|
$visitor->leftNodes
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($node, $visitor->afterTraverseNode);
|
|
|
|
}
|
|
|
|
|
2011-08-19 17:19:27 +02:00
|
|
|
public function testModifyingTraverse() {
|
2011-08-09 12:42:12 +02:00
|
|
|
$node = $this->getTestNode();
|
|
|
|
|
2011-08-10 11:13:37 +02:00
|
|
|
$visitor = new PHPParser_Tests_ModifyingNodeVisitor;
|
2011-08-09 12:42:12 +02:00
|
|
|
$traverser = new PHPParser_NodeTraverser;
|
|
|
|
|
|
|
|
$traverser->addVisitor($visitor);
|
|
|
|
$traverser->traverse($node);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
new PHPParser_Node_Stmt_Echo(array(
|
2011-08-11 10:25:51 +02:00
|
|
|
new PHPParser_Node_Scalar_String('Foo Bar')
|
2011-08-09 12:42:12 +02:00
|
|
|
)),
|
|
|
|
),
|
|
|
|
$node
|
2011-07-13 23:07:05 +02:00
|
|
|
);
|
|
|
|
}
|
2011-08-09 12:42:12 +02:00
|
|
|
}
|
2011-07-13 23:07:05 +02:00
|
|
|
|
2011-08-10 11:13:37 +02:00
|
|
|
class PHPParser_Tests_NodeVisitor extends PHPParser_NodeVisitorAbstract
|
2011-08-09 12:42:12 +02:00
|
|
|
{
|
|
|
|
public $beforeTraverseNode;
|
|
|
|
public $enteredNodes;
|
|
|
|
public $leftNodes;
|
|
|
|
public $afterTraverseNode;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->enteredNodes = $this->leftNodes = array();
|
|
|
|
}
|
2011-07-13 23:07:05 +02:00
|
|
|
|
2011-08-09 12:42:12 +02:00
|
|
|
public function beforeTraverse(&$node) {
|
|
|
|
$this->beforeTraverseNode = $node;
|
|
|
|
}
|
|
|
|
|
2011-09-21 21:43:19 +02:00
|
|
|
public function enterNode(PHPParser_Node &$node) {
|
2011-08-09 12:42:12 +02:00
|
|
|
$this->enteredNodes[] = $node->getType();
|
|
|
|
}
|
|
|
|
|
2011-09-21 21:43:19 +02:00
|
|
|
public function leaveNode(PHPParser_Node &$node) {
|
2011-08-09 12:42:12 +02:00
|
|
|
$this->leftNodes[] = $node->getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function afterTraverse(&$node) {
|
|
|
|
$this->afterTraverseNode = $node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-10 11:13:37 +02:00
|
|
|
class PHPParser_Tests_ModifyingNodeVisitor extends PHPParser_NodeVisitorAbstract
|
2011-08-09 12:42:12 +02:00
|
|
|
{
|
2011-09-21 21:43:19 +02:00
|
|
|
public function leaveNode(PHPParser_Node &$node) {
|
2011-08-09 12:42:12 +02:00
|
|
|
// delete namespace nodes by merging them
|
|
|
|
if ($node instanceof PHPParser_Node_Stmt_Namespace) {
|
|
|
|
return $node->stmts;
|
|
|
|
// remove print nodes completely
|
|
|
|
} elseif ($node instanceof PHPParser_Node_Expr_Print) {
|
|
|
|
return false;
|
|
|
|
// change string contents to 'Foo Bar'
|
|
|
|
} elseif ($node instanceof PHPParser_Node_Scalar_String) {
|
|
|
|
$node->value = 'Foo Bar';
|
|
|
|
}
|
2011-07-13 23:07:05 +02:00
|
|
|
}
|
|
|
|
}
|