Completed
Pull Request — master (#304)
by Enrico
02:41
created

EqualTest::testSecondUnexpectedTypes()   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
use Tests\PHPSA\Compiler\Expression\AbstractBinaryOp;
9
10
/**
11
 * Class EqualTest
12
 * @package Tests\PHPSA\Expression\BinaryOp
13
 */
14
class EqualTest extends AbstractBinaryOp
15
{
16
    /**
17
     * @return array
18
     */
19
    public function providerForStaticEqualsTrue()
20
    {
21
        return [
22
            [-1, -1],
23
            [-1, -1.0],
24
            [-5, -5],
25
            [-5, -5.0],
26
            [-5.0, -5],
27
            [-150, -150],
28
            [-150.0, -150],
29
            [-150, -150.0],
30
            [150, 150],
31
            [150, 150.0],
32
            [150.0, 150],
33
            [150.0, 150.0],
34
            //boolean true
35
            [true, true],
36
            [true, 1],
37
            [1, true],
38
            // boolean false
39
            [false, false],
40
            [false, 0],
41
            [0, false],
42
            // empty arrays
43
            [[], false],
44
            [false, []],
45
            [[], []],
46
        ];
47
    }
48
49
    /**
50
     * Tests {left-expr} == {right-expr}
51
     *
52
     * @param int $a
53
     * @param int $b
54
     *
55
     * @dataProvider providerForStaticEqualsTrue
56
     */
57
    public function testStaticEqualsTrue($a, $b)
58
    {
59
        $baseExpression = new Node\Expr\BinaryOp\Equal(
60
            $this->newScalarExpr($a),
61
            $this->newScalarExpr($b)
62
        );
63
        $compiledExpression = $this->compileExpression($baseExpression, $this->getContext());
64
65
        $this->assertInstanceOfCompiledExpression($compiledExpression);
66
        $this->assertSame(CompiledExpression::BOOLEAN, $compiledExpression->getType());
67
        $this->assertSame(true, $compiledExpression->getValue());
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function providerForStaticEqualsFalse()
74
    {
75
        return [
76
            [-1, 150],
77
            [-1, 1],
78
            [0, 1],
79
            [1, 0],
80
            [true, 0],
81
            [0, true],
82
            [false, true],
83
            [false, 1],
84
            [1, false],
85
            [true, []],
86
            [[], true],
87
        ];
88
    }
89
90
    /**
91
     * Tests {left-expr} == {right-expr} but for false
92
     *
93
     * @param int $a
94
     * @param int $b
95
     *
96
     * @dataProvider providerForStaticEqualsFalse
97
     */
98
    public function testStaticEqualsFalse($a, $b)
99
    {
100
        $baseExpression = new Node\Expr\BinaryOp\Equal(
101
            $this->newScalarExpr($a),
102
            $this->newScalarExpr($b)
103
        );
104
        $compiledExpression = $this->compileExpression($baseExpression, $this->getContext());
105
106
        $this->assertInstanceOfCompiledExpression($compiledExpression);
107
        $this->assertSame(CompiledExpression::BOOLEAN, $compiledExpression->getType());
108
        $this->assertSame(false, $compiledExpression->getValue());
109
    }
110
111
    /**
112
     * @param Node\Scalar $a
113
     * @param Node\Scalar $b
114
     * @return Node\Expr\BinaryOp\Equal
115
     */
116
    protected function buildExpression($a, $b)
117
    {
118
        return new Node\Expr\BinaryOp\Equal($a, $b);
119
    }
120
}
121