Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge 4e5d88a into 7561382
Browse files Browse the repository at this point in the history
  • Loading branch information
snapshotpl committed Jul 19, 2018
2 parents 7561382 + 4e5d88a commit b58b338
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Response implements ResponseInterface
/**
* @var string
*/
private $reasonPhrase = '';
private $reasonPhrase;

/**
* @var int
Expand Down Expand Up @@ -146,12 +146,6 @@ public function getStatusCode()
*/
public function getReasonPhrase()
{
if (! $this->reasonPhrase
&& isset($this->phrases[$this->statusCode])
) {
$this->reasonPhrase = $this->phrases[$this->statusCode];
}

return $this->reasonPhrase;
}

Expand All @@ -161,18 +155,18 @@ public function getReasonPhrase()
public function withStatus($code, $reasonPhrase = '')
{
$new = clone $this;
$new->setStatusCode($code);
$new->reasonPhrase = $reasonPhrase;
$new->setStatusCode($code, $reasonPhrase);
return $new;
}

/**
* Set a valid status code.
*
* @param int $code
* @param string $reasonPhrase
* @throws InvalidArgumentException on an invalid status code.
*/
private function setStatusCode($code)
private function setStatusCode($code, $reasonPhrase = '')
{
if (! is_numeric($code)
|| is_float($code)
Expand All @@ -181,11 +175,24 @@ private function setStatusCode($code)
) {
throw new InvalidArgumentException(sprintf(
'Invalid status code "%s"; must be an integer between %d and %d, inclusive',
(is_scalar($code) ? $code : gettype($code)),
is_scalar($code) ? $code : gettype($code),
static::MIN_STATUS_CODE_VALUE,
static::MAX_STATUS_CODE_VALUE
));
}

if (! is_string($reasonPhrase)) {
throw new InvalidArgumentException(sprintf(
'Unsupported response reason phrase; must be a string, received %s',
is_object($reasonPhrase) ? get_class($reasonPhrase) : gettype($reasonPhrase)
));
}

if ($reasonPhrase === '' && isset($this->phrases[$code])) {
$reasonPhrase = $this->phrases[$code];
}

$this->reasonPhrase = $reasonPhrase;
$this->statusCode = $code;
}
}
23 changes: 23 additions & 0 deletions test/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,29 @@ public function testCanSetCustomReasonPhrase()
$this->assertSame('Foo Bar!', $response->getReasonPhrase());
}

public function invalidReasonPhrases()
{
return [
'true' => [ true ],
'false' => [ false ],
'array' => [ [ 200 ] ],
'object' => [ (object) [ 'reasonPhrase' => 'Ok' ] ],
'integer' => [99],
'float' => [400.5],
'null' => [null],
];
}

/**
* @dataProvider invalidReasonPhrases
*/
public function testWithStatusRaisesAnExceptionForNonStringReasonPhrases($invalidReasonPhrase)
{
$this->expectException(InvalidArgumentException::class);

$this->response->withStatus(422, $invalidReasonPhrase);
}

public function testConstructorRaisesExceptionForInvalidStream()
{
$this->expectException(InvalidArgumentException::class);
Expand Down

0 comments on commit b58b338

Please sign in to comment.