* Add RestAdapaterTest
authorRobert Sesek <rsesek@bluestatic.org>
Fri, 5 Aug 2011 04:06:18 +0000 (00:06 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Fri, 5 Aug 2011 04:06:18 +0000 (00:06 -0400)
* Refactor common HTTP test fixtures out into a new file

http/rest_adapter.php
testing/tests/http/fixtures.php [new file with mode: 0644]
testing/tests/http/rest_action_test.php
testing/tests/http/rest_adapter_test.php [new file with mode: 0644]

index 2522b526b2a431e3899b49fdbf2e5aa289414f76..0329177f29bae0f0e73ab8ebc59ea5e866974c75 100644 (file)
@@ -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 (file)
index 0000000..fcd3082
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+// Hoplite
+// 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;
+
+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;
+  }
+}
index 4c89d14e6f45da75db6bfa565d27735d67046485..fb6c36b1a4f4abdf5f87ee0ec265ea0c78857bea 100644 (file)
@@ -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 (file)
index 0000000..629d52a
--- /dev/null
@@ -0,0 +1,150 @@
+<?php
+// Hoplite
+// 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/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);
+  }
+}