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

Commit 0d462e7

Browse files
committed
Fix inconsistences in phpdocs and tests
1 parent a60da17 commit 0d462e7

File tree

5 files changed

+55
-40
lines changed

5 files changed

+55
-40
lines changed

src/Request.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Psr\Http\Message\RequestInterface;
1313
use Psr\Http\Message\StreamInterface;
14+
use Psr\Http\Message\UriInterface;
1415

1516
/**
1617
* HTTP Request encapsulation

src/Response.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class Response implements ResponseInterface
2424
{
2525
use MessageTrait;
2626

27+
const MIN_STATUS_CODE_VALUE = 100;
28+
const MAX_STATUS_CODE_VALUE = 599;
29+
2730
/**
2831
* Map of standard HTTP status code/reason phrases
2932
*
@@ -154,21 +157,23 @@ public function withStatus($code, $reasonPhrase = '')
154157
}
155158

156159
/**
157-
* Validate a status code.
160+
* Set a valid status code.
158161
*
159-
* @param int|string $code
162+
* @param int $code
160163
* @throws InvalidArgumentException on an invalid status code.
161164
*/
162165
private function setStatusCode($code)
163166
{
164167
if (! is_numeric($code)
165168
|| is_float($code)
166-
|| $code < 100
167-
|| $code >= 600
169+
|| $code < static::MIN_STATUS_CODE_VALUE
170+
|| $code > static::MAX_STATUS_CODE_VALUE
168171
) {
169172
throw new InvalidArgumentException(sprintf(
170-
'Invalid status code "%s"; must be an integer between 100 and 599, inclusive',
171-
(is_scalar($code) ? $code : gettype($code))
173+
'Invalid status code "%s"; must be an integer between %d and %d, inclusive',
174+
(is_scalar($code) ? $code : gettype($code)),
175+
static::MIN_STATUS_CODE_VALUE,
176+
static::MAX_STATUS_CODE_VALUE
172177
));
173178
}
174179
$this->statusCode = $code;

src/Response/Serializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function fromString($message)
3737
* Parse a response from a stream.
3838
*
3939
* @param StreamInterface $stream
40-
* @return ResponseInterface
40+
* @return Response
4141
* @throws InvalidArgumentException when the stream is not readable.
4242
* @throws UnexpectedValueException when errors occur parsing the message.
4343
*/

src/Stream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class Stream implements StreamInterface
2020
{
2121
/**
22-
* @var resource
22+
* @var resource|null
2323
*/
2424
protected $resource;
2525

test/ResponseTest.php

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,6 @@ public function testStatusCodeMutatorReturnsCloneWithChanges()
3838
$this->assertEquals(400, $response->getStatusCode());
3939
}
4040

41-
public function invalidStatusCodes()
42-
{
43-
return [
44-
'too-low' => [99],
45-
'too-high' => [600],
46-
'null' => [null],
47-
'bool' => [true],
48-
'string' => ['foo'],
49-
'array' => [[200]],
50-
'object' => [(object) [200]],
51-
];
52-
}
53-
54-
/**
55-
* @dataProvider invalidStatusCodes
56-
*/
57-
public function testCannotSetInvalidStatusCode($code)
58-
{
59-
$this->setExpectedException('InvalidArgumentException');
60-
$response = $this->response->withStatus($code);
61-
}
62-
6341
public function testReasonPhraseDefaultsToStandards()
6442
{
6543
$response = $this->response->withStatus(422);
@@ -92,29 +70,60 @@ public function testConstructorCanAcceptAllMessageParts()
9270
$this->assertEquals($headers, $response->getHeaders());
9371
}
9472

95-
public function invalidStatus()
73+
/**
74+
* @dataProvider validStatusCodes
75+
*/
76+
public function testCreateWithValidStatusCodes($code)
77+
{
78+
$response = $this->response->withStatus($code);
79+
80+
$this->assertEquals($code, $response->getStatusCode());
81+
}
82+
83+
public function validStatusCodes()
9684
{
9785
return [
98-
'true' => [ true ],
99-
'false' => [ false ],
100-
'float' => [ 100.1 ],
101-
'bad-string' => [ 'Two hundred' ],
102-
'array' => [ [ 200 ] ],
103-
'object' => [ (object) [ 'statusCode' => 200 ] ],
104-
'too-small' => [ 1 ],
105-
'too-big' => [ 600 ],
86+
'minimum' => [100],
87+
'middle' => [300],
88+
'maximum' => [599],
10689
];
10790
}
10891

10992
/**
110-
* @dataProvider invalidStatus
93+
* @dataProvider invalidStatusCodes
11194
*/
11295
public function testConstructorRaisesExceptionForInvalidStatus($code)
11396
{
11497
$this->setExpectedException('InvalidArgumentException', 'Invalid status code');
98+
11599
new Response('php://memory', $code);
116100
}
117101

102+
/**
103+
* @dataProvider invalidStatusCodes
104+
*/
105+
public function testCannotSetInvalidStatusCode($code)
106+
{
107+
$this->setExpectedException('InvalidArgumentException');
108+
109+
$this->response->withStatus($code);
110+
}
111+
112+
public function invalidStatusCodes()
113+
{
114+
return [
115+
'true' => [ true ],
116+
'false' => [ false ],
117+
'array' => [ [ 200 ] ],
118+
'object' => [ (object) [ 'statusCode' => 200 ] ],
119+
'too-low' => [99],
120+
'float' => [400.5],
121+
'too-high' => [600],
122+
'null' => [null],
123+
'string' => ['foo'],
124+
];
125+
}
126+
118127
public function invalidResponseBody()
119128
{
120129
return [

0 commit comments

Comments
 (0)