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

Commit ab089c6

Browse files
committed
Merge pull request #236 from snapshotpl/inconsistences
Inconsistences
2 parents 817dcb9 + 6bf7529 commit ab089c6

File tree

4 files changed

+54
-40
lines changed

4 files changed

+54
-40
lines changed

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
*
@@ -159,21 +162,23 @@ public function withStatus($code, $reasonPhrase = '')
159162
}
160163

161164
/**
162-
* Validate a status code.
165+
* Set a valid status code.
163166
*
164-
* @param int|string $code
167+
* @param int $code
165168
* @throws InvalidArgumentException on an invalid status code.
166169
*/
167170
private function setStatusCode($code)
168171
{
169172
if (! is_numeric($code)
170173
|| is_float($code)
171-
|| $code < 100
172-
|| $code >= 600
174+
|| $code < static::MIN_STATUS_CODE_VALUE
175+
|| $code > static::MAX_STATUS_CODE_VALUE
173176
) {
174177
throw new InvalidArgumentException(sprintf(
175-
'Invalid status code "%s"; must be an integer between 100 and 599, inclusive',
176-
(is_scalar($code) ? $code : gettype($code))
178+
'Invalid status code "%s"; must be an integer between %d and %d, inclusive',
179+
(is_scalar($code) ? $code : gettype($code)),
180+
static::MIN_STATUS_CODE_VALUE,
181+
static::MAX_STATUS_CODE_VALUE
177182
));
178183
}
179184
$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
@@ -40,28 +40,6 @@ public function testStatusCodeMutatorReturnsCloneWithChanges()
4040
$this->assertEquals(400, $response->getStatusCode());
4141
}
4242

43-
public function invalidStatusCodes()
44-
{
45-
return [
46-
'too-low' => [99],
47-
'too-high' => [600],
48-
'null' => [null],
49-
'bool' => [true],
50-
'string' => ['foo'],
51-
'array' => [[200]],
52-
'object' => [(object) [200]],
53-
];
54-
}
55-
56-
/**
57-
* @dataProvider invalidStatusCodes
58-
*/
59-
public function testCannotSetInvalidStatusCode($code)
60-
{
61-
$this->setExpectedException('InvalidArgumentException');
62-
$response = $this->response->withStatus($code);
63-
}
64-
6543
public function testReasonPhraseDefaultsToStandards()
6644
{
6745
$response = $this->response->withStatus(422);
@@ -151,29 +129,60 @@ public function testConstructorCanAcceptAllMessageParts()
151129
$this->assertEquals($headers, $response->getHeaders());
152130
}
153131

154-
public function invalidStatus()
132+
/**
133+
* @dataProvider validStatusCodes
134+
*/
135+
public function testCreateWithValidStatusCodes($code)
136+
{
137+
$response = $this->response->withStatus($code);
138+
139+
$this->assertEquals($code, $response->getStatusCode());
140+
}
141+
142+
public function validStatusCodes()
155143
{
156144
return [
157-
'true' => [ true ],
158-
'false' => [ false ],
159-
'float' => [ 100.1 ],
160-
'bad-string' => [ 'Two hundred' ],
161-
'array' => [ [ 200 ] ],
162-
'object' => [ (object) [ 'statusCode' => 200 ] ],
163-
'too-small' => [ 1 ],
164-
'too-big' => [ 600 ],
145+
'minimum' => [100],
146+
'middle' => [300],
147+
'maximum' => [599],
165148
];
166149
}
167150

168151
/**
169-
* @dataProvider invalidStatus
152+
* @dataProvider invalidStatusCodes
170153
*/
171154
public function testConstructorRaisesExceptionForInvalidStatus($code)
172155
{
173156
$this->setExpectedException('InvalidArgumentException', 'Invalid status code');
157+
174158
new Response('php://memory', $code);
175159
}
176160

161+
/**
162+
* @dataProvider invalidStatusCodes
163+
*/
164+
public function testCannotSetInvalidStatusCode($code)
165+
{
166+
$this->setExpectedException('InvalidArgumentException');
167+
168+
$this->response->withStatus($code);
169+
}
170+
171+
public function invalidStatusCodes()
172+
{
173+
return [
174+
'true' => [ true ],
175+
'false' => [ false ],
176+
'array' => [ [ 200 ] ],
177+
'object' => [ (object) [ 'statusCode' => 200 ] ],
178+
'too-low' => [99],
179+
'float' => [400.5],
180+
'too-high' => [600],
181+
'null' => [null],
182+
'string' => ['foo'],
183+
];
184+
}
185+
177186
public function invalidResponseBody()
178187
{
179188
return [

0 commit comments

Comments
 (0)