Skip to content

Commit

Permalink
Reroll Pull request 334 guzzle#334
Browse files Browse the repository at this point in the history
  • Loading branch information
olstjos committed May 11, 2021
1 parent 148492f commit 1e10e0e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ private function assertStatusCodeIsInteger($statusCode): void

private function assertStatusCodeRange(int $statusCode): void
{
if ($statusCode < 100 || $statusCode >= 600) {
throw new \InvalidArgumentException('Status code must be an integer value between 1xx and 5xx.');
if ($statusCode < 100 || $statusCode > 999) {
throw new \InvalidArgumentException('Status code must be an integer value between 1xx and 9xx.');
}
}
}
46 changes: 35 additions & 11 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,32 @@ public function testDefaultConstructor(): void
self::assertSame('', (string) $r->getBody());
}

public function testCanConstructWithStatusCode(): void
/**
* @dataProvider statusCodeProvider
*/
public function testCanConstructWithStatusCode(int $statusCode, string $reasonPhrase = '')
{
$r = new Response(404);
self::assertSame(404, $r->getStatusCode());
self::assertSame('Not Found', $r->getReasonPhrase());
$r = new Response($statusCode);
self::assertSame($statusCode, $r->getStatusCode());
self::assertSame($reasonPhrase, $r->getReasonPhrase());
}

/**
* Although some of the status codes below seem invalid a status code can be above
*/
public function statusCodeProvider(): array
{
return [
[100, 'Continue'],
[200, 'OK'],
[201, 'Created'],
[401, 'Unauthorized'],
[404, 'Not Found'],
[500, 'Internal Server Error'],
[503, 'Service Unavailable'],
[600],
[999],
];
}

public function testConstructorDoesNotReadStreamBody(): void
Expand Down Expand Up @@ -110,11 +131,14 @@ public function testCanConstructWithProtocolVersion(): void
self::assertSame('1000', $r->getProtocolVersion());
}

public function testWithStatusCodeAndNoReason(): void
/**
* @dataProvider statusCodeProvider
*/
public function testWithStatusCodeAndNoReason(int $statusCode, string $reasonPhrase = '')
{
$r = (new Response())->withStatus(201);
self::assertSame(201, $r->getStatusCode());
self::assertSame('Created', $r->getReasonPhrase());
$r = (new Response())->withStatus($statusCode);
self::assertSame($statusCode, $r->getStatusCode());
self::assertSame($reasonPhrase, $r->getReasonPhrase());
}

public function testWithStatusCodeAndReason(): void
Expand Down Expand Up @@ -354,7 +378,7 @@ public function nonIntegerStatusCodeProvider(): iterable
public function testConstructResponseWithInvalidRangeStatusCode($invalidValues): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Status code must be an integer value between 1xx and 5xx.');
$this->expectExceptionMessage('Status code must be an integer value between 1xx and 9xx.');
new Response($invalidValues);
}

Expand All @@ -367,14 +391,14 @@ public function testResponseChangeStatusCodeWithWithInvalidRange($invalidValues)
{
$response = new Response();
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Status code must be an integer value between 1xx and 5xx.');
$this->expectExceptionMessage('Status code must be an integer value between 1xx and 9xx.');
$response->withStatus($invalidValues);
}

public function invalidStatusCodeRangeProvider(): iterable
{
return [
[600],
[1],
[99],
];
}
Expand Down

0 comments on commit 1e10e0e

Please sign in to comment.