2011-09-28 20:14:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class PHPParser_Tests_NodeVisitor_NameResolverTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2011-11-27 12:53:48 +01:00
|
|
|
/**
|
|
|
|
* @covers PHPParser_NodeVisitor_NameResolver
|
|
|
|
*/
|
2011-11-12 13:24:59 +01:00
|
|
|
public function testResolveNames() {
|
2011-09-28 20:14:27 +02:00
|
|
|
$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));
|
|
|
|
}
|
2011-11-12 13:24:59 +01:00
|
|
|
|
2011-11-27 12:53:48 +01:00
|
|
|
/**
|
|
|
|
* @covers PHPParser_NodeVisitor_NameResolver
|
|
|
|
*/
|
2011-11-12 13:24:59 +01:00
|
|
|
public function testAddNamespacedName() {
|
|
|
|
$code = <<<EOC
|
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Foo {
|
|
|
|
class A {}
|
2011-11-12 18:29:50 +01:00
|
|
|
interface B {}
|
|
|
|
trait C {}
|
|
|
|
function D() {}
|
|
|
|
const E = 'F';
|
2011-11-12 13:24:59 +01:00
|
|
|
}
|
|
|
|
namespace {
|
|
|
|
class A {}
|
2011-11-12 18:29:50 +01:00
|
|
|
interface B {}
|
|
|
|
trait C {}
|
|
|
|
function D() {}
|
|
|
|
const E = 'F';
|
2011-11-12 13:24:59 +01:00
|
|
|
}
|
|
|
|
EOC;
|
|
|
|
|
|
|
|
$parser = new PHPParser_Parser;
|
|
|
|
$traverser = new PHPParser_NodeTraverser;
|
|
|
|
$traverser->addVisitor(new PHPParser_NodeVisitor_NameResolver);
|
|
|
|
|
|
|
|
$stmts = $parser->parse(new PHPParser_Lexer($code));
|
|
|
|
$stmts = $traverser->traverse($stmts);
|
|
|
|
|
|
|
|
$this->assertEquals('Foo\\A', (string) $stmts[0]->stmts[0]->namespacedName);
|
|
|
|
$this->assertEquals('Foo\\B', (string) $stmts[0]->stmts[1]->namespacedName);
|
2011-11-12 18:29:50 +01:00
|
|
|
$this->assertEquals('Foo\\C', (string) $stmts[0]->stmts[2]->namespacedName);
|
|
|
|
$this->assertEquals('Foo\\D', (string) $stmts[0]->stmts[3]->namespacedName);
|
|
|
|
$this->assertEquals('Foo\\E', (string) $stmts[0]->stmts[4]->consts[0]->namespacedName);
|
2011-11-12 13:24:59 +01:00
|
|
|
$this->assertEquals('A', (string) $stmts[1]->stmts[0]->namespacedName);
|
|
|
|
$this->assertEquals('B', (string) $stmts[1]->stmts[1]->namespacedName);
|
2011-11-12 18:29:50 +01:00
|
|
|
$this->assertEquals('C', (string) $stmts[1]->stmts[2]->namespacedName);
|
|
|
|
$this->assertEquals('D', (string) $stmts[1]->stmts[3]->namespacedName);
|
|
|
|
$this->assertEquals('E', (string) $stmts[1]->stmts[4]->consts[0]->namespacedName);
|
2011-11-12 13:24:59 +01:00
|
|
|
}
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|