JMSSerializerExtensionTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
  20. use JMS\SerializerBundle\JMSSerializerBundle;
  21. use Doctrine\Common\Annotations\Reader;
  22. use JMS\SerializerBundle\Tests\Fixtures\VersionedObject;
  23. use Symfony\Component\DependencyInjection\ContainerBuilder;
  24. use JMS\SerializerBundle\DependencyInjection\JMSSerializerExtension;
  25. class JMSSerializerExtensionTest extends \PHPUnit_Framework_TestCase
  26. {
  27. protected function tearDown()
  28. {
  29. // clear temporary directory
  30. $dir = sys_get_temp_dir().'/serializer';
  31. if (is_dir($dir)) {
  32. foreach (new \RecursiveDirectoryIterator($dir) as $file) {
  33. $filename = $file->getFileName();
  34. if ('.' === $filename || '..' === $filename) {
  35. continue;
  36. }
  37. @unlink($file->getPathName());
  38. }
  39. @rmdir($dir);
  40. }
  41. }
  42. public function testLoad()
  43. {
  44. $extension = new JMSSerializerExtension();
  45. $container = new ContainerBuilder();
  46. $container->setParameter('kernel.debug', true);
  47. $container->setParameter('kernel.cache_dir', sys_get_temp_dir());
  48. $container->setParameter('kernel.bundles', array());
  49. $container->set('annotation_reader', new AnnotationReader());
  50. $container->set('service_container', $container);
  51. $extension->load(array(array()), $container);
  52. $bundle = new JMSSerializerBundle();
  53. $bundle->build($container);
  54. $container->getCompilerPassConfig()->setOptimizationPasses(array(
  55. new ResolveDefinitionTemplatesPass(),
  56. ));
  57. $container->getCompilerPassConfig()->setRemovingPasses(array());
  58. $container->compile();
  59. $object = new VersionedObject('foo', 'bar');
  60. $serializer = $container->get('serializer');
  61. $this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($object, 'json'));
  62. $serializer->setVersion('0.0.1');
  63. $this->assertEquals(json_encode(array('name' => 'foo')), $serializer->serialize($object, 'json'));
  64. $serializer->setVersion('1.1.1');
  65. $this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($object, 'json'));
  66. }
  67. }