|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace JMS\SerializerBundle\Tests\DependencyInjection; |
|
20
|
|
|
|
|
21
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
22
|
|
|
use JMS\Serializer\SerializationContext; |
|
23
|
|
|
use JMS\SerializerBundle\JMSSerializerBundle; |
|
24
|
|
|
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingExpressionLanguage; |
|
25
|
|
|
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingExpressionProperties; |
|
26
|
|
|
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\SimpleObject; |
|
27
|
|
|
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\VersionedObject; |
|
28
|
|
|
use PHPUnit\Framework\TestCase; |
|
29
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
30
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
31
|
|
|
|
|
32
|
|
|
class JMSSerializerExtensionTest extends TestCase |
|
33
|
|
|
{ |
|
34
|
|
|
protected function setUp() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->clearTempDir(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function tearDown() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->clearTempDir(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function clearTempDir() |
|
45
|
|
|
{ |
|
46
|
|
|
// clear temporary directory |
|
47
|
|
|
$dir = sys_get_temp_dir() . '/serializer'; |
|
48
|
|
|
if (is_dir($dir)) { |
|
49
|
|
|
foreach (new \RecursiveDirectoryIterator($dir) as $file) { |
|
50
|
|
|
$filename = $file->getFileName(); |
|
51
|
|
|
if ('.' === $filename || '..' === $filename) { |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
@unlink($file->getPathName()); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
@rmdir($dir); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testHasContextFactories() |
|
63
|
|
|
{ |
|
64
|
|
|
$container = $this->getContainerForConfig(array(array())); |
|
65
|
|
|
|
|
66
|
|
|
$factory = $container->get('jms_serializer.serialization_context_factory'); |
|
67
|
|
|
$this->assertInstanceOf('JMS\Serializer\ContextFactory\SerializationContextFactoryInterface', $factory); |
|
68
|
|
|
|
|
69
|
|
|
$factory = $container->get('jms_serializer.deserialization_context_factory'); |
|
70
|
|
|
$this->assertInstanceOf('JMS\Serializer\ContextFactory\DeserializationContextFactoryInterface', $factory); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testSerializerContextFactoriesAreSet() |
|
74
|
|
|
{ |
|
75
|
|
|
$container = $this->getContainerForConfig(array(array())); |
|
76
|
|
|
|
|
77
|
|
|
$def = $container->getDefinition('jms_serializer'); |
|
78
|
|
|
$calls = $def->getMethodCalls(); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertCount(2, $calls); |
|
81
|
|
|
|
|
82
|
|
|
$serializationCall = $calls[0]; |
|
83
|
|
|
$this->assertEquals('setSerializationContextFactory', $serializationCall[0]); |
|
84
|
|
|
$this->assertEquals('jms_serializer.serialization_context_factory', (string)$serializationCall[1][0]); |
|
85
|
|
|
|
|
86
|
|
|
$serializationCall = $calls[1]; |
|
87
|
|
|
$this->assertEquals('setDeserializationContextFactory', $serializationCall[0]); |
|
88
|
|
|
$this->assertEquals('jms_serializer.deserialization_context_factory', (string)$serializationCall[1][0]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testSerializerContextFactoriesWithId() |
|
92
|
|
|
{ |
|
93
|
|
|
$config = array( |
|
94
|
|
|
'default_context' => array( |
|
95
|
|
|
'serialization' => array( |
|
96
|
|
|
'id' => 'foo' |
|
97
|
|
|
), |
|
98
|
|
|
'deserialization' => array( |
|
99
|
|
|
'id' => 'bar' |
|
100
|
|
|
) |
|
101
|
|
|
) |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
$foo = new Definition('stdClass'); |
|
105
|
|
|
$foo->setPublic(true); |
|
106
|
|
|
$bar = new Definition('stdClass'); |
|
107
|
|
|
$bar->setPublic(true); |
|
108
|
|
|
|
|
109
|
|
|
$container = $this->getContainerForConfig(array($config), function (ContainerBuilder $containerBuilder) use ($foo, $bar) { |
|
110
|
|
|
$containerBuilder->setDefinition('foo', $foo); |
|
111
|
|
|
$containerBuilder->setDefinition('bar', $bar); |
|
112
|
|
|
}); |
|
113
|
|
|
|
|
114
|
|
|
$def = $container->getDefinition('jms_serializer'); |
|
115
|
|
|
$calls = $def->getMethodCalls(); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertCount(2, $calls); |
|
118
|
|
|
|
|
119
|
|
|
$serializationCall = $calls[0]; |
|
120
|
|
|
$this->assertEquals('setSerializationContextFactory', $serializationCall[0]); |
|
121
|
|
|
$this->assertEquals('foo', (string)$serializationCall[1][0]); |
|
122
|
|
|
|
|
123
|
|
|
$serializationCall = $calls[1]; |
|
124
|
|
|
$this->assertEquals('setDeserializationContextFactory', $serializationCall[0]); |
|
125
|
|
|
$this->assertEquals('bar', (string)$serializationCall[1][0]); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertEquals('bar', (string)$container->getAlias('jms_serializer.deserialization_context_factory')); |
|
128
|
|
|
$this->assertEquals('foo', (string)$container->getAlias('jms_serializer.serialization_context_factory')); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function testLoadWithoutTranslator() |
|
132
|
|
|
{ |
|
133
|
|
|
$container = $this->getContainerForConfig(array(array()), function (ContainerBuilder $containerBuilder) { |
|
134
|
|
|
$containerBuilder->set('translator', null); |
|
|
|
|
|
|
135
|
|
|
$containerBuilder->getDefinition('jms_serializer.form_error_handler')->setPublic(true); |
|
136
|
|
|
}); |
|
137
|
|
|
|
|
138
|
|
|
$def = $container->getDefinition('jms_serializer.form_error_handler'); |
|
139
|
|
|
$this->assertSame(null, $def->getArgument(0)); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testConfiguringContextFactories() |
|
143
|
|
|
{ |
|
144
|
|
|
$container = $this->getContainerForConfig(array(array())); |
|
145
|
|
|
|
|
146
|
|
|
$def = $container->getDefinition('jms_serializer.serialization_context_factory'); |
|
147
|
|
|
$this->assertCount(0, $def->getMethodCalls()); |
|
148
|
|
|
|
|
149
|
|
|
$def = $container->getDefinition('jms_serializer.deserialization_context_factory'); |
|
150
|
|
|
$this->assertCount(0, $def->getMethodCalls()); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function testConfiguringContextFactoriesWithParams() |
|
154
|
|
|
{ |
|
155
|
|
|
$config = array( |
|
156
|
|
|
'default_context' => array( |
|
157
|
|
|
'serialization' => array( |
|
158
|
|
|
'version' => 1600, |
|
159
|
|
|
'serialize_null' => true, |
|
160
|
|
|
'attributes' => array('x' => 1720), |
|
161
|
|
|
'groups' => array('Default', 'Registration'), |
|
162
|
|
|
'enable_max_depth_checks' => true, |
|
163
|
|
|
), |
|
164
|
|
|
'deserialization' => array( |
|
165
|
|
|
'version' => 1640, |
|
166
|
|
|
'serialize_null' => false, |
|
167
|
|
|
'attributes' => array('x' => 1740), |
|
168
|
|
|
'groups' => array('Default', 'Profile'), |
|
169
|
|
|
'enable_max_depth_checks' => true, |
|
170
|
|
|
) |
|
171
|
|
|
) |
|
172
|
|
|
); |
|
173
|
|
|
|
|
174
|
|
|
$container = $this->getContainerForConfig(array($config)); |
|
175
|
|
|
$services = [ |
|
176
|
|
|
'serialization' => 'jms_serializer.serialization_context_factory', |
|
177
|
|
|
'deserialization' => 'jms_serializer.deserialization_context_factory', |
|
178
|
|
|
]; |
|
179
|
|
|
foreach ($services as $configKey => $serviceId) { |
|
180
|
|
|
$def = $container->getDefinition($serviceId); |
|
181
|
|
|
$values = $config['default_context'][$configKey]; |
|
182
|
|
|
|
|
183
|
|
|
$this->assertSame($values['version'], $this->getDefinitionMethodCall($def, 'setVersion')[0]); |
|
184
|
|
|
$this->assertSame($values['serialize_null'], $this->getDefinitionMethodCall($def, 'setSerializeNulls')[0]); |
|
185
|
|
|
$this->assertSame($values['attributes'], $this->getDefinitionMethodCall($def, 'setAttributes')[0]); |
|
186
|
|
|
$this->assertSame($values['groups'], $this->getDefinitionMethodCall($def, 'setGroups')[0]); |
|
187
|
|
|
$this->assertSame($values['groups'], $this->getDefinitionMethodCall($def, 'setGroups')[0]); |
|
188
|
|
|
$this->assertSame(array(), $this->getDefinitionMethodCall($def, 'enableMaxDepthChecks')); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function testConfiguringContextFactoriesWithNullDefaults() |
|
193
|
|
|
{ |
|
194
|
|
|
$config = array( |
|
195
|
|
|
'default_context' => array( |
|
196
|
|
|
'serialization' => array( |
|
197
|
|
|
'version' => null, |
|
198
|
|
|
'serialize_null' => null, |
|
199
|
|
|
'attributes' => [], |
|
200
|
|
|
'groups' => null, |
|
201
|
|
|
), |
|
202
|
|
|
'deserialization' => array( |
|
203
|
|
|
'version' => null, |
|
204
|
|
|
'serialize_null' => null, |
|
205
|
|
|
'attributes' => null, |
|
206
|
|
|
'groups' => null, |
|
207
|
|
|
) |
|
208
|
|
|
) |
|
209
|
|
|
); |
|
210
|
|
|
|
|
211
|
|
|
$container = $this->getContainerForConfig(array($config)); |
|
212
|
|
|
$services = [ |
|
213
|
|
|
'serialization' => 'jms_serializer.serialization_context_factory', |
|
214
|
|
|
'deserialization' => 'jms_serializer.deserialization_context_factory', |
|
215
|
|
|
]; |
|
216
|
|
|
foreach ($services as $configKey => $serviceId) { |
|
217
|
|
|
$def = $container->getDefinition($serviceId); |
|
218
|
|
|
$this->assertCount(0, $def->getMethodCalls()); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
private function getDefinitionMethodCall(Definition $def, $method) |
|
223
|
|
|
{ |
|
224
|
|
|
foreach ($def->getMethodCalls() as $call) { |
|
225
|
|
|
if ($call[0] === $method) { |
|
226
|
|
|
return $call[1]; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
return false; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function testLoad() |
|
233
|
|
|
{ |
|
234
|
|
|
$container = $this->getContainerForConfig(array(array()), function (ContainerBuilder $container) { |
|
235
|
|
|
$container->getDefinition('jms_serializer.doctrine_object_constructor')->setPublic(true); |
|
236
|
|
|
$container->getDefinition('jms_serializer.array_collection_handler')->setPublic(true); |
|
237
|
|
|
$container->getDefinition('jms_serializer.doctrine_proxy_subscriber')->setPublic(true); |
|
238
|
|
|
$container->getAlias('JMS\Serializer\SerializerInterface')->setPublic(true); |
|
239
|
|
|
$container->getAlias('JMS\Serializer\ArrayTransformerInterface')->setPublic(true); |
|
240
|
|
|
}); |
|
241
|
|
|
|
|
242
|
|
|
$simpleObject = new SimpleObject('foo', 'bar'); |
|
243
|
|
|
$versionedObject = new VersionedObject('foo', 'bar'); |
|
244
|
|
|
$serializer = $container->get('jms_serializer'); |
|
245
|
|
|
|
|
246
|
|
|
$this->assertTrue($container->has('JMS\Serializer\SerializerInterface'), 'Alias should be defined to allow autowiring'); |
|
247
|
|
|
$this->assertTrue($container->has('JMS\Serializer\ArrayTransformerInterface'), 'Alias should be defined to allow autowiring'); |
|
248
|
|
|
|
|
249
|
|
|
$this->assertFalse($container->getDefinition('jms_serializer.array_collection_handler')->getArgument(0)); |
|
250
|
|
|
|
|
251
|
|
|
// the logic is inverted because arg 0 on doctrine_proxy_subscriber is $skipVirtualTypeInit = false |
|
252
|
|
|
$this->assertTrue($container->getDefinition('jms_serializer.doctrine_proxy_subscriber')->getArgument(0)); |
|
253
|
|
|
$this->assertFalse($container->getDefinition('jms_serializer.doctrine_proxy_subscriber')->getArgument(1)); |
|
254
|
|
|
|
|
255
|
|
|
$this->assertEquals("null", $container->getDefinition('jms_serializer.doctrine_object_constructor')->getArgument(2)); |
|
256
|
|
|
|
|
257
|
|
|
// test that all components have been wired correctly |
|
258
|
|
|
$this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($versionedObject, 'json')); |
|
259
|
|
|
$this->assertEquals($simpleObject, $serializer->deserialize($serializer->serialize($simpleObject, 'json'), get_class($simpleObject), 'json')); |
|
260
|
|
|
$this->assertEquals($simpleObject, $serializer->deserialize($serializer->serialize($simpleObject, 'xml'), get_class($simpleObject), 'xml')); |
|
261
|
|
|
|
|
262
|
|
|
$this->assertEquals(json_encode(array('name' => 'foo')), $serializer->serialize($versionedObject, 'json', SerializationContext::create()->setVersion('0.0.1'))); |
|
263
|
|
|
|
|
264
|
|
|
$this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($versionedObject, 'json', SerializationContext::create()->setVersion('1.1.1'))); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
public function testLoadWithOptions() |
|
268
|
|
|
{ |
|
269
|
|
|
$container = $this->getContainerForConfig(array(array( |
|
270
|
|
|
'subscribers' => [ |
|
271
|
|
|
'doctrine_proxy' => [ |
|
272
|
|
|
'initialize_virtual_types' => true, |
|
273
|
|
|
'initialize_excluded' => true, |
|
274
|
|
|
], |
|
275
|
|
|
], |
|
276
|
|
|
'object_constructors' => [ |
|
277
|
|
|
'doctrine' => [ |
|
278
|
|
|
'fallback_strategy' => "exception", |
|
279
|
|
|
], |
|
280
|
|
|
], |
|
281
|
|
|
'handlers' => [ |
|
282
|
|
|
'array_collection' => [ |
|
283
|
|
|
'initialize_excluded' => true, |
|
284
|
|
|
], |
|
285
|
|
|
], |
|
286
|
|
|
)), function ($container) { |
|
287
|
|
|
$container->getDefinition('jms_serializer.doctrine_object_constructor')->setPublic(true); |
|
288
|
|
|
$container->getDefinition('jms_serializer.array_collection_handler')->setPublic(true); |
|
289
|
|
|
$container->getDefinition('jms_serializer.doctrine_proxy_subscriber')->setPublic(true); |
|
290
|
|
|
}); |
|
291
|
|
|
|
|
292
|
|
|
$this->assertTrue($container->getDefinition('jms_serializer.array_collection_handler')->getArgument(0)); |
|
293
|
|
|
|
|
294
|
|
|
// the logic is inverted because arg 0 on doctrine_proxy_subscriber is $skipVirtualTypeInit = false |
|
295
|
|
|
$this->assertFalse($container->getDefinition('jms_serializer.doctrine_proxy_subscriber')->getArgument(0)); |
|
296
|
|
|
$this->assertTrue($container->getDefinition('jms_serializer.doctrine_proxy_subscriber')->getArgument(1)); |
|
297
|
|
|
|
|
298
|
|
|
$this->assertEquals("exception", $container->getDefinition('jms_serializer.doctrine_object_constructor')->getArgument(2)); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
public function testLoadExistentMetadataDir() |
|
302
|
|
|
{ |
|
303
|
|
|
$container = $this->getContainerForConfig(array(array( |
|
304
|
|
|
'metadata' => [ |
|
305
|
|
|
'directories' => [ |
|
306
|
|
|
'foo' => [ |
|
307
|
|
|
'namespace_prefix' => 'foo_ns', |
|
308
|
|
|
'path' => __DIR__, |
|
309
|
|
|
] |
|
310
|
|
|
] |
|
311
|
|
|
] |
|
312
|
|
|
)), function ($container) { |
|
313
|
|
|
$container->getDefinition('jms_serializer.metadata.file_locator')->setPublic(true); |
|
314
|
|
|
}); |
|
315
|
|
|
|
|
316
|
|
|
$fileLocatorDef = $container->getDefinition('jms_serializer.metadata.file_locator'); |
|
317
|
|
|
$directories = $fileLocatorDef->getArgument(0); |
|
318
|
|
|
$this->assertEquals(['foo_ns' => __DIR__], $directories); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
public function testWarmUpWithDirs() |
|
322
|
|
|
{ |
|
323
|
|
|
$container = $this->getContainerForConfig([[ |
|
324
|
|
|
'metadata' => [ |
|
325
|
|
|
'warmup' => [ |
|
326
|
|
|
'paths' => [ |
|
327
|
|
|
'included' => ['a'], |
|
328
|
|
|
'excluded' => ['b'] |
|
329
|
|
|
] |
|
330
|
|
|
] |
|
331
|
|
|
] |
|
332
|
|
|
]], function ($container){ |
|
333
|
|
|
$container->getDefinition('jms_serializer.cache.cache_warmer')->setPublic(true); |
|
334
|
|
|
}); |
|
335
|
|
|
|
|
336
|
|
|
$this->assertTrue($container->hasDefinition('jms_serializer.cache.cache_warmer')); |
|
337
|
|
|
|
|
338
|
|
|
$def = $container->getDefinition('jms_serializer.cache.cache_warmer'); |
|
339
|
|
|
|
|
340
|
|
|
$this->assertEquals(['a'], $def->getArgument(0)); |
|
341
|
|
|
$this->assertEquals(['b'], $def->getArgument(2)); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
public function testWarmUpWithDirsWithNoPaths() |
|
345
|
|
|
{ |
|
346
|
|
|
$this->getContainerForConfig([[]], function ($container) { |
|
347
|
|
|
$this->assertFalse($container->hasDefinition('jms_serializer.cache.cache_warmer')); |
|
348
|
|
|
}); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* @expectedException \JMS\Serializer\Exception\RuntimeException |
|
353
|
|
|
* @expectedExceptionMessage The metadata directory "foo_dir" does not exist for the namespace "foo_ns" |
|
354
|
|
|
*/ |
|
355
|
|
|
public function testLoadNotExistentMetadataDir() |
|
356
|
|
|
{ |
|
357
|
|
|
$this->getContainerForConfig(array(array( |
|
358
|
|
|
'metadata' => [ |
|
359
|
|
|
'directories' => [ |
|
360
|
|
|
'foo' => [ |
|
361
|
|
|
'namespace_prefix' => 'foo_ns', |
|
362
|
|
|
'path' => 'foo_dir', |
|
363
|
|
|
] |
|
364
|
|
|
] |
|
365
|
|
|
] |
|
366
|
|
|
))); |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* @dataProvider getJsonVisitorConfigs |
|
371
|
|
|
*/ |
|
372
|
|
View Code Duplication |
public function testJsonVisitorOptions($expectedOptions, $config) |
|
|
|
|
|
|
373
|
|
|
{ |
|
374
|
|
|
$container = $this->getContainerForConfig(array($config), function ($container) { |
|
375
|
|
|
$container->getDefinition('jms_serializer.json_serialization_visitor')->setPublic(true); |
|
376
|
|
|
}); |
|
377
|
|
|
$this->assertSame($expectedOptions, $container->get('jms_serializer.json_serialization_visitor')->getOptions()); |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
public function getJsonVisitorConfigs() |
|
381
|
|
|
{ |
|
382
|
|
|
$configs = array(); |
|
383
|
|
|
|
|
384
|
|
|
if (version_compare(PHP_VERSION, '5.4', '>=')) { |
|
385
|
|
|
$configs[] = array(JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT, array( |
|
386
|
|
|
'visitors' => array( |
|
387
|
|
|
'json' => array( |
|
388
|
|
|
'options' => array('JSON_UNESCAPED_UNICODE', 'JSON_PRETTY_PRINT') |
|
389
|
|
|
) |
|
390
|
|
|
) |
|
391
|
|
|
)); |
|
392
|
|
|
|
|
393
|
|
|
$configs[] = array(JSON_UNESCAPED_UNICODE, array( |
|
394
|
|
|
'visitors' => array( |
|
395
|
|
|
'json' => array( |
|
396
|
|
|
'options' => 'JSON_UNESCAPED_UNICODE' |
|
397
|
|
|
) |
|
398
|
|
|
) |
|
399
|
|
|
)); |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
$configs[] = array(128, array( |
|
403
|
|
|
'visitors' => array( |
|
404
|
|
|
'json' => array( |
|
405
|
|
|
'options' => 128 |
|
406
|
|
|
) |
|
407
|
|
|
) |
|
408
|
|
|
)); |
|
409
|
|
|
|
|
410
|
|
|
$configs[] = array(0, array()); |
|
411
|
|
|
|
|
412
|
|
|
return $configs; |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
public function testExpressionLanguage() |
|
416
|
|
|
{ |
|
417
|
|
|
if (!interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) { |
|
418
|
|
|
$this->markTestSkipped("The Symfony Expression Language is not available"); |
|
419
|
|
|
} |
|
420
|
|
|
$container = $this->getContainerForConfig(array(array())); |
|
421
|
|
|
$serializer = $container->get('jms_serializer'); |
|
422
|
|
|
// test that all components have been wired correctly |
|
423
|
|
|
$object = new ObjectUsingExpressionLanguage('foo', true); |
|
424
|
|
|
$this->assertEquals('{"name":"foo"}', $serializer->serialize($object, 'json')); |
|
425
|
|
|
$object = new ObjectUsingExpressionLanguage('foo', false); |
|
426
|
|
|
$this->assertEquals('{}', $serializer->serialize($object, 'json')); |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
public function testExpressionLanguageVirtualProperties() |
|
430
|
|
|
{ |
|
431
|
|
|
if (!interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) { |
|
432
|
|
|
$this->markTestSkipped("The Symfony Expression Language is not available"); |
|
433
|
|
|
} |
|
434
|
|
|
$container = $this->getContainerForConfig(array(array())); |
|
435
|
|
|
$serializer = $container->get('jms_serializer'); |
|
436
|
|
|
// test that all components have been wired correctly |
|
437
|
|
|
$object = new ObjectUsingExpressionProperties('foo'); |
|
438
|
|
|
$this->assertEquals('{"v_prop_name":"foo"}', $serializer->serialize($object, 'json')); |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
/** |
|
442
|
|
|
* @expectedException \JMS\Serializer\Exception\ExpressionLanguageRequiredException |
|
443
|
|
|
*/ |
|
444
|
|
|
public function testExpressionLanguageDisabledVirtualProperties() |
|
445
|
|
|
{ |
|
446
|
|
|
if (!interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) { |
|
447
|
|
|
$this->markTestSkipped("The Symfony Expression Language is not available"); |
|
448
|
|
|
} |
|
449
|
|
|
$container = $this->getContainerForConfig(array(array('expression_evaluator' => array('id' => null)))); |
|
450
|
|
|
$serializer = $container->get('jms_serializer'); |
|
451
|
|
|
// test that all components have been wired correctly |
|
452
|
|
|
$object = new ObjectUsingExpressionProperties('foo'); |
|
453
|
|
|
$serializer->serialize($object, 'json'); |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
/** |
|
457
|
|
|
* @expectedException \JMS\Serializer\Exception\ExpressionLanguageRequiredException |
|
458
|
|
|
* @expectedExceptionMessage To use conditional exclude/expose in JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingExpressionLanguage you must configure the expression language. |
|
459
|
|
|
*/ |
|
460
|
|
|
public function testExpressionLanguageNotLoaded() |
|
461
|
|
|
{ |
|
462
|
|
|
$container = $this->getContainerForConfig(array(array('expression_evaluator' => array('id' => null)))); |
|
463
|
|
|
$serializer = $container->get('jms_serializer'); |
|
464
|
|
|
// test that all components have been wired correctly |
|
465
|
|
|
$object = new ObjectUsingExpressionLanguage('foo', true); |
|
466
|
|
|
$serializer->serialize($object, 'json'); |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
|
|
/** |
|
470
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
|
471
|
|
|
* @expectedExceptionMessage Invalid configuration for path "jms_serializer.expression_evaluator.id": You need at least symfony/expression language v2.6 or v3.0 to use the expression evaluator features |
|
472
|
|
|
*/ |
|
473
|
|
|
public function testExpressionInvalidEvaluator() |
|
474
|
|
|
{ |
|
475
|
|
|
if (interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) { |
|
476
|
|
|
$this->markTestSkipped('To pass this test the "symfony/expression-language" component should be available'); |
|
477
|
|
|
} |
|
478
|
|
|
$this->getContainerForConfig(array(array('expression_evaluator' => array('id' => 'foo')))); |
|
479
|
|
|
} |
|
480
|
|
|
|
|
481
|
|
|
/** |
|
482
|
|
|
* @dataProvider getXmlVisitorWhitelists |
|
483
|
|
|
*/ |
|
484
|
|
View Code Duplication |
public function testXmlVisitorOptions($expectedOptions, $config) |
|
|
|
|
|
|
485
|
|
|
{ |
|
486
|
|
|
$container = $this->getContainerForConfig(array($config), function ($container) { |
|
487
|
|
|
$container->getDefinition('jms_serializer.xml_deserialization_visitor')->setPublic(true); |
|
488
|
|
|
}); |
|
489
|
|
|
$this->assertSame($expectedOptions, $container->get('jms_serializer.xml_deserialization_visitor')->getDoctypeWhitelist()); |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
public function getXmlVisitorWhitelists() |
|
493
|
|
|
{ |
|
494
|
|
|
$configs = array(); |
|
495
|
|
|
|
|
496
|
|
|
$configs[] = array(array('good document', 'other good document'), array( |
|
497
|
|
|
'visitors' => array( |
|
498
|
|
|
'xml' => array( |
|
499
|
|
|
'doctype_whitelist' => array('good document', 'other good document'), |
|
500
|
|
|
) |
|
501
|
|
|
) |
|
502
|
|
|
)); |
|
503
|
|
|
|
|
504
|
|
|
$configs[] = array(array(), array()); |
|
505
|
|
|
|
|
506
|
|
|
return $configs; |
|
507
|
|
|
} |
|
508
|
|
|
|
|
509
|
|
|
public function testXmlVisitorFormatOutput() |
|
510
|
|
|
{ |
|
511
|
|
|
$config = array( |
|
512
|
|
|
'visitors' => array( |
|
513
|
|
|
'xml' => array( |
|
514
|
|
|
'format_output' => false, |
|
515
|
|
|
) |
|
516
|
|
|
) |
|
517
|
|
|
); |
|
518
|
|
|
$container = $this->getContainerForConfig(array($config), function ($container) { |
|
519
|
|
|
$container->getDefinition('jms_serializer.xml_serialization_visitor')->setPublic(true); |
|
520
|
|
|
}); |
|
521
|
|
|
|
|
522
|
|
|
$this->assertFalse($container->get('jms_serializer.xml_serialization_visitor')->isFormatOutput()); |
|
523
|
|
|
} |
|
524
|
|
|
|
|
525
|
|
|
public function testXmlVisitorDefaultValueToFormatOutput() |
|
526
|
|
|
{ |
|
527
|
|
|
$container = $this->getContainerForConfig(array(), function ($container) { |
|
528
|
|
|
$container->getDefinition('jms_serializer.xml_serialization_visitor')->setPublic(true); |
|
529
|
|
|
}); |
|
530
|
|
|
$this->assertTrue($container->get('jms_serializer.xml_serialization_visitor')->isFormatOutput()); |
|
531
|
|
|
} |
|
532
|
|
|
|
|
533
|
|
|
private function getContainerForConfig(array $configs, callable $configurator = null) |
|
534
|
|
|
{ |
|
535
|
|
|
$bundle = new JMSSerializerBundle(); |
|
536
|
|
|
$extension = $bundle->getContainerExtension(); |
|
537
|
|
|
|
|
538
|
|
|
$container = new ContainerBuilder(); |
|
539
|
|
|
$container->setParameter('kernel.debug', true); |
|
540
|
|
|
$container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer'); |
|
541
|
|
|
$container->setParameter('kernel.bundles', array()); |
|
542
|
|
|
$container->set('annotation_reader', new AnnotationReader()); |
|
543
|
|
|
$container->setDefinition('doctrine', new Definition(Registry::class)); |
|
544
|
|
|
$container->set('translator', $this->getMockBuilder('Symfony\\Component\\Translation\\TranslatorInterface')->getMock()); |
|
545
|
|
|
$container->set('debug.stopwatch', $this->getMockBuilder('Symfony\\Component\\Stopwatch\\Stopwatch')->getMock()); |
|
546
|
|
|
$container->registerExtension($extension); |
|
|
|
|
|
|
547
|
|
|
$extension->load($configs, $container); |
|
548
|
|
|
|
|
549
|
|
|
$bundle->build($container); |
|
550
|
|
|
|
|
551
|
|
|
if ($configurator) { |
|
552
|
|
|
call_user_func($configurator, $container); |
|
553
|
|
|
} |
|
554
|
|
|
|
|
555
|
|
|
$container->compile(); |
|
556
|
|
|
|
|
557
|
|
|
return $container; |
|
558
|
|
|
} |
|
559
|
|
|
} |
|
560
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: