SerializerTest.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace Symfony\Tests\Component\Serializer;
  3. use Symfony\Component\Serializer\Serializer;
  4. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  5. use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
  6. use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
  7. use Symfony\Tests\Component\Serializer\Fixtures\TraversableDummy;
  8. use Symfony\Tests\Component\Serializer\Fixtures\NormalizableTraversableDummy;
  9. /*
  10. * This file is part of the Symfony framework.
  11. *
  12. * (c) Fabien Potencier <fabien@symfony.com>
  13. *
  14. * This source file is subject to the MIT license that is bundled
  15. * with this source code in the file LICENSE.
  16. */
  17. class SerializerTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
  21. */
  22. public function testNormalizeNoMatch()
  23. {
  24. $this->serializer = new Serializer(array($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer')));
  25. $this->serializer->normalize(new \stdClass, 'xml');
  26. }
  27. public function testNormalizeTraversable()
  28. {
  29. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  30. $result = $this->serializer->serialize(new TraversableDummy, 'json');
  31. $this->assertEquals('{"foo":"foo","bar":"bar"}', $result);
  32. }
  33. public function testNormalizeGivesPriorityToInterfaceOverTraversable()
  34. {
  35. $this->serializer = new Serializer(array(new CustomNormalizer), array('json' => new JsonEncoder()));
  36. $result = $this->serializer->serialize(new NormalizableTraversableDummy, 'json');
  37. $this->assertEquals('{"foo":"normalizedFoo","bar":"normalizedBar"}', $result);
  38. }
  39. /**
  40. * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
  41. */
  42. public function testDenormalizeNoMatch()
  43. {
  44. $this->serializer = new Serializer(array($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer')));
  45. $this->serializer->denormalize('foo', 'stdClass');
  46. }
  47. public function testSerialize()
  48. {
  49. $this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
  50. $data = array('title' => 'foo', 'numbers' => array(5, 3));
  51. $result = $this->serializer->serialize(Model::fromArray($data), 'json');
  52. $this->assertEquals(json_encode($data), $result);
  53. }
  54. public function testSerializeScalar()
  55. {
  56. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  57. $result = $this->serializer->serialize('foo', 'json');
  58. $this->assertEquals('"foo"', $result);
  59. }
  60. public function testSerializeArrayOfScalars()
  61. {
  62. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  63. $data = array('foo', array(5, 3));
  64. $result = $this->serializer->serialize($data, 'json');
  65. $this->assertEquals(json_encode($data), $result);
  66. }
  67. /**
  68. * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
  69. */
  70. public function testSerializeNoEncoder()
  71. {
  72. $this->serializer = new Serializer(array(), array());
  73. $data = array('title' => 'foo', 'numbers' => array(5, 3));
  74. $this->serializer->serialize($data, 'json');
  75. }
  76. /**
  77. * @expectedException \Symfony\Component\Serializer\Exception\LogicException
  78. */
  79. public function testSerializeNoNormalizer()
  80. {
  81. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  82. $data = array('title' => 'foo', 'numbers' => array(5, 3));
  83. $this->serializer->serialize(Model::fromArray($data), 'json');
  84. }
  85. public function testDeserialize()
  86. {
  87. $this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
  88. $data = array('title' => 'foo', 'numbers' => array(5, 3));
  89. $result = $this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
  90. $this->assertEquals($data, $result->toArray());
  91. }
  92. public function testDeserializeUseCache()
  93. {
  94. $this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
  95. $data = array('title' => 'foo', 'numbers' => array(5, 3));
  96. $this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
  97. $data = array('title' => 'bar', 'numbers' => array(2, 8));
  98. $result = $this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
  99. $this->assertEquals($data, $result->toArray());
  100. }
  101. /**
  102. * @expectedException \Symfony\Component\Serializer\Exception\LogicException
  103. */
  104. public function testDeserializeNoNormalizer()
  105. {
  106. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  107. $data = array('title' => 'foo', 'numbers' => array(5, 3));
  108. $this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
  109. }
  110. /**
  111. * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
  112. */
  113. public function testDeserializeWrongNormalizer()
  114. {
  115. $this->serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
  116. $data = array('title' => 'foo', 'numbers' => array(5, 3));
  117. $this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
  118. }
  119. /**
  120. * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
  121. */
  122. public function testDeerializeNoEncoder()
  123. {
  124. $this->serializer = new Serializer(array(), array());
  125. $data = array('title' => 'foo', 'numbers' => array(5, 3));
  126. $this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
  127. }
  128. public function testEncode()
  129. {
  130. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  131. $data = array('foo', array(5, 3));
  132. $result = $this->serializer->encode($data, 'json');
  133. $this->assertEquals(json_encode($data), $result);
  134. }
  135. public function testDecode()
  136. {
  137. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  138. $data = array('foo', array(5, 3));
  139. $result = $this->serializer->decode(json_encode($data), 'json');
  140. $this->assertEquals($data, $result);
  141. }
  142. }
  143. class Model
  144. {
  145. private $title;
  146. private $numbers;
  147. public static function fromArray($array)
  148. {
  149. $model = new self();
  150. if (isset($array['title'])) {
  151. $model->setTitle($array['title']);
  152. }
  153. if (isset($array['numbers'])) {
  154. $model->setNumbers($array['numbers']);
  155. }
  156. return $model;
  157. }
  158. public function getTitle()
  159. {
  160. return $this->title;
  161. }
  162. public function setTitle($title)
  163. {
  164. $this->title = $title;
  165. }
  166. public function getNumbers()
  167. {
  168. return $this->numbers;
  169. }
  170. public function setNumbers($numbers)
  171. {
  172. $this->numbers = $numbers;
  173. }
  174. public function toArray()
  175. {
  176. return array('title' => $this->title, 'numbers' => $this->numbers);
  177. }
  178. }