1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
<?php declare(strict_types=1);
namespace PhpParser\NodeVisitor;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\If_;
use PhpParser\NodeFinder;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
final class NodeConnectingVisitorTest extends \PHPUnit\Framework\TestCase {
public function testConnectsNodeToItsParentNodeAndItsSiblingNodes(): void {
$ast = (new ParserFactory())->createForNewestSupportedVersion()->parse(
'<?php if (true) {} else {}'
);
$traverser = new NodeTraverser();
$traverser->addVisitor(new NodeConnectingVisitor());
$ast = $traverser->traverse($ast);
$node = (new NodeFinder())->findFirstInstanceof($ast, Else_::class);
$this->assertSame(If_::class, get_class($node->getAttribute('parent')));
$this->assertSame(ConstFetch::class, get_class($node->getAttribute('previous')));
$node = (new NodeFinder())->findFirstInstanceof($ast, ConstFetch::class);
$this->assertSame(Else_::class, get_class($node->getAttribute('next')));
}
public function testWeakReferences(): void {
$ast = (new ParserFactory())->createForNewestSupportedVersion()->parse(
'<?php if (true) {} else {}'
);
$traverser = new NodeTraverser();
$traverser->addVisitor(new NodeConnectingVisitor(true));
$ast = $traverser->traverse($ast);
$node = (new NodeFinder())->findFirstInstanceof($ast, Else_::class);
$this->assertInstanceOf(\WeakReference::class, $node->getAttribute('weak_parent'));
$this->assertSame(If_::class, get_class($node->getAttribute('weak_parent')->get()));
$this->assertInstanceOf(\WeakReference::class, $node->getAttribute('weak_previous'));
$this->assertSame(ConstFetch::class, get_class($node->getAttribute('weak_previous')->get()));
$node = (new NodeFinder())->findFirstInstanceof($ast, ConstFetch::class);
$this->assertInstanceOf(\WeakReference::class, $node->getAttribute('weak_next'));
$this->assertSame(Else_::class, get_class($node->getAttribute('weak_next')->get()));
}
}
|