1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-15 02:47:04 +01:00
PHP-Parser/test/PHPParser/Tests/NodeTraverserTest.php

121 lines
3.4 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-07-13 23:07:05 +02:00
new PHPParser_Node_Stmt_Namespace(array(
'name' => new PHPParser_Node_Name(array('Foo', 'Bar')),
2011-07-13 23:07:05 +02:00
'stmts' => array(
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-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;
$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);
}
public function testModifyingTraverse() {
2011-08-09 12:42:12 +02:00
$node = $this->getTestNode();
$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(
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
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;
}
}
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
}
}