BaseSerializationTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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\Serializer;
  18. use Doctrine\Common\Collections\ArrayCollection;
  19. use JMS\SerializerBundle\Serializer\Handler\DeserializationHandlerInterface;
  20. use JMS\SerializerBundle\Tests\Fixtures\AuthorList;
  21. use JMS\SerializerBundle\Serializer\VisitorInterface;
  22. use JMS\SerializerBundle\Serializer\Handler\ArrayCollectionHandler;
  23. use JMS\SerializerBundle\Serializer\XmlDeserializationVisitor;
  24. use JMS\SerializerBundle\Serializer\Construction\UnserializeObjectConstructor;
  25. use JMS\SerializerBundle\Serializer\JsonDeserializationVisitor;
  26. use JMS\SerializerBundle\Tests\Fixtures\Log;
  27. use JMS\SerializerBundle\Serializer\Handler\DateTimeHandler;
  28. use JMS\SerializerBundle\Tests\Fixtures\Comment;
  29. use JMS\SerializerBundle\Tests\Fixtures\Author;
  30. use JMS\SerializerBundle\Tests\Fixtures\BlogPost;
  31. use JMS\SerializerBundle\Tests\Fixtures\ObjectWithLifecycleCallbacks;
  32. use JMS\SerializerBundle\Tests\Fixtures\CircularReferenceParent;
  33. use JMS\SerializerBundle\Serializer\XmlSerializationVisitor;
  34. use Doctrine\Common\Annotations\AnnotationReader;
  35. use JMS\SerializerBundle\Metadata\Driver\AnnotationDriver;
  36. use Metadata\MetadataFactory;
  37. use JMS\SerializerBundle\Tests\Fixtures\SimpleObject;
  38. use JMS\SerializerBundle\Serializer\Naming\CamelCaseNamingStrategy;
  39. use JMS\SerializerBundle\Serializer\Naming\SerializedNameAnnotationStrategy;
  40. use JMS\SerializerBundle\Serializer\JsonSerializationVisitor;
  41. use JMS\SerializerBundle\Serializer\Serializer;
  42. abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
  43. {
  44. public function testString()
  45. {
  46. $this->assertEquals($this->getContent('string'), $this->serialize('foo'));
  47. $this->assertEquals('foo', $this->deserialize($this->getContent('string'), 'string'));
  48. }
  49. /**
  50. * @dataProvider getBooleans
  51. */
  52. public function testBooleans($strBoolean, $boolean)
  53. {
  54. $this->assertEquals($this->getContent('boolean_'.$strBoolean), $this->serialize($boolean));
  55. $this->assertSame($boolean, $this->deserialize($this->getContent('boolean_'.$strBoolean), 'boolean'));
  56. }
  57. public function getBooleans()
  58. {
  59. return array(array('true', true), array('false', false));
  60. }
  61. /**
  62. * @dataProvider getNumerics
  63. */
  64. public function testNumerics($key, $value)
  65. {
  66. $this->assertEquals($this->getContent($key), $this->serialize($value));
  67. $this->assertEquals($value, $this->deserialize($this->getContent($key), is_double($value) ? 'double' : 'integer'));
  68. }
  69. public function getNumerics()
  70. {
  71. return array(
  72. array('integer', 1),
  73. array('float', 4.533),
  74. array('float_trailing_zero', 1.0),
  75. );
  76. }
  77. public function testSimpleObject()
  78. {
  79. $this->assertEquals($this->getContent('simple_object'), $this->serialize($obj = new SimpleObject('foo', 'bar')));
  80. $this->assertEquals($obj, $this->deserialize($this->getContent('simple_object'), get_class($obj)));
  81. }
  82. public function testArrayStrings()
  83. {
  84. $data = array('foo', 'bar');
  85. $this->assertEquals($this->getContent('array_strings'), $this->serialize($data));
  86. $this->assertEquals($data, $this->deserialize($this->getContent('array_strings'), 'array<string>'));
  87. }
  88. public function testArrayBooleans()
  89. {
  90. $data = array(true, false);
  91. $this->assertEquals($this->getContent('array_booleans'), $this->serialize($data));
  92. $this->assertEquals($data, $this->deserialize($this->getContent('array_booleans'), 'array<boolean>'));
  93. }
  94. public function testArrayIntegers()
  95. {
  96. $data = array(1, 3, 4);
  97. $this->assertEquals($this->getContent('array_integers'), $this->serialize($data));
  98. $this->assertEquals($data, $this->deserialize($this->getContent('array_integers'), 'array<integer>'));
  99. }
  100. public function testArrayFloats()
  101. {
  102. $data = array(1.34, 3.0, 6.42);
  103. $this->assertEquals($this->getContent('array_floats'), $this->serialize($data));
  104. $this->assertEquals($data, $this->deserialize($this->getContent('array_floats'), 'array<double>'));
  105. }
  106. public function testArrayObjects()
  107. {
  108. $data = array(new SimpleObject('foo', 'bar'), new SimpleObject('baz', 'boo'));
  109. $this->assertEquals($this->getContent('array_objects'), $this->serialize($data));
  110. $this->assertEquals($data, $this->deserialize($this->getContent('array_objects'), 'array<JMS\SerializerBundle\Tests\Fixtures\SimpleObject>'));
  111. }
  112. public function testArrayMixed()
  113. {
  114. $this->assertEquals($this->getContent('array_mixed'), $this->serialize(array('foo', 1, true, new SimpleObject('foo', 'bar'), array(1, 3, true))));
  115. }
  116. public function testBlogPost()
  117. {
  118. $post = new BlogPost('This is a nice title.', $author = new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')));
  119. $post->addComment($comment = new Comment($author, 'foo'));
  120. $this->assertEquals($this->getContent('blog_post'), $this->serialize($post));
  121. $deserialized = $this->deserialize($this->getContent('blog_post'), get_class($post));
  122. $this->assertEquals('2011-07-30T00:00:00+0000', $this->getField($deserialized, 'createdAt')->format(\DateTime::ISO8601));
  123. $this->assertAttributeEquals('This is a nice title.', 'title', $deserialized);
  124. $this->assertAttributeSame(false, 'published', $deserialized);
  125. $this->assertAttributeEquals(new ArrayCollection(array($comment)), 'comments', $deserialized);
  126. $this->assertAttributeEquals($author, 'author', $deserialized);
  127. }
  128. /**
  129. * @group test
  130. */
  131. public function testLog()
  132. {
  133. $this->assertEquals($this->getContent('log'), $this->serialize($log = new Log()));
  134. $deserialized = $this->deserialize($this->getContent('log'), get_class($log));
  135. $this->assertEquals($log, $deserialized);
  136. }
  137. public function testCircularReference()
  138. {
  139. $object = new CircularReferenceParent();
  140. $this->assertEquals($this->getContent('circular_reference'), $this->serialize($object));
  141. $deserialized = $this->deserialize($this->getContent('circular_reference'), get_class($object));
  142. $col = $this->getField($deserialized, 'collection');
  143. $this->assertEquals(2, count($col));
  144. $this->assertEquals('child1', $col[0]->getName());
  145. $this->assertEquals('child2', $col[1]->getName());
  146. $this->assertSame($deserialized, $col[0]->getParent());
  147. $this->assertSame($deserialized, $col[1]->getParent());
  148. $col = $this->getField($deserialized, 'anotherCollection');
  149. $this->assertEquals(2, count($col));
  150. $this->assertEquals('child1', $col[0]->getName());
  151. $this->assertEquals('child2', $col[1]->getName());
  152. $this->assertSame($deserialized, $col[0]->getParent());
  153. $this->assertSame($deserialized, $col[1]->getParent());
  154. }
  155. public function testLifecycleCallbacks()
  156. {
  157. $object = new ObjectWithLifecycleCallbacks();
  158. $this->assertEquals($this->getContent('lifecycle_callbacks'), $this->serialize($object));
  159. $this->assertAttributeSame(null, 'name', $object);
  160. $deserialized = $this->deserialize($this->getContent('lifecycle_callbacks'), get_class($object));
  161. $this->assertEquals($object, $deserialized);
  162. }
  163. abstract protected function getContent($key);
  164. abstract protected function getFormat();
  165. protected function serialize($data)
  166. {
  167. return $this->getSerializer()->serialize($data, $this->getFormat());
  168. }
  169. protected function deserialize($content, $type)
  170. {
  171. return $this->getSerializer()->deserialize($content, $type, $this->getFormat());
  172. }
  173. protected function getSerializer()
  174. {
  175. $customSerializationHandlers = $this->getSerializationHandlers();
  176. $customDeserializationHandlers = $this->getDeserializationHandlers();
  177. $factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
  178. $namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
  179. $objectConstructor = new UnserializeObjectConstructor();
  180. $serializationVisitors = array(
  181. 'json' => new JsonSerializationVisitor($namingStrategy, $customSerializationHandlers),
  182. 'xml' => new XmlSerializationVisitor($namingStrategy, $customSerializationHandlers),
  183. );
  184. $deserializationVisitors = array(
  185. 'json' => new JsonDeserializationVisitor($namingStrategy, $customDeserializationHandlers, $objectConstructor),
  186. 'xml' => new XmlDeserializationVisitor($namingStrategy, $customDeserializationHandlers, $objectConstructor),
  187. );
  188. return new Serializer($factory, $serializationVisitors, $deserializationVisitors);
  189. }
  190. protected function getSerializationHandlers()
  191. {
  192. $handlers = array(
  193. new DateTimeHandler()
  194. );
  195. return $handlers;
  196. }
  197. protected function getDeserializationHandlers()
  198. {
  199. $handlers = array(
  200. new DateTimeHandler(),
  201. new ArrayCollectionHandler(),
  202. new AuthorListDeserializationHandler(),
  203. );
  204. return $handlers;
  205. }
  206. private function getField($obj, $name)
  207. {
  208. $ref = new \ReflectionProperty($obj, $name);
  209. $ref->setAccessible(true);
  210. return $ref->getValue($obj);
  211. }
  212. private function setField($obj, $name, $value)
  213. {
  214. $ref = new \ReflectionProperty($obj, $name);
  215. $ref->setAccessible(true);
  216. $ref->setValue($obj, $value);
  217. }
  218. }
  219. class AuthorListDeserializationHandler implements DeserializationHandlerInterface
  220. {
  221. public function deserialize(VisitorInterface $visitor, $data, $type, &$visited)
  222. {
  223. if ('AuthorList' !== $type) {
  224. return;
  225. }
  226. $visited = true;
  227. $elements = $visitor->getNavigator()->accept($data, 'array<integer, JMS\SerializerBundle\Tests\Fixtures\Author>', $visitor);
  228. $list = new AuthorList();
  229. foreach ($elements as $author) {
  230. $list->add($author);
  231. }
  232. return $list;
  233. }
  234. }