summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorminstel <minstel@yandex.ru>2016-10-21 01:18:26 +0300
committerminstel <minstel@yandex.ru>2016-10-21 01:18:26 +0300
commite465455424a7e1a44265bffd95589242e3642835 (patch)
tree8ef84f414683662dfcc751e67f257c6835e6ccfe
parent298f38a4347b8b29e3d145f8b5a0a24ea5775640 (diff)
downloadcontroller-e465455424a7e1a44265bffd95589242e3642835.zip
controller-e465455424a7e1a44265bffd95589242e3642835.tar.gz
controller-e465455424a7e1a44265bffd95589242e3642835.tar.bz2
Controller request method methods
-rw-r--r--src/Controller.php98
-rw-r--r--tests/ControllerTest.php69
2 files changed, 147 insertions, 20 deletions
diff --git a/src/Controller.php b/src/Controller.php
index ddec2c0..c480a07 100644
--- a/src/Controller.php
+++ b/src/Controller.php
@@ -111,25 +111,89 @@ abstract class Controller
}
/**
- * Check if response is 4xx or 5xx error
- *
- * @return boolean
- */
- public function isError()
- {
- return $this->isClientError() || $this->isServerError();
- }
-
- /**
- * Get status code of response
- *
- * @return int
- */
- protected function getResponseStatusCode()
- {
+ * Check if response is 4xx or 5xx error
+ *
+ * @return boolean
+ */
+ 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';
+ }
+
+ /**
+ * Get status code of response
+ *
+ * @return int
+ */
+ protected function getResponseStatusCode()
+ {
$response = $this->getResponse();
return $response ? $response->getStatusCode() : 0;
- }
+ }
+
+ /**
+ * 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 8cdbf7d..72c2f79 100644
--- a/tests/ControllerTest.php
+++ b/tests/ControllerTest.php
@@ -4,6 +4,9 @@ use Jasny\Controller;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
+/**
+ * @covers Jasny\Controller
+ */
class ControllerTest extends PHPUnit_Framework_TestCase
{
/**
@@ -11,7 +14,7 @@ class ControllerTest extends PHPUnit_Framework_TestCase
*/
public function testInvoke()
{
- $controller = $this->getMockBuilder(Controller::class)->disableOriginalConstructor()->getMockForAbstractClass();
+ $controller = $this->getController();
list($request, $response) = $this->getRequests();
$controller->expects($this->once())->method('run')->will($this->returnValue($response));
@@ -28,7 +31,7 @@ class ControllerTest extends PHPUnit_Framework_TestCase
*/
public function testResponseStatusEmptyResponse()
{
- $controller = $this->getMockBuilder(Controller::class)->disableOriginalConstructor()->getMockForAbstractClass();
+ $controller = $this->getController();
$data = $this->getStatusCodesMap(null);
foreach ($data as $func => $value) {
@@ -44,7 +47,7 @@ class ControllerTest extends PHPUnit_Framework_TestCase
*/
public function testResponseStatus($code)
{
- $controller = $this->getMockBuilder(Controller::class)->disableOriginalConstructor()->getMockForAbstractClass();
+ $controller = $this->getController();
list($request, $response) = $this->getRequests();
$response->method('getStatusCode')->will($this->returnValue($code));
@@ -76,6 +79,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']
+ ];
+ }
+
+ /**
* Get map of status codes to states
*
* @param int $code
@@ -92,6 +128,33 @@ class ControllerTest extends PHPUnit_Framework_TestCase
}
/**
+ * Get map of request methods
+ *
+ * @param string $method
+ * @return array
+ */
+ public function getMethodsMap($method)
+ {
+ return [
+ 'isGetRequest' => $method === 'GET',
+ 'isPostRequest' => $method === 'POST',
+ 'isPutRequest' => $method === 'PUT',
+ 'isDeleteRequest' => $method === 'DELETE',
+ 'isHeadRequest' => $method === 'HEAD'
+ ];
+ }
+
+ /**
+ * Get controller instance
+ *
+ * @return Controller
+ */
+ public function getController()
+ {
+ return $this->getMockBuilder(Controller::class)->disableOriginalConstructor()->getMockForAbstractClass();
+ }
+
+ /**
* Get request and response instances
*
* @return array