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

IndenticalTest::testStaticFailIntToFloat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 2
dl 0
loc 17
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 IndenticalTest
11
 * @package Tests\PHPSA\Expression\BinaryOp
12
 */
13
class IndenticalTest extends \Tests\PHPSA\TestCase
14
{
15
    /**
16
     * @return array
17
     */
18
    public function testProviderForStaticIntToIntCases()
19
    {
20
        return array(
21
            array(-1, -1),
22
            array(-5, -5),
23
            array(-150, -150),
24
            array(150, 150),
25
            array(150, 150),
26
        );
27
    }
28
29
    /**
30
     * Tests (int) {left-expr} ==== (int) {right-expr}
31
     *
32
     * @param int $a
33
     * @param int $b
34
     *
35
     * @dataProvider testProviderForStaticIntToIntCases
36
     */
37
    public function testStaticIntToInt($a, $b)
38
    {
39
        $this->assertInternalType('int', $a);
40
        $this->assertInternalType('int', $b);
41
42
        $baseExpression = new Node\Expr\BinaryOp\Identical(
43
            new Node\Scalar\LNumber($a),
44
            new Node\Scalar\LNumber($b)
45
        );
46
        $compiledExpression = $this->compileExpression($baseExpression, $this->getContext());
47
48
        $this->assertInstanceOfCompiledExpression($compiledExpression);
49
        $this->assertSame(CompiledExpression::BOOLEAN, $compiledExpression->getType());
50
        $this->assertSame(true, $compiledExpression->getValue());
51
    }
52
53
    /**
54
     * Tests (float) {left-expr} ==== (float) {right-expr}
55
     *
56
     * @param int $a
57
     * @param int $b
58
     *
59
     * @dataProvider testProviderForStaticIntToIntCases
60
     */
61
    public function testStaticFloatToFloat($a, $b)
62
    {
63
        $a = (float) $a;
64
        $b = (float) $b;
65
66
        $this->assertInternalType('double', $a);
67
        $this->assertInternalType('double', $b);
68
69
        $baseExpression = new Node\Expr\BinaryOp\Identical(
70
            /**
71
             * Cheating - float casting
72
             */
73
            new Node\Scalar\DNumber($a),
74
            new Node\Scalar\DNumber($b)
75
        );
76
        $compiledExpression = $this->compileExpression($baseExpression, $this->getContext());
77
78
        $this->assertInstanceOfCompiledExpression($compiledExpression);
79
        $this->assertSame(CompiledExpression::BOOLEAN, $compiledExpression->getType());
80
        $this->assertSame(true, $compiledExpression->getValue());
81
    }
82
83
    public function testProviderForStaticIntToFloatCases()
84
    {
85
        return array(
86
            array(-1, -1.0),
87
            array(-5, -5.0),
88
            array(-150, -150.0),
89
            array(150, 150.0),
90
            array(150, 150.0),
91
        );
92
    }
93
94
    /**
95
     * Tests (int) {left-expr} ==== (float) {right-expr}
96
     *
97
     * @param int $a
98
     * @param int $b
99
     *
100
     * @dataProvider testProviderForStaticIntToIntCases
101
     */
102
    public function testStaticFailIntToFloat($a, $b)
103
    {
104
        $b = (float) $b;
105
106
        $this->assertInternalType('int', $a);
107
        $this->assertInternalType('double', $b);
108
109
        $baseExpression = new Node\Expr\BinaryOp\Identical(
110
            new Node\Scalar\LNumber($a),
111
            new Node\Scalar\DNumber($b)
112
        );
113
        $compiledExpression = $this->compileExpression($baseExpression, $this->getContext());
114
115
        $this->assertInstanceOfCompiledExpression($compiledExpression);
116
        $this->assertSame(CompiledExpression::BOOLEAN, $compiledExpression->getType());
117
        $this->assertSame(false, $compiledExpression->getValue());
118
    }
119
120
    /**
121
     * Tests (float) {left-expr} ==== (int) {right-expr}
122
     *
123
     * @param int $a
124
     * @param int $b
125
     *
126
     * @dataProvider testProviderForStaticIntToIntCases
127
     */
128
    public function testStaticFailFloatToInt($a, $b)
129
    {
130
        $a = (float) $a;
131
132
        $this->assertInternalType('double', $a);
133
        $this->assertInternalType('int', $b);
134
135
        $baseExpression = new Node\Expr\BinaryOp\Identical(
136
            new Node\Scalar\DNumber($a),
137
            new Node\Scalar\LNumber($b)
138
        );
139
        $compiledExpression = $this->compileExpression($baseExpression, $this->getContext());
140
141
        $this->assertInstanceOfCompiledExpression($compiledExpression);
142
        $this->assertSame(CompiledExpression::BOOLEAN, $compiledExpression->getType());
143
        $this->assertSame(false, $compiledExpression->getValue());
144
    }
145
146
    /**
147
     * Tests {left-expr::UNKNOWN} === {right-expr}
148
     */
149
    public function testFirstUnexpectedTypes()
150
    {
151
        $baseExpression = new Node\Expr\BinaryOp\Identical(
152
            $this->newFakeScalarExpr(),
153
            $this->newScalarExpr(1)
154
        );
155
        $compiledExpression = $this->compileExpression($baseExpression, $this->getContext());
156
157
        $this->assertInstanceOfCompiledExpression($compiledExpression);
158
        $this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType());
159
        $this->assertSame(null, $compiledExpression->getValue());
160
    }
161
162
    /**
163
     * Tests {left-expr} === {right-expr::UNKNOWN}
164
     */
165
    public function testSecondUnexpectedTypes()
166
    {
167
        $baseExpression = new Node\Expr\BinaryOp\Identical(
168
            $this->newScalarExpr(1),
169
            $this->newFakeScalarExpr()
170
        );
171
        $compiledExpression = $this->compileExpression($baseExpression, $this->getContext());
172
173
        $this->assertInstanceOfCompiledExpression($compiledExpression);
174
        $this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType());
175
        $this->assertSame(null, $compiledExpression->getValue());
176
    }
177
}
178