JMSSerializerExtensionTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\Tests\DependencyInjection;
  18. use Symfony\Component\HttpKernel\KernelInterface;
  19. use Symfony\Component\DependencyInjection\Compiler\InlineServiceDefinitionsPass;
  20. use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
  21. use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
  22. use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
  23. use JMS\SerializerBundle\Tests\Fixtures\SimpleObject;
  24. use Doctrine\Common\Annotations\AnnotationReader;
  25. use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
  26. use JMS\SerializerBundle\JMSSerializerBundle;
  27. use Doctrine\Common\Annotations\Reader;
  28. use JMS\SerializerBundle\Tests\Fixtures\VersionedObject;
  29. use Symfony\Component\DependencyInjection\ContainerBuilder;
  30. use JMS\SerializerBundle\DependencyInjection\JMSSerializerExtension;
  31. class JMSSerializerExtensionTest extends \PHPUnit_Framework_TestCase
  32. {
  33. protected function setUp()
  34. {
  35. $this->clearTempDir();
  36. }
  37. protected function tearDown()
  38. {
  39. $this->clearTempDir();
  40. }
  41. private function clearTempDir()
  42. {
  43. // clear temporary directory
  44. $dir = sys_get_temp_dir().'/serializer';
  45. if (is_dir($dir)) {
  46. foreach (new \RecursiveDirectoryIterator($dir) as $file) {
  47. $filename = $file->getFileName();
  48. if ('.' === $filename || '..' === $filename) {
  49. continue;
  50. }
  51. @unlink($file->getPathName());
  52. }
  53. @rmdir($dir);
  54. }
  55. }
  56. public function testLoad()
  57. {
  58. $container = $this->getContainerForConfig(array(array()));
  59. $simpleObject = new SimpleObject('foo', 'bar');
  60. $versionedObject = new VersionedObject('foo', 'bar');
  61. $serializer = $container->get('serializer');
  62. // test that all components have been wired correctly
  63. $this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($versionedObject, 'json'));
  64. $this->assertEquals($simpleObject, $serializer->deserialize($serializer->serialize($simpleObject, 'json'), get_class($simpleObject), 'json'));
  65. $this->assertEquals($simpleObject, $serializer->deserialize($serializer->serialize($simpleObject, 'xml'), get_class($simpleObject), 'xml'));
  66. $serializer->setVersion('0.0.1');
  67. $this->assertEquals(json_encode(array('name' => 'foo')), $serializer->serialize($versionedObject, 'json'));
  68. $serializer->setVersion('1.1.1');
  69. $this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($versionedObject, 'json'));
  70. }
  71. /**
  72. * @dataProvider getJsonVisitorConfigs
  73. */
  74. public function testJsonVisitorOptions($expectedOptions, $config)
  75. {
  76. $container = $this->getContainerForConfig(array($config));
  77. $this->assertSame($expectedOptions, $container->get('jms_serializer.json_serialization_visitor')->getOptions());
  78. }
  79. public function getJsonVisitorConfigs()
  80. {
  81. $configs = array();
  82. if (version_compare(PHP_VERSION, '5.4', '>=')) {
  83. $configs[] = array(JSON_UNESCAPE_UNICODE | JSON_PRETTY_PRINT, array(
  84. 'visitors' => array(
  85. 'json' => array(
  86. 'options' => array('JSON_UNESCAPED_UNICODE', 'JSON_PRETTY_PRINT')
  87. )
  88. )
  89. ));
  90. $configs[] = array(JSON_UNESCAPE_UNICODE, array(
  91. 'visitors' => array(
  92. 'json' => array(
  93. 'options' => 'JSON_UNESCAPED_UNICODE'
  94. )
  95. )
  96. ));
  97. }
  98. $configs[] = array(128, array(
  99. 'visitors' => array(
  100. 'json' => array(
  101. 'options' => 128
  102. )
  103. )
  104. ));
  105. $configs[] = array(0, array());
  106. return $configs;
  107. }
  108. private function getContainerForConfig(array $configs, KernelInterface $kernel = null)
  109. {
  110. if (null === $kernel) {
  111. $kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
  112. $kernel
  113. ->expects($this->any())
  114. ->method('getBundles')
  115. ->will($this->returnValue(array()))
  116. ;
  117. }
  118. $bundle = new JMSSerializerBundle($kernel);
  119. $extension = $bundle->getContainerExtension();
  120. $container = new ContainerBuilder();
  121. $container->setParameter('kernel.debug', true);
  122. $container->setParameter('kernel.cache_dir', sys_get_temp_dir());
  123. $container->setParameter('kernel.bundles', array());
  124. $container->set('annotation_reader', new AnnotationReader());
  125. $container->set('service_container', $container);
  126. $container->set('translator', $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface'));
  127. $extension->load($configs, $container);
  128. $bundle->build($container);
  129. $container->getCompilerPassConfig()->setOptimizationPasses(array(
  130. new ResolveDefinitionTemplatesPass(),
  131. ));
  132. $container->getCompilerPassConfig()->setRemovingPasses(array());
  133. $container->compile();
  134. return $container;
  135. }
  136. }