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
|
|
|
self::assertInstanceOfCompiledExpression($compiledExpression); |
13
|
|
|
|
14
|
|
|
$resultVariable = $compiledExpression->toVariable('test'); |
15
|
|
|
self::assertInstanceOf('PHPSA\Variable', $resultVariable); |
16
|
|
|
self::assertSame($compiledExpression->getType(), $resultVariable->getType()); |
17
|
|
|
self::assertSame($compiledExpression->getValue(), $resultVariable->getValue()); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testFromZvalInteger() |
21
|
|
|
{ |
22
|
|
|
$result = CompiledExpression::fromZvalValue(1); |
23
|
|
|
self::assertInstanceOfCompiledExpression($result); |
24
|
|
|
self::assertSame(CompiledExpression::INTEGER, $result->getType()); |
25
|
|
|
self::assertSame(1, $result->getValue()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testFromZvalBoolean() |
29
|
|
|
{ |
30
|
|
|
$result = CompiledExpression::fromZvalValue(true); |
31
|
|
|
self::assertInstanceOfCompiledExpression($result); |
32
|
|
|
self::assertSame(CompiledExpression::BOOLEAN, $result->getType()); |
33
|
|
|
self::assertSame(true, $result->getValue()); |
34
|
|
|
|
35
|
|
|
$result = CompiledExpression::fromZvalValue(false); |
36
|
|
|
self::assertInstanceOfCompiledExpression($result); |
37
|
|
|
self::assertSame(CompiledExpression::BOOLEAN, $result->getType()); |
38
|
|
|
self::assertSame(false, $result->getValue()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testFromZvalArray() |
42
|
|
|
{ |
43
|
|
|
$result = CompiledExpression::fromZvalValue([]); |
44
|
|
|
self::assertInstanceOfCompiledExpression($result); |
45
|
|
|
self::assertSame(CompiledExpression::ARR, $result->getType()); |
46
|
|
|
self::assertSame([], $result->getValue()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testFromZvalString() |
50
|
|
|
{ |
51
|
|
|
$result = CompiledExpression::fromZvalValue('test string'); |
52
|
|
|
self::assertInstanceOfCompiledExpression($result); |
53
|
|
|
self::assertSame(CompiledExpression::STRING, $result->getType()); |
54
|
|
|
self::assertSame('test string', $result->getValue()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testFromZvalDouble() |
58
|
|
|
{ |
59
|
|
|
$result = CompiledExpression::fromZvalValue(1.0); |
60
|
|
|
self::assertInstanceOfCompiledExpression($result); |
61
|
|
|
self::assertSame(CompiledExpression::DOUBLE, $result->getType()); |
62
|
|
|
self::assertSame(1.0, $result->getValue()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testFromZvalNull() |
66
|
|
|
{ |
67
|
|
|
$result = CompiledExpression::fromZvalValue(null); |
68
|
|
|
self::assertInstanceOfCompiledExpression($result); |
69
|
|
|
self::assertSame(CompiledExpression::NULL, $result->getType()); |
70
|
|
|
self::assertSame(null, $result->getValue()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @dataProvider scalarTypeProvider |
75
|
|
|
*/ |
76
|
|
|
public function testIsScalarWithScalarTypes($expressionType) |
77
|
|
|
{ |
78
|
|
|
$compiledExpression = new CompiledExpression($expressionType); |
79
|
|
|
self::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
|
|
|
self::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 isTypeKnownProvider() |
117
|
|
|
{ |
118
|
|
|
return [ |
119
|
|
|
[ CompiledExpression::INTEGER ], |
120
|
|
|
[ CompiledExpression::DOUBLE ], |
121
|
|
|
[ CompiledExpression::NUMBER ], |
122
|
|
|
[ CompiledExpression::STRING ], |
123
|
|
|
[ CompiledExpression::ARR ], |
124
|
|
|
[ CompiledExpression::BOOLEAN ], |
125
|
|
|
[ CompiledExpression::RESOURCE ], |
126
|
|
|
[ CompiledExpression::CALLABLE_TYPE ], |
127
|
|
|
[ CompiledExpression::NULL ], |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @dataProvider isTypeKnownProvider |
133
|
|
|
* @param int $type |
134
|
|
|
*/ |
135
|
|
|
public function testIsTypeKnownTrue($type) |
136
|
|
|
{ |
137
|
|
|
$compiledExpression = new CompiledExpression($type); |
138
|
|
|
self::assertTrue($compiledExpression->isTypeKnown()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testIsTypeKnownWithUnknownType() |
142
|
|
|
{ |
143
|
|
|
$compiledExpression = new CompiledExpression(CompiledExpression::UNKNOWN); |
144
|
|
|
self::assertFalse($compiledExpression->isTypeKnown()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testHasValueWithAValue() |
148
|
|
|
{ |
149
|
|
|
$compiledExpression = new CompiledExpression(CompiledExpression::BOOLEAN, false); |
150
|
|
|
self::assertTrue($compiledExpression->hasValue()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testHasValueWithAScalarTypeAndNoValue() |
154
|
|
|
{ |
155
|
|
|
$compiledExpression = new CompiledExpression(CompiledExpression::BOOLEAN, /* just to be explicit */ null); |
156
|
|
|
self::assertFalse($compiledExpression->hasValue()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testHasValueWithANullType() |
160
|
|
|
{ |
161
|
|
|
$compiledExpression = new CompiledExpression(CompiledExpression::NULL); |
162
|
|
|
self::assertTrue($compiledExpression->hasValue()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testCanBeObject() |
166
|
|
|
{ |
167
|
|
|
// Mixed type can be object |
168
|
|
|
$expr = new CompiledExpression(CompiledExpression::MIXED, null); |
169
|
|
|
self::assertTrue($expr->canBeObject()); |
170
|
|
|
|
171
|
|
|
// Integer type can't be object |
172
|
|
|
$expr2 = new CompiledExpression(CompiledExpression::INTEGER, 1); |
173
|
|
|
self::assertFalse($expr2->canBeObject()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testIsObject() |
177
|
|
|
{ |
178
|
|
|
// Mixed type could be object but it's unclear |
179
|
|
|
$expr = new CompiledExpression(CompiledExpression::MIXED, null); |
180
|
|
|
self::assertFalse($expr->isObject()); |
181
|
|
|
|
182
|
|
|
// Object type is object |
183
|
|
|
$expr2 = new CompiledExpression(CompiledExpression::OBJECT, null); |
184
|
|
|
self::assertTrue($expr2->isObject()); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testIsArray() |
188
|
|
|
{ |
189
|
|
|
$compiledExpression = new CompiledExpression(CompiledExpression::ARR); |
190
|
|
|
self::assertTrue($compiledExpression->isArray()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testIsArrayWhenFalse() |
194
|
|
|
{ |
195
|
|
|
$compiledExpression = new CompiledExpression(CompiledExpression::BOOLEAN); |
196
|
|
|
self::assertFalse($compiledExpression->isArray()); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|