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