1
0
mirror of synced 2024-11-22 11:56:03 +03:00

Keep false values while deserializing (#24)

Co-authored-by: Maria Tyschitskaya <tyschitskaya@retailcrm.ru>
This commit is contained in:
tishmaria90 2021-12-23 14:33:44 +03:00 committed by GitHub
parent 98563e2e60
commit cb94fd8e02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 3 deletions

View File

@ -125,7 +125,7 @@ class Message implements ModelInterface
* @var bool $isRead * @var bool $isRead
* *
* @Type("bool") * @Type("bool")
* @Accessor(getter="getIsRead",setter="setIsRead") * @Accessor(getter="isRead",setter="setIsRead")
* @SkipWhenEmpty() * @SkipWhenEmpty()
*/ */
private $isRead; private $isRead;
@ -134,7 +134,7 @@ class Message implements ModelInterface
* @var bool $isEdit * @var bool $isEdit
* *
* @Type("bool") * @Type("bool")
* @Accessor(getter="getIsEdit",setter="setIsEdit") * @Accessor(getter="isEdit",setter="setIsEdit")
* @SkipWhenEmpty() * @SkipWhenEmpty()
*/ */
private $isEdit; private $isEdit;

View File

@ -73,8 +73,11 @@ class Serializer
if ($context instanceof DeserializationContext) { if ($context instanceof DeserializationContext) {
switch ($from) { switch ($from) {
case self::S_ARRAY: case self::S_ARRAY:
$filtered = array_filter($data, static function ($el) {
return $el !== null;
});
$deserialized = $serializer $deserialized = $serializer
->fromArray(array_filter($data), self::normalizeNamespace($entityType), $context); ->fromArray($filtered, self::normalizeNamespace($entityType), $context);
break; break;
case self::S_JSON: case self::S_JSON:
$deserialized = $serializer $deserialized = $serializer

View File

@ -0,0 +1,79 @@
<?php
namespace RetailCrm\Mg\Bot\Tests\Model;
use RetailCrm\Common\Serializer;
use RetailCrm\Mg\Bot\Model\Entity\Message\Message;
use PHPUnit\Framework\TestCase;
class MessageTest extends TestCase
{
/**
* @dataProvider dataProvider_noContent
* @param $content
*/
public function testDeserialization_NoContent($content): void
{
$item = ['content' => $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"}'],
];
}
}