JsonSerializableNormalizerTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Tests\Normalizer;
  11. use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
  12. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  13. use Symfony\Component\Serializer\SerializerInterface;
  14. use Symfony\Component\Serializer\Tests\Fixtures\JsonSerializableDummy;
  15. /**
  16. * @author Fred Cox <mcfedr@gmail.com>
  17. */
  18. class JsonSerializableNormalizerTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var JsonSerializableNormalizer
  22. */
  23. private $normalizer;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject|SerializerInterface
  26. */
  27. private $serializer;
  28. protected function setUp()
  29. {
  30. $this->serializer = $this->getMockBuilder(JsonSerializerNormalizer::class)->getMock();
  31. $this->normalizer = new JsonSerializableNormalizer();
  32. $this->normalizer->setSerializer($this->serializer);
  33. }
  34. public function testSupportNormalization()
  35. {
  36. $this->assertTrue($this->normalizer->supportsNormalization(new JsonSerializableDummy()));
  37. $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
  38. }
  39. public function testNormalize()
  40. {
  41. $this->serializer
  42. ->expects($this->once())
  43. ->method('normalize')
  44. ->will($this->returnCallback(function ($data) {
  45. $this->assertArraySubset(array('foo' => 'a', 'bar' => 'b', 'baz' => 'c'), $data);
  46. return 'string_object';
  47. }))
  48. ;
  49. $this->assertEquals('string_object', $this->normalizer->normalize(new JsonSerializableDummy()));
  50. }
  51. /**
  52. * @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException
  53. */
  54. public function testCircularNormalize()
  55. {
  56. $this->normalizer->setCircularReferenceLimit(1);
  57. $this->serializer
  58. ->expects($this->once())
  59. ->method('normalize')
  60. ->will($this->returnCallback(function ($data, $format, $context) {
  61. $this->normalizer->normalize($data['qux'], $format, $context);
  62. return 'string_object';
  63. }))
  64. ;
  65. $this->assertEquals('string_object', $this->normalizer->normalize(new JsonSerializableDummy()));
  66. }
  67. /**
  68. * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
  69. * @expectedExceptionMessage The object must implement "JsonSerializable".
  70. */
  71. public function testInvalidDataThrowException()
  72. {
  73. $this->normalizer->normalize(new \stdClass());
  74. }
  75. }
  76. abstract class JsonSerializerNormalizer implements SerializerInterface, NormalizerInterface
  77. {
  78. }