diff options
author | minstel <minstel@yandex.ru> | 2016-10-18 00:37:08 +0300 |
---|---|---|
committer | minstel <minstel@yandex.ru> | 2016-10-18 00:37:08 +0300 |
commit | 73c52de728e0bbd5c1a6fa6cb7801de3639734c8 (patch) | |
tree | 4c5f78a1c33129ed1cca7356f9ffec905cd35c32 /tests/ControllerTest.php | |
parent | b51040901730a22c990c0f3a52baf8dcbefa0554 (diff) | |
download | controller-73c52de728e0bbd5c1a6fa6cb7801de3639734c8.zip controller-73c52de728e0bbd5c1a6fa6cb7801de3639734c8.tar.gz controller-73c52de728e0bbd5c1a6fa6cb7801de3639734c8.tar.bz2 |
Controller check response status methods
Diffstat (limited to 'tests/ControllerTest.php')
-rw-r--r-- | tests/ControllerTest.php | 84 |
1 files changed, 82 insertions, 2 deletions
diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index 9a4caef..8cdbf7d 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -12,8 +12,7 @@ class ControllerTest extends PHPUnit_Framework_TestCase public function testInvoke() { $controller = $this->getMockBuilder(Controller::class)->disableOriginalConstructor()->getMockForAbstractClass(); - $request = $this->createMock(ServerRequestInterface::class); - $response = $this->createMock(ResponseInterface::class); + list($request, $response) = $this->getRequests(); $controller->expects($this->once())->method('run')->will($this->returnValue($response)); @@ -23,4 +22,85 @@ class ControllerTest extends PHPUnit_Framework_TestCase $this->assertEquals($response, $controller->getResponse(), "Can not get 'ResponseInterface' instance from controller"); $this->assertEquals($request, $controller->getRequest(), "Can not get 'ServerRequestInterface' instance from controller"); } + + /** + * Test response status functions if response object is not set + */ + public function testResponseStatusEmptyResponse() + { + $controller = $this->getMockBuilder(Controller::class)->disableOriginalConstructor()->getMockForAbstractClass(); + $data = $this->getStatusCodesMap(null); + + foreach ($data as $func => $value) { + $this->assertEquals($value, $controller->$func(), "Method '$func' returns incorrect value"); + } + } + + /** + * Test functions that check response status code + * + * @dataProvider responseStatusProvider + * @param int $statusCode + */ + public function testResponseStatus($code) + { + $controller = $this->getMockBuilder(Controller::class)->disableOriginalConstructor()->getMockForAbstractClass(); + list($request, $response) = $this->getRequests(); + $response->method('getStatusCode')->will($this->returnValue($code)); + + $controller($request, $response); + + $data = $this->getStatusCodesMap($code); + + foreach ($data as $func => $value) { + $this->assertEquals($value, $controller->$func(), "Method '$func' returns incorrect value"); + } + + $this->assertEquals($data['isClientError'] || $data['isServerError'], $controller->isError(), "Method 'isError' returns incorrect value"); + } + + /** + * Provide data for testing status methods + * + * @return array + */ + public function responseStatusProvider() + { + return [ + [null], [199], + [200], [201], [299], + [300], [304], [399], + [400], [403], [499], + [500], [503] + ]; + } + + /** + * Get map of status codes to states + * + * @param int $code + * @return [] + */ + public function getStatusCodesMap($code) + { + return [ + 'isSuccessful' => !$code || ($code >= 200 && $code < 300), + 'isRedirection' => $code >= 300 && $code < 400, + 'isClientError' => $code >= 400 && $code < 500, + 'isServerError' => $code >= 500 + ]; + } + + /** + * Get request and response instances + * + * @return array + */ + public function getRequests() + { + return [ + $this->createMock(ServerRequestInterface::class), + $this->createMock(ResponseInterface::class) + ]; + } } |