1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-15 10:57:11 +01:00
PHP-Parser/test/PHPParser/Tests/NodeVisitor/NameResolverTest.php
nikic ab72f98570 Add NameResolver
The NameResolver visitor tries to resolve all names to fully qualified names. It will resolve all non-dynamic names, apart from unqualified function and constant names. The latter can not be resolved properly without running the code.
2011-09-28 20:14:27 +02:00

83 lines
1.5 KiB
PHP

<?php
class PHPParser_Tests_NodeVisitor_NameResolverTest extends PHPUnit_Framework_TestCase
{
public function testResolve() {
$code = <<<EOC
<?php
namespace Foo {
use Hallo as Hi;
new Bar();
new Hi();
new Hi\\Bar();
new \\Bar();
new namespace\\Bar();
bar();
hi();
Hi\\bar();
foo\\bar();
\\bar();
namespace\\bar();
}
namespace {
use Hallo as Hi;
new Bar();
new Hi();
new Hi\\Bar();
new \\Bar();
new namespace\\Bar();
bar();
hi();
Hi\\bar();
foo\\bar();
\\bar();
namespace\\bar();
}
EOC;
$expectedCode = <<<EOC
namespace Foo {
use Hallo as Hi;
new \\Foo\\Bar();
new \\Hallo();
new \\Hallo\\Bar();
new \\Bar();
new \\Foo\\Bar();
bar();
hi();
\\Hallo\\bar();
\\Foo\\foo\\bar();
\\bar();
\\Foo\\bar();
}
namespace {
use Hallo as Hi;
new \\Bar();
new \\Hallo();
new \\Hallo\\Bar();
new \\Bar();
new \\Bar();
bar();
hi();
\\Hallo\\bar();
\\foo\\bar();
\\bar();
\\bar();
}
EOC;
$parser = new PHPParser_Parser;
$prettyPrinter = new PHPParser_PrettyPrinter_Zend;
$traverser = new PHPParser_NodeTraverser;
$traverser->addVisitor(new PHPParser_NodeVisitor_NameResolver);
$stmts = $parser->parse(new PHPParser_Lexer($code));
$stmts = $traverser->traverse($stmts);
$this->assertEquals($expectedCode, $prettyPrinter->prettyPrint($stmts));
}
}