[HttpFoundation] Add create method to Json & Redirect responses

This commit is contained in:
Jordi Boggiano 2012-03-15 16:28:15 +01:00
parent 1c86ad78ee
commit ff13528ad0
4 changed files with 32 additions and 0 deletions

View File

@ -38,4 +38,12 @@ class JsonResponse extends Response
array_merge(array('Content-Type' => 'application/json'), $headers)
);
}
/**
* {@inheritDoc}
*/
public static function create($data = array(), $status = 200, $headers = array())
{
return new static($data, $status, $headers);
}
}

View File

@ -63,6 +63,14 @@ class RedirectResponse extends Response
}
}
/**
* {@inheritDoc}
*/
public static function create($url = '', $status = 302, $headers = array())
{
return new static($url, $status, $headers);
}
/**
* Returns the target URL.
*

View File

@ -79,4 +79,13 @@ class JsonResponseTest extends \PHPUnit_Framework_TestCase
$response = new JsonResponse(array(), 200, $headers);
$this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
}
public function testCreate()
{
$response = JsonResponse::create(array('foo' => 'bar'), 204);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertEquals('{"foo":"bar"}', $response->getContent());
$this->assertEquals(204, $response->getStatusCode());
}
}

View File

@ -40,4 +40,11 @@ class RedirectResponseTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('foo.bar', $response->getTargetUrl());
}
public function testCreate()
{
$response = RedirectResponse::create('foo', 301);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertEquals(301, $response->getStatusCode());
}
}