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