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

NotIndenticalTest::testStaticFloatToFloat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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