1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\PHPSA; |
4
|
|
|
|
5
|
|
|
use PHPSA\CompiledExpression; |
6
|
|
|
use PHPSA\Variable; |
7
|
|
|
|
8
|
|
|
class VariableTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
public function testSimpleInstanceWithDefaultValue() |
11
|
|
|
{ |
12
|
|
|
$variable = new Variable('test', 1, CompiledExpression::INTEGER); |
13
|
|
|
|
14
|
|
|
static::assertSame(CompiledExpression::INTEGER, $variable->getType()); |
15
|
|
|
static::assertSame('test', $variable->getName()); |
16
|
|
|
static::assertSame(1, $variable->getValue()); |
17
|
|
|
static::assertSame(0, $variable->getGets()); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* it's one via default value is present |
21
|
|
|
*/ |
22
|
|
|
static::assertSame(1, $variable->getSets()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testIsNumericMethodIntSuccess() |
26
|
|
|
{ |
27
|
|
|
$variable = new Variable('a', 1, CompiledExpression::INTEGER); |
28
|
|
|
static::assertTrue($variable->isNumeric()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testIsNumericMethodDoubleSuccess() |
32
|
|
|
{ |
33
|
|
|
$variable = new Variable('a', 1, CompiledExpression::DOUBLE); |
34
|
|
|
static::assertTrue($variable->isNumeric()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testIsNumericMethodFalseOnBoolean() |
38
|
|
|
{ |
39
|
|
|
$variable = new Variable('a', 1, CompiledExpression::BOOLEAN); |
40
|
|
|
static::assertFalse($variable->isNumeric()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testIncValue() |
44
|
|
|
{ |
45
|
|
|
$variable = new Variable('a', 1, CompiledExpression::INTEGER); |
46
|
|
|
static::assertSame(1, $variable->getValue()); |
47
|
|
|
|
48
|
|
|
$variable->inc(); |
49
|
|
|
static::assertSame(2, $variable->getValue()); |
50
|
|
|
|
51
|
|
|
$variable->inc(); |
52
|
|
|
static::assertSame(3, $variable->getValue()); |
53
|
|
|
|
54
|
|
|
$variable->inc(); |
55
|
|
|
$variable->inc(); |
56
|
|
|
static::assertSame(5, $variable->getValue()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testDecValue() |
60
|
|
|
{ |
61
|
|
|
$variable = new Variable('a', 1, CompiledExpression::INTEGER); |
62
|
|
|
static::assertSame(1, $variable->getValue()); |
63
|
|
|
|
64
|
|
|
$variable->dec(); |
65
|
|
|
static::assertSame(0, $variable->getValue()); |
66
|
|
|
|
67
|
|
|
$variable->dec(); |
68
|
|
|
static::assertSame(-1, $variable->getValue()); |
69
|
|
|
|
70
|
|
|
$variable->dec(); |
71
|
|
|
$variable->dec(); |
72
|
|
|
static::assertSame(-3, $variable->getValue()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testIncUse() |
76
|
|
|
{ |
77
|
|
|
$variable = new Variable('a', null, CompiledExpression::UNKNOWN); |
78
|
|
|
static::assertSame(0, $variable->getGets()); |
79
|
|
|
static::assertSame(0, $variable->getSets()); |
80
|
|
|
|
81
|
|
|
$variable->incUse(); |
82
|
|
|
static::assertSame(1, $variable->getGets()); |
83
|
|
|
static::assertSame(1, $variable->getSets()); |
84
|
|
|
|
85
|
|
|
$variable->incUse(); |
86
|
|
|
static::assertSame(2, $variable->getGets()); |
87
|
|
|
static::assertSame(2, $variable->getSets()); |
88
|
|
|
|
89
|
|
|
$variable->incUse(); |
90
|
|
|
$variable->incUse(); |
91
|
|
|
static::assertSame(4, $variable->getGets()); |
92
|
|
|
static::assertSame(4, $variable->getSets()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testIncSets() |
96
|
|
|
{ |
97
|
|
|
$variable = new Variable('a', null, CompiledExpression::UNKNOWN); |
98
|
|
|
static::assertSame(0, $variable->getSets()); |
99
|
|
|
|
100
|
|
|
$variable->incSets(); |
101
|
|
|
static::assertSame(1, $variable->getSets()); |
102
|
|
|
|
103
|
|
|
$variable->incSets(); |
104
|
|
|
static::assertSame(2, $variable->getSets()); |
105
|
|
|
|
106
|
|
|
$variable->incSets(); |
107
|
|
|
$variable->incSets(); |
108
|
|
|
static::assertSame(4, $variable->getSets()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testIncGets() |
112
|
|
|
{ |
113
|
|
|
$variable = new Variable('a', null, CompiledExpression::UNKNOWN); |
114
|
|
|
static::assertSame(0, $variable->getGets()); |
115
|
|
|
|
116
|
|
|
$variable->incGets(); |
117
|
|
|
static::assertSame(1, $variable->getGets()); |
118
|
|
|
|
119
|
|
|
$variable->incGets(); |
120
|
|
|
static::assertSame(2, $variable->getGets()); |
121
|
|
|
|
122
|
|
|
$variable->incGets(); |
123
|
|
|
$variable->incGets(); |
124
|
|
|
static::assertSame(4, $variable->getGets()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testModifyType() |
128
|
|
|
{ |
129
|
|
|
$variable = new Variable('a', 1, CompiledExpression::INTEGER); |
130
|
|
|
static::assertSame(CompiledExpression::INTEGER, $variable->getType()); |
131
|
|
|
|
132
|
|
|
$newType = CompiledExpression::BOOLEAN; |
133
|
|
|
$variable->modifyType($newType); |
134
|
|
|
static::assertSame($newType, $variable->getType()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testIsUnusedTrue() |
138
|
|
|
{ |
139
|
|
|
$variable = new Variable('a', 1, CompiledExpression::INTEGER); |
140
|
|
|
static::assertTrue($variable->isUnused()); |
141
|
|
|
|
142
|
|
|
$variable = new Variable('a', null, CompiledExpression::UNKNOWN); |
143
|
|
|
$variable->incSets(); |
144
|
|
|
static::assertTrue($variable->isUnused()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testIsUnusedFalse() |
148
|
|
|
{ |
149
|
|
|
$variable = new Variable('a', null, CompiledExpression::UNKNOWN); |
150
|
|
|
static::assertFalse($variable->isUnused()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testReferenceToChange() |
154
|
|
|
{ |
155
|
|
|
/** |
156
|
|
|
* $a = 1; |
157
|
|
|
* $b = &$a; |
158
|
|
|
*/ |
159
|
|
|
$parentVariable = new Variable('a', 1, CompiledExpression::INTEGER); |
160
|
|
|
$variable = new Variable('b', $parentVariable->getValue(), $parentVariable->getType()); |
161
|
|
|
|
162
|
|
|
static::assertFalse($variable->isReferenced()); |
163
|
|
|
$variable->setReferencedTo($parentVariable); |
164
|
|
|
static::assertTrue($variable->isReferenced()); |
165
|
|
|
static::assertSame($parentVariable, $variable->getReferencedTo()); |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* $b = 55.00 |
169
|
|
|
*/ |
170
|
|
|
$variable->modify(CompiledExpression::DOUBLE, 55.00); |
171
|
|
|
|
172
|
|
|
static::assertSame($variable->getValue(), $parentVariable->getValue()); |
173
|
|
|
static::assertSame($variable->getType(), $parentVariable->getType()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testGetTypeName() |
177
|
|
|
{ |
178
|
|
|
$int = new Variable('a', 1, CompiledExpression::INTEGER); |
179
|
|
|
parent::assertSame("integer", $int->getTypeName()); |
|
|
|
|
180
|
|
|
|
181
|
|
|
$double = new Variable('b', 1, CompiledExpression::DOUBLE); |
182
|
|
|
parent::assertSame("double", $double->getTypeName()); |
|
|
|
|
183
|
|
|
|
184
|
|
|
$number = new Variable('c', 1, CompiledExpression::NUMBER); |
185
|
|
|
parent::assertSame("number", $number->getTypeName()); |
|
|
|
|
186
|
|
|
|
187
|
|
|
$arr = new Variable('d', [1,2], CompiledExpression::ARR); |
188
|
|
|
parent::assertSame("array", $arr->getTypeName()); |
|
|
|
|
189
|
|
|
|
190
|
|
|
$object = new Variable('e', 1, CompiledExpression::OBJECT); |
191
|
|
|
parent::assertSame("object", $object->getTypeName()); |
|
|
|
|
192
|
|
|
|
193
|
|
|
$resource = new Variable('f', 1, CompiledExpression::RESOURCE); |
194
|
|
|
parent::assertSame("resource", $resource->getTypeName()); |
|
|
|
|
195
|
|
|
|
196
|
|
|
$callable = new Variable('g', 1, CompiledExpression::CALLABLE_TYPE); |
197
|
|
|
parent::assertSame("callable", $callable->getTypeName()); |
|
|
|
|
198
|
|
|
|
199
|
|
|
$boolean = new Variable('h', 1, CompiledExpression::BOOLEAN); |
200
|
|
|
parent::assertSame("boolean", $boolean->getTypeName()); |
|
|
|
|
201
|
|
|
|
202
|
|
|
$null = new Variable('i', 1, CompiledExpression::NULL); |
203
|
|
|
parent::assertSame("null", $null->getTypeName()); |
|
|
|
|
204
|
|
|
|
205
|
|
|
$unknown = new Variable('j', 1, CompiledExpression::UNKNOWN); |
206
|
|
|
parent::assertSame("unknown", $unknown->getTypeName()); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
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.