Skip to content

Commit

Permalink
Use serializer provided by component lib (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
liquetsoft authored Oct 25, 2024
1 parent 37e7326 commit 2da2f87
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 32 deletions.
18 changes: 2 additions & 16 deletions src/Serializer/FiasSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

/**
* Преднастроенный объект сериализатора для ФИАС.
*/
final class FiasSerializer implements SerializerInterface
{
private readonly Serializer $nestedSerializer;
private readonly SerializerInterface $nestedSerializer;

/**
* @param array<DenormalizerInterface|NormalizerInterface>|null $normalizers
Expand Down Expand Up @@ -57,18 +54,7 @@ public function __construct(?array $normalizers = null, ?array $encoders = null)
];
}

if ($encoders === null) {
$encoders = [
new XmlEncoder(
[
XmlEncoder::TYPE_CAST_ATTRIBUTES => false,
]
),
new JsonEncoder(),
];
}

$this->nestedSerializer = new Serializer($normalizers, $encoders);
$this->nestedSerializer = new \Liquetsoft\Fias\Component\Serializer\FiasSerializer($normalizers, $encoders);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/MockModel/FiasSerializerMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Database\Eloquent\Model;

/**
* Мок для проверки десериалищации.
* Мок для проверки десериализации.
*/
class FiasSerializerMock extends Model
{
Expand Down
16 changes: 16 additions & 0 deletions tests/MockModel/FiasSerializerMockJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\MockModel;

/**
* Мок для проверки десериализации json.
*/
final class FiasSerializerMockJson
{
public int $id = 0;
public string $name = '';
public float $floatNum = .0;
public ?\DateTimeInterface $date = null;
}
42 changes: 27 additions & 15 deletions tests/Serializer/FiasSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\FiasSerializer;
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\BaseCase;
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\MockModel\FiasSerializerMock;
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\MockModel\FiasSerializerMockJson;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;

Expand Down Expand Up @@ -51,6 +52,32 @@ public function testDeserialize(): void
$this->assertNull($model->nullableCast);
}

/**
* Проверяет, что json верно конвертируется в модель.
*/
public function testDeserializeJson(): void
{
$data = json_encode(
[
'id' => '10',
'name' => 'test',
'floatNum' => '10.1',
'date' => '10.10.2019 10:10:10',
]
);

$type = FiasSerializerMockJson::class;

$serializer = new FiasSerializer();
$model = $serializer->deserialize($data, $type, 'json');

$this->assertInstanceOf($type, $model);
$this->assertSame(10, $model->id);
$this->assertSame('test', $model->name);
$this->assertSame(10.1, $model->floatNum);
$this->assertSame('2019-10-10 10:10', $model->date?->format('Y-m-d H:i'));
}

/**
* Проверяет, что xml верно конвертируется в указанный объект.
*/
Expand Down Expand Up @@ -125,19 +152,4 @@ public function testDeserializeWrongObjectToPopulate(): void
]
);
}

/**
* Проверяет, что пустая строка вернется как пустая строка, а не null.
*/
public function testDeserializeEmptyString(): void
{
$data = '<ActualStatus name=""/>';
$type = FiasSerializerMock::class;

$serializer = new FiasSerializer();
$model = $serializer->deserialize($data, $type, 'xml');

$this->assertInstanceOf(FiasSerializerMock::class, $model);
$this->assertSame('', $model->getAttribute('name'));
}
}

0 comments on commit 2da2f87

Please sign in to comment.