SerializerTest.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /**
  17. * @expectedException \UnexpectedValueException
  18. */
  19. public function testNormalizeNoMatch()
  20. {
  21. $this->serializer = new Serializer(array($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer')));
  22. $this->serializer->normalize(new \stdClass, 'xml');
  23. }
  24. /**
  25. * @expectedException \UnexpectedValueException
  26. */
  27. public function testDenormalizeNoMatch()
  28. {
  29. $this->serializer = new Serializer(array($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer')));
  30. $this->serializer->denormalize('foo', 'stdClass');
  31. }
  32. public function testSerializeScalar()
  33. {
  34. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  35. $result = $this->serializer->serialize('foo', 'json');
  36. $this->assertEquals('"foo"', $result);
  37. }
  38. public function testSerializeArrayOfScalars()
  39. {
  40. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  41. $data = array('foo', array(5, 3));
  42. $result = $this->serializer->serialize($data, 'json');
  43. $this->assertEquals(json_encode($data), $result);
  44. }
  45. public function testEncode()
  46. {
  47. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  48. $data = array('foo', array(5, 3));
  49. $result = $this->serializer->encode($data, 'json');
  50. $this->assertEquals(json_encode($data), $result);
  51. }
  52. public function testDecode()
  53. {
  54. $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
  55. $data = array('foo', array(5, 3));
  56. $result = $this->serializer->decode(json_encode($data), 'json');
  57. $this->assertEquals($data, $result);
  58. }
  59. }