SerializerTest.php 2.2 KB

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