addVisitor(new NameResolver); $stmts = $parser->parse($code); $stmts = $traverser->traverse($stmts); $this->assertSame( $this->canonicalize($expectedCode), $prettyPrinter->prettyPrint($stmts) ); } /** * @covers PhpParser\NodeVisitor\NameResolver */ public function testResolveLocations() { $code = <<<'EOC' addVisitor(new NameResolver); $stmts = $parser->parse($code); $stmts = $traverser->traverse($stmts); $this->assertSame( $this->canonicalize($expectedCode), $prettyPrinter->prettyPrint($stmts) ); } public function testNoResolveSpecialName() { $stmts = array(new Node\Expr\New_(new Name('self'))); $traverser = new PhpParser\NodeTraverser; $traverser->addVisitor(new NameResolver); $this->assertEquals($stmts, $traverser->traverse($stmts)); } protected function createNamespacedAndNonNamespaced(array $stmts) { return array( new Stmt\Namespace_(new Name('NS'), $stmts), new Stmt\Namespace_(null, $stmts), ); } public function testAddNamespacedName() { $stmts = $this->createNamespacedAndNonNamespaced(array( new Stmt\Class_('A'), new Stmt\Interface_('B'), new Stmt\Function_('C'), new Stmt\Const_(array( new Node\Const_('D', new Node\Scalar\String_('E')) )), new Expr\New_(new Stmt\Class_(null)), )); $traverser = new PhpParser\NodeTraverser; $traverser->addVisitor(new NameResolver); $stmts = $traverser->traverse($stmts); $this->assertSame('NS\\A', (string) $stmts[0]->stmts[0]->namespacedName); $this->assertSame('NS\\B', (string) $stmts[0]->stmts[1]->namespacedName); $this->assertSame('NS\\C', (string) $stmts[0]->stmts[2]->namespacedName); $this->assertSame('NS\\D', (string) $stmts[0]->stmts[3]->consts[0]->namespacedName); $this->assertObjectNotHasAttribute('namespacedName', $stmts[0]->stmts[4]->class); $this->assertSame('A', (string) $stmts[1]->stmts[0]->namespacedName); $this->assertSame('B', (string) $stmts[1]->stmts[1]->namespacedName); $this->assertSame('C', (string) $stmts[1]->stmts[2]->namespacedName); $this->assertSame('D', (string) $stmts[1]->stmts[3]->consts[0]->namespacedName); $this->assertObjectNotHasAttribute('namespacedName', $stmts[1]->stmts[4]->class); } public function testAddTraitNamespacedName() { $stmts = $this->createNamespacedAndNonNamespaced(array( new Stmt\Trait_('A') )); $traverser = new PhpParser\NodeTraverser; $traverser->addVisitor(new NameResolver); $stmts = $traverser->traverse($stmts); $this->assertSame('NS\\A', (string) $stmts[0]->stmts[0]->namespacedName); $this->assertSame('A', (string) $stmts[1]->stmts[0]->namespacedName); } /** * @dataProvider provideTestError */ public function testError(Node $stmt, $errorMsg) { $this->setExpectedException('PhpParser\Error', $errorMsg); $traverser = new PhpParser\NodeTraverser; $traverser->addVisitor(new NameResolver); $traverser->traverse(array($stmt)); } public function provideTestError() { return array( array( new Stmt\Use_(array( new Stmt\UseUse(new Name('A\B'), 'B', array('startLine' => 1)), new Stmt\UseUse(new Name('C\D'), 'B', array('startLine' => 2)), ), Stmt\Use_::TYPE_NORMAL), 'Cannot use C\D as B because the name is already in use on line 2' ), array( new Stmt\Use_(array( new Stmt\UseUse(new Name('a\b'), 'b', array('startLine' => 1)), new Stmt\UseUse(new Name('c\d'), 'B', array('startLine' => 2)), ), Stmt\Use_::TYPE_FUNCTION), 'Cannot use function c\d as B because the name is already in use on line 2' ), array( new Stmt\Use_(array( new Stmt\UseUse(new Name('A\B'), 'B', array('startLine' => 1)), new Stmt\UseUse(new Name('C\D'), 'B', array('startLine' => 2)), ), Stmt\Use_::TYPE_CONSTANT), 'Cannot use const C\D as B because the name is already in use on line 2' ), array( new Expr\New_(new Name\FullyQualified('self', array('startLine' => 3))), "'\\self' is an invalid class name on line 3" ), array( new Expr\New_(new Name\Relative('self', array('startLine' => 3))), "'\\self' is an invalid class name on line 3" ), array( new Expr\New_(new Name\FullyQualified('PARENT', array('startLine' => 3))), "'\\PARENT' is an invalid class name on line 3" ), array( new Expr\New_(new Name\Relative('STATIC', array('startLine' => 3))), "'\\STATIC' is an invalid class name on line 3" ), ); } public function testClassNameIsCaseInsensitive() { $source = <<<'EOC' parse($source); $traverser = new PhpParser\NodeTraverser; $traverser->addVisitor(new NameResolver); $stmts = $traverser->traverse($stmts); $stmt = $stmts[0]; $this->assertSame(array('Bar', 'Baz'), $stmt->stmts[1]->expr->class->parts); } public function testSpecialClassNamesAreCaseInsensitive() { $source = <<<'EOC' parse($source); $traverser = new PhpParser\NodeTraverser; $traverser->addVisitor(new NameResolver); $stmts = $traverser->traverse($stmts); $classStmt = $stmts[0]; $methodStmt = $classStmt->stmts[0]->stmts[0]; $this->assertSame('SELF', (string)$methodStmt->stmts[0]->class); $this->assertSame('PARENT', (string)$methodStmt->stmts[1]->class); $this->assertSame('STATIC', (string)$methodStmt->stmts[2]->class); } }