Open Journal Systems  3.3.0
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 
13 
14 use PHPUnit\Framework\TestCase;
16 
17 class JsonResponseTest extends TestCase
18 {
20  {
21  $response = new JsonResponse();
22  $this->assertSame('{}', $response->getContent());
23  }
24 
26  {
27  $response = new JsonResponse(array(0, 1, 2, 3));
28  $this->assertSame('[0,1,2,3]', $response->getContent());
29  }
30 
32  {
33  $response = new JsonResponse(array('foo' => 'bar'));
34  $this->assertSame('{"foo":"bar"}', $response->getContent());
35  }
36 
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 
53  {
54  $response = new JsonResponse(array(), 202);
55  $this->assertSame(202, $response->getStatusCode());
56  }
57 
59  {
60  $response = new JsonResponse();
61  $this->assertSame('application/json', $response->headers->get('Content-Type'));
62  }
63 
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 
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 
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 
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 
142  {
143  $response = JsonResponse::create(array(), 202);
144  $this->assertSame(202, $response->getStatusCode());
145  }
146 
148  {
149  $response = JsonResponse::create();
150  $this->assertSame('application/json', $response->headers->get('Content-Type'));
151  }
152 
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 
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 
212  {
213  $response = new JsonResponse('foo');
214  $response->setCallback('+invalid');
215  }
216 
220  public function testSetContent()
221  {
222  JsonResponse::create("\xB1\x31");
223  }
224 
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 }
Symfony\Component\HttpFoundation\Tests\JsonResponseTest
Definition: JsonResponseTest.php:17
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testStaticCreateWithCustomHeaders
testStaticCreateWithCustomHeaders()
Definition: JsonResponseTest.php:153
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testConstructorWithCustomHeaders
testConstructorWithCustomHeaders()
Definition: JsonResponseTest.php:64
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testSetComplexCallback
testSetComplexCallback()
Definition: JsonResponseTest.php:236
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testItAcceptsJsonAsString
testItAcceptsJsonAsString()
Definition: JsonResponseTest.php:202
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testConstructorWithCustomContentType
testConstructorWithCustomContentType()
Definition: JsonResponseTest.php:71
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testConstructorWithCustomStatus
testConstructorWithCustomStatus()
Definition: JsonResponseTest.php:52
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testSetEncodingOptions
testSetEncodingOptions()
Definition: JsonResponseTest.php:190
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testSetContent
testSetContent()
Definition: JsonResponseTest.php:220
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testCreate
testCreate()
Definition: JsonResponseTest.php:92
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testConstructorEmptyCreatesJsonObject
testConstructorEmptyCreatesJsonObject()
Definition: JsonResponseTest.php:19
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testStaticCreateJsonArray
testStaticCreateJsonArray()
Definition: JsonResponseTest.php:108
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testSetContentJsonSerializeError
testSetContentJsonSerializeError()
Definition: JsonResponseTest.php:229
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testConstructorWithAssocArrayCreatesJsonObject
testConstructorWithAssocArrayCreatesJsonObject()
Definition: JsonResponseTest.php:31
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testGetEncodingOptions
testGetEncodingOptions()
Definition: JsonResponseTest.php:183
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testConstructorWithArrayCreatesJsonArray
testConstructorWithArrayCreatesJsonArray()
Definition: JsonResponseTest.php:25
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testConstructorWithSimpleTypes
testConstructorWithSimpleTypes()
Definition: JsonResponseTest.php:37
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testStaticCreateAddsContentTypeHeader
testStaticCreateAddsContentTypeHeader()
Definition: JsonResponseTest.php:147
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testSetCallback
testSetCallback()
Definition: JsonResponseTest.php:168
Symfony\Component\HttpFoundation\JsonResponse\fromJsonString
static fromJsonString($data=null, $status=200, $headers=array())
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:75
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testStaticCreateEmptyJsonObject
testStaticCreateEmptyJsonObject()
Definition: JsonResponseTest.php:101
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testStaticCreateJsonObject
testStaticCreateJsonObject()
Definition: JsonResponseTest.php:115
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testStaticCreateWithCustomContentType
testStaticCreateWithCustomContentType()
Definition: JsonResponseTest.php:160
Symfony\Component\HttpFoundation\JsonResponse\create
static create($data=null, $status=200, $headers=array())
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:67
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testSetJson
testSetJson()
Definition: JsonResponseTest.php:79
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testSetCallbackInvalidIdentifier
testSetCallbackInvalidIdentifier()
Definition: JsonResponseTest.php:211
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testConstructorAddsContentTypeHeader
testConstructorAddsContentTypeHeader()
Definition: JsonResponseTest.php:58
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testStaticCreateWithCustomStatus
testStaticCreateWithCustomStatus()
Definition: JsonResponseTest.php:141
Symfony\Component\HttpFoundation\Tests
Definition: AcceptHeaderItemTest.php:12
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testStaticCreateWithSimpleTypes
testStaticCreateWithSimpleTypes()
Definition: JsonResponseTest.php:122
Symfony\Component\HttpFoundation\JsonResponse
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:25
Symfony\Component\HttpFoundation\Tests\JsonResponseTest\testJsonEncodeFlags
testJsonEncodeFlags()
Definition: JsonResponseTest.php:176