Add a test for RestAction
authorRobert Sesek <rsesek@bluestatic.org>
Sun, 12 Jun 2011 14:45:34 +0000 (10:45 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sun, 12 Jun 2011 14:45:34 +0000 (10:45 -0400)
http/rest_action.php
testing/tests/http/rest_action_test.php [new file with mode: 0644]

index 9300d4c8c00da13ec483932d3ad17cc90c55dd66..91170be1e9c32fb81373374ab106316044dde377 100644 (file)
@@ -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 (file)
index 0000000..cf39869
--- /dev/null
@@ -0,0 +1,116 @@
+<?php
+// { PRODUCT_NAME }
+// Copyright (c) 2011 Blue Static
+// 
+// This program is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation, either version 3 of the License, or any later version.
+// 
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program.  If not, see <http://www.gnu.org/licenses/>.
+
+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);
+  }
+}