From 061736dc69c65188bc74b93cf730d6fe41798a84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 8 Sep 2023 20:26:51 -0300 Subject: [PATCH 1/2] Add tests for int backed enums MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- tests/JsonSerializerTest.php | 10 ++++++++++ tests/SupportEnums/MyIntBackedEnum.php | 9 +++++++++ 2 files changed, 19 insertions(+) create mode 100644 tests/SupportEnums/MyIntBackedEnum.php diff --git a/tests/JsonSerializerTest.php b/tests/JsonSerializerTest.php index 15fd8df..c7ce024 100644 --- a/tests/JsonSerializerTest.php +++ b/tests/JsonSerializerTest.php @@ -260,6 +260,10 @@ 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)); } /** @@ -283,6 +287,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); 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 @@ + Date: Fri, 8 Sep 2023 21:04:19 -0300 Subject: [PATCH 2/2] Fix error when unserializing enums MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unserialized enums where not saved in the objectMapping property. Signed-off-by: MaurĂ­cio Meneghini Fauth --- src/JsonSerializer/JsonSerializer.php | 4 ++- tests/JsonSerializerTest.php | 48 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) 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 c7ce024..3b9fb2c 100644 --- a/tests/JsonSerializerTest.php +++ b/tests/JsonSerializerTest.php @@ -266,6 +266,29 @@ public function testSerializeEnums() $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)); + } + /** * Test unserialization of Enums * @@ -301,6 +324,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 *