Completed
Pull Request — master (#129)
by Kévin
03:41
created

CompiledExpressionTest::testIsArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 9.4285
1
<?php
2
3
namespace Tests\PHPSA;
4
5
use PHPSA\CompiledExpression;
6
7
class CompiledExpressionTest extends TestCase
8
{
9
    public function testToVariableMethod()
10
    {
11
        $compiledExpression = new CompiledExpression(CompiledExpression::INTEGER, 1);
12
        $this->assertInstanceOfCompiledExpression($compiledExpression);
13
14
        $resultVariable = $compiledExpression->toVariable('test');
15
        static::assertInstanceOf('PHPSA\Variable', $resultVariable);
16
        static::assertSame($compiledExpression->getType(), $resultVariable->getType());
17
        static::assertSame($compiledExpression->getValue(), $resultVariable->getValue());
18
    }
19
20
    public function testFromZvalInteger()
21
    {
22
        $result = CompiledExpression::fromZvalValue(1);
23
        $this->assertInstanceOfCompiledExpression($result);
24
        $this->assertSame(CompiledExpression::INTEGER, $result->getType());
25
        $this->assertSame(1, $result->getValue());
26
    }
27
28
    public function testFromZvalBoolean()
29
    {
30
        $result = CompiledExpression::fromZvalValue(true);
31
        $this->assertInstanceOfCompiledExpression($result);
32
        $this->assertSame(CompiledExpression::BOOLEAN, $result->getType());
33
        $this->assertSame(true, $result->getValue());
34
35
        $result = CompiledExpression::fromZvalValue(false);
36
        $this->assertInstanceOfCompiledExpression($result);
37
        $this->assertSame(CompiledExpression::BOOLEAN, $result->getType());
38
        $this->assertSame(false, $result->getValue());
39
    }
40
41
    public function testFromZvalArray()
42
    {
43
        $result = CompiledExpression::fromZvalValue([]);
44
        $this->assertInstanceOfCompiledExpression($result);
45
        $this->assertSame(CompiledExpression::ARR, $result->getType());
46
        $this->assertSame([], $result->getValue());
47
    }
48
49
    public function testFromZvalString()
50
    {
51
        $result = CompiledExpression::fromZvalValue("test string");
52
        $this->assertInstanceOfCompiledExpression($result);
53
        $this->assertSame(CompiledExpression::STRING, $result->getType());
54
        $this->assertSame("test string", $result->getValue());
55
    }
56
57
    public function testFromZvalDouble()
58
    {
59
        $result = CompiledExpression::fromZvalValue(1.0);
60
        $this->assertInstanceOfCompiledExpression($result);
61
        $this->assertSame(CompiledExpression::DOUBLE, $result->getType());
62
        $this->assertSame(1.0, $result->getValue());
63
    }
64
65
    public function testFromZvalNull()
66
    {
67
        $result = CompiledExpression::fromZvalValue(null);
68
        $this->assertInstanceOfCompiledExpression($result);
69
        $this->assertSame(CompiledExpression::NULL, $result->getType());
70
        $this->assertSame(null, $result->getValue());
71
    }
72
73
    /**
74
     * @dataProvider scalarTypeProvider
75
     */
76
    public function testIsScalarWithScalarTypes($expressionType)
77
    {
78
        $compiledExpression = new CompiledExpression($expressionType);
79
        $this->assertTrue($compiledExpression->isScalar());
80
    }
81
82
    public function scalarTypeProvider()
83
    {
84
        return [
85
            [ CompiledExpression::BOOLEAN ],
86
            [ CompiledExpression::STRING ],
87
            [ CompiledExpression::DOUBLE ],
88
            [ CompiledExpression::NUMBER ],
89
            [ CompiledExpression::INTEGER ],
90
        ];
91
    }
92
93
    /**
94
     * @dataProvider nonScalarTypeProvider
95
     */
96
    public function testIsScalarWithNonScalarTypes($expressionType)
97
    {
98
        $compiledExpression = new CompiledExpression($expressionType);
99
        $this->assertFalse($compiledExpression->isScalar());
100
    }
101
102
    public function nonScalarTypeProvider()
103
    {
104
        return [
105
            [ CompiledExpression::UNKNOWN ],
106
            [ CompiledExpression::NULL ],
107
            [ CompiledExpression::ARR ],
108
            [ CompiledExpression::RESOURCE ],
109
            [ CompiledExpression::OBJECT ],
110
            [ CompiledExpression::VOID ],
111
            [ CompiledExpression::CALLABLE_TYPE ],
112
            [ CompiledExpression::VARIABLE ],
113
        ];
114
    }
115
116
    public function testIsTypeKnownWithUnknownType()
117
    {
118
        $compiledExpression = new CompiledExpression(CompiledExpression::UNKNOWN);
119
        $this->assertFalse($compiledExpression->isTypeKnown());
120
    }
121
122
    public function testIsTypeKnownWithKnownType()
123
    {
124
        $compiledExpression = new CompiledExpression(CompiledExpression::BOOLEAN);
125
        $this->assertTrue($compiledExpression->isTypeKnown());
126
    }
127
128
    public function testHasValueWithAValue()
129
    {
130
        $compiledExpression = new CompiledExpression(CompiledExpression::BOOLEAN, false);
131
        $this->assertTrue($compiledExpression->hasValue());
132
    }
133
134
    public function testHasValueWithAScalarTypeAndNoValue()
135
    {
136
        $compiledExpression = new CompiledExpression(CompiledExpression::BOOLEAN, /* just to be explicit */ null);
137
        $this->assertFalse($compiledExpression->hasValue());
138
    }
139
140
    public function testHasValueWithANullType()
141
    {
142
        $compiledExpression = new CompiledExpression(CompiledExpression::NULL);
143
        $this->assertTrue($compiledExpression->hasValue());
144
    }
145
146
    public function testCanBeObject()
147
    {
148
        // Mixed type can be object
149
        $expr = new CompiledExpression(CompiledExpression::MIXED, null);
150
        self::assertTrue($expr->canBeObject());
151
152
        // Integer type can't be object
153
        $expr2 = new CompiledExpression(CompiledExpression::INTEGER, 1);
154
        self::assertFalse($expr2->canBeObject());
155
    }
156
157
    public function testIsObject()
158
    {
159
        // Mixed type could be object but it's unclear
160
        $expr = new CompiledExpression(CompiledExpression::MIXED, null);
161
        self::assertFalse($expr->isObject());
162
163
        // Object type is object
164
        $expr2 = new CompiledExpression(CompiledExpression::OBJECT, null);
165
        self::assertTrue($expr2->isObject());
166
    }
167
168
    public function testIsArray()
169
    {
170
        $compiledExpression = new CompiledExpression(CompiledExpression::ARR);
171
        $this->assertTrue($compiledExpression->isArray());
172
    }
173
174
    public function testIsArrayWhenFalse()
175
    {
176
        $compiledExpression = new CompiledExpression(CompiledExpression::BOOLEAN);
177
        $this->assertFalse($compiledExpression->isArray());
178
    }
179
}
180