SerializerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace JMS\SerializerBundle\Tests\Serializer;
  3. use JMS\SerializerBundle\Metadata\Driver\AnnotationDriver;
  4. use JMS\SerializerBundle\Metadata\MetadataFactory;
  5. use JMS\SerializerBundle\Serializer\Exclusion\AllExclusionStrategy;
  6. use JMS\SerializerBundle\Serializer\Normalizer\PropertyBasedNormalizer;
  7. use JMS\SerializerBundle\Tests\Fixtures\Comment;
  8. use JMS\SerializerBundle\Tests\Fixtures\Author;
  9. use JMS\SerializerBundle\Tests\Fixtures\BlogPost;
  10. use JMS\SerializerBundle\Serializer\Normalizer\ArrayCollectionNormalizer;
  11. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  12. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  13. use JMS\SerializerBundle\Serializer\Naming\SerializedNameAnnotationStrategy;
  14. use JMS\SerializerBundle\Serializer\SerializerFactory;
  15. use JMS\SerializerBundle\Serializer\Exclusion\NoneExclusionStrategy;
  16. use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyFactory;
  17. use JMS\SerializerBundle\Serializer\Naming\CamelCaseNamingStrategy;
  18. use JMS\SerializerBundle\Serializer\Naming\AnnotatedNamingStrategy;
  19. use Annotations\Reader;
  20. use JMS\SerializerBundle\Serializer\Normalizer\NativePhpTypeNormalizer;
  21. use JMS\SerializerBundle\Serializer\Serializer;
  22. class SerializerTest extends \PHPUnit_Framework_TestCase
  23. {
  24. public function testNormalize()
  25. {
  26. $serializer = $this->getSerializer();
  27. $post = new BlogPost('Foo', new Author('Bar'));
  28. $normalized = $serializer->normalize($post);
  29. $this->assertTrue(isset($normalized['created_at']));
  30. unset($normalized['created_at']);
  31. $this->assertEquals(array(
  32. 'title' => 'Foo',
  33. 'published' => false,
  34. 'author' => array(
  35. 'full_name' => 'Bar',
  36. ),
  37. 'comments' => array(),
  38. ), $normalized);
  39. }
  40. public function testDenormalize()
  41. {
  42. $serializer = $this->getSerializer();
  43. $post = new BlogPost('Foo', new Author('Bar'));
  44. $post->addComment(new Comment(new Author('Johannes'), 'FooBar'));
  45. $normalized = $serializer->normalize($post);
  46. $post2 = $serializer->denormalize($normalized, 'JMS\SerializerBundle\Tests\Fixtures\BlogPost');
  47. $this->assertEquals($post, $post2);
  48. }
  49. public function testSerializeUnserialize()
  50. {
  51. $serializer = $this->getSerializer();
  52. $post = new BlogPost('foo', new Author('bar'));
  53. $post->setPublished();
  54. $post->addComment(new Comment(new Author('foo'), 'bar'));
  55. $post->addComment(new Comment(new Author('bar'), 'foo'));
  56. $serialized = $serializer->serialize($post, 'xml');
  57. $post2 = $serializer->deserialize($serialized, 'JMS\SerializerBundle\Tests\Fixtures\BlogPost', 'xml');
  58. $this->assertEquals($post, $post2);
  59. $serialized = $serializer->serialize($post, 'json');
  60. $post2 = $serializer->deserialize($serialized, 'JMS\SerializerBundle\Tests\Fixtures\BlogPost', 'json');
  61. $this->assertEquals($post, $post2);
  62. }
  63. private function getSerializer($propertyNamingStrategy = null, $encoders = null, $customNormalizers = null)
  64. {
  65. if (null === $propertyNamingStrategy) {
  66. $propertyNamingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
  67. }
  68. if (null === $encoders) {
  69. $encoders = array(
  70. 'xml' => new XmlEncoder(),
  71. 'json' => new JsonEncoder(),
  72. );
  73. }
  74. if (null === $customNormalizers) {
  75. $customNormalizers = array(
  76. new ArrayCollectionNormalizer(),
  77. );
  78. }
  79. $exclusionStrategyFactory = new ExclusionStrategyFactory(array(
  80. 'ALL' => new AllExclusionStrategy(),
  81. 'NONE' => new NoneExclusionStrategy(),
  82. ));
  83. return new Serializer(
  84. new NativePhpTypeNormalizer(),
  85. new PropertyBasedNormalizer(new MetadataFactory(new AnnotationDriver(new Reader())), $propertyNamingStrategy, $exclusionStrategyFactory),
  86. $customNormalizers,
  87. $encoders
  88. );
  89. }
  90. }