SerializerTest.php 4.1 KB

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