Skip to content

Commit 2c09b96

Browse files
committed
fixes #43: throw an LogicException on un/-serialize an enumeration instance
1 parent 4ef4947 commit 2c09b96

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/MabeEnum/Enum.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ final private function __clone()
7575
throw new LogicException('Enums are not cloneable');
7676
}
7777

78+
/**
79+
* @throws LogicException Enums are not serializable
80+
* because instances are implemented as singletons
81+
*/
82+
final public function __sleep()
83+
{
84+
throw new LogicException('Enums are not serializable');
85+
}
86+
87+
/**
88+
* @throws LogicException Enums are not serializable
89+
* because instances are implemented as singletons
90+
*/
91+
final public function __wakeup()
92+
{
93+
throw new LogicException('Enums are not serializable');
94+
}
95+
7896
/**
7997
* Get the current selected value
8098
*

tests/MabeEnumTest/EnumTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,18 @@ public function testCloneNotCallableAndThrowsLogicException()
224224
$this->setExpectedException('LogicException');
225225
$reflectionMethod->invoke($enum);
226226
}
227+
228+
public function testNotSerializable()
229+
{
230+
$enum = EnumBasic::ONE();
231+
232+
$this->setExpectedException('LogicException');
233+
serialize($enum);
234+
}
235+
236+
public function testNotUnserializable()
237+
{
238+
$this->setExpectedException('LogicException');
239+
unserialize("O:32:\"MabeEnumTest\TestAsset\EnumBasic\":0:{}");
240+
}
227241
}

0 commit comments

Comments
 (0)