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