6687fde5be519a2baa10f4bf6942995a6e1ed34d
[yaffs-website] / vendor / symfony / http-foundation / Tests / JsonResponseTest.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\HttpFoundation\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\JsonResponse;
16
17 class JsonResponseTest extends TestCase
18 {
19     protected function setUp()
20     {
21         parent::setUp();
22
23         if (!\defined('HHVM_VERSION')) {
24             $this->iniSet('serialize_precision', 14);
25         }
26     }
27
28     public function testConstructorEmptyCreatesJsonObject()
29     {
30         $response = new JsonResponse();
31         $this->assertSame('{}', $response->getContent());
32     }
33
34     public function testConstructorWithArrayCreatesJsonArray()
35     {
36         $response = new JsonResponse(array(0, 1, 2, 3));
37         $this->assertSame('[0,1,2,3]', $response->getContent());
38     }
39
40     public function testConstructorWithAssocArrayCreatesJsonObject()
41     {
42         $response = new JsonResponse(array('foo' => 'bar'));
43         $this->assertSame('{"foo":"bar"}', $response->getContent());
44     }
45
46     public function testConstructorWithSimpleTypes()
47     {
48         $response = new JsonResponse('foo');
49         $this->assertSame('"foo"', $response->getContent());
50
51         $response = new JsonResponse(0);
52         $this->assertSame('0', $response->getContent());
53
54         $response = new JsonResponse(0.1);
55         $this->assertSame('0.1', $response->getContent());
56
57         $response = new JsonResponse(true);
58         $this->assertSame('true', $response->getContent());
59     }
60
61     public function testConstructorWithCustomStatus()
62     {
63         $response = new JsonResponse(array(), 202);
64         $this->assertSame(202, $response->getStatusCode());
65     }
66
67     public function testConstructorAddsContentTypeHeader()
68     {
69         $response = new JsonResponse();
70         $this->assertSame('application/json', $response->headers->get('Content-Type'));
71     }
72
73     public function testConstructorWithCustomHeaders()
74     {
75         $response = new JsonResponse(array(), 200, array('ETag' => 'foo'));
76         $this->assertSame('application/json', $response->headers->get('Content-Type'));
77         $this->assertSame('foo', $response->headers->get('ETag'));
78     }
79
80     public function testConstructorWithCustomContentType()
81     {
82         $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
83
84         $response = new JsonResponse(array(), 200, $headers);
85         $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
86     }
87
88     public function testSetJson()
89     {
90         $response = new JsonResponse('1', 200, array(), true);
91         $this->assertEquals('1', $response->getContent());
92
93         $response = new JsonResponse('[1]', 200, array(), true);
94         $this->assertEquals('[1]', $response->getContent());
95
96         $response = new JsonResponse(null, 200, array());
97         $response->setJson('true');
98         $this->assertEquals('true', $response->getContent());
99     }
100
101     public function testCreate()
102     {
103         $response = JsonResponse::create(array('foo' => 'bar'), 204);
104
105         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
106         $this->assertEquals('{"foo":"bar"}', $response->getContent());
107         $this->assertEquals(204, $response->getStatusCode());
108     }
109
110     public function testStaticCreateEmptyJsonObject()
111     {
112         $response = JsonResponse::create();
113         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
114         $this->assertSame('{}', $response->getContent());
115     }
116
117     public function testStaticCreateJsonArray()
118     {
119         $response = JsonResponse::create(array(0, 1, 2, 3));
120         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
121         $this->assertSame('[0,1,2,3]', $response->getContent());
122     }
123
124     public function testStaticCreateJsonObject()
125     {
126         $response = JsonResponse::create(array('foo' => 'bar'));
127         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
128         $this->assertSame('{"foo":"bar"}', $response->getContent());
129     }
130
131     public function testStaticCreateWithSimpleTypes()
132     {
133         $response = JsonResponse::create('foo');
134         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
135         $this->assertSame('"foo"', $response->getContent());
136
137         $response = JsonResponse::create(0);
138         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
139         $this->assertSame('0', $response->getContent());
140
141         $response = JsonResponse::create(0.1);
142         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
143         $this->assertSame('0.1', $response->getContent());
144
145         $response = JsonResponse::create(true);
146         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
147         $this->assertSame('true', $response->getContent());
148     }
149
150     public function testStaticCreateWithCustomStatus()
151     {
152         $response = JsonResponse::create(array(), 202);
153         $this->assertSame(202, $response->getStatusCode());
154     }
155
156     public function testStaticCreateAddsContentTypeHeader()
157     {
158         $response = JsonResponse::create();
159         $this->assertSame('application/json', $response->headers->get('Content-Type'));
160     }
161
162     public function testStaticCreateWithCustomHeaders()
163     {
164         $response = JsonResponse::create(array(), 200, array('ETag' => 'foo'));
165         $this->assertSame('application/json', $response->headers->get('Content-Type'));
166         $this->assertSame('foo', $response->headers->get('ETag'));
167     }
168
169     public function testStaticCreateWithCustomContentType()
170     {
171         $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
172
173         $response = JsonResponse::create(array(), 200, $headers);
174         $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
175     }
176
177     public function testSetCallback()
178     {
179         $response = JsonResponse::create(array('foo' => 'bar'))->setCallback('callback');
180
181         $this->assertEquals('/**/callback({"foo":"bar"});', $response->getContent());
182         $this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
183     }
184
185     public function testJsonEncodeFlags()
186     {
187         $response = new JsonResponse('<>\'&"');
188
189         $this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
190     }
191
192     public function testGetEncodingOptions()
193     {
194         $response = new JsonResponse();
195
196         $this->assertEquals(JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT, $response->getEncodingOptions());
197     }
198
199     public function testSetEncodingOptions()
200     {
201         $response = new JsonResponse();
202         $response->setData(array(array(1, 2, 3)));
203
204         $this->assertEquals('[[1,2,3]]', $response->getContent());
205
206         $response->setEncodingOptions(JSON_FORCE_OBJECT);
207
208         $this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
209     }
210
211     public function testItAcceptsJsonAsString()
212     {
213         $response = JsonResponse::fromJsonString('{"foo":"bar"}');
214         $this->assertSame('{"foo":"bar"}', $response->getContent());
215     }
216
217     /**
218      * @expectedException \InvalidArgumentException
219      */
220     public function testSetCallbackInvalidIdentifier()
221     {
222         $response = new JsonResponse('foo');
223         $response->setCallback('+invalid');
224     }
225
226     /**
227      * @expectedException \InvalidArgumentException
228      */
229     public function testSetContent()
230     {
231         JsonResponse::create("\xB1\x31");
232     }
233
234     /**
235      * @expectedException \Exception
236      * @expectedExceptionMessage This error is expected
237      */
238     public function testSetContentJsonSerializeError()
239     {
240         if (!interface_exists('JsonSerializable', false)) {
241             $this->markTestSkipped('JsonSerializable is required.');
242         }
243
244         $serializable = new JsonSerializableObject();
245
246         JsonResponse::create($serializable);
247     }
248
249     public function testSetComplexCallback()
250     {
251         $response = JsonResponse::create(array('foo' => 'bar'));
252         $response->setCallback('ಠ_ಠ["foo"].bar[0]');
253
254         $this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
255     }
256 }
257
258 if (interface_exists('JsonSerializable', false)) {
259     class JsonSerializableObject implements \JsonSerializable
260     {
261         public function jsonSerialize()
262         {
263             throw new \Exception('This error is expected');
264         }
265     }
266 }