JMSSerializerExtensionTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\DependencyInjection\Compiler\InlineServiceDefinitionsPass;
  19. use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
  20. use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
  21. use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
  22. use JMS\SerializerBundle\Tests\Fixtures\SimpleObject;
  23. use Doctrine\Common\Annotations\AnnotationReader;
  24. use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
  25. use JMS\SerializerBundle\JMSSerializerBundle;
  26. use Doctrine\Common\Annotations\Reader;
  27. use JMS\SerializerBundle\Tests\Fixtures\VersionedObject;
  28. use Symfony\Component\DependencyInjection\ContainerBuilder;
  29. use JMS\SerializerBundle\DependencyInjection\JMSSerializerExtension;
  30. class JMSSerializerExtensionTest extends \PHPUnit_Framework_TestCase
  31. {
  32. protected function tearDown()
  33. {
  34. // clear temporary directory
  35. $dir = sys_get_temp_dir().'/serializer';
  36. if (is_dir($dir)) {
  37. foreach (new \RecursiveDirectoryIterator($dir) as $file) {
  38. $filename = $file->getFileName();
  39. if ('.' === $filename || '..' === $filename) {
  40. continue;
  41. }
  42. @unlink($file->getPathName());
  43. }
  44. @rmdir($dir);
  45. }
  46. }
  47. public function testLoad()
  48. {
  49. $container = $this->getContainerForConfig(array(array()));
  50. $simpleObject = new SimpleObject('foo', 'bar');
  51. $versionedObject = new VersionedObject('foo', 'bar');
  52. $serializer = $container->get('serializer');
  53. // test that all components have been wired correctly
  54. $this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($versionedObject, 'json'));
  55. $this->assertEquals($simpleObject, $serializer->deserialize($serializer->serialize($simpleObject, 'json'), get_class($simpleObject), 'json'));
  56. $this->assertEquals($simpleObject, $serializer->deserialize($serializer->serialize($simpleObject, 'xml'), get_class($simpleObject), 'xml'));
  57. $serializer->setVersion('0.0.1');
  58. $this->assertEquals(json_encode(array('name' => 'foo')), $serializer->serialize($versionedObject, 'json'));
  59. $serializer->setVersion('1.1.1');
  60. $this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($versionedObject, 'json'));
  61. }
  62. private function getContainerForConfig(array $configs)
  63. {
  64. $extension = new JMSSerializerExtension();
  65. $container = new ContainerBuilder();
  66. $container->setParameter('kernel.debug', true);
  67. $container->setParameter('kernel.cache_dir', sys_get_temp_dir());
  68. $container->setParameter('kernel.bundles', array());
  69. $container->set('annotation_reader', new AnnotationReader());
  70. $container->set('service_container', $container);
  71. $container->set('translator', $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface'));
  72. $extension->load($configs, $container);
  73. $bundle = new JMSSerializerBundle();
  74. $bundle->build($container);
  75. $container->getCompilerPassConfig()->setOptimizationPasses(array(
  76. new ResolveDefinitionTemplatesPass(),
  77. ));
  78. $container->getCompilerPassConfig()->setRemovingPasses(array());
  79. $container->compile();
  80. return $container;
  81. }
  82. }