File tree Expand file tree Collapse file tree 3 files changed +24
-3
lines changed Expand file tree Collapse file tree 3 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -271,8 +271,9 @@ class CardinalDirection extends Enum implements Serializable
271
271
$north1 = CardinalDirection::NORTH();
272
272
$north2 = unserialize(serialize($north1));
273
273
274
- // The following could be FALSE as described above
275
- var_dump($north1 === $north2);
274
+ var_dump($north1 === $north2); // returns FALSE as described above
275
+ var_dump($north1->is($north2)); // returns TRUE - this way the two instances are treated equal
276
+ var_dump($north2->is($north1)); // returns TRUE - equality works in both directions
276
277
```
277
278
278
279
# Why not ` SplEnum `
Original file line number Diff line number Diff line change @@ -146,7 +146,13 @@ final public function getOrdinal()
146
146
*/
147
147
final public function is ($ enumerator )
148
148
{
149
- return $ this === $ enumerator || $ this ->value === $ enumerator ;
149
+ return $ this === $ enumerator || $ this ->value === $ enumerator
150
+
151
+ // The following additional conditions are required only because of the issue of serializable singletons
152
+ || ($ enumerator instanceof static
153
+ && get_class ($ enumerator ) === get_called_class ()
154
+ && $ enumerator ->value === $ this ->value
155
+ );
150
156
}
151
157
152
158
/**
Original file line number Diff line number Diff line change 8
8
use MabeEnumTest \TestAsset \EnumExtendedAmbiguous ;
9
9
use MabeEnumTest \TestAsset \ConstVisibilityEnum ;
10
10
use MabeEnumTest \TestAsset \ConstVisibilityEnumExtended ;
11
+ use MabeEnumTest \TestAsset \SerializableEnum ;
11
12
use PHPUnit_Framework_TestCase as TestCase ;
12
13
use ReflectionClass ;
13
14
@@ -285,4 +286,17 @@ public function testConstVisibilityExtended()
285
286
'PUB2 ' => ConstVisibilityEnumExtended::PUB2 ,
286
287
), $ constants );
287
288
}
289
+
290
+ public function testIsSerializableIssue ()
291
+ {
292
+ if (PHP_VERSION_ID < 50400 ) {
293
+ $ this ->markTestSkipped ('This test is for PHP-5.4 and upper only ' );
294
+ }
295
+
296
+ $ enum1 = SerializableEnum::INT ();
297
+ $ enum2 = unserialize (serialize ($ enum1 ));
298
+
299
+ $ this ->assertFalse ($ enum1 === $ enum2 , 'Wrong test implementation ' );
300
+ $ this ->assertTrue ($ enum1 ->is ($ enum2 ), 'Two different instances of exact the same enumerator should be equal ' );
301
+ }
288
302
}
You can’t perform that action at this time.
0 commit comments