Skip to content

Commit 5360635

Browse files
committed
test method Response setStatusCode
1 parent 76b665c commit 5360635

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

tests/unit/ResponseTest.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
use PhpRestfulApiResponse\Response;
1212
use PhpRestfulApiResponse\Tests\unit\Lib\Book;
13+
use ReflectionClass;
14+
use InvalidArgumentException;
1315

1416
class ResponseTest extends Base
1517
{
@@ -60,19 +62,6 @@ public function test_withCollection()
6062
$this->assertEquals('{"data":[{"title":"how to be a ninja","author":{"name":"harry","email":"harryosmarsitohang"},"year":2017,"price":100000},{"title":"how to be a mage","author":{"name":"harry","email":"harryosmarsitohang"},"year":2016,"price":500000},{"title":"how to be a samurai","author":{"name":"harry","email":"harryosmarsitohang"},"year":2000,"price":25000}]}', $response->getBody()->__toString());
6163
}
6264

63-
private function withError(Response $response, $code, $message = null)
64-
{
65-
$this->assertEquals($code, $response->getStatusCode());
66-
$this->assertEquals(json_encode([
67-
'error' => array_filter([
68-
'http_code' => $response->getStatusCode(),
69-
'phrase' => $response->getReasonPhrase(),
70-
'message' => $message
71-
])
72-
]), $response->getBody()->__toString());
73-
74-
}
75-
7665
public function test_withError()
7766
{
7867
$code = 400;
@@ -273,4 +262,46 @@ public function test_errorUnprocessable_with_message()
273262
$message
274263
);
275264
}
265+
266+
public function test_setStatusCode()
267+
{
268+
$responseReflect = new ReflectionClass(Response::class);
269+
$method = $responseReflect->getMethod('setStatusCode');
270+
$method->setAccessible(true);
271+
272+
try {
273+
$method->invokeArgs($this->response, [99]);
274+
} catch (InvalidArgumentException $exception) {
275+
$this->assertEquals(
276+
sprintf('Invalid status code "%s"; must be an integer between %d and %d, inclusive', 99, Response::MIN_STATUS_CODE_VALUE, Response::MAX_STATUS_CODE_VALUE),
277+
$exception->getMessage()
278+
);
279+
}
280+
281+
try {
282+
$method->invokeArgs($this->response, [600]);
283+
} catch (InvalidArgumentException $exception) {
284+
$this->assertEquals(
285+
sprintf('Invalid status code "%s"; must be an integer between %d and %d, inclusive', 600, Response::MIN_STATUS_CODE_VALUE, Response::MAX_STATUS_CODE_VALUE),
286+
$exception->getMessage()
287+
);
288+
}
289+
290+
$method->invokeArgs($this->response, [201]);
291+
292+
$this->assertEquals(201, $this->response->getStatusCode());
293+
}
294+
295+
private function withError(Response $response, $code, $message = null)
296+
{
297+
$this->assertEquals($code, $response->getStatusCode());
298+
$this->assertEquals(json_encode([
299+
'error' => array_filter([
300+
'http_code' => $response->getStatusCode(),
301+
'phrase' => $response->getReasonPhrase(),
302+
'message' => $message
303+
])
304+
]), $response->getBody()->__toString());
305+
306+
}
276307
}

0 commit comments

Comments
 (0)