Completed
Pull Request — master (#178)
by Cláudio
03:52 queued 21s
created

PlusTest::plusResultIntDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Tests\PHPSA\Compiler\Expression\AssignOp;
4
5
use PhpParser\Node;
6
use PHPSA\CompiledExpression;
7
use PHPSA\Compiler\Expression;
8
9
class PlusTest extends \Tests\PHPSA\TestCase
10
{
11
    /**
12
     * Data provider for {var} += {expr} with result type = int
13
     *
14
     * @return array
15
     */
16
    public function plusResultIntDataProvider()
17
    {
18
        return array(
19
            array(2, 2, 4),
20
            array(true, 2, 3),
21
            array(3, true, 4),
22
            array(true, true, 2),
23
            array(2, 0, 2),
24
            array(false, -1, -1),
25
            array(2, false, 2),
26
            array(false, false, 0),
27
            array(-2, 1, -1),
28
            array(true, false, 1),
29
        );
30
    }
31
32
    /**
33
     * Tests {var} += {expr} with result type = int
34
     *
35
     * @dataProvider plusResultIntDataProvider
36
     */
37
    public function testPlusResultInt($a, $b, $c)
38
    {
39
40
        $baseExpression = new Node\Expr\AssignOp\Plus(
41
            $this->newScalarExpr($a),
42
            $this->newScalarExpr($b)
43
        );
44
        $compiledExpression = $this->compileExpression($baseExpression);
45
46
        $this->assertInstanceOfCompiledExpression($compiledExpression);
47
        $this->assertSame(CompiledExpression::INTEGER, $compiledExpression->getType());
48
        $this->assertSame($c, $compiledExpression->getValue());
49
    }
50
51
    /**
52
     * Data provider for {var} += {expr} with result type = double
53
     *
54
     * @return array
55
     */
56
    public function plusResultDoubleDataProvider()
57
    {
58
        return array(
59
            array(2, -1.5, 0.5),
60
            array(1.5, 2, 3.5),
61
            array(true, 1.5, 2.5),
62
            array(1.5, false, 1.5),
63
            array(true, -2.5, -1.5),
64
        );
65
    }
66
67
    /**
68
     * Tests {var} += {expr} with result type = double
69
     *
70
     * @dataProvider plusResultDoubleDataProvider
71
     */
72
    public function testPlusResultDouble($a, $b, $c)
73
    {
74
75
        $baseExpression = new Node\Expr\AssignOp\Plus(
76
            $this->newScalarExpr($a),
77
            $this->newScalarExpr($b)
78
        );
79
        $compiledExpression = $this->compileExpression($baseExpression);
80
81
        $this->assertInstanceOfCompiledExpression($compiledExpression);
82
        $this->assertSame(CompiledExpression::DOUBLE, $compiledExpression->getType());
83
        $this->assertSame($c, $compiledExpression->getValue());
84
    }
85
86
    /**
87
     * Tests {var-type::UNKNOWN} += {right-expr}
88
     */
89
    public function testFirstUnexpectedType()
90
    {
91
        $baseExpression = new Node\Expr\AssignOp\Plus(
92
            $this->newFakeScalarExpr(),
93
            $this->newScalarExpr(1)
94
        );
95
        $compiledExpression = $this->compileExpression($baseExpression);
96
97
        $this->assertInstanceOfCompiledExpression($compiledExpression);
98
        $this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType());
99
        $this->assertSame(null, $compiledExpression->getValue());
100
    }
101
102
    /**
103
     * Tests {var} += {right-expr::UNKNOWN}
104
     */
105
    public function testSecondUnexpectedType()
106
    {
107
        $baseExpression = new Node\Expr\AssignOp\Plus(
108
            $this->newScalarExpr(1),
109
            $this->newFakeScalarExpr()
110
        );
111
        $compiledExpression = $this->compileExpression($baseExpression);
112
113
        $this->assertInstanceOfCompiledExpression($compiledExpression);
114
        $this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType());
115
        $this->assertSame(null, $compiledExpression->getValue());
116
    }
117
}
118