10000 [HttpFoundation] Add create method to Json & Redirect responses · symfony/symfony@ff13528 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff13528

Browse files
committed
[HttpFoundation] Add create method to Json & Redirect responses
1 parent 1c86ad7 commit ff13528

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/Symfony/Component/HttpFoundation/JsonResponse.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,12 @@ public function __construct($data = array(), $status = 200, $headers = array())
3838
array_merge(array('Content-Type' => 'application/json'), $headers)
3939
);
4040
}
41+
42+
/**
43+
* {@inheritDoc}
44+
*/
45+
public static function create($data = array(), $status = 200, $headers = array())
46+
{
47+
return new static($data, $status, $headers);
48+
}
4149
}

src/Symfony/Component/HttpFoundation/RedirectResponse.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ public function __construct($url, $status = 302, $headers = array())
6363
}
6464
}
6565

66+
/**
67+
* {@inheritDoc}
68+
*/
69+
public static function create($url = '', $status = 302, $headers = array())
70+
{
71+
return new static($url, $status, $headers);
72+
}
73+
6674
/**
6775
* Returns the target URL.
6876
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,13 @@ public function testConstructorWithCustomContentType()
7979
$response = new JsonResponse(array(), 200, $headers);
8080
$this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
8181
}
82+
83+
public function testCreate()
84+
{
85+
$response = JsonResponse::create(array('foo' => 'bar'), 204);
86+
87+
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
88+
$this->assertEquals('{"foo":"bar"}', $response->getContent());
89+
$this->assertEquals(204, $response->getStatusCode());
90+
}
8291
}

tests/Symfony/Tests/Component/HttpFoundation/RedirectResponseTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,11 @@ public function testGetTargetUrl()
4040
$this->assertEquals('foo.bar', $response->getTargetUrl());
4141
}
4242

43+
public function testCreate()
44+
{
45+
$response = RedirectResponse::create('foo', 301);
46+
47+
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
48+
$this->assertEquals(301, $response->getStatusCode());
49+
}
4350
}

0 commit comments

Comments
 (0)
0