Completed
Pull Request — master (#124)
by Enrico
03:49
created

EqualTest::testFirstUnexpectedTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Tests\PHPSA\Compiler\Expression\BinaryOp;
4
5
use PhpParser\Node;
6
use PHPSA\CompiledExpression;
7
use PHPSA\Compiler\Expression;
8
9
/**
10
 * Class EqualTest
11
 * @package Tests\PHPSA\Expression\BinaryOp
12
 */
13
class EqualTest extends \Tests\PHPSA\TestCase
14
{
15
    /**
16
     * @return array
17
     */
18
    public function providerForStaticEqualsTrue()
19
    {
20
        return array(
21
            array(-1, -1),
22
            array(-1, -1.0),
23
            array(-5, -5),
24
            array(-5, -5.0),
25
            array(-5.0, -5),
26
            array(-150, -150),
27
            array(-150.0, -150),
28
            array(-150, -150.0),
29
            array(150, 150),
30
            array(150, 150.0),
31
            array(150.0, 150),
32
            array(150.0, 150.0),
33
            //boolean true
34
            array(true, true),
35
            array(true, 1),
36
            array(1, true),
37
            // boolean false
38
            array(false, false),
39
            array(false, 0),
40
            array(0, false),
41
            // empty arrays
42
            array(array(), false),
43
            array(false, array()),
44
            array(array(), array()),
45
        );
46
    }
47
48
    /**
49
     * Tests {left-expr} == {right-expr}
50
     *
51
     * @param int $a
52
     * @param int $b
53
     *
54
     * @dataProvider providerForStaticEqualsTrue
55
     */
56
    public function testStaticEqualsTrue($a, $b)
57
    {
58
        $baseExpression = new Node\Expr\BinaryOp\Equal(
59
            $this->newScalarExpr($a),
60
            $this->newScalarExpr($b)
61
        );
62
        $compiledExpression = $this->compileExpression($baseExpression, $this->getContext());
63
64
        $this->assertInstanceOfCompiledExpression($compiledExpression);
65
        $this->assertSame(CompiledExpression::BOOLEAN, $compiledExpression->getType());
66
        $this->assertSame(true, $compiledExpression->getValue());
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function providerForStaticEqualsFalse()
73
    {
74
        return array(
75
            array(-1, 150),
76
            array(-1, 1),
77
            array(0, 1),
78
            array(1, 0),
79
            array(true, 0),
80
            array(0, true),
81
            array(false, true),
82
            array(false, 1),
83
            array(1, false),
84
            array(true, array()),
85
            array(array(), true),
86
        );
87
    }
88
89
    /**
90
     * Tests {left-expr} == {right-expr} but for false
91
     *
92
     * @param int $a
93
     * @param int $b
94
     *
95
     * @dataProvider providerForStaticEqualsFalse
96
     */
97
    public function testStaticEqualsFalse($a, $b)
98
    {
99
        $baseExpression = new Node\Expr\BinaryOp\Equal(
100
            $this->newScalarExpr($a),
101
            $this->newScalarExpr($b)
102
        );
103
        $compiledExpression = $this->compileExpression($baseExpression, $this->getContext());
104
105
        $this->assertInstanceOfCompiledExpression($compiledExpression);
106
        $this->assertSame(CompiledExpression::BOOLEAN, $compiledExpression->getType());
107
        $this->assertSame(false, $compiledExpression->getValue());
108
    }
109
110
    /**
111
     * Tests {left-expr::UNKNOWN} == {right-expr}
112
     */
113
    public function testFirstUnexpectedTypes()
114
    {
115
        $baseExpression = new Node\Expr\BinaryOp\Equal(
116
            $this->newFakeScalarExpr(),
117
            $this->newScalarExpr(1)
118
        );
119
        $compiledExpression = $this->compileExpression($baseExpression, $this->getContext());
120
121
        $this->assertInstanceOfCompiledExpression($compiledExpression);
122
        $this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType());
123
        $this->assertSame(null, $compiledExpression->getValue());
124
    }
125
126
    /**
127
     * Tests {left-expr} == {right-expr::UNKNOWN}
128
     */
129
    public function testSecondUnexpectedTypes()
130
    {
131
        $baseExpression = new Node\Expr\BinaryOp\Equal(
132
            $this->newScalarExpr(1),
133
            $this->newFakeScalarExpr()
134
        );
135
        $compiledExpression = $this->compileExpression($baseExpression, $this->getContext());
136
137
        $this->assertInstanceOfCompiledExpression($compiledExpression);
138
        $this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType());
139
        $this->assertSame(null, $compiledExpression->getValue());
140
    }
141
}
142