JMSSerializerExtensionTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. public function testJsonVisitorOptions()
  72. {
  73. $configs = array(
  74. 384 => array(array(
  75. 'visitors' => array(
  76. 'json' => array(
  77. 'options' => array('JSON_UNESCAPED_UNICODE', 'JSON_PRETTY_PRINT')
  78. )
  79. )
  80. )),
  81. 256 => array(array(
  82. 'visitors' => array(
  83. 'json' => array(
  84. 'options' => 'JSON_UNESCAPED_UNICODE'
  85. )
  86. )
  87. )),
  88. 128 => array(array(
  89. 'visitors' => array(
  90. 'json' => array(
  91. 'options' => 128
  92. )
  93. )
  94. )),
  95. 0 => array(array())
  96. );
  97. foreach ($configs as $jsonOptions => $config) {
  98. $container = $this->getContainerForConfig($config);
  99. $jsonSerializationVisitor = $container->get('jms_serializer.json_serialization_visitor');
  100. $this->assertEquals($jsonOptions, $jsonSerializationVisitor->getOptions());
  101. }
  102. }
  103. private function getContainerForConfig(array $configs, KernelInterface $kernel = null)
  104. {
  105. if (null === $kernel) {
  106. $kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
  107. $kernel
  108. ->expects($this->any())
  109. ->method('getBundles')
  110. ->will($this->returnValue(array()))
  111. ;
  112. }
  113. $bundle = new JMSSerializerBundle($kernel);
  114. $extension = $bundle->getContainerExtension();
  115. $container = new ContainerBuilder();
  116. $container->setParameter('kernel.debug', true);
  117. $container->setParameter('kernel.cache_dir', sys_get_temp_dir());
  118. $container->setParameter('kernel.bundles', array());
  119. $container->set('annotation_reader', new AnnotationReader());
  120. $container->set('service_container', $container);
  121. $container->set('translator', $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface'));
  122. $extension->load($configs, $container);
  123. $bundle->build($container);
  124. $container->getCompilerPassConfig()->setOptimizationPasses(array(
  125. new ResolveDefinitionTemplatesPass(),
  126. ));
  127. $container->getCompilerPassConfig()->setRemovingPasses(array());
  128. $container->compile();
  129. return $container;
  130. }
  131. }