4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Tests\Normalizer;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
16 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
17 use Symfony\Component\Serializer\SerializerInterface;
18 use Symfony\Component\Serializer\Tests\Fixtures\JsonSerializableDummy;
21 * @author Fred Cox <mcfedr@gmail.com>
23 class JsonSerializableNormalizerTest extends TestCase
26 * @var JsonSerializableNormalizer
31 * @var \PHPUnit_Framework_MockObject_MockObject|SerializerInterface
35 protected function setUp()
37 $this->serializer = $this->getMockBuilder(JsonSerializerNormalizer::class)->getMock();
38 $this->normalizer = new JsonSerializableNormalizer();
39 $this->normalizer->setSerializer($this->serializer);
42 public function testSupportNormalization()
44 $this->assertTrue($this->normalizer->supportsNormalization(new JsonSerializableDummy()));
45 $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
48 public function testNormalize()
51 ->expects($this->once())
53 ->will($this->returnCallback(function ($data) {
54 $this->assertArraySubset(array('foo' => 'a', 'bar' => 'b', 'baz' => 'c'), $data);
56 return 'string_object';
60 $this->assertEquals('string_object', $this->normalizer->normalize(new JsonSerializableDummy()));
64 * @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException
66 public function testCircularNormalize()
68 $this->normalizer->setCircularReferenceLimit(1);
71 ->expects($this->once())
73 ->will($this->returnCallback(function ($data, $format, $context) {
74 $this->normalizer->normalize($data['qux'], $format, $context);
76 return 'string_object';
80 $this->assertEquals('string_object', $this->normalizer->normalize(new JsonSerializableDummy()));
84 * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
85 * @expectedExceptionMessage The object must implement "JsonSerializable".
87 public function testInvalidDataThrowException()
89 $this->normalizer->normalize(new \stdClass());
93 abstract class JsonSerializerNormalizer implements SerializerInterface, NormalizerInterface