1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MabeEnumTest; |
4
|
|
|
|
5
|
|
|
use MabeEnumTest\TestAsset\EnumBasic; |
6
|
|
|
use MabeEnumTest\TestAsset\EnumInheritance; |
7
|
|
|
use MabeEnumTest\TestAsset\EnumAmbiguous; |
8
|
|
|
use MabeEnumTest\TestAsset\EnumExtendedAmbiguous; |
9
|
|
|
use MabeEnumTest\TestAsset\ConstVisibilityEnum; |
10
|
|
|
use MabeEnumTest\TestAsset\ConstVisibilityEnumExtended; |
11
|
|
|
use MabeEnumTest\TestAsset\SerializableEnum; |
12
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
13
|
|
|
use ReflectionClass; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Unit tests for the class MabeEnum\Enum |
17
|
|
|
* |
18
|
|
|
* @link http://github.com/marc-mabe/php-enum for the canonical source repository |
19
|
|
|
* @copyright Copyright (c) 2015 Marc Bennewitz |
20
|
|
|
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License |
21
|
|
|
*/ |
22
|
|
|
class EnumTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
public function setUp() |
25
|
|
|
{ |
26
|
|
|
EnumBasic::clear(); |
27
|
|
|
EnumInheritance::clear(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testGetNameReturnsConstantNameOfCurrentValue() |
31
|
|
|
{ |
32
|
|
|
$enum = EnumBasic::get(EnumBasic::ONE); |
33
|
|
|
$this->assertSame('ONE', $enum->getName()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testToStringMagicMethodReturnsName() |
37
|
|
|
{ |
38
|
|
|
$enum = EnumBasic::get(EnumBasic::ONE); |
39
|
|
|
$this->assertSame('ONE', $enum->__toString()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testEnumInheritance() |
43
|
|
|
{ |
44
|
|
|
$this->assertSame(array( |
45
|
|
|
'ONE' => 1, |
46
|
|
|
'TWO' => 2, |
47
|
|
|
'THREE' => 3, |
48
|
|
|
'FOUR' => 4, |
49
|
|
|
'FIVE' => 5, |
50
|
|
|
'SIX' => 6, |
51
|
|
|
'SEVEN' => 7, |
52
|
|
|
'EIGHT' => 8, |
53
|
|
|
'NINE' => 9, |
54
|
|
|
'ZERO' => 0, |
55
|
|
|
'FLOAT' => 0.123, |
56
|
|
|
'STR' => 'str', |
57
|
|
|
'STR_EMPTY' => '', |
58
|
|
|
'NIL' => null, |
59
|
|
|
'BOOLEAN_TRUE' => true, |
60
|
|
|
'BOOLEAN_FALSE' => false, |
61
|
|
|
'INHERITANCE' => 'Inheritance', |
62
|
|
|
), EnumInheritance::getConstants()); |
63
|
|
|
|
64
|
|
|
$enum = EnumInheritance::get(EnumInheritance::ONE); |
65
|
|
|
$this->assertSame(EnumInheritance::ONE, $enum->getValue()); |
66
|
|
|
$this->assertSame(0, $enum->getOrdinal()); |
67
|
|
|
|
68
|
|
|
$enum = EnumInheritance::get(EnumInheritance::INHERITANCE); |
69
|
|
|
$this->assertSame(EnumInheritance::INHERITANCE, $enum->getValue()); |
70
|
|
|
$this->assertSame(16, $enum->getOrdinal()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testGetWithStrictValue() |
74
|
|
|
{ |
75
|
|
|
$enum = EnumBasic::get(EnumBasic::ONE); |
76
|
|
|
$this->assertSame(1, $enum->getValue()); |
77
|
|
|
$this->assertSame(0, $enum->getOrdinal()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testGetWithNonStrictValueThrowsInvalidArgumentException() |
81
|
|
|
{ |
82
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
83
|
|
|
EnumBasic::get((string)EnumBasic::TWO); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testGetWithInvalidValueThrowsInvalidArgumentException() |
87
|
|
|
{ |
88
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
89
|
|
|
EnumBasic::get('unknown'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testGetWithInvalidTypeOfValueThrowsInvalidArgumentException() |
93
|
|
|
{ |
94
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
95
|
|
|
EnumBasic::get(array()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testGetByInstance() |
99
|
|
|
{ |
100
|
|
|
$enum1 = EnumBasic::get(EnumBasic::ONE); |
101
|
|
|
$enum2 = EnumBasic::get($enum1); |
102
|
|
|
$this->assertSame($enum1, $enum2); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testGetByExtendedInstanceOfKnownValue() |
106
|
|
|
{ |
107
|
|
|
$enum = EnumInheritance::get(EnumInheritance::ONE); |
108
|
|
|
|
109
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
110
|
|
|
EnumBasic::get($enum); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testGetEnumerators() |
114
|
|
|
{ |
115
|
|
|
$constants = EnumInheritance::getConstants(); |
116
|
|
|
$enumerators = EnumInheritance::getEnumerators(); |
117
|
|
|
$count = count($enumerators); |
118
|
|
|
|
119
|
|
|
$this->assertSame(count($constants), $count); |
120
|
|
|
for ($i = 0; $i < $count; ++$i) { |
121
|
|
|
$this->assertArrayHasKey($i, $enumerators); |
122
|
|
|
$this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enumerators[$i]); |
123
|
|
|
|
124
|
|
|
$enumerator = $enumerators[$i]; |
125
|
|
|
$this->assertArrayHasKey($enumerator->getName(), $constants); |
126
|
|
|
$this->assertSame($constants[$enumerator->getName()], $enumerator->getValue()); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testGetAllValues() |
131
|
|
|
{ |
132
|
|
|
$constants = EnumBasic::getConstants(); |
133
|
|
|
foreach ($constants as $name => $value) { |
134
|
|
|
$enum = EnumBasic::get($value); |
135
|
|
|
$this->assertSame($value, $enum->getValue()); |
136
|
|
|
$this->assertSame($name, $enum->getName()); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
public function testIsBasic() |
141
|
|
|
{ |
142
|
|
|
$enum = EnumBasic::ONE(); |
143
|
|
|
|
144
|
|
|
// by value |
145
|
|
|
$this->assertTrue($enum->is(EnumBasic::ONE)); // same |
146
|
|
|
$this->assertFalse($enum->is('1')); // wrong value by strict comparison |
147
|
|
|
|
148
|
|
|
// by instance |
149
|
|
|
$this->assertTrue($enum->is(EnumBasic::ONE())); // same |
150
|
|
|
$this->assertFalse($enum->is(EnumBasic::TWO())); // different enumerators |
151
|
|
|
$this->assertFalse($enum->is(EnumInheritance::ONE())); // different enumeration type |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testCallingGetOrdinalTwoTimesWillResultTheSameValue() |
155
|
|
|
{ |
156
|
|
|
$enum = EnumBasic::get(EnumBasic::TWO); |
157
|
|
|
$this->assertSame(1, $enum->getOrdinal()); |
158
|
|
|
$this->assertSame(1, $enum->getOrdinal()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testInstantiateUsingOrdinalNumber() |
162
|
|
|
{ |
163
|
|
|
$enum = EnumInheritance::getByOrdinal(16); |
164
|
|
|
$this->assertSame(16, $enum->getOrdinal()); |
165
|
|
|
$this->assertSame('INHERITANCE', $enum->getName()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testInstantiateUsingInvalidOrdinalNumberThrowsInvalidArgumentException() |
169
|
|
|
{ |
170
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
171
|
|
|
EnumInheritance::getByOrdinal(17); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testInstantiateByName() |
175
|
|
|
{ |
176
|
|
|
$enum = EnumInheritance::getByName('ONE'); |
177
|
|
|
$this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enum); |
178
|
|
|
$this->assertSame(EnumInheritance::ONE, $enum->getValue()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testInstantiateByUnknownNameThrowsInvalidArgumentException() |
182
|
|
|
{ |
183
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
184
|
|
|
EnumInheritance::getByName('UNKNOWN'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testInstantiateUsingMagicMethod() |
188
|
|
|
{ |
189
|
|
|
$enum = EnumInheritance::ONE(); |
190
|
|
|
$this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enum); |
191
|
|
|
$this->assertSame(EnumInheritance::ONE, $enum->getValue()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testAmbiguousConstantsThrowsLogicException() |
195
|
|
|
{ |
196
|
|
|
$this->setExpectedException('LogicException'); |
197
|
|
|
EnumAmbiguous::get('unknown'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testExtendedAmbiguousCanstantsThrowsLogicException() |
201
|
|
|
{ |
202
|
|
|
$this->setExpectedException('LogicException'); |
203
|
|
|
EnumExtendedAmbiguous::get('unknown'); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function testSingleton() |
207
|
|
|
{ |
208
|
|
|
$enum1 = EnumBasic::get(EnumBasic::ONE); |
209
|
|
|
$enum2 = EnumBasic::ONE(); |
210
|
|
|
$this->assertSame($enum1, $enum2); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testClear() |
214
|
|
|
{ |
215
|
|
|
$enum1 = EnumBasic::ONE(); |
216
|
|
|
EnumBasic::clear(); |
217
|
|
|
$enum2 = EnumBasic::ONE(); |
218
|
|
|
$enum3 = EnumBasic::ONE(); |
219
|
|
|
|
220
|
|
|
$this->assertNotSame($enum1, $enum2); |
221
|
|
|
$this->assertSame($enum2, $enum3); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
View Code Duplication |
public function testCloneNotCallableAndThrowsLogicException() |
225
|
|
|
{ |
226
|
|
|
$enum = EnumBasic::ONE(); |
227
|
|
|
|
228
|
|
|
$reflectionClass = new ReflectionClass($enum); |
229
|
|
|
$reflectionMethod = $reflectionClass->getMethod('__clone'); |
230
|
|
|
$this->assertTrue($reflectionMethod->isPrivate(), 'The method __clone must be private'); |
231
|
|
|
$this->assertTrue($reflectionMethod->isFinal(), 'The method __clone must be final'); |
232
|
|
|
|
233
|
|
|
$reflectionMethod->setAccessible(true); |
234
|
|
|
$this->setExpectedException('LogicException'); |
235
|
|
|
$reflectionMethod->invoke($enum); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function testNotSerializable() |
239
|
|
|
{ |
240
|
|
|
$enum = EnumBasic::ONE(); |
241
|
|
|
|
242
|
|
|
$this->setExpectedException('LogicException'); |
243
|
|
|
serialize($enum); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testNotUnserializable() |
247
|
|
|
{ |
248
|
|
|
$this->setExpectedException('LogicException'); |
249
|
|
|
unserialize("O:32:\"MabeEnumTest\TestAsset\EnumBasic\":0:{}"); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
View Code Duplication |
public function testHas() |
253
|
|
|
{ |
254
|
|
|
$enum = EnumBasic::ONE(); |
255
|
|
|
|
256
|
|
|
$this->assertFalse($enum->has('invalid')); |
257
|
|
|
$this->assertFalse($enum->has(EnumInheritance::ONE())); |
258
|
|
|
$this->assertTrue($enum->has(EnumBasic::ONE())); |
259
|
|
|
$this->assertTrue($enum->has(EnumBasic::ONE)); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function testConstVisibility() |
263
|
|
|
{ |
264
|
|
|
if (PHP_VERSION_ID < 70100) { |
265
|
|
|
$this->markTestSkipped('This test is for PHP-7.1 and upper only'); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$constants = ConstVisibilityEnum::getConstants(); |
269
|
|
|
$this->assertSame(array( |
270
|
|
|
'IPUB' => ConstVisibilityEnum::IPUB, |
271
|
|
|
'PUB' => ConstVisibilityEnum::PUB, |
272
|
|
|
), $constants); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function testConstVisibilityExtended() |
276
|
|
|
{ |
277
|
|
|
if (PHP_VERSION_ID < 70100) { |
278
|
|
|
$this->markTestSkipped('This test is for PHP-7.1 and upper only'); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$constants = ConstVisibilityEnumExtended::getConstants(); |
282
|
|
|
$this->assertSame(array( |
283
|
|
|
'IPUB' => ConstVisibilityEnumExtended::IPUB, |
284
|
|
|
'PUB' => ConstVisibilityEnumExtended::PUB, |
285
|
|
|
'IPUB2' => ConstVisibilityEnumExtended::IPUB2, |
286
|
|
|
'PUB2' => ConstVisibilityEnumExtended::PUB2, |
287
|
|
|
), $constants); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
public function testIsSerializableIssue() |
291
|
|
|
{ |
292
|
|
|
$enum1 = SerializableEnum::INT(); |
293
|
|
|
$enum2 = unserialize(serialize($enum1)); |
294
|
|
|
|
295
|
|
|
$this->assertFalse($enum1 === $enum2, 'Wrong test implementation'); |
296
|
|
|
$this->assertTrue($enum1->is($enum2), 'Two different instances of exact the same enumerator shoulw be equal'); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|