Completed
Pull Request — master (#304)
by Enrico
03:09
created

NotIndenticalTest::testStaticIntToFloat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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