-
Notifications
You must be signed in to change notification settings - Fork 8
/
DenormalizeUsingPhpDocTest.php
126 lines (105 loc) · 3.25 KB
/
DenormalizeUsingPhpDocTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Tests\GBProd\UuidNormalizer;
use GBProd\UuidNormalizer\UuidDenormalizer;
use GBProd\UuidNormalizer\UuidNormalizer;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class DenormalizeUsingPhpDocTest extends TestCase
{
private $serializer;
public function setUp()
{
$normalizers = [
new UuidNormalizer(),
new UuidDenormalizer(),
new ObjectNormalizer(
null,
null,
null,
class_exists('Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor') ? new PhpDocExtractor() : null
)
];
if (class_exists('Symfony\Component\Serializer\Normalizer\ArrayDenormalizer')) {
$normalizers[] = new ArrayDenormalizer();
}
$this->serializer = new Serializer($normalizers, [
new JsonEncoder(),
]);
}
public function testDenormalizeWithUuid()
{
$uuid = Uuid::uuid4();
$denormalized = $this->serializer->denormalize(
['uuid' => $uuid->toString()],
ClassWithUuidAttribute::class
);
$this->assertEquals($uuid, $denormalized->uuid);
}
public function testDenormalizeWithUuidInterface()
{
$uuid = Uuid::uuid4();
$denormalized = $this->serializer->denormalize(
['uuid' => $uuid->toString()],
ClassWithUuidInterfaceAttribute::class
);
$this->assertEquals($uuid, $denormalized->uuid);
}
public function testDenormalizeWithArrayOfUuid()
{
$uuids = [
Uuid::uuid1(),
Uuid::uuid3(Uuid::NAMESPACE_DNS, 'php.net'),
Uuid::uuid4(),
Uuid::uuid5(Uuid::NAMESPACE_DNS, 'php.net'),
];
$denormalized = $this->serializer->denormalize(
['uuids' => array_map(function ($uuid) {
return $uuid->toString();
}, $uuids)],
ClassWithArrayOfUuidAttribute::class
);
$this->assertEquals($uuids, $denormalized->uuids);
}
public function testDenormalizeWithArrayOfUuidInterface()
{
$uuids = [
Uuid::uuid1(),
Uuid::uuid3(Uuid::NAMESPACE_DNS, 'php.net'),
Uuid::uuid4(),
Uuid::uuid5(Uuid::NAMESPACE_DNS, 'php.net'),
];
$denormalized = $this->serializer->denormalize(
['uuids' => array_map(function ($uuid) {
return $uuid->toString();
}, $uuids)],
ClassWithArrayOfUuidInterfaceAttribute::class
);
$this->assertEquals($uuids, $denormalized->uuids);
}
}
class ClassWithUuidAttribute
{
/** @var Uuid */
public $uuid;
}
class ClassWithUuidInterfaceAttribute
{
/** @var UuidInterface */
public $uuid;
}
class ClassWithArrayOfUuidAttribute
{
/** @var Uuid[] */
public $uuids;
}
class ClassWithArrayOfUuidInterfaceAttribute
{
/** @var UuidInterface[] */
public $uuids;
}