ResolveExpressionTraitTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDataProviderWithFunctionCallExpr() 0 43 1
A testResolveFunctionName() 0 12 1
A testFindReturnStatement() 0 17 1
A testFindYieldStatement() 0 17 1
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace Tests\PHPSA\Analyzer\Helper;
7
8
use PhpParser\Node;
9
use PHPSA\Analyzer\Helper\ResolveExpressionTrait;
10
use PHPSA\CompiledExpression;
11
12
class ResolveExpressionTraitTest extends \Tests\PHPSA\TestCase
13
{
14
    use ResolveExpressionTrait;
15
16
    public function getDataProviderWithFunctionCallExpr()
17
    {
18
        $context = $this->getContext();
19
        $context->addVariable(
20
            new \PHPSA\Variable(
21
                'variableWithFunctionName',
22
                'testFunctionName',
23
                CompiledExpression::STRING
24
            )
25
        );
26
        $context->addVariable(
27
            new \PHPSA\Variable(
28
                'variableWithIntValue',
29
                12345,
30
                CompiledExpression::INTEGER
31
            )
32
        );
33
34
        return [
35
            // success
36
            [
37
                new \PhpParser\Node\Name(['testFn']),
38
                'testFn',
39
                $context
40
            ],
41
            [
42
                new \PhpParser\Node\Expr\Variable('variableWithFunctionName'),
43
                'testFunctionName',
44
                $context
45
            ],
46
            // not success
47
            [
48
                new \PhpParser\Node\Expr\Variable('unknown'),
49
                false,
50
                $context
51
            ],
52
            [
53
                new \PhpParser\Node\Expr\Variable('variableWithIntValue'),
54
                false,
55
                $context
56
            ],
57
        ];
58
    }
59
60
    /**
61
     * @dataProvider getDataProviderWithFunctionCallExpr
62
     *
63
     * @param $nameExpr
64
     * @param $expectedFunctionName
65
     */
66
    public function testResolveFunctionName($nameExpr, $expectedFunctionName, $context)
67
    {
68
        self::assertSame(
69
            $expectedFunctionName,
70
            $this->resolveFunctionName(
71
                new \PhpParser\Node\Expr\FuncCall(
72
                    $nameExpr
73
                ),
74
                $context
75
            )
76
        );
77
    }
78
79
    public function testFindReturnStatement()
80
    {
81
        $returnStatement = new \PhpParser\Node\Stmt\Return_();
82
83
        self::assertSame(
84
            [$returnStatement],
85
            // findReturnStatement will return \Generator, We should iterate it
86
            iterator_to_array(
87
                $this->findNode(
88
                    [
89
                        $returnStatement
90
                    ],
91
                    Node\Stmt\Return_::class
92
                )
93
            )
94
        );
95
    }
96
97
    public function testFindYieldStatement()
98
    {
99
        $returnStatement = new \PhpParser\Node\Expr\Yield_();
100
101
        self::assertSame(
102
            [$returnStatement],
103
            // findReturnStatement will return \Generator, We should iterate it
104
            iterator_to_array(
105
                $this->findNode(
106
                    [
107
                        $returnStatement
108
                    ],
109
                    Node\Expr\Yield_::class
110
                )
111
            )
112
        );
113
    }
114
}
115