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