Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use serializer provided by component lib #101

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
}
}
Loading