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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
namespace JMS\Serializer\Tests;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\Expression\ExpressionEvaluator;
use JMS\Serializer\Handler\HandlerRegistry;
use JMS\Serializer\JsonSerializationVisitor;
use JMS\Serializer\Naming\CamelCaseNamingStrategy;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\Tests\Fixtures\ContextualNamingStrategy;
use JMS\Serializer\Tests\Fixtures\Person;
use JMS\Serializer\Tests\Fixtures\PersonSecret;
use JMS\Serializer\Tests\Fixtures\PersonSecretWithVariables;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Filesystem\Filesystem;
class SerializerBuilderTest extends \PHPUnit_Framework_TestCase
{
/** @var SerializerBuilder */
private $builder;
private $fs;
private $tmpDir;
public function testBuildWithoutAnythingElse()
{
$serializer = $this->builder->build();
$this->assertEquals('"foo"', $serializer->serialize('foo', 'json'));
$this->assertEquals('<?xml version="1.0" encoding="UTF-8"?>
<result><![CDATA[foo]]></result>
', $serializer->serialize('foo', 'xml'));
$this->assertEquals('foo
', $serializer->serialize('foo', 'yml'));
$this->assertEquals('foo', $serializer->deserialize('"foo"', 'string', 'json'));
$this->assertEquals('foo', $serializer->deserialize('<?xml version="1.0" encoding="UTF-8"?><result><![CDATA[foo]]></result>', 'string', 'xml'));
}
public function testWithCache()
{
$this->assertFileNotExists($this->tmpDir);
$this->assertSame($this->builder, $this->builder->setCacheDir($this->tmpDir));
$serializer = $this->builder->build();
$this->assertFileExists($this->tmpDir);
$this->assertFileExists($this->tmpDir . '/annotations');
$this->assertFileExists($this->tmpDir . '/metadata');
$factory = $this->getField($serializer, 'factory');
$this->assertAttributeSame(false, 'debug', $factory);
$this->assertAttributeNotSame(null, 'cache', $factory);
}
public function testDoesAddDefaultHandlers()
{
$serializer = $this->builder->build();
$this->assertEquals('"2020-04-16T00:00:00+0000"', $serializer->serialize(new \DateTime('2020-04-16', new \DateTimeZone('UTC')), 'json'));
}
public function testDoesNotAddDefaultHandlersWhenExplicitlyConfigured()
{
$this->assertSame($this->builder, $this->builder->configureHandlers(function (HandlerRegistry $registry) {
}));
$this->assertEquals('{}', $this->builder->build()->serialize(new \DateTime('2020-04-16'), 'json'));
}
/**
* @expectedException JMS\Serializer\Exception\UnsupportedFormatException
* @expectedExceptionMessage The format "xml" is not supported for serialization.
*/
public function testDoesNotAddOtherVisitorsWhenConfiguredExplicitly()
{
$this->assertSame(
$this->builder,
$this->builder->setSerializationVisitor('json', new JsonSerializationVisitor(new CamelCaseNamingStrategy()))
);
$this->builder->build()->serialize('foo', 'xml');
}
public function testIncludeInterfaceMetadata()
{
$this->assertFalse(
$this->getIncludeInterfaces($this->builder),
'Interface metadata are not included by default'
);
$this->assertTrue(
$this->getIncludeInterfaces($this->builder->includeInterfaceMetadata(true)),
'Force including interface metadata'
);
$this->assertFalse(
$this->getIncludeInterfaces($this->builder->includeInterfaceMetadata(false)),
'Force not including interface metadata'
);
$this->assertSame(
$this->builder,
$this->builder->includeInterfaceMetadata(true)
);
}
public function testSetSerializationContext()
{
$contextFactoryMock = $this->getMockForAbstractClass('JMS\\Serializer\\ContextFactory\\SerializationContextFactoryInterface');
$context = new SerializationContext();
$context->setSerializeNull(true);
$contextFactoryMock
->expects($this->once())
->method('createSerializationContext')
->will($this->returnValue($context));
$this->builder->setSerializationContextFactory($contextFactoryMock);
$serializer = $this->builder->build();
$result = $serializer->serialize(array('value' => null), 'json');
$this->assertEquals('{"value":null}', $result);
}
public function testSetDeserializationContext()
{
$contextFactoryMock = $this->getMockForAbstractClass('JMS\\Serializer\\ContextFactory\\DeserializationContextFactoryInterface');
$context = new DeserializationContext();
$contextFactoryMock
->expects($this->once())
->method('createDeserializationContext')
->will($this->returnValue($context));
$this->builder->setDeserializationContextFactory($contextFactoryMock);
$serializer = $this->builder->build();
$result = $serializer->deserialize('{"value":null}', 'array', 'json');
$this->assertEquals(array('value' => null), $result);
}
public function testSetCallbackSerializationContextWithSerializeNull()
{
$this->builder->setSerializationContextFactory(function () {
return SerializationContext::create()
->setSerializeNull(true);
});
$serializer = $this->builder->build();
$result = $serializer->serialize(array('value' => null), 'json');
$this->assertEquals('{"value":null}', $result);
}
public function testSetCallbackSerializationContextWithNotSerializeNull()
{
$this->builder->setSerializationContextFactory(function () {
return SerializationContext::create()
->setSerializeNull(false);
});
$serializer = $this->builder->build();
$result = $serializer->serialize(array('value' => null, 'not_null' => 'ok'), 'json');
$this->assertEquals('{"not_null":"ok"}', $result);
}
public function expressionFunctionProvider()
{
return [
[
new ExpressionFunction('show_data', function () {
return "true";
}, function () {
return true;
}),
'{"name":"mike"}'
],
[
new ExpressionFunction('show_data', function () {
return "false";
}, function () {
return false;
}),
'{"name":"mike","gender":"f"}'
]
];
}
/**
* @dataProvider expressionFunctionProvider
* @param ExpressionFunction $function
* @param $json
*/
public function testExpressionEngine(ExpressionFunction $function, $json)
{
$language = new ExpressionLanguage();
$language->addFunction($function);
$this->builder->setExpressionEvaluator(new ExpressionEvaluator($language));
$serializer = $this->builder->build();
$person = new PersonSecret();
$person->gender = 'f';
$person->name = 'mike';
$this->assertEquals($json, $serializer->serialize($person, 'json'));
}
public function testExpressionEngineWhenDeserializing()
{
$language = new ExpressionLanguage();
$this->builder->setExpressionEvaluator(new ExpressionEvaluator($language));
$serializer = $this->builder->build();
$person = new PersonSecretWithVariables();
$person->gender = 'f';
$person->name = 'mike';
$serialized = $serializer->serialize($person, 'json');
$this->assertEquals('{"name":"mike","gender":"f"}', $serialized);
$object = $serializer->deserialize($serialized, PersonSecretWithVariables::class, 'json');
$this->assertEquals($person, $object);
}
public function testAdvancedNamingStrategy()
{
$this->builder->setAdvancedNamingStrategy(new ContextualNamingStrategy());
$serializer = $this->builder->build();
$person = new Person();
$person->name = "bar";
$json = $serializer->serialize($person, "json");
$this->assertEquals('{"NAME":"bar"}', $json);
$json = '{"Name": "bar"}';
$person = $serializer->deserialize($json, Person::class, "json");
$this->assertEquals("bar", $person->name);
}
protected function setUp()
{
$this->builder = SerializerBuilder::create();
$this->fs = new Filesystem();
$this->tmpDir = sys_get_temp_dir() . '/serializer';
$this->fs->remove($this->tmpDir);
clearstatcache();
}
protected function tearDown()
{
$this->fs->remove($this->tmpDir);
}
private function getField($obj, $name)
{
$ref = new \ReflectionProperty($obj, $name);
$ref->setAccessible(true);
return $ref->getValue($obj);
}
private function getIncludeInterfaces(SerializerBuilder $builder)
{
$factory = $this->getField($builder->build(), 'factory');
return $this->getField($factory, 'includeInterfaces');
}
}