SerializerTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\CustomNormalizer;
  6. use Symfony\Tests\Component\Serializer\Fixtures\TraversableDummy;
  7. use Symfony\Tests\Component\Serializer\Fixtures\NormalizableTraversableDummy;
  8. /*
  9. * This file is part of the Symfony framework.
  10. *
  11. * (c) Fabien Potencier <fabien@symfony.com>
  12. *
  13. * This source file is subject to the MIT license that is bundled
  14. * with this source code in the file LICENSE.
  15. */
  16. class SerializerTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @expectedException \UnexpectedValueException
  20. */
  21. public function testNormalizeNoMatch()
  22. {
  23. $this->serializer = new Serializer(array($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer')));
  24. $this->serializer->normalize(new \stdClass, 'xml');
  25. }
  26. public function testNormalizeTraversable()
  27. {
  28. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  29. $result = $this->serializer->serialize(new TraversableDummy, 'json');
  30. $this->assertEquals('{"foo":"foo","bar":"bar"}', $result);
  31. }
  32. public function testNormalizeGivesPriorityToInterfaceOverTraversable()
  33. {
  34. $this->serializer = new Serializer(array(new CustomNormalizer), array('json' => new JsonEncoder()));
  35. $result = $this->serializer->serialize(new NormalizableTraversableDummy, 'json');
  36. $this->assertEquals('{"foo":"normalizedFoo","bar":"normalizedBar"}', $result);
  37. }
  38. /**
  39. * @expectedException \UnexpectedValueException
  40. */
  41. public function testDenormalizeNoMatch()
  42. {
  43. $this->serializer = new Serializer(array($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer')));
  44. $this->serializer->denormalize('foo', 'stdClass');
  45. }
  46. public function testSerializeScalar()
  47. {
  48. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  49. $result = $this->serializer->serialize('foo', 'json');
  50. $this->assertEquals('"foo"', $result);
  51. }
  52. public function testSerializeArrayOfScalars()
  53. {
  54. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  55. $data = array('foo', array(5, 3));
  56. $result = $this->serializer->serialize($data, 'json');
  57. $this->assertEquals(json_encode($data), $result);
  58. }
  59. public function testEncode()
  60. {
  61. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  62. $data = array('foo', array(5, 3));
  63. $result = $this->serializer->encode($data, 'json');
  64. $this->assertEquals(json_encode($data), $result);
  65. }
  66. public function testDecode()
  67. {
  68. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  69. $data = array('foo', array(5, 3));
  70. $result = $this->serializer->decode(json_encode($data), 'json');
  71. $this->assertEquals($data, $result);
  72. }
  73. }