From cb94fd8e02c44aca0fb71ae0095a6a2765dbd825 Mon Sep 17 00:00:00 2001 From: tishmaria90 Date: Thu, 23 Dec 2021 14:33:44 +0300 Subject: [PATCH] Keep false values while deserializing (#24) Co-authored-by: Maria Tyschitskaya --- src/Bot/Model/Entity/Message/Message.php | 4 +- src/Serializer.php | 5 +- tests/Bot/Tests/Model/MessageTest.php | 79 ++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 tests/Bot/Tests/Model/MessageTest.php diff --git a/src/Bot/Model/Entity/Message/Message.php b/src/Bot/Model/Entity/Message/Message.php index 324f6a8..f67c2c0 100644 --- a/src/Bot/Model/Entity/Message/Message.php +++ b/src/Bot/Model/Entity/Message/Message.php @@ -125,7 +125,7 @@ class Message implements ModelInterface * @var bool $isRead * * @Type("bool") - * @Accessor(getter="getIsRead",setter="setIsRead") + * @Accessor(getter="isRead",setter="setIsRead") * @SkipWhenEmpty() */ private $isRead; @@ -134,7 +134,7 @@ class Message implements ModelInterface * @var bool $isEdit * * @Type("bool") - * @Accessor(getter="getIsEdit",setter="setIsEdit") + * @Accessor(getter="isEdit",setter="setIsEdit") * @SkipWhenEmpty() */ private $isEdit; diff --git a/src/Serializer.php b/src/Serializer.php index c8c6075..3f92541 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -73,8 +73,11 @@ class Serializer if ($context instanceof DeserializationContext) { switch ($from) { case self::S_ARRAY: + $filtered = array_filter($data, static function ($el) { + return $el !== null; + }); $deserialized = $serializer - ->fromArray(array_filter($data), self::normalizeNamespace($entityType), $context); + ->fromArray($filtered, self::normalizeNamespace($entityType), $context); break; case self::S_JSON: $deserialized = $serializer diff --git a/tests/Bot/Tests/Model/MessageTest.php b/tests/Bot/Tests/Model/MessageTest.php new file mode 100644 index 0000000..593d733 --- /dev/null +++ b/tests/Bot/Tests/Model/MessageTest.php @@ -0,0 +1,79 @@ + $content]; + + /** @var Message $result */ + $result = Serializer::deserialize($item, Message::class, Serializer::S_ARRAY); + + self::assertNull($result->getContent()); + } + + /** + * @dataProvider dataProvider_withContent + * @param $content + */ + public function testDeserialization_WithContent($content): void + { + $item = ['content' => $content]; + + /** @var Message $result */ + $result = Serializer::deserialize($item, Message::class, Serializer::S_ARRAY); + + self::assertNotNull($result->getContent()); + self::assertEquals($content, $result->getContent()); + } + + public function testSerialization_NoContent(): void + { + $item = new Message(); + + $result = Serializer::serialize($item); + + self::assertEquals('{}', $result); + } + + /** + * @dataProvider dataProvider_withContent + * @param $content + * @param $json + */ + public function testSerialization_WithContent($content, $json): void + { + $item = new Message(); + $item->setContent($content); + + $result = Serializer::serialize($item); + + self::assertEquals($json, $result); + } + + public function dataProvider_noContent(): array + { + return [ + [null], + ]; + } + + public function dataProvider_withContent(): array + { + return [ + ['', '{"content":""}'], + ['0', '{"content":"0"}'], + ['Something', '{"content":"Something"}'], + ]; + } +}