ad8d57065f7213796bc79f33f9c10e081d40c9af
[yaffs-website] / vendor / symfony / serializer / Tests / Encoder / JsonEncoderTest.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\Encoder;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Serializer\Encoder\JsonEncoder;
16 use Symfony\Component\Serializer\Serializer;
17 use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
18
19 class JsonEncoderTest extends TestCase
20 {
21     private $encoder;
22     private $serializer;
23
24     protected function setUp()
25     {
26         $this->encoder = new JsonEncoder();
27         $this->serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
28     }
29
30     public function testEncodeScalar()
31     {
32         $obj = new \stdClass();
33         $obj->foo = 'foo';
34
35         $expected = '{"foo":"foo"}';
36
37         $this->assertEquals($expected, $this->encoder->encode($obj, 'json'));
38     }
39
40     public function testComplexObject()
41     {
42         $obj = $this->getObject();
43
44         $expected = $this->getJsonSource();
45
46         $this->assertEquals($expected, $this->encoder->encode($obj, 'json'));
47     }
48
49     public function testOptions()
50     {
51         $context = array('json_encode_options' => JSON_NUMERIC_CHECK);
52
53         $arr = array();
54         $arr['foo'] = '3';
55
56         $expected = '{"foo":3}';
57
58         $this->assertEquals($expected, $this->serializer->serialize($arr, 'json', $context));
59
60         $arr = array();
61         $arr['foo'] = '3';
62
63         $expected = '{"foo":"3"}';
64
65         $this->assertEquals($expected, $this->serializer->serialize($arr, 'json'), 'Context should not be persistent');
66     }
67
68     protected function getJsonSource()
69     {
70         return '{"foo":"foo","bar":["a","b"],"baz":{"key":"val","key2":"val","A B":"bar","item":[{"title":"title1"},{"title":"title2"}],"Barry":{"FooBar":{"Baz":"Ed","@id":1}}},"qux":"1"}';
71     }
72
73     protected function getObject()
74     {
75         $obj = new \stdClass();
76         $obj->foo = 'foo';
77         $obj->bar = array('a', 'b');
78         $obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar', 'item' => array(array('title' => 'title1'), array('title' => 'title2')), 'Barry' => array('FooBar' => array('Baz' => 'Ed', '@id' => 1)));
79         $obj->qux = '1';
80
81         return $obj;
82     }
83 }