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\SimpleObject; |
26
|
|
|
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\VersionedObject; |
27
|
|
|
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass; |
28
|
|
|
use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass; |
29
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
30
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
31
|
|
|
|
32
|
|
|
class JMSSerializerExtensionTest extends \PHPUnit_Framework_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.context_factory'); |
67
|
|
|
$this->assertInstanceOf('JMS\Serializer\ContextFactory\SerializationContextFactoryInterface', $factory); |
68
|
|
|
|
69
|
|
|
$factory = $container->get('jms_serializer.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.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.context_factory', (string)$serializationCall[1][0]); |
85
|
|
|
|
86
|
|
|
$serializationCall = $calls[1]; |
87
|
|
|
$this->assertEquals('setDeserializationContextFactory', $serializationCall[0]); |
88
|
|
|
$this->assertEquals('jms_serializer.context_factory', (string)$serializationCall[1][0]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testConfiguringContextFactories() |
92
|
|
|
{ |
93
|
|
|
$container = $this->getContainerForConfig(array(array())); |
94
|
|
|
|
95
|
|
|
$def = $container->getDefinition('jms_serializer.context_factory'); |
96
|
|
|
$argument = $def->getArgument(0); |
97
|
|
|
$this->assertNotEmpty($argument); |
98
|
|
|
$this->assertArrayNotHasKey('context', $argument); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testLoad() |
102
|
|
|
{ |
103
|
|
|
$container = $this->getContainerForConfig(array(array())); |
104
|
|
|
|
105
|
|
|
$simpleObject = new SimpleObject('foo', 'bar'); |
106
|
|
|
$versionedObject = new VersionedObject('foo', 'bar'); |
107
|
|
|
$serializer = $container->get('serializer'); |
108
|
|
|
|
109
|
|
|
// test that all components have been wired correctly |
110
|
|
|
$this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($versionedObject, 'json')); |
111
|
|
|
$this->assertEquals($simpleObject, $serializer->deserialize($serializer->serialize($simpleObject, 'json'), get_class($simpleObject), 'json')); |
112
|
|
|
$this->assertEquals($simpleObject, $serializer->deserialize($serializer->serialize($simpleObject, 'xml'), get_class($simpleObject), 'xml')); |
113
|
|
|
|
114
|
|
|
$this->assertEquals(json_encode(array('name' => 'foo')), $serializer->serialize($versionedObject, 'json', SerializationContext::create()->setVersion('0.0.1'))); |
115
|
|
|
|
116
|
|
|
$this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($versionedObject, 'json', SerializationContext::create()->setVersion('1.1.1'))); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @dataProvider getJsonVisitorConfigs |
121
|
|
|
*/ |
122
|
|
|
public function testJsonVisitorOptions($expectedOptions, $config) |
123
|
|
|
{ |
124
|
|
|
$container = $this->getContainerForConfig(array($config)); |
125
|
|
|
$this->assertSame($expectedOptions, $container->get('jms_serializer.json_serialization_visitor')->getOptions()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getJsonVisitorConfigs() |
129
|
|
|
{ |
130
|
|
|
$configs = array(); |
131
|
|
|
|
132
|
|
|
if (version_compare(PHP_VERSION, '5.4', '>=')) { |
133
|
|
|
$configs[] = array(JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT, array( |
134
|
|
|
'visitors' => array( |
135
|
|
|
'json' => array( |
136
|
|
|
'options' => array('JSON_UNESCAPED_UNICODE', 'JSON_PRETTY_PRINT') |
137
|
|
|
) |
138
|
|
|
) |
139
|
|
|
)); |
140
|
|
|
|
141
|
|
|
$configs[] = array(JSON_UNESCAPED_UNICODE, array( |
142
|
|
|
'visitors' => array( |
143
|
|
|
'json' => array( |
144
|
|
|
'options' => 'JSON_UNESCAPED_UNICODE' |
145
|
|
|
) |
146
|
|
|
) |
147
|
|
|
)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$configs[] = array(128, array( |
151
|
|
|
'visitors' => array( |
152
|
|
|
'json' => array( |
153
|
|
|
'options' => 128 |
154
|
|
|
) |
155
|
|
|
) |
156
|
|
|
)); |
157
|
|
|
|
158
|
|
|
$configs[] = array(0, array()); |
159
|
|
|
|
160
|
|
|
return $configs; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testExpressionLanguage() |
164
|
|
|
{ |
165
|
|
|
if (!interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) { |
166
|
|
|
$this->markTestSkipped("The Symfony Expression Language is not available"); |
167
|
|
|
} |
168
|
|
|
$container = $this->getContainerForConfig(array(array())); |
169
|
|
|
$serializer = $container->get('serializer'); |
170
|
|
|
// test that all components have been wired correctly |
171
|
|
|
$object = new ObjectUsingExpressionLanguage('foo', true); |
172
|
|
|
$this->assertEquals('{"name":"foo"}', $serializer->serialize($object, 'json')); |
173
|
|
|
$object = new ObjectUsingExpressionLanguage('foo', false); |
174
|
|
|
$this->assertEquals('{}', $serializer->serialize($object, 'json')); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @expectedException \JMS\Serializer\Exception\ExpressionLanguageRequiredException |
179
|
|
|
* @expectedExceptionMessage To use conditional exclude/expose in JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingExpressionLanguage you must configure the expression language. |
180
|
|
|
*/ |
181
|
|
|
public function testExpressionLanguageNotLoaded() |
182
|
|
|
{ |
183
|
|
|
$container = $this->getContainerForConfig(array(array('expression_evaluator' => array('id' => null)))); |
184
|
|
|
$serializer = $container->get('serializer'); |
185
|
|
|
// test that all components have been wired correctly |
186
|
|
|
$object = new ObjectUsingExpressionLanguage('foo', true); |
187
|
|
|
$serializer->serialize($object, 'json'); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
192
|
|
|
* @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 |
193
|
|
|
*/ |
194
|
|
|
public function testExpressionInvalidEvaluator() |
195
|
|
|
{ |
196
|
|
|
$this->getContainerForConfig(array(array('expression_evaluator' => array('id' => 'foo')))); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @dataProvider getXmlVisitorWhitelists |
201
|
|
|
*/ |
202
|
|
|
public function testXmlVisitorOptions($expectedOptions, $config) |
203
|
|
|
{ |
204
|
|
|
$container = $this->getContainerForConfig(array($config)); |
205
|
|
|
$this->assertSame($expectedOptions, $container->get('jms_serializer.xml_deserialization_visitor')->getDoctypeWhitelist()); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function getXmlVisitorWhitelists() |
209
|
|
|
{ |
210
|
|
|
$configs = array(); |
211
|
|
|
|
212
|
|
|
$configs[] = array(array('good document', 'other good document'), array( |
213
|
|
|
'visitors' => array( |
214
|
|
|
'xml' => array( |
215
|
|
|
'doctype_whitelist' => array('good document', 'other good document'), |
216
|
|
|
) |
217
|
|
|
) |
218
|
|
|
)); |
219
|
|
|
|
220
|
|
|
$configs[] = array(array(), array()); |
221
|
|
|
|
222
|
|
|
return $configs; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testXmlVisitorFormatOutput() |
226
|
|
|
{ |
227
|
|
|
$config = array( |
228
|
|
|
'visitors' => array( |
229
|
|
|
'xml' => array( |
230
|
|
|
'format_output' => false, |
231
|
|
|
) |
232
|
|
|
) |
233
|
|
|
); |
234
|
|
|
$container = $this->getContainerForConfig(array($config)); |
235
|
|
|
|
236
|
|
|
$this->assertFalse($container->get('jms_serializer.xml_serialization_visitor')->isFormatOutput()); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function testXmlVisitorDefaultValueToFormatOutput() |
240
|
|
|
{ |
241
|
|
|
$container = $this->getContainerForConfig(array()); |
242
|
|
|
$this->assertTrue($container->get('jms_serializer.xml_serialization_visitor')->isFormatOutput()); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
private function getContainerForConfig(array $configs, KernelInterface $kernel = null) |
246
|
|
|
{ |
247
|
|
|
if (null === $kernel) { |
248
|
|
|
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock(); |
249
|
|
|
$kernel |
250
|
|
|
->expects($this->any()) |
251
|
|
|
->method('getBundles') |
252
|
|
|
->will($this->returnValue(array())) |
253
|
|
|
; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
$bundle = new JMSSerializerBundle($kernel); |
|
|
|
|
257
|
|
|
$extension = $bundle->getContainerExtension(); |
258
|
|
|
|
259
|
|
|
$container = new ContainerBuilder(); |
260
|
|
|
$container->setParameter('kernel.debug', true); |
261
|
|
|
$container->setParameter('kernel.cache_dir', sys_get_temp_dir().'/serializer'); |
262
|
|
|
$container->setParameter('kernel.bundles', array()); |
263
|
|
|
$container->set('annotation_reader', new AnnotationReader()); |
264
|
|
|
$container->set('translator', $this->getMockBuilder('Symfony\\Component\\Translation\\TranslatorInterface')->getMock()); |
265
|
|
|
$container->set('debug.stopwatch', $this->getMockBuilder('Symfony\\Component\\Stopwatch\\Stopwatch')->getMock()); |
266
|
|
|
$container->registerExtension($extension); |
|
|
|
|
267
|
|
|
$extension->load($configs, $container); |
268
|
|
|
|
269
|
|
|
$bundle->build($container); |
270
|
|
|
|
271
|
|
|
$container->getCompilerPassConfig()->setOptimizationPasses(array( |
272
|
|
|
new ResolveParameterPlaceHoldersPass(), |
273
|
|
|
new ResolveDefinitionTemplatesPass(), |
274
|
|
|
)); |
275
|
|
|
$container->getCompilerPassConfig()->setRemovingPasses(array()); |
276
|
|
|
$container->compile(); |
277
|
|
|
|
278
|
|
|
return $container; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: