NodeConnectingVisitorTest.php 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\NodeVisitor;
  3. use PhpParser\Node\Expr\ConstFetch;
  4. use PhpParser\Node\Stmt\Else_;
  5. use PhpParser\Node\Stmt\If_;
  6. use PhpParser\NodeFinder;
  7. use PhpParser\NodeTraverser;
  8. use PhpParser\ParserFactory;
  9. final class NodeConnectingVisitorTest extends \PHPUnit\Framework\TestCase {
  10. public function testConnectsNodeToItsParentNodeAndItsSiblingNodes(): void {
  11. $ast = (new ParserFactory())->createForNewestSupportedVersion()->parse(
  12. '<?php if (true) {} else {}'
  13. );
  14. $traverser = new NodeTraverser();
  15. $traverser->addVisitor(new NodeConnectingVisitor());
  16. $ast = $traverser->traverse($ast);
  17. $node = (new NodeFinder())->findFirstInstanceof($ast, Else_::class);
  18. $this->assertSame(If_::class, get_class($node->getAttribute('parent')));
  19. $this->assertSame(ConstFetch::class, get_class($node->getAttribute('previous')));
  20. $node = (new NodeFinder())->findFirstInstanceof($ast, ConstFetch::class);
  21. $this->assertSame(Else_::class, get_class($node->getAttribute('next')));
  22. }
  23. }