[HttpFoundation] Allow setting an unknown status code without specifying a text
This commit is contained in:
parent
50eb170ead
commit
ed8823c168
@ -254,7 +254,10 @@ class Response
|
|||||||
* Sets the response status code.
|
* Sets the response status code.
|
||||||
*
|
*
|
||||||
* @param integer $code HTTP status code
|
* @param integer $code HTTP status code
|
||||||
* @param string $text HTTP status text
|
* @param mixed $text HTTP status text
|
||||||
|
*
|
||||||
|
* If the status text is null it will be automatically populated for the known
|
||||||
|
* status codes and left empty otherwise.
|
||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException When the HTTP status code is not valid
|
* @throws \InvalidArgumentException When the HTTP status code is not valid
|
||||||
*
|
*
|
||||||
@ -262,12 +265,24 @@ class Response
|
|||||||
*/
|
*/
|
||||||
public function setStatusCode($code, $text = null)
|
public function setStatusCode($code, $text = null)
|
||||||
{
|
{
|
||||||
$this->statusCode = (int) $code;
|
$this->statusCode = $code = (int) $code;
|
||||||
if ($this->isInvalid()) {
|
if ($this->isInvalid()) {
|
||||||
throw new \InvalidArgumentException(sprintf('The HTTP status code "%s" is not valid.', $code));
|
throw new \InvalidArgumentException(sprintf('The HTTP status code "%s" is not valid.', $code));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->statusText = false === $text ? '' : (null === $text ? self::$statusTexts[$this->statusCode] : $text);
|
if (null === $text) {
|
||||||
|
$this->statusText = isset(self::$statusTexts[$code]) ? self::$statusTexts[$code] : '';
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (false === $text) {
|
||||||
|
$this->statusText = '';
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->statusText = $text;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -323,6 +323,33 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertFalse($response->isInvalid());
|
$this->assertFalse($response->isInvalid());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getStatusCodeFixtures
|
||||||
|
*/
|
||||||
|
public function testSetStatusCode($code, $text, $expectedText)
|
||||||
|
{
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$response->setStatusCode($code, $text);
|
||||||
|
|
||||||
|
$statusText = new \ReflectionProperty($response, 'statusText');
|
||||||
|
$statusText->setAccessible(true);
|
||||||
|
|
||||||
|
$this->assertEquals($expectedText, $statusText->getValue($response));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getStatusCodeFixtures()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('200', null, 'OK'),
|
||||||
|
array('200', false, ''),
|
||||||
|
array('200', 'foo', 'foo'),
|
||||||
|
array('199', null, ''),
|
||||||
|
array('199', false, ''),
|
||||||
|
array('199', 'foo', 'foo')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testIsInformational()
|
public function testIsInformational()
|
||||||
{
|
{
|
||||||
$response = new Response('', 100);
|
$response = new Response('', 100);
|
||||||
|
Reference in New Issue
Block a user