Skip to content

Commit 3036b00

Browse files
author
Michal Piotrowski
committed
JsonResponseTest
1 parent 2cf50b7 commit 3036b00

File tree

1 file changed

+67
-5
lines changed

1 file changed

+67
-5
lines changed

src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313

1414
use Symfony\Component\HttpFoundation\JsonResponse;
1515

16-
/**
17-
* @covers Symfony\Component\HttpFoundation\JsonResponse::__construct
18-
* @covers Symfony\Component\HttpFoundation\JsonResponse::setData
19-
* @covers Symfony\Component\HttpFoundation\JsonResponse::setCallback
20-
*/
2116
class JsonResponseTest extends \PHPUnit_Framework_TestCase
2217
{
2318
public function testConstructorEmptyCreatesJsonObject()
@@ -89,6 +84,73 @@ public function testCreate()
8984
$this->assertEquals(204, $response->getStatusCode());
9085
}
9186

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+
92154
public function testSetCallback()
93155
{
94156
$response = JsonResponse::create(array('foo' => 'bar'))->setCallback('callback');

0 commit comments

Comments
 (0)