JMSSerializerExtensionTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 Doctrine\Common\Annotations\AnnotationReader;
  19. use JMS\SerializerBundle\JMSSerializerBundle;
  20. use JMS\SerializerBundle\Tests\Fixtures\SimpleObject;
  21. use JMS\SerializerBundle\Tests\Fixtures\VersionedObject;
  22. use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
  23. use Symfony\Component\DependencyInjection\ContainerBuilder;
  24. use Symfony\Component\HttpKernel\KernelInterface;
  25. class JMSSerializerExtensionTest extends \PHPUnit_Framework_TestCase
  26. {
  27. protected function setUp()
  28. {
  29. $this->clearTempDir();
  30. }
  31. protected function tearDown()
  32. {
  33. $this->clearTempDir();
  34. }
  35. private function clearTempDir()
  36. {
  37. // clear temporary directory
  38. $dir = sys_get_temp_dir().'/serializer';
  39. if (is_dir($dir)) {
  40. foreach (new \RecursiveDirectoryIterator($dir) as $file) {
  41. $filename = $file->getFileName();
  42. if ('.' === $filename || '..' === $filename) {
  43. continue;
  44. }
  45. @unlink($file->getPathName());
  46. }
  47. @rmdir($dir);
  48. }
  49. }
  50. public function testLoad()
  51. {
  52. $container = $this->getContainerForConfig(array(array()));
  53. $simpleObject = new SimpleObject('foo', 'bar');
  54. $versionedObject = new VersionedObject('foo', 'bar');
  55. $serializer = $container->get('serializer');
  56. // test that all components have been wired correctly
  57. $this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($versionedObject, 'json'));
  58. $this->assertEquals($simpleObject, $serializer->deserialize($serializer->serialize($simpleObject, 'json'), get_class($simpleObject), 'json'));
  59. $this->assertEquals($simpleObject, $serializer->deserialize($serializer->serialize($simpleObject, 'xml'), get_class($simpleObject), 'xml'));
  60. $serializer->setVersion('0.0.1');
  61. $this->assertEquals(json_encode(array('name' => 'foo')), $serializer->serialize($versionedObject, 'json'));
  62. $serializer->setVersion('1.1.1');
  63. $this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($versionedObject, 'json'));
  64. }
  65. /**
  66. * @dataProvider getJsonVisitorConfigs
  67. */
  68. public function testJsonVisitorOptions($expectedOptions, $config)
  69. {
  70. $container = $this->getContainerForConfig(array($config));
  71. $this->assertSame($expectedOptions, $container->get('jms_serializer.json_serialization_visitor')->getOptions());
  72. }
  73. public function getJsonVisitorConfigs()
  74. {
  75. $configs = array();
  76. if (version_compare(PHP_VERSION, '5.4', '>=')) {
  77. $configs[] = array(JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT, array(
  78. 'visitors' => array(
  79. 'json' => array(
  80. 'options' => array('JSON_UNESCAPED_UNICODE', 'JSON_PRETTY_PRINT')
  81. )
  82. )
  83. ));
  84. $configs[] = array(JSON_UNESCAPED_UNICODE, array(
  85. 'visitors' => array(
  86. 'json' => array(
  87. 'options' => 'JSON_UNESCAPED_UNICODE'
  88. )
  89. )
  90. ));
  91. }
  92. $configs[] = array(128, array(
  93. 'visitors' => array(
  94. 'json' => array(
  95. 'options' => 128
  96. )
  97. )
  98. ));
  99. $configs[] = array(0, array());
  100. return $configs;
  101. }
  102. private function getContainerForConfig(array $configs, KernelInterface $kernel = null)
  103. {
  104. if (null === $kernel) {
  105. $kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
  106. $kernel
  107. ->expects($this->any())
  108. ->method('getBundles')
  109. ->will($this->returnValue(array()))
  110. ;
  111. }
  112. $bundle = new JMSSerializerBundle($kernel);
  113. $extension = $bundle->getContainerExtension();
  114. $container = new ContainerBuilder();
  115. $container->setParameter('kernel.debug', true);
  116. $container->setParameter('kernel.cache_dir', sys_get_temp_dir());
  117. $container->setParameter('kernel.bundles', array());
  118. $container->set('annotation_reader', new AnnotationReader());
  119. $container->set('service_container', $container);
  120. $container->set('translator', $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface'));
  121. $extension->load($configs, $container);
  122. $bundle->build($container);
  123. $container->getCompilerPassConfig()->setOptimizationPasses(array(
  124. new ResolveDefinitionTemplatesPass(),
  125. ));
  126. $container->getCompilerPassConfig()->setRemovingPasses(array());
  127. $container->compile();
  128. return $container;
  129. }
  130. }