Skip to content

Commit 7c2a28b

Browse files
committed
Fix FiasSerializer
1 parent 7027095 commit 7c2a28b

File tree

2 files changed

+130
-5
lines changed

2 files changed

+130
-5
lines changed

src/LiquetsoftFiasBundleServiceProvider.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,9 @@
6868
/**
6969
* Service provider для модуля.
7070
*/
71-
class LiquetsoftFiasBundleServiceProvider extends ServiceProvider
71+
final class LiquetsoftFiasBundleServiceProvider extends ServiceProvider
7272
{
73-
/**
74-
* @var string
75-
*/
76-
protected $bundlePrefix = 'liquetsoft_fias';
73+
private string $bundlePrefix = 'liquetsoft_fias';
7774

7875
/**
7976
* Регистрирует сервисы модуля в приложении.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\Serializer;
6+
7+
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Entity\AddrObj;
8+
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Serializer\CompiledEntitesDenormalizer;
9+
use Liquetsoft\Fias\Laravel\LiquetsoftFiasBundle\Tests\BaseCase;
10+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
11+
12+
/**
13+
* @internal
14+
*/
15+
final class CompiledEntitesDenormalizerTest extends BaseCase
16+
{
17+
/**
18+
* Проверяет, что денормалайзер правильно определит, что может преобразовать тип.
19+
*
20+
* @dataProvider provideSupportsDenormalization
21+
*/
22+
public function testSupportsDenormalization(string $type, bool $expected): void
23+
{
24+
$denormalizer = new CompiledEntitesDenormalizer();
25+
$res = $denormalizer->supportsDenormalization([], $type);
26+
27+
$this->assertSame($expected, $res);
28+
}
29+
30+
public static function provideSupportsDenormalization(): array
31+
{
32+
return [
33+
'supported type' => [
34+
AddrObj::class,
35+
true,
36+
],
37+
'unsupported type' => [
38+
'test',
39+
false,
40+
],
41+
];
42+
}
43+
44+
/**
45+
* Проверяет, что денормалайзер правильно преобразует массив в модель.
46+
*/
47+
public function testDenormalize(): void
48+
{
49+
$data = [
50+
'@ID' => '100',
51+
'@OBJECTID' => '101',
52+
'@OBJECTGUID' => '102',
53+
'@PREVID' => null,
54+
];
55+
56+
$denormalizer = new CompiledEntitesDenormalizer();
57+
$res = $denormalizer->denormalize($data, AddrObj::class);
58+
59+
$this->assertInstanceOf(AddrObj::class, $res);
60+
$this->assertSame((int) $data['@ID'], $res->getAttribute('id'));
61+
$this->assertSame((int) $data['@OBJECTID'], $res->getAttribute('objectid'));
62+
$this->assertSame($data['@OBJECTGUID'], $res->getAttribute('objectguid'));
63+
$this->assertNull($res->getAttribute('@PREVID'));
64+
}
65+
66+
/**
67+
* Проверяет, что денормалайзер правильно передаст массив в инициированную модель.
68+
*/
69+
public function testDenormalizeWithObjectToPopulate(): void
70+
{
71+
$data = [
72+
'@ID' => '100',
73+
'@OBJECTID' => '101',
74+
'@OBJECTGUID' => '102',
75+
'@PREVID' => null,
76+
];
77+
$model = new AddrObj();
78+
79+
$denormalizer = new CompiledEntitesDenormalizer();
80+
$res = $denormalizer->denormalize(
81+
data: $data,
82+
type: AddrObj::class,
83+
context: [
84+
'object_to_populate' => $model,
85+
]
86+
);
87+
88+
$this->assertInstanceOf(AddrObj::class, $res);
89+
$this->assertSame($model, $res);
90+
$this->assertSame((int) $data['@ID'], $res->getAttribute('id'));
91+
$this->assertSame((int) $data['@OBJECTID'], $res->getAttribute('objectid'));
92+
$this->assertSame($data['@OBJECTGUID'], $res->getAttribute('objectguid'));
93+
$this->assertNull($res->getAttribute('@PREVID'));
94+
}
95+
96+
/**
97+
* Проверяет, что денормалайзер выьросит исключение, если указана неверная модель для наполнения.
98+
*/
99+
public function testDenormalizeWithObjectToPopulateException(): void
100+
{
101+
$data = [
102+
'id' => '100',
103+
];
104+
$model = new \stdClass();
105+
106+
$denormalizer = new CompiledEntitesDenormalizer();
107+
108+
$this->expectException(InvalidArgumentException::class);
109+
$denormalizer->denormalize(
110+
data: $data,
111+
type: AddrObj::class,
112+
context: [
113+
'object_to_populate' => $model,
114+
]
115+
);
116+
}
117+
118+
/**
119+
* Проверяет, что денормалайзер вернет верный список поддерживаемых объектов.
120+
*/
121+
public function testGetSupportedTypes(): void
122+
{
123+
$denormalizer = new CompiledEntitesDenormalizer();
124+
$res = $denormalizer->getSupportedTypes(null);
125+
126+
$this->assertNotEmpty($res);
127+
}
128+
}

0 commit comments

Comments
 (0)