php-parser/test/PHPParser/Tests/NodeTraverserTest.php

119 lines
3.3 KiB
PHP
Raw Normal View History

2011-07-13 23:07:05 +02:00
<?php
class PHPParser_Tests_NodeTraverserTest extends PHPUnit_Framework_TestCase
2011-07-13 23:07:05 +02:00
{
public function getTestNode() {
2011-08-09 12:42:12 +02:00
return array(
2011-10-26 18:35:37 +02:00
new PHPParser_Node_Stmt_Namespace(
new PHPParser_Node_Name(array('Foo', 'Bar')),
array(
2011-07-13 23:07:05 +02:00
new PHPParser_Node_Stmt_Echo(array(
new PHPParser_Node_Scalar_String('Hallo World')
2011-08-09 12:42:12 +02:00
)),
new PHPParser_Node_Expr_Print(
new PHPParser_Node_Scalar_String('Hallo World, again!')
),
2011-07-13 23:07:05 +02:00
)
2011-10-26 18:35:37 +02:00
),
2011-07-13 23:07:05 +02:00
);
2011-08-09 12:42:12 +02:00
}
public function testTraverse() {
2011-08-09 12:42:12 +02:00
$node = $this->getTestNode();
$visitor = new PHPParser_Tests_NodeVisitor;
2011-08-09 12:42:12 +02:00
$traverser = new PHPParser_NodeTraverser;
2011-08-09 12:42:12 +02:00
$traverser->addVisitor($visitor);
$node = $traverser->traverse($node);
$this->assertEquals($node, $visitor->beforeTraverseNodes);
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->afterTraverseNodes);
2011-08-09 12:42:12 +02:00
}
public function testModifyingTraverse() {
2011-08-09 12:42:12 +02:00
$node = $this->getTestNode();
$traverser = new PHPParser_NodeTraverser;
$traverser->addVisitor(new PHPParser_Tests_ModifyingNodeVisitor);
2011-08-09 12:42:12 +02:00
$this->assertEquals(
array(
new PHPParser_Node_Stmt_Echo(array(
new PHPParser_Node_Scalar_String('Foo Bar')
2011-08-09 12:42:12 +02:00
)),
),
$traverser->traverse($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
class PHPParser_Tests_NodeVisitor extends PHPParser_NodeVisitorAbstract
2011-08-09 12:42:12 +02:00
{
public $beforeTraverseNodes;
2011-08-09 12:42:12 +02:00
public $enteredNodes;
public $leftNodes;
public $afterTraverseNodes;
2011-08-09 12:42:12 +02:00
public function __construct() {
$this->enteredNodes = $this->leftNodes = array();
}
2011-07-13 23:07:05 +02:00
public function beforeTraverse(array $nodes) {
$this->beforeTraverseNodes = $nodes;
2011-08-09 12:42:12 +02:00
}
public function enterNode(PHPParser_Node $node) {
2011-08-09 12:42:12 +02:00
$this->enteredNodes[] = $node->getType();
}
public function leaveNode(PHPParser_Node $node) {
2011-08-09 12:42:12 +02:00
$this->leftNodes[] = $node->getType();
}
public function afterTraverse(array $nodes) {
$this->afterTraverseNodes = $nodes;
2011-08-09 12:42:12 +02:00
}
}
class PHPParser_Tests_ModifyingNodeVisitor extends PHPParser_NodeVisitorAbstract
2011-08-09 12:42:12 +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
}
}