From 97ebdc1e0d69827a7bb1d603201a010ac47f13d9 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 12 Jun 2011 10:45:34 -0400 Subject: [PATCH] Add a test for RestAction --- http/rest_action.php | 6 +- testing/tests/http/rest_action_test.php | 116 ++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 testing/tests/http/rest_action_test.php diff --git a/http/rest_action.php b/http/rest_action.php index 9300d4c..91170be 100644 --- a/http/rest_action.php +++ b/http/rest_action.php @@ -33,13 +33,13 @@ class RestAction extends Action $valid_methods = array('get', 'post', 'delete', 'put'); $method = strtolower($request->http_method); if (!in_array($method, $valid_methods)) { - $response->http_code = ResponseCode::METHOD_NOT_ALLOWED; + $response->response_code = ResponseCode::METHOD_NOT_ALLOWED; $this->controller()->Stop(); return; } - $invoke = 'Do' . ucwords($method); - $this->$invoke(); + $invoke = '_Do' . ucwords($method); + $this->$invoke($request, $response); } /*! Methods for each of the different HTTP methods. */ diff --git a/testing/tests/http/rest_action_test.php b/testing/tests/http/rest_action_test.php new file mode 100644 index 0000000..cf39869 --- /dev/null +++ b/testing/tests/http/rest_action_test.php @@ -0,0 +1,116 @@ +. + +namespace hoplite\test; +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; + + protected function _DoGet(http\Request $request, http\Response $response) + { + parent::_DoGet($request, $response); + $this->did_get = TRUE; + } + protected function _DoPost(http\Request $request, http\Response $response) + { + parent::_DoPost($request, $response); + $this->did_post = TRUE; + } + protected function _DoDelete(http\Request $request, http\Response $response) + { + parent::_DoDelete($request, $response); + $this->did_delete = TRUE; + } + protected function _DoPut(http\Request $request, http\Response $response) + { + parent::_DoPut($request, $response); + $this->did_put = TRUE; + } +} + +class RestActionTest extends \PHPUnit_Framework_TestCase +{ + public function setUp() + { + $globals = array(); + $this->fixture = new TestRestAction(new http\RootController($globals)); + $this->request = new http\Request(); + $this->response = new http\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->$var); + else + $this->assertFalse($this->fixture->$var); + } + + public function testGet() + { + $this->request->http_method = 'GET'; + $this->fixture->Invoke($this->request, $this->response); + $this->RestExpectSingleTrue('did_get'); + } + + public function testPost() + { + $this->request->http_method = 'POST'; + $this->fixture->Invoke($this->request, $this->response); + $this->RestExpectSingleTrue('did_post'); + } + + public function testDelete() + { + $this->request->http_method = 'DELETE'; + $this->fixture->Invoke($this->request, $this->response); + $this->RestExpectSingleTrue('did_delete'); + } + + public function testPut() + { + $this->request->http_method = 'PUT'; + $this->fixture->Invoke($this->request, $this->response); + $this->RestExpectSingleTrue('did_put'); + } + + public function testInvalid() + { + $globals = array(); + $mock = $this->getMock('hoplite\http\RootController', array('Stop'), array($globals)); + + $this->fixture = new TestRestAction($mock); + + $mock->expects($this->once()) + ->method('Stop'); + + $this->request->http_method = 'HEAD'; + $this->fixture->Invoke($this->request, $this->response); + $this->RestExpectSingleTrue('___none___'); + + $this->assertEquals(http\ResponseCode::METHOD_NOT_ALLOWED, $this->response->response_code); + } +} -- 2.43.5