JMSSerializerExtensionTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 tearDown()
  34. {
  35. // clear temporary directory
  36. $dir = sys_get_temp_dir().'/serializer';
  37. if (is_dir($dir)) {
  38. foreach (new \RecursiveDirectoryIterator($dir) as $file) {
  39. $filename = $file->getFileName();
  40. if ('.' === $filename || '..' === $filename) {
  41. continue;
  42. }
  43. @unlink($file->getPathName());
  44. }
  45. @rmdir($dir);
  46. }
  47. }
  48. public function testLoad()
  49. {
  50. $container = $this->getContainerForConfig(array(array()));
  51. $simpleObject = new SimpleObject('foo', 'bar');
  52. $versionedObject = new VersionedObject('foo', 'bar');
  53. $serializer = $container->get('serializer');
  54. // test that all components have been wired correctly
  55. $this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($versionedObject, 'json'));
  56. $this->assertEquals($simpleObject, $serializer->deserialize($serializer->serialize($simpleObject, 'json'), get_class($simpleObject), 'json'));
  57. $this->assertEquals($simpleObject, $serializer->deserialize($serializer->serialize($simpleObject, 'xml'), get_class($simpleObject), 'xml'));
  58. $serializer->setVersion('0.0.1');
  59. $this->assertEquals(json_encode(array('name' => 'foo')), $serializer->serialize($versionedObject, 'json'));
  60. $serializer->setVersion('1.1.1');
  61. $this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($versionedObject, 'json'));
  62. }
  63. private function getContainerForConfig(array $configs, KernelInterface $kernel = null)
  64. {
  65. if (null === $kernel) {
  66. $kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
  67. $kernel
  68. ->expects($this->any())
  69. ->method('getBundles')
  70. ->will($this->returnValue(array()))
  71. ;
  72. }
  73. $bundle = new JMSSerializerBundle($kernel);
  74. $extension = $bundle->getContainerExtension();
  75. $container = new ContainerBuilder();
  76. $container->setParameter('kernel.debug', true);
  77. $container->setParameter('kernel.cache_dir', sys_get_temp_dir());
  78. $container->setParameter('kernel.bundles', array());
  79. $container->set('annotation_reader', new AnnotationReader());
  80. $container->set('service_container', $container);
  81. $container->set('translator', $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface'));
  82. $extension->load($configs, $container);
  83. $bundle->build($container);
  84. $container->getCompilerPassConfig()->setOptimizationPasses(array(
  85. new ResolveDefinitionTemplatesPass(),
  86. ));
  87. $container->getCompilerPassConfig()->setRemovingPasses(array());
  88. $container->compile();
  89. return $container;
  90. }
  91. }