BaseSerializationTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 JMS\SerializerBundle\Tests\Fixtures\AccessorOrderParent;
  19. use JMS\SerializerBundle\Tests\Fixtures\AccessorOrderChild;
  20. use JMS\SerializerBundle\Tests\Fixtures\GetSetObject;
  21. use JMS\SerializerBundle\Tests\Fixtures\IndexedCommentsBlogPost;
  22. use JMS\SerializerBundle\Tests\Fixtures\CurrencyAwareOrder;
  23. use JMS\SerializerBundle\Tests\Fixtures\CurrencyAwarePrice;
  24. use JMS\SerializerBundle\Tests\Fixtures\Order;
  25. use Symfony\Component\Yaml\Inline;
  26. use JMS\SerializerBundle\Serializer\YamlSerializationVisitor;
  27. use Doctrine\Common\Collections\ArrayCollection;
  28. use Symfony\Component\Form\Form;
  29. use Symfony\Component\Form\FormError;
  30. use Symfony\Component\Validator\ConstraintViolation;
  31. use Symfony\Component\Validator\ConstraintViolationList;
  32. use JMS\SerializerBundle\Serializer\Handler\DeserializationHandlerInterface;
  33. use JMS\SerializerBundle\Serializer\Handler\SerializationHandlerInterface;
  34. use JMS\SerializerBundle\Tests\Fixtures\AuthorList;
  35. use JMS\SerializerBundle\Serializer\VisitorInterface;
  36. use JMS\SerializerBundle\Serializer\XmlDeserializationVisitor;
  37. use JMS\SerializerBundle\Serializer\Construction\UnserializeObjectConstructor;
  38. use JMS\SerializerBundle\Serializer\JsonDeserializationVisitor;
  39. use JMS\SerializerBundle\Tests\Fixtures\Log;
  40. use JMS\SerializerBundle\Serializer\Handler\ArrayCollectionHandler;
  41. use JMS\SerializerBundle\Serializer\Handler\ObjectBasedCustomHandler;
  42. use JMS\SerializerBundle\Serializer\Handler\DateTimeHandler;
  43. use JMS\SerializerBundle\Serializer\Handler\FormErrorHandler;
  44. use JMS\SerializerBundle\Serializer\Handler\ConstraintViolationHandler;
  45. use JMS\SerializerBundle\Serializer\Handler\DoctrineProxyHandler;
  46. use JMS\SerializerBundle\Tests\Fixtures\Comment;
  47. use JMS\SerializerBundle\Tests\Fixtures\Author;
  48. use JMS\SerializerBundle\Tests\Fixtures\AuthorReadOnly;
  49. use JMS\SerializerBundle\Tests\Fixtures\BlogPost;
  50. use JMS\SerializerBundle\Tests\Fixtures\ObjectWithLifecycleCallbacks;
  51. use JMS\SerializerBundle\Tests\Fixtures\CircularReferenceParent;
  52. use JMS\SerializerBundle\Tests\Fixtures\InlineParent;
  53. use JMS\SerializerBundle\Serializer\XmlSerializationVisitor;
  54. use Doctrine\Common\Annotations\AnnotationReader;
  55. use JMS\SerializerBundle\Metadata\Driver\AnnotationDriver;
  56. use Metadata\MetadataFactory;
  57. use JMS\SerializerBundle\Tests\Fixtures\SimpleObject;
  58. use JMS\SerializerBundle\Tests\Fixtures\SimpleObjectProxy;
  59. use JMS\SerializerBundle\Tests\Fixtures\Price;
  60. use JMS\SerializerBundle\Serializer\Naming\CamelCaseNamingStrategy;
  61. use JMS\SerializerBundle\Serializer\Naming\SerializedNameAnnotationStrategy;
  62. use JMS\SerializerBundle\Serializer\JsonSerializationVisitor;
  63. use JMS\SerializerBundle\Serializer\Serializer;
  64. abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
  65. {
  66. public function testString()
  67. {
  68. $this->assertEquals($this->getContent('string'), $this->serialize('foo'));
  69. if ($this->hasDeserializer()) {
  70. $this->assertEquals('foo', $this->deserialize($this->getContent('string'), 'string'));
  71. }
  72. }
  73. /**
  74. * @dataProvider getBooleans
  75. */
  76. public function testBooleans($strBoolean, $boolean)
  77. {
  78. $this->assertEquals($this->getContent('boolean_'.$strBoolean), $this->serialize($boolean));
  79. if ($this->hasDeserializer()) {
  80. $this->assertSame($boolean, $this->deserialize($this->getContent('boolean_'.$strBoolean), 'boolean'));
  81. }
  82. }
  83. public function getBooleans()
  84. {
  85. return array(array('true', true), array('false', false));
  86. }
  87. /**
  88. * @dataProvider getNumerics
  89. */
  90. public function testNumerics($key, $value)
  91. {
  92. $this->assertEquals($this->getContent($key), $this->serialize($value));
  93. if ($this->hasDeserializer()) {
  94. $this->assertEquals($value, $this->deserialize($this->getContent($key), is_double($value) ? 'double' : 'integer'));
  95. }
  96. }
  97. public function getNumerics()
  98. {
  99. return array(
  100. array('integer', 1),
  101. array('float', 4.533),
  102. array('float_trailing_zero', 1.0),
  103. );
  104. }
  105. public function testSimpleObject()
  106. {
  107. $this->assertEquals($this->getContent('simple_object'), $this->serialize($obj = new SimpleObject('foo', 'bar')));
  108. if ($this->hasDeserializer()) {
  109. $this->assertEquals($obj, $this->deserialize($this->getContent('simple_object'), get_class($obj)));
  110. }
  111. }
  112. public function testArrayStrings()
  113. {
  114. $data = array('foo', 'bar');
  115. $this->assertEquals($this->getContent('array_strings'), $this->serialize($data));
  116. if ($this->hasDeserializer()) {
  117. $this->assertEquals($data, $this->deserialize($this->getContent('array_strings'), 'array<string>'));
  118. }
  119. }
  120. public function testArrayBooleans()
  121. {
  122. $data = array(true, false);
  123. $this->assertEquals($this->getContent('array_booleans'), $this->serialize($data));
  124. if ($this->hasDeserializer()) {
  125. $this->assertEquals($data, $this->deserialize($this->getContent('array_booleans'), 'array<boolean>'));
  126. }
  127. }
  128. public function testArrayIntegers()
  129. {
  130. $data = array(1, 3, 4);
  131. $this->assertEquals($this->getContent('array_integers'), $this->serialize($data));
  132. if ($this->hasDeserializer()) {
  133. $this->assertEquals($data, $this->deserialize($this->getContent('array_integers'), 'array<integer>'));
  134. }
  135. }
  136. public function testArrayFloats()
  137. {
  138. $data = array(1.34, 3.0, 6.42);
  139. $this->assertEquals($this->getContent('array_floats'), $this->serialize($data));
  140. if ($this->hasDeserializer()) {
  141. $this->assertEquals($data, $this->deserialize($this->getContent('array_floats'), 'array<double>'));
  142. }
  143. }
  144. public function testArrayObjects()
  145. {
  146. $data = array(new SimpleObject('foo', 'bar'), new SimpleObject('baz', 'boo'));
  147. $this->assertEquals($this->getContent('array_objects'), $this->serialize($data));
  148. if ($this->hasDeserializer()) {
  149. $this->assertEquals($data, $this->deserialize($this->getContent('array_objects'), 'array<JMS\SerializerBundle\Tests\Fixtures\SimpleObject>'));
  150. }
  151. }
  152. public function testArrayMixed()
  153. {
  154. $this->assertEquals($this->getContent('array_mixed'), $this->serialize(array('foo', 1, true, new SimpleObject('foo', 'bar'), array(1, 3, true))));
  155. }
  156. public function testBlogPost()
  157. {
  158. $post = new BlogPost('This is a nice title.', $author = new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')));
  159. $post->addComment($comment = new Comment($author, 'foo'));
  160. $this->assertEquals($this->getContent('blog_post'), $this->serialize($post));
  161. if ($this->hasDeserializer()) {
  162. $deserialized = $this->deserialize($this->getContent('blog_post'), get_class($post));
  163. $this->assertEquals('2011-07-30T00:00:00+0000', $this->getField($deserialized, 'createdAt')->format(\DateTime::ISO8601));
  164. $this->assertAttributeEquals('This is a nice title.', 'title', $deserialized);
  165. $this->assertAttributeSame(false, 'published', $deserialized);
  166. $this->assertAttributeEquals(new ArrayCollection(array($comment)), 'comments', $deserialized);
  167. $this->assertAttributeEquals($author, 'author', $deserialized);
  168. }
  169. }
  170. public function testReadOnly()
  171. {
  172. $author = new AuthorReadOnly(123, 'Ruud Kamphuis');
  173. $this->assertEquals($this->getContent('readonly'), $this->serialize($author));
  174. if ($this->hasDeserializer()) {
  175. $deserialized = $this->deserialize($this->getContent('readonly'), get_class($author));
  176. $this->assertNull($this->getField($deserialized, 'id'));
  177. $this->assertEquals('Ruud Kamphuis', $this->getField($deserialized, 'name'));
  178. }
  179. }
  180. public function testPrice()
  181. {
  182. $price = new Price(3);
  183. $this->assertEquals($this->getContent('price'), $this->serialize($price));
  184. if ($this->hasDeserializer()) {
  185. $deserialized = $this->deserialize($this->getContent('price'), get_class($price));
  186. $this->assertEquals(3, $this->getField($deserialized, 'price'));
  187. }
  188. }
  189. public function testOrder()
  190. {
  191. $order = new Order(new Price(12.34));
  192. $this->assertEquals($this->getContent('order'), $this->serialize($order));
  193. if ($this->hasDeserializer()) {
  194. $this->assertEquals($order, $this->deserialize($this->getContent('order'), get_class($order)));
  195. }
  196. }
  197. public function testCurrencyAwarePrice()
  198. {
  199. $price = new CurrencyAwarePrice(2.34);
  200. $this->assertEquals($this->getContent('currency_aware_price'), $this->serialize($price));
  201. if ($this->hasDeserializer()) {
  202. $this->assertEquals($price, $this->deserialize($this->getContent('currency_aware_price'), get_class($price)));
  203. }
  204. }
  205. public function testOrderWithCurrencyAwarePrice()
  206. {
  207. $order = new CurrencyAwareOrder(new CurrencyAwarePrice(1.23));
  208. $this->assertEquals($this->getContent('order_with_currency_aware_price'), $this->serialize($order));
  209. if ($this->hasDeserializer()) {
  210. $this->assertEquals($order, $this->deserialize($this->getContent('order_with_currency_aware_price'), get_class($order)));
  211. }
  212. }
  213. public function testArticle()
  214. {
  215. $article = new Article();
  216. $article->element = 'custom';
  217. $article->value = 'serialized';
  218. $result = $this->serialize($article);
  219. $this->assertEquals($this->getContent('article'), $result);
  220. if ($this->hasDeserializer()) {
  221. $this->assertEquals($article, $this->deserialize($result, 'JMS\SerializerBundle\Tests\Serializer\Article'));
  222. }
  223. }
  224. public function testInline()
  225. {
  226. $inline = new InlineParent();
  227. $result = $this->serialize($inline);
  228. $this->assertEquals($this->getContent('inline'), $result);
  229. //no deserialization support
  230. /*if ($this->hasDeserializer()) {
  231. $this->assertEquals($inline, $this->deserialize($result, 'JMS\SerializerBundle\Tests\Serializer\InlineParent'));
  232. }*/
  233. }
  234. /**
  235. * @group test
  236. */
  237. public function testLog()
  238. {
  239. $this->assertEquals($this->getContent('log'), $this->serialize($log = new Log()));
  240. if ($this->hasDeserializer()) {
  241. $deserialized = $this->deserialize($this->getContent('log'), get_class($log));
  242. $this->assertEquals($log, $deserialized);
  243. }
  244. }
  245. public function testCircularReference()
  246. {
  247. $object = new CircularReferenceParent();
  248. $this->assertEquals($this->getContent('circular_reference'), $this->serialize($object));
  249. if ($this->hasDeserializer()) {
  250. $deserialized = $this->deserialize($this->getContent('circular_reference'), get_class($object));
  251. $col = $this->getField($deserialized, 'collection');
  252. $this->assertEquals(2, count($col));
  253. $this->assertEquals('child1', $col[0]->getName());
  254. $this->assertEquals('child2', $col[1]->getName());
  255. $this->assertSame($deserialized, $col[0]->getParent());
  256. $this->assertSame($deserialized, $col[1]->getParent());
  257. $col = $this->getField($deserialized, 'anotherCollection');
  258. $this->assertEquals(2, count($col));
  259. $this->assertEquals('child1', $col[0]->getName());
  260. $this->assertEquals('child2', $col[1]->getName());
  261. $this->assertSame($deserialized, $col[0]->getParent());
  262. $this->assertSame($deserialized, $col[1]->getParent());
  263. }
  264. }
  265. public function testLifecycleCallbacks()
  266. {
  267. $object = new ObjectWithLifecycleCallbacks();
  268. $this->assertEquals($this->getContent('lifecycle_callbacks'), $this->serialize($object));
  269. $this->assertAttributeSame(null, 'name', $object);
  270. if ($this->hasDeserializer()) {
  271. $deserialized = $this->deserialize($this->getContent('lifecycle_callbacks'), get_class($object));
  272. $this->assertEquals($object, $deserialized);
  273. }
  274. }
  275. public function testFormErrors()
  276. {
  277. $errors = array(
  278. new FormError('This is the form error'),
  279. new FormError('Another error')
  280. );
  281. $this->assertEquals($this->getContent('form_errors'), $this->serialize($errors));
  282. }
  283. public function testNestedFormErrors()
  284. {
  285. $dispather = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  286. $form = new Form('foo', $dispather);
  287. $form->addError(new FormError('This is the form error'));
  288. $child = new Form('bar', $dispather);
  289. $child->addError(new FormError('Error of the child form'));
  290. $form->add($child);
  291. $this->assertEquals($this->getContent('nested_form_errors'), $this->serialize($form));
  292. }
  293. public function testConstraintViolation()
  294. {
  295. $violation = new ConstraintViolation('Message of violation', array(), null, 'foo', null);
  296. $this->assertEquals($this->getContent('constraint_violation'), $this->serialize($violation));
  297. }
  298. public function testConstraintViolationList()
  299. {
  300. $violations = new ConstraintViolationList();
  301. $violations->add(new ConstraintViolation('Message of violation', array(), null, 'foo', null));
  302. $violations->add(new ConstraintViolation('Message of another violation', array(), null, 'bar', null));
  303. $this->assertEquals($this->getContent('constraint_violation_list'), $this->serialize($violations));
  304. }
  305. public function testDoctrineProxy()
  306. {
  307. if (!class_exists('Doctrine\ORM\Version')) {
  308. $this->markTestSkipped('Doctrine is not available.');
  309. }
  310. $object = new SimpleObjectProxy('foo', 'bar');
  311. $this->assertEquals($this->getContent('orm_proxy'), $this->serialize($object));
  312. }
  313. public function testInitializedDoctrineProxy()
  314. {
  315. if (!class_exists('Doctrine\ORM\Version')) {
  316. $this->markTestSkipped('Doctrine is not available.');
  317. }
  318. $object = new SimpleObjectProxy('foo', 'bar');
  319. $object->__load();
  320. $this->assertEquals($this->getContent('orm_proxy'), $this->serialize($object));
  321. }
  322. public function testCustomAccessor()
  323. {
  324. $post = new IndexedCommentsBlogPost();
  325. $this->assertEquals($this->getContent('custom_accessor'), $this->serialize($post));
  326. }
  327. public function testMixedAccessTypes()
  328. {
  329. $object = new GetSetObject();
  330. $this->assertEquals($this->getContent('mixed_access_types'), $this->serialize($object));
  331. if ($this->hasDeserializer()) {
  332. $object = $this->deserialize($this->getContent('mixed_access_types'), 'JMS\SerializerBundle\Tests\Fixtures\GetSetObject');
  333. $this->assertAttributeEquals(1, 'id', $object);
  334. $this->assertAttributeEquals('Johannes', 'name', $object);
  335. }
  336. }
  337. public function testAccessorOrder()
  338. {
  339. $this->assertEquals($this->getContent('accessor_order_child'), $this->serialize(new AccessorOrderChild()));
  340. $this->assertEquals($this->getContent('accessor_order_parent'), $this->serialize(new AccessorOrderParent()));
  341. }
  342. abstract protected function getContent($key);
  343. abstract protected function getFormat();
  344. protected function hasDeserializer()
  345. {
  346. return true;
  347. }
  348. protected function serialize($data)
  349. {
  350. return $this->getSerializer()->serialize($data, $this->getFormat());
  351. }
  352. protected function deserialize($content, $type)
  353. {
  354. return $this->getSerializer()->deserialize($content, $type, $this->getFormat());
  355. }
  356. protected function getSerializer()
  357. {
  358. $factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
  359. $namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
  360. $objectConstructor = new UnserializeObjectConstructor();
  361. $customSerializationHandlers = $this->getSerializationHandlers();
  362. $customDeserializationHandlers = $this->getDeserializationHandlers();
  363. $serializationVisitors = array(
  364. 'json' => new JsonSerializationVisitor($namingStrategy, $customSerializationHandlers),
  365. 'xml' => new XmlSerializationVisitor($namingStrategy, $customSerializationHandlers),
  366. 'yml' => new YamlSerializationVisitor($namingStrategy, $customSerializationHandlers),
  367. );
  368. $deserializationVisitors = array(
  369. 'json' => new JsonDeserializationVisitor($namingStrategy, $customDeserializationHandlers, $objectConstructor),
  370. 'xml' => new XmlDeserializationVisitor($namingStrategy, $customDeserializationHandlers, $objectConstructor),
  371. );
  372. return new Serializer($factory, $serializationVisitors, $deserializationVisitors);
  373. }
  374. protected function getSerializationHandlers()
  375. {
  376. $translatorMock = $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface');
  377. $translatorMock
  378. ->expects($this->any())
  379. ->method('trans')
  380. ->will($this->returnArgument(0));
  381. $factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
  382. $objectConstructor = new UnserializeObjectConstructor();
  383. $handlers = array(
  384. new ObjectBasedCustomHandler($objectConstructor, $factory),
  385. new DateTimeHandler(),
  386. new FormErrorHandler($translatorMock),
  387. new ConstraintViolationHandler(),
  388. new DoctrineProxyHandler(),
  389. );
  390. return $handlers;
  391. }
  392. protected function getDeserializationHandlers()
  393. {
  394. $factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
  395. $objectConstructor = new UnserializeObjectConstructor();
  396. $handlers = array(
  397. new ObjectBasedCustomHandler($objectConstructor, $factory),
  398. new DateTimeHandler(),
  399. new ArrayCollectionHandler(),
  400. new AuthorListDeserializationHandler(),
  401. );
  402. return $handlers;
  403. }
  404. private function getField($obj, $name)
  405. {
  406. $ref = new \ReflectionProperty($obj, $name);
  407. $ref->setAccessible(true);
  408. return $ref->getValue($obj);
  409. }
  410. private function setField($obj, $name, $value)
  411. {
  412. $ref = new \ReflectionProperty($obj, $name);
  413. $ref->setAccessible(true);
  414. $ref->setValue($obj, $value);
  415. }
  416. }
  417. class AuthorListDeserializationHandler implements DeserializationHandlerInterface
  418. {
  419. public function deserialize(VisitorInterface $visitor, $data, $type, &$visited)
  420. {
  421. if ('AuthorList' !== $type) {
  422. return;
  423. }
  424. $visited = true;
  425. $elements = $visitor->getNavigator()->accept($data, 'array<integer, JMS\SerializerBundle\Tests\Fixtures\Author>', $visitor);
  426. $list = new AuthorList();
  427. foreach ($elements as $author) {
  428. $list->add($author);
  429. }
  430. return $list;
  431. }
  432. }
  433. class Article implements SerializationHandlerInterface, DeserializationHandlerInterface
  434. {
  435. public $element;
  436. public $value;
  437. public function serialize(VisitorInterface $visitor, $data, $type, &$visited)
  438. {
  439. if (!$data instanceof Article) {
  440. return;
  441. }
  442. if ($visitor instanceof XmlSerializationVisitor) {
  443. $visited = true;
  444. if (null === $visitor->document) {
  445. $visitor->document = $visitor->createDocument(null, null, false);
  446. }
  447. $visitor->document->appendChild($visitor->document->createElement($this->element, $this->value));
  448. } elseif ($visitor instanceof JsonSerializationVisitor) {
  449. $visited = true;
  450. $visitor->setRoot(array($this->element => $this->value));
  451. } elseif ($visitor instanceof YamlSerializationVisitor) {
  452. $visited = true;
  453. $visitor->writer->writeln(Inline::dump($this->element).': '.Inline::dump($this->value));
  454. }
  455. }
  456. public function deserialize(VisitorInterface $visitor, $data, $type, &$visited)
  457. {
  458. if ('JMS\SerializerBundle\Tests\Serializer\Article' !== $type) {
  459. return;
  460. }
  461. if ($visitor instanceof XmlDeserializationVisitor) {
  462. $visited = true;
  463. $this->element = $data->getName();
  464. $this->value = (string)$data;
  465. } elseif ($visitor instanceof JsonDeserializationVisitor) {
  466. $visited = true;
  467. $this->element = key($data);
  468. $this->value = reset($data);
  469. }
  470. return $this;
  471. }
  472. }