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