File: NodeFinderTest.php

package info (click to toggle)
php-parser 5.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,532 kB
  • sloc: php: 23,585; yacc: 1,272; makefile: 39; sh: 8
file content (62 lines) | stat: -rw-r--r-- 2,440 bytes parent folder | download | duplicates (3)
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
58
59
60
61
62
<?php declare(strict_types=1);

namespace PhpParser;

use PhpParser\Node\Expr;

class NodeFinderTest extends \PHPUnit\Framework\TestCase {
    private function getStmtsAndVars() {
        $assign = new Expr\Assign(new Expr\Variable('a'), new Expr\BinaryOp\Concat(
            new Expr\Variable('b'), new Expr\Variable('c')
        ));
        $stmts = [new Node\Stmt\Expression($assign)];
        $vars = [$assign->var, $assign->expr->left, $assign->expr->right];
        return [$stmts, $vars];
    }

    public function testFind(): void {
        $finder = new NodeFinder();
        list($stmts, $vars) = $this->getStmtsAndVars();
        $varFilter = function (Node $node) {
            return $node instanceof Expr\Variable;
        };
        $this->assertSame($vars, $finder->find($stmts, $varFilter));
        $this->assertSame($vars, $finder->find($stmts[0], $varFilter));

        $noneFilter = function () {
            return false;
        };
        $this->assertSame([], $finder->find($stmts, $noneFilter));
    }

    public function testFindInstanceOf(): void {
        $finder = new NodeFinder();
        list($stmts, $vars) = $this->getStmtsAndVars();
        $this->assertSame($vars, $finder->findInstanceOf($stmts, Expr\Variable::class));
        $this->assertSame($vars, $finder->findInstanceOf($stmts[0], Expr\Variable::class));
        $this->assertSame([], $finder->findInstanceOf($stmts, Expr\BinaryOp\Mul::class));
    }

    public function testFindFirst(): void {
        $finder = new NodeFinder();
        list($stmts, $vars) = $this->getStmtsAndVars();
        $varFilter = function (Node $node) {
            return $node instanceof Expr\Variable;
        };
        $this->assertSame($vars[0], $finder->findFirst($stmts, $varFilter));
        $this->assertSame($vars[0], $finder->findFirst($stmts[0], $varFilter));

        $noneFilter = function () {
            return false;
        };
        $this->assertNull($finder->findFirst($stmts, $noneFilter));
    }

    public function testFindFirstInstanceOf(): void {
        $finder = new NodeFinder();
        list($stmts, $vars) = $this->getStmtsAndVars();
        $this->assertSame($vars[0], $finder->findFirstInstanceOf($stmts, Expr\Variable::class));
        $this->assertSame($vars[0], $finder->findFirstInstanceOf($stmts[0], Expr\Variable::class));
        $this->assertNull($finder->findFirstInstanceOf($stmts, Expr\BinaryOp\Mul::class));
    }
}