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

IndenticalTest::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 Method

Rating   Name   Duplication   Size   Complexity  
A IndenticalTest::buildExpression() 0 4 1
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 IndenticalTest
12
 * @package Tests\PHPSA\Expression\BinaryOp
13
 */
14
class IndenticalTest extends AbstractBinaryOp
15
{
16
    /**
17
     * @return array
18
     */
19
    public function providerForStaticIntToIntCases()
20
    {
21
        return [
22
            [-1, -1],
23
            [-5, -5],
24
            [-150, -150],
25
            [150, 150],
26
            [150, 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 providerForStaticIntToIntCases
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\Identical(
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 providerForStaticIntToIntCases
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\Identical(
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 providerForStaticIntToFloatCases()
85
    {
86
        return [
87
            [-1, -1.0],
88
            [-5, -5.0],
89
            [-150, -150.0],
90
            [150, 150.0],
91
            [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 providerForStaticIntToIntCases
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 (float) {left-expr} ==== (int) {right-expr}
123
     *
124
     * @param int $a
125
     * @param int $b
126
     *
127
     * @dataProvider providerForStaticIntToIntCases
128
     */
129
    public function testStaticFailFloatToInt($a, $b)
130
    {
131
        $a = (float) $a;
132
133
        $this->assertInternalType('double', $a);
134
        $this->assertInternalType('int', $b);
135
136
        $baseExpression = new Node\Expr\BinaryOp\Identical(
137
            new Node\Scalar\DNumber($a),
138
            new Node\Scalar\LNumber($b)
139
        );
140
        $compiledExpression = $this->compileExpression($baseExpression, $this->getContext());
141
142
        $this->assertInstanceOfCompiledExpression($compiledExpression);
143
        $this->assertSame(CompiledExpression::BOOLEAN, $compiledExpression->getType());
144
        $this->assertSame(false, $compiledExpression->getValue());
145
    }
146
147
    /**
148
     * @param Node\Scalar $a
149
     * @param Node\Scalar $b
150
     * @return Node\Expr\BinaryOp\Identical
151
     */
152
    protected function buildExpression($a, $b)
153
    {
154
        return new Node\Expr\BinaryOp\Identical($a, $b);
155
    }
156
}
157