1977d4f19ce84bd48c39318ca70f555ab46dd44d
[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\Normalizer\CustomNormalizer;
17 use Symfony\Component\Serializer\Serializer;
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     /**
69      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
70      */
71     public function testEncodeNotUtf8WithoutPartialOnError()
72     {
73         $arr = array(
74             'utf8' => 'Hello World!',
75             'notUtf8' => "\xb0\xd0\xb5\xd0",
76         );
77
78         $this->encoder->encode($arr, 'json');
79     }
80
81     public function testEncodeNotUtf8WithPartialOnError()
82     {
83         $context = array('json_encode_options' => JSON_PARTIAL_OUTPUT_ON_ERROR);
84
85         $arr = array(
86             'utf8' => 'Hello World!',
87             'notUtf8' => "\xb0\xd0\xb5\xd0",
88         );
89
90         $result = $this->encoder->encode($arr, 'json', $context);
91         $jsonLastError = json_last_error();
92
93         $this->assertSame(JSON_ERROR_UTF8, $jsonLastError);
94         $this->assertEquals('{"utf8":"Hello World!","notUtf8":null}', $result);
95
96         $this->assertEquals('0', $this->serializer->serialize(NAN, 'json', $context));
97     }
98
99     public function testDecodeFalseString()
100     {
101         $result = $this->encoder->decode('false', 'json');
102         $this->assertSame(JSON_ERROR_NONE, json_last_error());
103         $this->assertFalse($result);
104     }
105
106     protected function getJsonSource()
107     {
108         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"}';
109     }
110
111     protected function getObject()
112     {
113         $obj = new \stdClass();
114         $obj->foo = 'foo';
115         $obj->bar = array('a', 'b');
116         $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)));
117         $obj->qux = '1';
118
119         return $obj;
120     }
121 }