1 | <?php |
||
19 | class EnumSerializableTraitTest extends TestCase |
||
20 | { |
||
21 | public function testSerializeSerializableEnum() |
||
22 | { |
||
23 | $serialized = serialize(SerializableEnum::get(SerializableEnum::NIL)); |
||
24 | $this->assertInternalType('string', $serialized); |
||
25 | |||
26 | $unserialized = unserialize($serialized); |
||
27 | $this->assertInstanceOf(SerializableEnum::class, $unserialized); |
||
28 | } |
||
29 | |||
30 | public function testUnserializeFirstWillHoldTheSameInstance() |
||
31 | { |
||
32 | $serialized = serialize(SerializableEnum::get(SerializableEnum::STR)); |
||
33 | $this->assertInternalType('string', $serialized); |
||
34 | |||
35 | // clear all instantiated instances so we can virtual test unserializing first |
||
36 | $this->clearEnumeration(SerializableEnum::class); |
||
37 | |||
38 | // First unserialize |
||
39 | $unserialized = unserialize($serialized); |
||
40 | $this->assertInstanceOf(SerializableEnum::class, $unserialized); |
||
41 | |||
42 | // second instantiate |
||
43 | $enum = SerializableEnum::get($unserialized->getValue()); |
||
44 | |||
45 | // check if it's the same instance |
||
46 | $this->assertSame($enum, $unserialized); |
||
47 | } |
||
48 | |||
49 | public function testUnserializeThrowsRuntimeExceptionOnUnknownValue() |
||
50 | { |
||
51 | $this->setExpectedException(RuntimeException::class); |
||
52 | unserialize('C:' . strlen(SerializableEnum::class) . ':"' . SerializableEnum::class . '":11:{s:4:"test";}'); |
||
53 | } |
||
54 | |||
55 | public function testUnserializeThrowsRuntimeExceptionOnInvalidValue() |
||
56 | { |
||
57 | $this->setExpectedException(RuntimeException::class); |
||
58 | unserialize('C:' . strlen(SerializableEnum::class) . ':"' . SerializableEnum::class . '":19:{O:8:"stdClass":0:{}}'); |
||
59 | } |
||
60 | |||
61 | public function testUnserializeThrowsLogicExceptionOnChangingValue() |
||
62 | { |
||
63 | $this->setExpectedException(LogicException::class); |
||
64 | $enum = SerializableEnum::get(SerializableEnum::INT); |
||
65 | $enum->unserialize(serialize(SerializableEnum::STR)); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Clears all instantiated enumerations and detected constants of the given enumerator |
||
70 | * @param string $enumeration |
||
71 | */ |
||
72 | private function clearEnumeration($enumeration) |
||
73 | { |
||
74 | $reflClass = new ReflectionClass($enumeration); |
||
75 | while ($reflClass->getName() !== Enum::class) { |
||
76 | $reflClass = $reflClass->getParentClass(); |
||
77 | } |
||
78 | |||
79 | $reflPropInstances = $reflClass->getProperty('instances'); |
||
80 | $reflPropInstances->setAccessible(true); |
||
81 | $reflPropInstances->setValue(null, array()); |
||
82 | $reflPropConstants = $reflClass->getProperty('constants'); |
||
83 | $reflPropConstants->setAccessible(true); |
||
84 | $reflPropConstants->setValue(null, array()); |
||
85 | } |
||
86 | } |
||
87 |