Completed
Pull Request — master (#149)
by Enrico
04:04
created

IfTest::testIfStatementCreatesVar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 22
rs 9.2
1
<?php
2
3
namespace Tests\PHPSA\Compiler\Statement;
4
5
use PhpParser\Node;
6
use PHPSA\Variable;
7
8
class IfTest extends \Tests\PHPSA\TestCase
9
{
10
    /**
11
     * Tests if ($stmtTest = 2) creates the variable
12
     */
13
    public function testIfConditionCreatesVar()
14
    {
15
        $context = $this->getContext();
16
17
        $statement = new Node\Stmt\If_(
18
            new Node\Expr\Assign(
19
                new Node\Expr\Variable(
20
                    new Node\Name("stmtTest")
21
                ),
22
                $this->newScalarExpr(2)
23
            )
24
        );
25
        
26
        \PHPSA\nodeVisitorFactory($statement, $context);
27
28
        $variable = $context->getSymbol("stmtTest");
29
30
        parent::assertTrue($variable instanceof Variable);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (assertTrue() instead of testIfConditionCreatesVar()). Are you sure this is correct? If so, you might want to change this to $this->assertTrue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
31
    }
32
33
    /**
34
     * Tests if (1 == 1) { $stmtTest = 2; } creates the variable
35
     */
36
    public function testIfStatementCreatesVar()
37
    {
38
        $context = $this->getContext();
39
40
        $statement = new Node\Stmt\If_(
41
            new Node\Expr\BinaryOp\Equal($this->newScalarExpr(1), $this->newScalarExpr(1)),
42
            ["stmts" =>
43
                [new Node\Expr\Assign(
44
                    new Node\Expr\Variable(
45
                        new Node\Name("stmtTest")
46
                    ),
47
                    $this->newScalarExpr(2)
48
                )]
49
            ]
50
        );
51
        
52
        \PHPSA\nodeVisitorFactory($statement, $context);
53
54
        $variable = $context->getSymbol("stmtTest");
55
56
        parent::assertTrue($variable instanceof Variable);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (assertTrue() instead of testIfStatementCreatesVar()). Are you sure this is correct? If so, you might want to change this to $this->assertTrue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
57
    }
58
59
    /**
60
     * Tests if (1 == 2) { } else { $stmtTest = 2; } creates the variable
61
     */
62
    public function testIfElseCreatesVar()
63
    {
64
        $context = $this->getContext();
65
66
        $statement = new Node\Stmt\If_(
67
            new Node\Expr\BinaryOp\Equal($this->newScalarExpr(1), $this->newScalarExpr(2)),
68
            ["else" =>
69
                new Node\Stmt\Else_(
70
                    [new Node\Expr\Assign(
71
                        new Node\Expr\Variable(
72
                            new Node\Name("stmtTest")
73
                        ),
74
                        $this->newScalarExpr(2)
75
                    )]
76
                )
77
            ]
78
        );
79
        
80
        \PHPSA\nodeVisitorFactory($statement, $context);
81
82
        $variable = $context->getSymbol("stmtTest");
83
84
        parent::assertTrue($variable instanceof Variable);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (assertTrue() instead of testIfElseCreatesVar()). Are you sure this is correct? If so, you might want to change this to $this->assertTrue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
85
    }
86
87
    /**
88
     * Tests if (1 == 2) { } elseif( 1 == 1) { $stmtTest = 2; } creates the variable
89
     */
90
    public function testIfElseIfCreatesVar()
91
    {
92
        $context = $this->getContext();
93
94
        $statement = new Node\Stmt\If_(
95
            new Node\Expr\BinaryOp\Equal($this->newScalarExpr(1), $this->newScalarExpr(2)),
96
            ["elseifs" =>
97
                [new Node\Stmt\ElseIf_(
98
                    new Node\Expr\BinaryOp\Equal($this->newScalarExpr(1), $this->newScalarExpr(1)),
99
                    [new Node\Expr\Assign(
100
                        new Node\Expr\Variable(
101
                            new Node\Name("stmtTest")
102
                        ),
103
                        $this->newScalarExpr(2)
104
                    )]
105
                )]
106
            ]
107
        );
108
        
109
        \PHPSA\nodeVisitorFactory($statement, $context);
110
111
        $variable = $context->getSymbol("stmtTest");
112
113
        parent::assertTrue($variable instanceof Variable);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (assertTrue() instead of testIfElseIfCreatesVar()). Are you sure this is correct? If so, you might want to change this to $this->assertTrue().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
114
    }
115
}
116