BaseSerializationTest.php 23 KB

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