5
5
use MabeEnum \Enum ;
6
6
use MabeEnum \EnumMap ;
7
7
use MabeEnumTest \TestAsset \EnumWithoutDefaultValue ;
8
+ use MabeEnumTest \TestAsset \EnumInheritance ;
8
9
use PHPUnit_Framework_TestCase as TestCase ;
10
+ use ReflectionClass ;
9
11
10
12
/**
11
13
* Unit tests for the class MabeEnum\EnumMap
@@ -31,11 +33,13 @@ public function testBasic()
31
33
$ this ->assertNull ($ enumMap ->attach ($ enum1 , $ value1 ));
32
34
$ this ->assertTrue ($ enumMap ->contains ($ enum1 ));
33
35
$ this ->assertSame ($ value1 , $ enumMap [$ enum1 ]);
36
+ $ this ->assertSame (spl_object_hash ($ enum1 ), $ enumMap ->getHash ($ enum1 ));
34
37
35
38
$ this ->assertFalse ($ enumMap ->contains ($ enum2 ));
36
39
$ this ->assertNull ($ enumMap ->attach ($ enum2 , $ value2 ));
37
40
$ this ->assertTrue ($ enumMap ->contains ($ enum2 ));
38
41
$ this ->assertSame ($ value2 , $ enumMap [$ enum2 ]);
42
+ $ this ->assertSame (spl_object_hash ($ enum2 ), $ enumMap ->getHash ($ enum2 ));
39
43
40
44
$ this ->assertNull ($ enumMap ->detach ($ enum1 ));
41
45
$ this ->assertFalse ($ enumMap ->contains ($ enum1 ));
@@ -58,11 +62,13 @@ public function testBasicWithConstantValuesAsEnums()
58
62
$ this ->assertNull ($ enumMap ->attach ($ enum1 , $ value1 ));
59
63
$ this ->assertTrue ($ enumMap ->contains ($ enum1 ));
60
64
$ this ->assertSame ($ value1 , $ enumMap [$ enum1 ]);
65
+ $ this ->assertSame (spl_object_hash (EnumWithoutDefaultValue::ONE ()), $ enumMap ->getHash ($ enum1 ));
61
66
62
67
$ this ->assertFalse ($ enumMap ->contains ($ enum2 ));
63
68
$ this ->assertNull ($ enumMap ->attach ($ enum2 , $ value2 ));
64
69
$ this ->assertTrue ($ enumMap ->contains ($ enum2 ));
65
70
$ this ->assertSame ($ value2 , $ enumMap [$ enum2 ]);
71
+ $ this ->assertSame (spl_object_hash (EnumWithoutDefaultValue::TWO ()), $ enumMap ->getHash ($ enum2 ));
66
72
67
73
$ this ->assertNull ($ enumMap ->detach ($ enum1 ));
68
74
$ this ->assertFalse ($ enumMap ->contains ($ enum1 ));
@@ -197,9 +203,114 @@ public function testIterateWithFlags()
197
203
$ this ->assertSame ('ONE ' , $ enumMap ->current ());
198
204
}
199
205
206
+ public function testArrayAccessWithObjects ()
207
+ {
208
+ $ enumMap = new EnumMap ('MabeEnumTest\TestAsset\EnumWithoutDefaultValue ' );
209
+
210
+ $ enumMap [EnumWithoutDefaultValue::ONE ()] = 'first ' ;
211
+ $ enumMap [EnumWithoutDefaultValue::TWO ()] = 'second ' ;
212
+
213
+ $ this ->assertTrue (isset ($ enumMap [EnumWithoutDefaultValue::ONE ()]));
214
+ $ this ->assertTrue (isset ($ enumMap [EnumWithoutDefaultValue::TWO ()]));
215
+
216
+ $ this ->assertSame ('first ' , $ enumMap [EnumWithoutDefaultValue::ONE ()]);
217
+ $ this ->assertSame ('second ' , $ enumMap [EnumWithoutDefaultValue::TWO ()]);
218
+
219
+ unset($ enumMap [EnumWithoutDefaultValue::ONE ()], $ enumMap [EnumWithoutDefaultValue::TWO ()]);
220
+
221
+ $ this ->assertFalse (isset ($ enumMap [EnumWithoutDefaultValue::ONE ()]));
222
+ $ this ->assertFalse (isset ($ enumMap [EnumWithoutDefaultValue::TWO ()]));
223
+ }
224
+
225
+ public function testArrayAccessWithValues ()
226
+ {
227
+ $ enumMap = new EnumMap ('MabeEnumTest\TestAsset\EnumWithoutDefaultValue ' );
228
+
229
+ $ enumMap [EnumWithoutDefaultValue::ONE ] = 'first ' ;
230
+ $ enumMap [EnumWithoutDefaultValue::TWO ] = 'second ' ;
231
+
232
+ $ this ->assertTrue (isset ($ enumMap [EnumWithoutDefaultValue::ONE ]));
233
+ $ this ->assertTrue (isset ($ enumMap [EnumWithoutDefaultValue::TWO ]));
234
+
235
+ $ this ->assertSame ('first ' , $ enumMap [EnumWithoutDefaultValue::ONE ]);
236
+ $ this ->assertSame ('second ' , $ enumMap [EnumWithoutDefaultValue::TWO ]);
237
+
238
+ unset($ enumMap [EnumWithoutDefaultValue::ONE ], $ enumMap [EnumWithoutDefaultValue::TWO ]);
239
+
240
+ $ this ->assertFalse (isset ($ enumMap [EnumWithoutDefaultValue::ONE ]));
241
+ $ this ->assertFalse (isset ($ enumMap [EnumWithoutDefaultValue::TWO ]));
242
+ }
243
+
200
244
public function testConstructThrowsInvalidArgumentExceptionIfEnumClassDoesNotExtendBaseEnum ()
201
245
{
202
246
$ this ->setExpectedException ('InvalidArgumentException ' );
203
247
new EnumMap ('stdClass ' );
204
248
}
249
+
250
+ public function testSetFlagsThrowsInvalidArgumentExceptionOnUnsupportedKeyFlag ()
251
+ {
252
+ $ enumMap = new EnumMap ('MabeEnumTest\TestAsset\EnumWithoutDefaultValue ' );
253
+
254
+ $ this ->setExpectedException ('InvalidArgumentException ' );
255
+ $ enumMap ->setFlags (5 );
256
+ }
257
+
258
+ public function testCurrentThrowsRuntimeExceptionOnInvalidFlag ()
259
+ {
260
+ $ enumMap = new EnumMap ('MabeEnumTest\TestAsset\EnumWithoutDefaultValue ' );
261
+ $ enumMap ->attach (EnumWithoutDefaultValue::ONE ());
262
+ $ enumMap ->rewind ();
263
+
264
+ // change internal flags to an invalid current flag
265
+ $ reflectionClass = new ReflectionClass ($ enumMap );
266
+ $ reflectionProp = $ reflectionClass ->getProperty ('flags ' );
267
+ $ reflectionProp ->setAccessible (true );
268
+ $ reflectionProp ->setValue ($ enumMap , 0 );
269
+
270
+ $ this ->setExpectedException ('RuntimeException ' );
271
+ $ enumMap ->current ();
272
+ }
273
+
274
+ public function testKeyThrowsRuntimeExceptionOnInvalidFlag ()
275
+ {
276
+ $ enumMap = new EnumMap ('MabeEnumTest\TestAsset\EnumWithoutDefaultValue ' );
277
+ $ enumMap ->attach (EnumWithoutDefaultValue::ONE ());
278
+ $ enumMap ->rewind ();
279
+
280
+ // change internal flags to an invalid current flag
281
+ $ reflectionClass = new ReflectionClass ($ enumMap );
282
+ $ reflectionProp = $ reflectionClass ->getProperty ('flags ' );
283
+ $ reflectionProp ->setAccessible (true );
284
+ $ reflectionProp ->setValue ($ enumMap , 0 );
285
+
286
+ $ this ->setExpectedException ('RuntimeException ' );
287
+ $ enumMap ->key ();
288
+ }
289
+
290
+ public function testSetFlagsThrowsInvalidArgumentExceptionOnUnsupportedCurrentFlag ()
291
+ {
292
+ $ enumMap = new EnumMap ('MabeEnumTest\TestAsset\EnumWithoutDefaultValue ' );
293
+
294
+ $ this ->setExpectedException ('InvalidArgumentException ' );
295
+ $ enumMap ->setFlags (48 );
296
+ }
297
+
298
+ public function testInitEnumThrowsInvalidArgumentExceptionOnInvalidEnumGiven ()
299
+ {
300
+ $ enumMap = new EnumMap ('MabeEnumTest\TestAsset\EnumWithoutDefaultValue ' );
301
+
302
+ $ this ->setExpectedException ('InvalidArgumentException ' );
303
+ $ enumMap ->offsetSet (EnumInheritance::INHERITANCE (), 'test ' );
304
+ }
305
+
306
+ public function testContainsAndOffsetExistsReturnsFalseOnInvalidEnum ()
307
+ {
308
+ $ enumMap = new EnumMap ('MabeEnumTest\TestAsset\EnumWithoutDefaultValue ' );
309
+
310
+ $ this ->assertFalse ($ enumMap ->contains (EnumInheritance::INHERITANCE ()));
311
+ $ this ->assertFalse ($ enumMap ->contains (EnumInheritance::INHERITANCE ));
312
+
313
+ $ this ->assertFalse (isset ($ enumMap [EnumInheritance::INHERITANCE ()]));
314
+ $ this ->assertFalse (isset ($ enumMap [EnumInheritance::INHERITANCE ]));
315
+ }
205
316
}
0 commit comments