Chris@13
|
1 <?php declare(strict_types=1);
|
Chris@13
|
2
|
Chris@13
|
3 namespace PhpParser;
|
Chris@13
|
4
|
Chris@13
|
5 use PhpParser\Node\Expr;
|
Chris@13
|
6
|
Chris@17
|
7 class NodeFinderTest extends \PHPUnit\Framework\TestCase
|
Chris@13
|
8 {
|
Chris@13
|
9 private function getStmtsAndVars() {
|
Chris@13
|
10 $assign = new Expr\Assign(new Expr\Variable('a'), new Expr\BinaryOp\Concat(
|
Chris@13
|
11 new Expr\Variable('b'), new Expr\Variable('c')
|
Chris@13
|
12 ));
|
Chris@13
|
13 $stmts = [new Node\Stmt\Expression($assign)];
|
Chris@13
|
14 $vars = [$assign->var, $assign->expr->left, $assign->expr->right];
|
Chris@13
|
15 return [$stmts, $vars];
|
Chris@13
|
16 }
|
Chris@13
|
17
|
Chris@13
|
18 public function testFind() {
|
Chris@13
|
19 $finder = new NodeFinder;
|
Chris@13
|
20 list($stmts, $vars) = $this->getStmtsAndVars();
|
Chris@13
|
21 $varFilter = function(Node $node) {
|
Chris@13
|
22 return $node instanceof Expr\Variable;
|
Chris@13
|
23 };
|
Chris@13
|
24 $this->assertSame($vars, $finder->find($stmts, $varFilter));
|
Chris@13
|
25 $this->assertSame($vars, $finder->find($stmts[0], $varFilter));
|
Chris@13
|
26
|
Chris@13
|
27 $noneFilter = function () { return false; };
|
Chris@13
|
28 $this->assertSame([], $finder->find($stmts, $noneFilter));
|
Chris@13
|
29 }
|
Chris@13
|
30
|
Chris@13
|
31 public function testFindInstanceOf() {
|
Chris@13
|
32 $finder = new NodeFinder;
|
Chris@13
|
33 list($stmts, $vars) = $this->getStmtsAndVars();
|
Chris@13
|
34 $this->assertSame($vars, $finder->findInstanceOf($stmts, Expr\Variable::class));
|
Chris@13
|
35 $this->assertSame($vars, $finder->findInstanceOf($stmts[0], Expr\Variable::class));
|
Chris@13
|
36 $this->assertSame([], $finder->findInstanceOf($stmts, Expr\BinaryOp\Mul::class));
|
Chris@13
|
37 }
|
Chris@13
|
38
|
Chris@13
|
39 public function testFindFirst() {
|
Chris@13
|
40 $finder = new NodeFinder;
|
Chris@13
|
41 list($stmts, $vars) = $this->getStmtsAndVars();
|
Chris@13
|
42 $varFilter = function(Node $node) {
|
Chris@13
|
43 return $node instanceof Expr\Variable;
|
Chris@13
|
44 };
|
Chris@13
|
45 $this->assertSame($vars[0], $finder->findFirst($stmts, $varFilter));
|
Chris@13
|
46 $this->assertSame($vars[0], $finder->findFirst($stmts[0], $varFilter));
|
Chris@13
|
47
|
Chris@13
|
48 $noneFilter = function () { return false; };
|
Chris@13
|
49 $this->assertNull($finder->findFirst($stmts, $noneFilter));
|
Chris@13
|
50 }
|
Chris@13
|
51
|
Chris@13
|
52 public function testFindFirstInstanceOf() {
|
Chris@13
|
53 $finder = new NodeFinder;
|
Chris@13
|
54 list($stmts, $vars) = $this->getStmtsAndVars();
|
Chris@13
|
55 $this->assertSame($vars[0], $finder->findFirstInstanceOf($stmts, Expr\Variable::class));
|
Chris@13
|
56 $this->assertSame($vars[0], $finder->findFirstInstanceOf($stmts[0], Expr\Variable::class));
|
Chris@13
|
57 $this->assertNull($finder->findFirstInstanceOf($stmts, Expr\BinaryOp\Mul::class));
|
Chris@13
|
58 }
|
Chris@13
|
59 }
|