diff --git a/src/Enumeration/Enumeration.php b/src/Enumeration/Enumeration.php index 08ceba2..db2f84a 100644 --- a/src/Enumeration/Enumeration.php +++ b/src/Enumeration/Enumeration.php @@ -6,6 +6,7 @@ use Dbalabka\Enumeration\Exception\EnumerationException; use Dbalabka\Enumeration\Exception\InvalidArgumentException; use Dbalabka\StaticConstructorLoader\StaticConstructorInterface; +use Serializable; use function array_search; use function get_class_vars; use function sprintf; @@ -18,7 +19,7 @@ * * @author Dmitry Balabka */ -abstract class Enumeration implements StaticConstructorInterface +abstract class Enumeration implements StaticConstructorInterface, Serializable { const INITIAL_ORDINAL = 0; @@ -158,6 +159,7 @@ final public function __clone() /** * Serialization is not allowed right now. It is not possible to properly serialize the singleton. + * See the documentation for workaround. */ final public function __sleep() { @@ -168,4 +170,24 @@ final public function __wakeup() { throw new EnumerationException('Enum unserialization is not allowed'); } + + final public function __serialize() + { + throw new EnumerationException('Enum serialization is not allowed'); + } + + final public function __unserialize() + { + throw new EnumerationException('Enum unserialization is not allowed'); + } + + final public function serialize() + { + throw new EnumerationException('Enum serialization is not allowed'); + } + + final public function unserialize($data) + { + throw new EnumerationException('Enum unserialization is not allowed'); + } } diff --git a/tests/Enumeration/EnumerationTest.php b/tests/Enumeration/EnumerationTest.php index e60d3ec..7b21ff8 100644 --- a/tests/Enumeration/EnumerationTest.php +++ b/tests/Enumeration/EnumerationTest.php @@ -11,6 +11,7 @@ use Dbalabka\Enumeration\Tests\Fixtures\ActionWithPublicConstructor; use Dbalabka\Enumeration\Tests\Fixtures\Flag; use Error; +use PHPUnit\Framework\Error\Warning; use PHPUnit\Framework\TestCase; use function serialize; use function version_compare; @@ -96,7 +97,11 @@ public function testSerialization() public function testUnserialization() { Flag::initialize(); - $this->expectException(EnumerationException::class); + if (version_compare(PHP_VERSION, '7.4.0-dev', '<')) { + $this->expectException(Warning::class); + } else { + $this->expectException(EnumerationException::class); + } unserialize('O:40:"Dbalabka\Enumeration\Tests\Fixtures\Flag":2:{s:51:" Dbalabka\Enumeration\Tests\Fixtures\Flag flagValue";i:2;s:10:" * ordinal";i:1;}'); }