diff options
author | Arnold Daniels <arnold@jasny.net> | 2016-11-17 16:05:57 +0100 |
---|---|---|
committer | Arnold Daniels <arnold@jasny.net> | 2016-11-17 16:05:57 +0100 |
commit | 14bc7de53fda2c44482c7cf46b896cfdd41aa68d (patch) | |
tree | 07fe440ef3febcc8ec9a1e702388cc7bab134b6a | |
parent | 75b13f1c90828f9593a68caf1a35c6e12ea9189d (diff) | |
parent | 87578c0415e85b7021a47a334e66c3e843ea9260 (diff) | |
download | controller-14bc7de53fda2c44482c7cf46b896cfdd41aa68d.zip controller-14bc7de53fda2c44482c7cf46b896cfdd41aa68d.tar.gz controller-14bc7de53fda2c44482c7cf46b896cfdd41aa68d.tar.bz2 |
Merge branch 'Minstel-Controller_request_method_methods'
-rw-r--r-- | src/Controller.php | 64 | ||||
-rw-r--r-- | tests/ControllerTest.php | 66 |
2 files changed, 124 insertions, 6 deletions
diff --git a/src/Controller.php b/src/Controller.php index 93853d1..057653e 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -337,6 +337,58 @@ abstract class Controller public function isError() { return $this->isClientError() || $this->isServerError(); + } + + /** + * Check if request is GET request + * + * @return boolean + */ + public function isGetRequest() + { + $method = $this->getRequestMethod(); + + return !$method || $method === 'GET'; + } + + /** + * Check if request is POST request + * + * @return boolean + */ + public function isPostRequest() + { + return $this->getRequestMethod() === 'POST'; + } + + /** + * Check if request is PUT request + * + * @return boolean + */ + public function isPutRequest() + { + return $this->getRequestMethod() === 'PUT'; + } + + /** + * Check if request is DELETE request + * + * @return boolean + */ + public function isDeleteRequest() + { + return $this->getRequestMethod() === 'DELETE'; + } + + /** + * Check if request is HEAD request + * + * @return boolean + */ + public function isHeadRequest() + { + return $this->getRequestMethod() === 'HEAD'; } /** @@ -451,5 +503,17 @@ abstract class Controller { return array_search($format, $this->contentFormats) ?: $format; } + + /** + * Get method of request + * + * @return string + */ + protected function getRequestMethod() + { + $request = $this->getRequest(); + + return $request ? $request->getMethod() : ''; + } } diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index f50e892..e0ebdc2 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -5,6 +5,9 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; +/** + * @covers Jasny\Controller + */ class ControllerTest extends PHPUnit_Framework_TestCase { /** @@ -41,7 +44,7 @@ class ControllerTest extends PHPUnit_Framework_TestCase * Test functions that check response status code * * @dataProvider responseStatusProvider - * @param int $statusCode + * @param int status code */ public function testResponseStatus($code) { @@ -57,7 +60,8 @@ class ControllerTest extends PHPUnit_Framework_TestCase $this->assertEquals($value, $controller->$func(), "Method '$func' returns incorrect value"); } - $this->assertEquals($data['isClientError'] || $data['isServerError'], $controller->isError(), "Method 'isError' returns incorrect value"); + $this->assertEquals($data['isClientError'] || $data['isServerError'], $controller->isError() + , "Method 'isError' returns incorrect value"); } /** @@ -77,6 +81,39 @@ class ControllerTest extends PHPUnit_Framework_TestCase } /** + * Test functions that check request method + * + * @dataProvider requestMethodProvider + * @param string $method + */ + public function testRequestMethod($method) + { + $controller = $this->getController(); + list($request, $response) = $this->getRequests(); + $request->method('getMethod')->will($this->returnValue($method)); + + $controller($request, $response); + + $data = $this->getMethodsMap($method); + + foreach ($data as $func => $value) { + $this->assertEquals($value, $controller->$func(), "Method '$func' returns incorrect value"); + } + } + + /** + * Provide data for testing functions that determine request method + * + * @return array + */ + public function requestMethodProvider() + { + return [ + ['GET'], ['POST'], ['PUT'], ['DELETE'], ['HEAD'] + ]; + } + + /** * Test encodeData method, positive tests * * @dataProvider encodeDataPositiveProvider @@ -616,6 +653,19 @@ class ControllerTest extends PHPUnit_Framework_TestCase } /** + * Get request and response instances + * + * @return array + */ + public function getRequests() + { + return [ + $this->createMock(ServerRequestInterface::class), + $this->createMock(ResponseInterface::class) + ]; + } + + /** * Get map of status codes to states * * @param int $code @@ -632,15 +682,19 @@ class ControllerTest extends PHPUnit_Framework_TestCase } /** - * Get request and response instances + * Get map of request methods * + * @param string $method * @return array */ - public function getRequests() + public function getMethodsMap($method) { return [ - $this->createMock(ServerRequestInterface::class), - $this->createMock(ResponseInterface::class) + 'isGetRequest' => $method === 'GET', + 'isPostRequest' => $method === 'POST', + 'isPutRequest' => $method === 'PUT', + 'isDeleteRequest' => $method === 'DELETE', + 'isHeadRequest' => $method === 'HEAD' ]; } } |