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