Yaffs site version 1.1
[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 testCreate()
80     {
81         $response = JsonResponse::create(array('foo' => 'bar'), 204);
82
83         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
84         $this->assertEquals('{"foo":"bar"}', $response->getContent());
85         $this->assertEquals(204, $response->getStatusCode());
86     }
87
88     public function testStaticCreateEmptyJsonObject()
89     {
90         $response = JsonResponse::create();
91         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
92         $this->assertSame('{}', $response->getContent());
93     }
94
95     public function testStaticCreateJsonArray()
96     {
97         $response = JsonResponse::create(array(0, 1, 2, 3));
98         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
99         $this->assertSame('[0,1,2,3]', $response->getContent());
100     }
101
102     public function testStaticCreateJsonObject()
103     {
104         $response = JsonResponse::create(array('foo' => 'bar'));
105         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
106         $this->assertSame('{"foo":"bar"}', $response->getContent());
107     }
108
109     public function testStaticCreateWithSimpleTypes()
110     {
111         $response = JsonResponse::create('foo');
112         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
113         $this->assertSame('"foo"', $response->getContent());
114
115         $response = JsonResponse::create(0);
116         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
117         $this->assertSame('0', $response->getContent());
118
119         $response = JsonResponse::create(0.1);
120         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
121         $this->assertSame('0.1', $response->getContent());
122
123         $response = JsonResponse::create(true);
124         $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
125         $this->assertSame('true', $response->getContent());
126     }
127
128     public function testStaticCreateWithCustomStatus()
129     {
130         $response = JsonResponse::create(array(), 202);
131         $this->assertSame(202, $response->getStatusCode());
132     }
133
134     public function testStaticCreateAddsContentTypeHeader()
135     {
136         $response = JsonResponse::create();
137         $this->assertSame('application/json', $response->headers->get('Content-Type'));
138     }
139
140     public function testStaticCreateWithCustomHeaders()
141     {
142         $response = JsonResponse::create(array(), 200, array('ETag' => 'foo'));
143         $this->assertSame('application/json', $response->headers->get('Content-Type'));
144         $this->assertSame('foo', $response->headers->get('ETag'));
145     }
146
147     public function testStaticCreateWithCustomContentType()
148     {
149         $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
150
151         $response = JsonResponse::create(array(), 200, $headers);
152         $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
153     }
154
155     public function testSetCallback()
156     {
157         $response = JsonResponse::create(array('foo' => 'bar'))->setCallback('callback');
158
159         $this->assertEquals('/**/callback({"foo":"bar"});', $response->getContent());
160         $this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
161     }
162
163     public function testJsonEncodeFlags()
164     {
165         $response = new JsonResponse('<>\'&"');
166
167         $this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
168     }
169
170     public function testGetEncodingOptions()
171     {
172         $response = new JsonResponse();
173
174         $this->assertEquals(JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT, $response->getEncodingOptions());
175     }
176
177     public function testSetEncodingOptions()
178     {
179         $response = new JsonResponse();
180         $response->setData(array(array(1, 2, 3)));
181
182         $this->assertEquals('[[1,2,3]]', $response->getContent());
183
184         $response->setEncodingOptions(JSON_FORCE_OBJECT);
185
186         $this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
187     }
188
189     /**
190      * @expectedException \InvalidArgumentException
191      */
192     public function testSetCallbackInvalidIdentifier()
193     {
194         $response = new JsonResponse('foo');
195         $response->setCallback('+invalid');
196     }
197
198     /**
199      * @expectedException \InvalidArgumentException
200      */
201     public function testSetContent()
202     {
203         JsonResponse::create("\xB1\x31");
204     }
205
206     /**
207      * @expectedException \Exception
208      * @expectedExceptionMessage This error is expected
209      * @requires PHP 5.4
210      */
211     public function testSetContentJsonSerializeError()
212     {
213         $serializable = new JsonSerializableObject();
214
215         JsonResponse::create($serializable);
216     }
217
218     public function testSetComplexCallback()
219     {
220         $response = JsonResponse::create(array('foo' => 'bar'));
221         $response->setCallback('ಠ_ಠ["foo"].bar[0]');
222
223         $this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
224     }
225 }
226
227 if (interface_exists('JsonSerializable')) {
228     class JsonSerializableObject implements \JsonSerializable
229     {
230         public function jsonSerialize()
231         {
232             throw new \Exception('This error is expected');
233         }
234     }
235 }