55378c773a557eec551c8e7a60d1d531a9d53915
[yaffs-website] / vendor / symfony / serializer / Tests / Normalizer / CustomNormalizerTest.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\CustomNormalizer;
16 use Symfony\Component\Serializer\Serializer;
17 use Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy;
18
19 class CustomNormalizerTest extends TestCase
20 {
21     /**
22      * @var CustomNormalizer
23      */
24     private $normalizer;
25
26     protected function setUp()
27     {
28         $this->normalizer = new CustomNormalizer();
29         $this->normalizer->setSerializer(new Serializer());
30     }
31
32     public function testInterface()
33     {
34         $this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\NormalizerInterface', $this->normalizer);
35         $this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\DenormalizerInterface', $this->normalizer);
36         $this->assertInstanceOf('Symfony\Component\Serializer\SerializerAwareInterface', $this->normalizer);
37     }
38
39     public function testSerialize()
40     {
41         $obj = new ScalarDummy();
42         $obj->foo = 'foo';
43         $obj->xmlFoo = 'xml';
44         $this->assertEquals('foo', $this->normalizer->normalize($obj, 'json'));
45         $this->assertEquals('xml', $this->normalizer->normalize($obj, 'xml'));
46     }
47
48     public function testDeserialize()
49     {
50         $obj = $this->normalizer->denormalize('foo', \get_class(new ScalarDummy()), 'xml');
51         $this->assertEquals('foo', $obj->xmlFoo);
52         $this->assertNull($obj->foo);
53
54         $obj = $this->normalizer->denormalize('foo', \get_class(new ScalarDummy()), 'json');
55         $this->assertEquals('foo', $obj->foo);
56         $this->assertNull($obj->xmlFoo);
57     }
58
59     public function testDenormalizeWithObjectToPopulateUsesProvidedObject()
60     {
61         $expected = new ScalarDummy();
62         $obj = $this->normalizer->denormalize('foo', ScalarDummy::class, 'json', array(
63             'object_to_populate' => $expected,
64         ));
65
66         $this->assertSame($expected, $obj);
67         $this->assertEquals('foo', $obj->foo);
68         $this->assertNull($obj->xmlFoo);
69     }
70
71     public function testSupportsNormalization()
72     {
73         $this->assertTrue($this->normalizer->supportsNormalization(new ScalarDummy()));
74         $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
75     }
76
77     public function testSupportsDenormalization()
78     {
79         $this->assertTrue($this->normalizer->supportsDenormalization(array(), 'Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy'));
80         $this->assertFalse($this->normalizer->supportsDenormalization(array(), 'stdClass'));
81         $this->assertTrue($this->normalizer->supportsDenormalization(array(), 'Symfony\Component\Serializer\Tests\Fixtures\DenormalizableDummy'));
82     }
83 }