diff --git a/src/JsonSerializer/JsonSerializer.php b/src/JsonSerializer/JsonSerializer.php index 6c5fb41..b478293 100644 --- a/src/JsonSerializer/JsonSerializer.php +++ b/src/JsonSerializer/JsonSerializer.php @@ -455,7 +455,9 @@ protected function unserializeObject($value) } if (is_subclass_of($className, \UnitEnum::class)) { - return constant("$className::{$value['name']}"); + $obj = constant("$className::{$value['name']}"); + $this->objectMapping[$this->objectMappingIndex++] = $obj; + return $obj; } if (!$this->isSplList($className)) { diff --git a/tests/JsonSerializerTest.php b/tests/JsonSerializerTest.php index afa4512..6889e17 100644 --- a/tests/JsonSerializerTest.php +++ b/tests/JsonSerializerTest.php @@ -256,6 +256,33 @@ public function testSerializeEnums() $backedEnum = SupportEnums\MyBackedEnum::Hearts; $expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Hearts","value":"H"}'; $this->assertSame($expected, $this->serializer->serialize($backedEnum)); + + $intBackedEnum = SupportEnums\MyIntBackedEnum::One; + $expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyIntBackedEnum","name":"One","value":1}'; + $this->assertSame($expected, $this->serializer->serialize($intBackedEnum)); + } + + /** + * Test serialization of multiple Enums + * + * @return void + */ + public function testSerializeMultipleEnums() + { + if (PHP_VERSION_ID < 80100) { + $this->markTestSkipped("Enums are only available since PHP 8.1"); + } + + $obj = new stdClass(); + $obj->enum1 = SupportEnums\MyUnitEnum::Hearts; + $obj->enum2 = SupportEnums\MyBackedEnum::Hearts; + $obj->enum3 = SupportEnums\MyIntBackedEnum::One; + $obj->enum4 = SupportEnums\MyUnitEnum::Hearts; + $obj->enum5 = SupportEnums\MyBackedEnum::Hearts; + $obj->enum6 = SupportEnums\MyIntBackedEnum::One; + + $expected = '{"@type":"stdClass","enum1":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyUnitEnum","name":"Hearts"},"enum2":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Hearts","value":"H"},"enum3":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyIntBackedEnum","name":"One","value":1},"enum4":{"@type":"@1"},"enum5":{"@type":"@2"},"enum6":{"@type":"@3"}}'; + $this->assertSame($expected, $this->serializer->serialize($obj)); } /** @@ -279,6 +306,12 @@ public function testUnserializeEnums() $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportEnums\MyBackedEnum', $obj); $this->assertSame(SupportEnums\MyBackedEnum::Hearts, $obj); + $serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyIntBackedEnum","name":"Two","value":2}'; + $obj = $this->serializer->unserialize($serialized); + $this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportEnums\MyIntBackedEnum', $obj); + $this->assertSame(SupportEnums\MyIntBackedEnum::Two, $obj); + $this->assertSame(SupportEnums\MyIntBackedEnum::Two->value, $obj->value); + // wrong value of BackedEnum is ignored $serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Hearts","value":"S"}'; $obj = $this->serializer->unserialize($serialized); @@ -287,6 +320,31 @@ public function testUnserializeEnums() $this->assertSame(SupportEnums\MyBackedEnum::Hearts->value, $obj->value); } + /** + * Test unserialization of multiple Enums + * + * @return void + */ + public function testUnserializeMultipleEnums() + { + if (PHP_VERSION_ID < 80100) { + $this->markTestSkipped("Enums are only available since PHP 8.1"); + } + + $obj = new stdClass(); + $obj->enum1 = SupportEnums\MyUnitEnum::Hearts; + $obj->enum2 = SupportEnums\MyBackedEnum::Hearts; + $obj->enum3 = SupportEnums\MyIntBackedEnum::One; + $obj->enum4 = SupportEnums\MyUnitEnum::Hearts; + $obj->enum5 = SupportEnums\MyBackedEnum::Hearts; + $obj->enum6 = SupportEnums\MyIntBackedEnum::One; + + $serialized = '{"@type":"stdClass","enum1":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyUnitEnum","name":"Hearts"},"enum2":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Hearts","value":"H"},"enum3":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyIntBackedEnum","name":"One","value":1},"enum4":{"@type":"@1"},"enum5":{"@type":"@2"},"enum6":{"@type":"@3"}}'; + $actualObj = $this->serializer->unserialize($serialized); + $this->assertInstanceOf('stdClass', $actualObj); + $this->assertEquals($obj, $actualObj); + } + /** * Test unserialization of wrong UnitEnum * diff --git a/tests/SupportEnums/MyIntBackedEnum.php b/tests/SupportEnums/MyIntBackedEnum.php new file mode 100644 index 0000000..d338c6d --- /dev/null +++ b/tests/SupportEnums/MyIntBackedEnum.php @@ -0,0 +1,9 @@ +