1<?php declare(strict_types=1); 2 3namespace PhpParser\NodeVisitor; 4 5use PhpParser\Node\Stmt\ClassMethod; 6use PhpParser\NodeFinder; 7use PhpParser\NodeTraverser; 8use PhpParser\ParserFactory; 9 10final class ParentConnectingVisitorTest extends \PHPUnit\Framework\TestCase { 11 public function testConnectsChildNodeToParentNode(): void { 12 $ast = (new ParserFactory())->createForNewestSupportedVersion()->parse( 13 '<?php class C { public function m() {} }' 14 ); 15 16 $traverser = new NodeTraverser(); 17 18 $traverser->addVisitor(new ParentConnectingVisitor()); 19 20 $ast = $traverser->traverse($ast); 21 22 $node = (new NodeFinder())->findFirstInstanceof($ast, ClassMethod::class); 23 24 $this->assertSame('C', $node->getAttribute('parent')->name->toString()); 25 } 26} 27