SerializerTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Symfony\Tests\Component\Serializer;
  3. use Symfony\Component\Serializer\Serializer;
  4. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  5. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  6. /*
  7. * This file is part of the Symfony framework.
  8. *
  9. * (c) Fabien Potencier <fabien@symfony.com>
  10. *
  11. * This source file is subject to the MIT license that is bundled
  12. * with this source code in the file LICENSE.
  13. */
  14. class SerializerTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function setUp()
  17. {
  18. $this->serializer = new Serializer();
  19. }
  20. /**
  21. * @expectedException \UnexpectedValueException
  22. */
  23. public function testNormalizeObjectNoMatch()
  24. {
  25. $this->serializer->addNormalizer($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer'));
  26. $this->serializer->normalizeObject(new \stdClass, 'xml');
  27. }
  28. /**
  29. * @expectedException \UnexpectedValueException
  30. */
  31. public function testDenormalizeObjectNoMatch()
  32. {
  33. $this->serializer->addNormalizer($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer'));
  34. $this->serializer->denormalizeObject('foo', 'stdClass');
  35. }
  36. public function testSerializeScalar()
  37. {
  38. $this->serializer->setEncoder('json', new JsonEncoder());
  39. $result = $this->serializer->serialize('foo', 'json');
  40. $this->assertEquals('"foo"', $result);
  41. }
  42. public function testSerializeArrayOfScalars()
  43. {
  44. $this->serializer->setEncoder('json', new JsonEncoder());
  45. $data = array('foo', array(5, 3));
  46. $result = $this->serializer->serialize($data, 'json');
  47. $this->assertEquals(json_encode($data), $result);
  48. }
  49. public function testEncode()
  50. {
  51. $this->serializer->setEncoder('json', new JsonEncoder());
  52. $data = array('foo', array(5, 3));
  53. $result = $this->serializer->encode($data, 'json');
  54. $this->assertEquals(json_encode($data), $result);
  55. }
  56. public function testDecode()
  57. {
  58. $this->serializer->setDecoder('json', new JsonEncoder());
  59. $data = array('foo', array(5, 3));
  60. $result = $this->serializer->decode(json_encode($data), 'json');
  61. $this->assertEquals($data, $result);
  62. }
  63. /**
  64. * @expectedException \UnexpectedValueException
  65. */
  66. public function testNormalizeNoMatchObject()
  67. {
  68. $this->serializer->addNormalizer($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer'));
  69. $this->serializer->normalizeObject(new \stdClass, 'xml');
  70. }
  71. }