14 use PHPUnit\Framework\TestCase;
22 $this->assertSame(
'{}', $response->getContent());
28 $this->assertSame(
'[0,1,2,3]', $response->getContent());
34 $this->assertSame(
'{"foo":"bar"}', $response->getContent());
40 $this->assertSame(
'"foo"', $response->getContent());
43 $this->assertSame(
'0', $response->getContent());
46 $this->assertSame(
'0.1', $response->getContent());
49 $this->assertSame(
'true', $response->getContent());
55 $this->assertSame(202, $response->getStatusCode());
61 $this->assertSame(
'application/json', $response->headers->get(
'Content-Type'));
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'));
73 $headers = array(
'Content-Type' =>
'application/vnd.acme.blog-v1+json');
76 $this->assertSame(
'application/vnd.acme.blog-v1+json', $response->headers->get(
'Content-Type'));
82 $this->assertEquals(
'1', $response->getContent());
85 $this->assertEquals(
'[1]', $response->getContent());
88 $response->setJson(
'true');
89 $this->assertEquals(
'true', $response->getContent());
96 $this->assertInstanceOf(
'Symfony\Component\HttpFoundation\JsonResponse', $response);
97 $this->assertEquals(
'{"foo":"bar"}', $response->getContent());
98 $this->assertEquals(204, $response->getStatusCode());
104 $this->assertInstanceOf(
'Symfony\Component\HttpFoundation\JsonResponse', $response);
105 $this->assertSame(
'{}', $response->getContent());
111 $this->assertInstanceOf(
'Symfony\Component\HttpFoundation\JsonResponse', $response);
112 $this->assertSame(
'[0,1,2,3]', $response->getContent());
118 $this->assertInstanceOf(
'Symfony\Component\HttpFoundation\JsonResponse', $response);
119 $this->assertSame(
'{"foo":"bar"}', $response->getContent());
125 $this->assertInstanceOf(
'Symfony\Component\HttpFoundation\JsonResponse', $response);
126 $this->assertSame(
'"foo"', $response->getContent());
129 $this->assertInstanceOf(
'Symfony\Component\HttpFoundation\JsonResponse', $response);
130 $this->assertSame(
'0', $response->getContent());
133 $this->assertInstanceOf(
'Symfony\Component\HttpFoundation\JsonResponse', $response);
134 $this->assertSame(
'0.1', $response->getContent());
137 $this->assertInstanceOf(
'Symfony\Component\HttpFoundation\JsonResponse', $response);
138 $this->assertSame(
'true', $response->getContent());
144 $this->assertSame(202, $response->getStatusCode());
150 $this->assertSame(
'application/json', $response->headers->get(
'Content-Type'));
156 $this->assertSame(
'application/json', $response->headers->get(
'Content-Type'));
157 $this->assertSame(
'foo', $response->headers->get(
'ETag'));
162 $headers = array(
'Content-Type' =>
'application/vnd.acme.blog-v1+json');
165 $this->assertSame(
'application/vnd.acme.blog-v1+json', $response->headers->get(
'Content-Type'));
172 $this->assertEquals(
'/**/callback({"foo":"bar"});', $response->getContent());
173 $this->assertEquals(
'text/javascript', $response->headers->get(
'Content-Type'));
180 $this->assertEquals(
'"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
187 $this->assertEquals(JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT, $response->getEncodingOptions());
193 $response->setData(array(array(1, 2, 3)));
195 $this->assertEquals(
'[[1,2,3]]', $response->getContent());
197 $response->setEncodingOptions(JSON_FORCE_OBJECT);
199 $this->assertEquals(
'{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
205 $this->assertSame(
'{"foo":"bar"}', $response->getContent());
214 $response->setCallback(
'+invalid');
231 $serializable =
new JsonSerializableObject();
239 $response->setCallback(
'ಠ_ಠ["foo"].bar[0]');
241 $this->assertEquals(
'/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
245 if (interface_exists(
'JsonSerializable')) {
246 class JsonSerializableObject
implements \JsonSerializable
248 public function jsonSerialize()
250 throw new \Exception(
'This error is expected');