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-09 12:42:12 +02:00
|
|
|
function getTestNode() {
|
|
|
|
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(
|
|
|
|
'exprs' => array(
|
2011-08-09 14:55:45 +02:00
|
|
|
new PHPParser_Node_Scalar_String('Hallo World')
|
2011-07-13 23:07:05 +02:00
|
|
|
)
|
|
|
|
)),
|
|
|
|
new PHPParser_Node_Expr_Print(array(
|
2011-08-09 14:55:45 +02:00
|
|
|
'expr' => new PHPParser_Node_Scalar_String('Hallo World, again!')
|
2011-08-09 12:42:12 +02:00
|
|
|
)),
|
2011-07-13 23:07:05 +02:00
|
|
|
)
|
|
|
|
)),
|
|
|
|
);
|
2011-08-09 12:42:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testTraverse() {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testModifyingTraverse() {
|
|
|
|
$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(
|
|
|
|
'exprs' => array(
|
2011-08-09 14:55:45 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function enterNode(PHPParser_NodeAbstract &$node) {
|
|
|
|
$this->enteredNodes[] = $node->getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function leaveNode(PHPParser_NodeAbstract &$node) {
|
|
|
|
$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
|
|
|
{
|
|
|
|
public function leaveNode(PHPParser_NodeAbstract &$node) {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|