Skip to content

Commit 77de2c1

Browse files
authored
Check format in serializer (#96)
1 parent 2f63322 commit 77de2c1

File tree

6 files changed

+101
-33
lines changed

6 files changed

+101
-33
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: 8 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\Serializer\TypeCaster\EloquentTypeCaster;
910
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\TypeCaster\TypeCaster;
1011
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
@@ -14,7 +15,7 @@
1415
/**
1516
* Нормализатор для объектов eloquent.
1617
*/
17-
final class EloquentDenormalizer implements DenormalizerInterface
18+
final class FiasEloquentDenormalizer implements DenormalizerInterface
1819
{
1920
private readonly TypeCaster $typeCaster;
2021

@@ -28,7 +29,8 @@ public function __construct(?TypeCaster $typeCaster = null)
2829
*/
2930
public function supportsDenormalization($data, string $type, ?string $format = null, array $context = []): bool
3031
{
31-
return is_subclass_of($type, Model::class);
32+
return FiasSerializerFormat::XML->isEqual($format)
33+
&& is_subclass_of($type, Model::class);
3234
}
3335

3436
/**
@@ -67,6 +69,10 @@ public function denormalize($data, string $type, ?string $format = null, array $
6769
*/
6870
public function getSupportedTypes(?string $format): array
6971
{
72+
if (!FiasSerializerFormat::XML->isEqual($format)) {
73+
return [];
74+
}
75+
7076
return [
7177
Model::class => true,
7278
];

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: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\Serializer;
66

77
use Illuminate\Database\Eloquent\Model;
8-
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\EloquentDenormalizer;
8+
use Liquetsoft\Fias\Component\Serializer\FiasSerializerFormat;
9+
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\FiasEloquentDenormalizer;
910
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\TypeCaster\TypeCaster;
1011
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\BaseCase;
1112
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\MockModel\FiasSerializerMock;
@@ -15,32 +16,39 @@
1516
/**
1617
* @internal
1718
*/
18-
final class EloquentDenormalizerTest extends BaseCase
19+
final class FiasEloquentDenormalizerTest extends BaseCase
1920
{
2021
/**
2122
* Проверяет, что денормалайзер правильно определит, что может преобразовать тип.
2223
*
2324
* @dataProvider provideSupportsDenormalization
2425
*/
25-
public function testSupportsDenormalization(string $type, bool $expected): void
26+
public function testSupportsDenormalization(string $type, string $format, bool $expected): void
2627
{
2728
$caster = $this->mock(TypeCaster::class);
2829

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

3233
$this->assertSame($expected, $res);
3334
}
3435

3536
public static function provideSupportsDenormalization(): array
3637
{
3738
return [
38-
'supported type' => [
39+
'supported type and format' => [
3940
FiasSerializerMock::class,
41+
FiasSerializerFormat::XML->value,
4042
true,
4143
],
4244
'unsupported type' => [
4345
'test',
46+
FiasSerializerFormat::XML->value,
47+
false,
48+
],
49+
'unsupported format' => [
50+
FiasSerializerMock::class,
51+
'json',
4452
false,
4553
],
4654
];
@@ -61,8 +69,8 @@ public function testDenormalize(): void
6169
'wrong_attr' => 'wrong attr value',
6270
];
6371

64-
$denormalizer = new EloquentDenormalizer();
65-
$res = $denormalizer->denormalize($data, FiasSerializerMock::class);
72+
$denormalizer = new FiasEloquentDenormalizer();
73+
$res = $denormalizer->denormalize($data, FiasSerializerMock::class, FiasSerializerFormat::XML->value);
6674

6775
$this->assertInstanceOf(FiasSerializerMock::class, $res);
6876
$this->assertSame((int) $data['actstatid'], $res->getAttribute('ACTSTATID'));
@@ -88,10 +96,11 @@ public function testDenormalizeWithObjectToPopulate(): void
8896
];
8997
$model = new FiasSerializerMock();
9098

91-
$denormalizer = new EloquentDenormalizer();
99+
$denormalizer = new FiasEloquentDenormalizer();
92100
$res = $denormalizer->denormalize(
93101
data: $data,
94102
type: FiasSerializerMock::class,
103+
format: FiasSerializerFormat::XML->value,
95104
context: [
96105
'object_to_populate' => $model,
97106
]
@@ -117,12 +126,13 @@ public function testDenormalizeWithObjectToPopulateException(): void
117126
];
118127
$model = new \stdClass();
119128

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

122131
$this->expectException(InvalidArgumentException::class);
123132
$denormalizer->denormalize(
124133
data: $data,
125134
type: FiasSerializerMock::class,
135+
format: FiasSerializerFormat::XML->value,
126136
context: [
127137
'object_to_populate' => $model,
128138
]
@@ -143,25 +153,44 @@ public function testDenormalizeNotNormalizableValueException(): void
143153
->method('canCast')
144154
->willThrowException(new \RuntimeException());
145155

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

148158
$this->expectException(NotNormalizableValueException::class);
149-
$denormalizer->denormalize($data, FiasSerializerMock::class);
159+
$denormalizer->denormalize($data, FiasSerializerMock::class, FiasSerializerFormat::XML->value);
150160
}
151161

152162
/**
153163
* Проверяет, что денормалайзер вернет верный список поддерживаемых объектов.
164+
*
165+
* @dataProvider provideGetSupportedTypes
154166
*/
155-
public function testGetSupportedTypes(): void
167+
public function testGetSupportedTypes(?string $format, array $expected): void
156168
{
157-
$expected = [
158-
Model::class => true,
159-
];
160169
$caster = $this->mock(TypeCaster::class);
161170

162-
$denormalizer = new EloquentDenormalizer($caster);
163-
$res = $denormalizer->getSupportedTypes(null);
171+
$denormalizer = new FiasEloquentDenormalizer($caster);
172+
$res = $denormalizer->getSupportedTypes($format);
164173

165174
$this->assertSame($expected, $res);
166175
}
176+
177+
public static function provideGetSupportedTypes(): array
178+
{
179+
return [
180+
'xml format' => [
181+
FiasSerializerFormat::XML->value,
182+
[
183+
Model::class => true,
184+
],
185+
],
186+
'null format' => [
187+
null,
188+
[],
189+
],
190+
'json format' => [
191+
'json',
192+
[],
193+
],
194+
];
195+
}
167196
}

0 commit comments

Comments
 (0)