From dc77ed182ac93e234ce20aee69b391e10132fc23 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Fri, 5 Aug 2011 00:06:18 -0400 Subject: [PATCH] * Add RestAdapaterTest * Refactor common HTTP test fixtures out into a new file --- http/rest_adapter.php | 2 +- testing/tests/http/fixtures.php | 47 +++++++ testing/tests/http/rest_action_test.php | 30 +---- testing/tests/http/rest_adapter_test.php | 150 +++++++++++++++++++++++ 4 files changed, 199 insertions(+), 30 deletions(-) create mode 100644 testing/tests/http/fixtures.php create mode 100644 testing/tests/http/rest_adapter_test.php diff --git a/http/rest_adapter.php b/http/rest_adapter.php index 2522b52..0329177 100644 --- a/http/rest_adapter.php +++ b/http/rest_adapter.php @@ -39,7 +39,7 @@ abstract class RestAdapter extends ActionController public function ActionFetch(Request $request, Response $response) { - if ($request->http_method != 'GET' || $request->http_method != 'POST') { + if ($request->http_method != 'GET' && $request->http_method != 'POST') { $response->response_code = ResponseCode::METHOD_NOT_ALLOWED; return; } diff --git a/testing/tests/http/fixtures.php b/testing/tests/http/fixtures.php new file mode 100644 index 0000000..fcd3082 --- /dev/null +++ b/testing/tests/http/fixtures.php @@ -0,0 +1,47 @@ +. + +namespace hoplite\test; +use \hoplite\http as http; + +class TestRestAction extends \hoplite\http\RestAction +{ + public $did_get = FALSE; + public $did_post = FALSE; + public $did_delete = FALSE; + public $did_put = FALSE; + + public function DoGet(http\Request $request, http\Response $response) + { + parent::DoGet($request, $response); + $this->did_get = TRUE; + } + public function DoPost(http\Request $request, http\Response $response) + { + parent::DoPost($request, $response); + $this->did_post = TRUE; + } + public function DoDelete(http\Request $request, http\Response $response) + { + parent::DoDelete($request, $response); + $this->did_delete = TRUE; + } + public function DoPut(http\Request $request, http\Response $response) + { + parent::DoPut($request, $response); + $this->did_put = TRUE; + } +} diff --git a/testing/tests/http/rest_action_test.php b/testing/tests/http/rest_action_test.php index 4c89d14..fb6c36b 100644 --- a/testing/tests/http/rest_action_test.php +++ b/testing/tests/http/rest_action_test.php @@ -19,35 +19,7 @@ use hoplite\http as http; require_once HOPLITE_ROOT . '/http/rest_action.php'; require_once HOPLITE_ROOT . '/http/root_controller.php'; - -class TestRestAction extends http\RestAction -{ - public $did_get = FALSE; - public $did_post = FALSE; - public $did_delete = FALSE; - public $did_put = FALSE; - - public function DoGet(http\Request $request, http\Response $response) - { - parent::DoGet($request, $response); - $this->did_get = TRUE; - } - public function DoPost(http\Request $request, http\Response $response) - { - parent::DoPost($request, $response); - $this->did_post = TRUE; - } - public function DoDelete(http\Request $request, http\Response $response) - { - parent::DoDelete($request, $response); - $this->did_delete = TRUE; - } - public function DoPut(http\Request $request, http\Response $response) - { - parent::DoPut($request, $response); - $this->did_put = TRUE; - } -} +require_once TEST_ROOT . '/tests/http/fixtures.php'; class RestActionTest extends \PHPUnit_Framework_TestCase { diff --git a/testing/tests/http/rest_adapter_test.php b/testing/tests/http/rest_adapter_test.php new file mode 100644 index 0000000..629d52a --- /dev/null +++ b/testing/tests/http/rest_adapter_test.php @@ -0,0 +1,150 @@ +. + +namespace hoplite\test; +use hoplite\http as http; + +require_once HOPLITE_ROOT . '/http/rest_action.php'; +require_once HOPLITE_ROOT . '/http/rest_adapter.php'; +require_once TEST_ROOT . '/tests/http/fixtures.php'; + +class TestRestAdapter extends http\RestAdapter +{ + protected function _GetRestAction() + { + return new TestRestAction($this->controller()); + } + + public function action() + { + return $this->action; + } +} + +class RestAdapterTest extends \PHPUnit_Framework_TestCase +{ + public function setUp() + { + $globals = array(); + $this->controller = new http\RootController($globals); + $this->fixture = new TestRestAdapter($this->controller); + $this->request = $this->controller->request(); + $this->response = $this->controller->response(); + } + + public function RestExpectSingleTrue($true_var) + { + $vars = array('did_get', 'did_post', 'did_delete', 'did_put'); + foreach ($vars as $var) + if ($var == $true_var) + $this->assertTrue($this->fixture->action()->$var); + else + $this->assertFalse($this->fixture->action()->$var); + } + + public function testFetchGet() + { + $this->request->http_method = 'GET'; + $this->request->data['action'] = 'fetch'; + $this->controller->InvokeAction($this->fixture); + $this->RestExpectSingleTrue('did_get'); + } + + public function testFetchPost() + { + $this->request->http_method = 'POST'; + $this->request->data['action'] = 'fetch'; + $this->controller->InvokeAction($this->fixture); + $this->RestExpectSingleTrue('did_get'); + } + + public function testFetchInvalid() + { + $this->request->http_method = 'PUT'; + $this->request->data['action'] = 'fetch'; + $this->controller->InvokeAction($this->fixture); + $this->RestExpectSingleTrue(NULL); + $this->assertEquals(http\ResponseCode::METHOD_NOT_ALLOWED, $this->response->response_code); + } + + public function testUpdate() + { + $this->request->http_method = 'POST'; + $this->request->data['action'] = 'update'; + $this->controller->InvokeAction($this->fixture); + $this->RestExpectSingleTrue('did_post'); + } + + public function testUpdateInvalid() + { + $this->request->http_method = 'GET'; + $this->request->data['action'] = 'update'; + $this->controller->InvokeAction($this->fixture); + $this->RestExpectSingleTrue(NULL); + $this->assertEquals(http\ResponseCode::METHOD_NOT_ALLOWED, $this->response->response_code); + } + + public function testDelete() + { + $this->request->http_method = 'POST'; + $this->request->data['action'] = 'delete'; + $this->controller->InvokeAction($this->fixture); + $this->RestExpectSingleTrue('did_delete'); + } + + public function testDeleteInvalid() + { + $this->request->http_method = 'GET'; + $this->request->data['action'] = 'delete'; + $this->controller->InvokeAction($this->fixture); + $this->RestExpectSingleTrue(NULL); + $this->assertEquals(http\ResponseCode::METHOD_NOT_ALLOWED, $this->response->response_code); + } + + public function testPut() + { + $this->request->http_method = 'POST'; + $this->request->data['action'] = 'insert'; + $this->controller->InvokeAction($this->fixture); + $this->RestExpectSingleTrue('did_put'); + } + + public function testInsertInvalid() + { + $this->request->http_method = 'GET'; + $this->request->data['action'] = 'insert'; + $this->controller->InvokeAction($this->fixture); + $this->RestExpectSingleTrue(NULL); + $this->assertEquals(http\ResponseCode::METHOD_NOT_ALLOWED, $this->response->response_code); + } + + public function testInvalid() + { + $globals = array(); + $mock = $this->getMock('hoplite\http\RootController', array('Stop'), array($globals)); + + $this->fixture = new TestRestAdapter($mock); + + $mock->expects($this->once()) + ->method('Stop'); + + $this->request->http_method = 'HEAD'; + $this->controller->InvokeAction($this->fixture); + $this->RestExpectSingleTrue('___none___'); + + $this->assertEquals(http\ResponseCode::NOT_FOUND, $this->response->response_code); + } +} -- 2.43.5