JMSSerializerExtensionTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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->set('annotation_reader', new AnnotationReader());
  49. $container->set('service_container', $container);
  50. $extension->load(array(array()), $container);
  51. $bundle = new JMSSerializerBundle();
  52. $bundle->build($container);
  53. $container->getCompilerPassConfig()->setOptimizationPasses(array(
  54. new ResolveDefinitionTemplatesPass(),
  55. ));
  56. $container->getCompilerPassConfig()->setRemovingPasses(array());
  57. $container->compile();
  58. $object = new VersionedObject('foo', 'bar');
  59. $serializer = $container->get('serializer');
  60. $this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($object, 'json'));
  61. $serializer->setVersion('0.0.1');
  62. $this->assertEquals(json_encode(array('name' => 'foo')), $serializer->serialize($object, 'json'));
  63. $serializer->setVersion('1.1.1');
  64. $this->assertEquals(json_encode(array('name' => 'bar')), $serializer->serialize($object, 'json'));
  65. }
  66. }