Skip to content

Commit 33556aa

Browse files
committed
Add format check to CompiledEntitesDenormalizer
1 parent 0aebfeb commit 33556aa

File tree

6 files changed

+55
-22
lines changed

6 files changed

+55
-22
lines changed

generator/SerializerGenerator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Database\Eloquent\Model;
88
use Liquetsoft\Fias\Component\EntityDescriptor\EntityDescriptor;
9+
use Liquetsoft\Fias\Component\Serializer\FiasSerializerFormat;
910
use Nette\PhpGenerator\ClassType;
1011
use Nette\PhpGenerator\Literal;
1112
use Nette\PhpGenerator\Method;
@@ -50,6 +51,7 @@ protected function decorateNamespace(PhpNamespace $namespace): void
5051
$namespace->addUse(AbstractNormalizer::class);
5152
$namespace->addUse(Model::class);
5253
$namespace->addUse(InvalidArgumentException::class);
54+
$namespace->addUse(FiasSerializerFormat::class);
5355

5456
$descriptors = $this->registry->getDescriptors();
5557
foreach ($descriptors as $descriptor) {
@@ -102,7 +104,7 @@ protected function decorateClass(ClassType $class): void
102104
->addComment("{@inheritDoc}\n")
103105
->setVisibility('public')
104106
->setReturnType('bool')
105-
->setBody('return \\array_key_exists(trim($type, " \t\n\r\0\x0B\\\\/"), self::ALLOWED_ENTITIES);');
107+
->setBody('return FiasSerializerFormat::XML->isEqual($format) && \\array_key_exists(trim($type, " \t\n\r\0\x0B\\\\/"), self::ALLOWED_ENTITIES);');
106108
$supports->addParameter('data');
107109
$supports->addParameter('type')->setType('string');
108110
$supports->addParameter('format', new Literal('null'))->setType('string');
@@ -123,7 +125,7 @@ protected function decorateClass(ClassType $class): void
123125
->addComment("{@inheritDoc}\n")
124126
->setVisibility('public')
125127
->setReturnType('array')
126-
->setBody('return self::ALLOWED_ENTITIES;');
128+
->setBody('return FiasSerializerFormat::XML->isEqual($format) ? self::ALLOWED_ENTITIES : [];');
127129
$getSupportedTypes->addParameter('format')->setType('string')->setNullable(true);
128130

129131
$convertDataToInternalFormatBody = <<<EOT

src/Serializer/CompiledEntitesDenormalizer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer;
66

77
use Illuminate\Database\Eloquent\Model;
8+
use Liquetsoft\Fias\Component\Serializer\FiasSerializerFormat;
89
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Entity\AddrObj;
910
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Entity\AddrObjDivision;
1011
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Entity\AddrObjTypes;
@@ -68,7 +69,7 @@ final class CompiledEntitesDenormalizer implements DenormalizerInterface
6869
*/
6970
public function supportsDenormalization($data, string $type, ?string $format = null, array $context = []): bool
7071
{
71-
return \array_key_exists(trim($type, " \t\n\r\0\x0B\\/"), self::ALLOWED_ENTITIES);
72+
return FiasSerializerFormat::XML->isEqual($format) && \array_key_exists(trim($type, " \t\n\r\0\x0B\\/"), self::ALLOWED_ENTITIES);
7273
}
7374

7475
/**
@@ -172,7 +173,7 @@ public function denormalize($data, string $type, ?string $format = null, array $
172173
*/
173174
public function getSupportedTypes(?string $format): array
174175
{
175-
return self::ALLOWED_ENTITIES;
176+
return FiasSerializerFormat::XML->isEqual($format) ? self::ALLOWED_ENTITIES : [];
176177
}
177178

178179
private function convertDataToInternalFormat(mixed $data): array

src/Serializer/EloquentDenormalizer.php renamed to src/Serializer/FiasEloquentDenormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Нормализатор для объектов eloquent.
1717
*/
18-
final class EloquentDenormalizer implements DenormalizerInterface
18+
final class FiasEloquentDenormalizer implements DenormalizerInterface
1919
{
2020
private readonly TypeCaster $typeCaster;
2121

src/Serializer/FiasSerializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(?array $normalizers = null, ?array $encoders = null)
3232
if ($normalizers === null) {
3333
$normalizers = [
3434
new CompiledEntitesDenormalizer(),
35-
new EloquentDenormalizer(),
35+
new FiasEloquentDenormalizer(),
3636
new DateTimeNormalizer(),
3737
new ObjectNormalizer(
3838
nameConverter: new FiasNameConverter(),

tests/Serializer/CompiledEntitesDenormalizerTest.php

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\Serializer;
66

7+
use Liquetsoft\Fias\Component\Serializer\FiasSerializerFormat;
78
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Entity\AddrObj;
89
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\CompiledEntitesDenormalizer;
910
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\BaseCase;
@@ -19,23 +20,30 @@ final class CompiledEntitesDenormalizerTest extends BaseCase
1920
*
2021
* @dataProvider provideSupportsDenormalization
2122
*/
22-
public function testSupportsDenormalization(string $type, bool $expected): void
23+
public function testSupportsDenormalization(string $type, string $format, bool $expected): void
2324
{
2425
$denormalizer = new CompiledEntitesDenormalizer();
25-
$res = $denormalizer->supportsDenormalization([], $type);
26+
$res = $denormalizer->supportsDenormalization([], $type, $format);
2627

2728
$this->assertSame($expected, $res);
2829
}
2930

3031
public static function provideSupportsDenormalization(): array
3132
{
3233
return [
33-
'supported type' => [
34+
'supported type and format' => [
3435
AddrObj::class,
36+
FiasSerializerFormat::XML->value,
3537
true,
3638
],
3739
'unsupported type' => [
3840
'test',
41+
FiasSerializerFormat::XML->value,
42+
false,
43+
],
44+
'unsupported format' => [
45+
AddrObj::class,
46+
'json',
3947
false,
4048
],
4149
];
@@ -54,7 +62,7 @@ public function testDenormalize(): void
5462
];
5563

5664
$denormalizer = new CompiledEntitesDenormalizer();
57-
$res = $denormalizer->denormalize($data, AddrObj::class);
65+
$res = $denormalizer->denormalize($data, AddrObj::class, FiasSerializerFormat::XML->value);
5866

5967
$this->assertInstanceOf(AddrObj::class, $res);
6068
$this->assertSame((int) $data['@ID'], $res->getAttribute('id'));
@@ -76,7 +84,7 @@ public function testDenormalizeModelNames(): void
7684
];
7785

7886
$denormalizer = new CompiledEntitesDenormalizer();
79-
$res = $denormalizer->denormalize($data, AddrObj::class);
87+
$res = $denormalizer->denormalize($data, AddrObj::class, FiasSerializerFormat::XML->value);
8088

8189
$this->assertInstanceOf(AddrObj::class, $res);
8290
$this->assertSame((int) $data['id'], $res->getAttribute('id'));
@@ -102,6 +110,7 @@ public function testDenormalizeWithObjectToPopulate(): void
102110
$res = $denormalizer->denormalize(
103111
data: $data,
104112
type: AddrObj::class,
113+
format: FiasSerializerFormat::XML->value,
105114
context: [
106115
'object_to_populate' => $model,
107116
]
@@ -131,6 +140,7 @@ public function testDenormalizeWithObjectToPopulateException(): void
131140
$denormalizer->denormalize(
132141
data: $data,
133142
type: AddrObj::class,
143+
format: FiasSerializerFormat::XML->value,
134144
context: [
135145
'object_to_populate' => $model,
136146
]
@@ -139,12 +149,32 @@ public function testDenormalizeWithObjectToPopulateException(): void
139149

140150
/**
141151
* Проверяет, что денормалайзер вернет верный список поддерживаемых объектов.
152+
*
153+
* @dataProvider provideGetSupportedTypes
142154
*/
143-
public function testGetSupportedTypes(): void
155+
public function testGetSupportedTypes(?string $format, array|true $expected): void
144156
{
145157
$denormalizer = new CompiledEntitesDenormalizer();
146-
$res = $denormalizer->getSupportedTypes(null);
158+
$res = $denormalizer->getSupportedTypes($format);
159+
160+
if ($expected === true) {
161+
$this->assertNotEmpty($res);
162+
} else {
163+
$this->assertSame($expected, $res);
164+
}
165+
}
147166

148-
$this->assertNotEmpty($res);
167+
public static function provideGetSupportedTypes(): array
168+
{
169+
return [
170+
'xml format' => [
171+
FiasSerializerFormat::XML->value,
172+
true,
173+
],
174+
'non xml format' => [
175+
'json',
176+
[],
177+
],
178+
];
149179
}
150180
}

tests/Serializer/EloquentDenormalizerTest.php renamed to tests/Serializer/FiasEloquentDenormalizerTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Illuminate\Database\Eloquent\Model;
88
use Liquetsoft\Fias\Component\Serializer\FiasSerializerFormat;
9-
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\EloquentDenormalizer;
9+
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\FiasEloquentDenormalizer;
1010
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\TypeCaster\TypeCaster;
1111
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\BaseCase;
1212
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\MockModel\FiasSerializerMock;
@@ -16,7 +16,7 @@
1616
/**
1717
* @internal
1818
*/
19-
final class EloquentDenormalizerTest extends BaseCase
19+
final class FiasEloquentDenormalizerTest extends BaseCase
2020
{
2121
/**
2222
* Проверяет, что денормалайзер правильно определит, что может преобразовать тип.
@@ -27,7 +27,7 @@ public function testSupportsDenormalization(string $type, string $format, bool $
2727
{
2828
$caster = $this->mock(TypeCaster::class);
2929

30-
$denormalizer = new EloquentDenormalizer($caster);
30+
$denormalizer = new FiasEloquentDenormalizer($caster);
3131
$res = $denormalizer->supportsDenormalization([], $type, $format);
3232

3333
$this->assertSame($expected, $res);
@@ -69,7 +69,7 @@ public function testDenormalize(): void
6969
'wrong_attr' => 'wrong attr value',
7070
];
7171

72-
$denormalizer = new EloquentDenormalizer();
72+
$denormalizer = new FiasEloquentDenormalizer();
7373
$res = $denormalizer->denormalize($data, FiasSerializerMock::class, FiasSerializerFormat::XML->value);
7474

7575
$this->assertInstanceOf(FiasSerializerMock::class, $res);
@@ -96,7 +96,7 @@ public function testDenormalizeWithObjectToPopulate(): void
9696
];
9797
$model = new FiasSerializerMock();
9898

99-
$denormalizer = new EloquentDenormalizer();
99+
$denormalizer = new FiasEloquentDenormalizer();
100100
$res = $denormalizer->denormalize(
101101
data: $data,
102102
type: FiasSerializerMock::class,
@@ -126,7 +126,7 @@ public function testDenormalizeWithObjectToPopulateException(): void
126126
];
127127
$model = new \stdClass();
128128

129-
$denormalizer = new EloquentDenormalizer();
129+
$denormalizer = new FiasEloquentDenormalizer();
130130

131131
$this->expectException(InvalidArgumentException::class);
132132
$denormalizer->denormalize(
@@ -153,7 +153,7 @@ public function testDenormalizeNotNormalizableValueException(): void
153153
->method('canCast')
154154
->willThrowException(new \RuntimeException());
155155

156-
$denormalizer = new EloquentDenormalizer($caster);
156+
$denormalizer = new FiasEloquentDenormalizer($caster);
157157

158158
$this->expectException(NotNormalizableValueException::class);
159159
$denormalizer->denormalize($data, FiasSerializerMock::class, FiasSerializerFormat::XML->value);
@@ -168,7 +168,7 @@ public function testGetSupportedTypes(?string $format, array $expected): void
168168
{
169169
$caster = $this->mock(TypeCaster::class);
170170

171-
$denormalizer = new EloquentDenormalizer($caster);
171+
$denormalizer = new FiasEloquentDenormalizer($caster);
172172
$res = $denormalizer->getSupportedTypes($format);
173173

174174
$this->assertSame($expected, $res);

0 commit comments

Comments
 (0)