Permalink
Browse files

Fix issue where REST actions were not easily testable.

Fixes #2198
  • Loading branch information...
1 parent a5d4970 commit 493ce3a442660e4079b4992d009e07ab0516d02b @markstory markstory committed Nov 12, 2011
Showing with 36 additions and 9 deletions.
  1. +15 −0 lib/Cake/Test/Case/TestSuite/ControllerTestCaseTest.php
  2. +21 −9 lib/Cake/TestSuite/ControllerTestCase.php
@@ -426,6 +426,21 @@ public function testTestActionGetData() {
}
/**
+ * Test that REST actions with XML/JSON input work.
+ *
+ * @return void
+ */
+ public function testTestActionJsonData() {
+ $result = $this->Case->testAction('/tests_apps_posts/input_data', array(
+ 'return' => 'vars',
+ 'method' => 'post',
+ 'data' => '{"key":"value","json":true}'
+ ));
+ $this->assertEquals('value', $result['data']['key']);
+ $this->assertTrue($result['data']['json']);
+ }
+
+/**
* Tests autoMock ability
*/
public function testAutoMock() {
@@ -187,11 +187,14 @@ public function __call($name, $arguments) {
}
/**
- * Tests a controller action.
+ * Lets you do functional tests of a controller action.
*
* ### Options:
*
- * - `data` POST or GET data to pass. Depends on the method.
+ * - `data` Will be used as the request data. If the `method` is GET,
+ * data will be used a GET params. If the `method` is POST, it will be used
+ * as POST data. By setting `$options['data']` to a string, you can simulate XML or JSON
+ * payloads to your controllers allowing you to test REST webservices.
* - `method` POST or GET. Defaults to POST.
* - `return` Specify the return type you want. Choose from:
* - `vars` Get the set view variables.
@@ -213,14 +216,23 @@ protected function _testAction($url = '', $options = array()) {
), $options);
$_SERVER['REQUEST_METHOD'] = strtoupper($options['method']);
- if (strtoupper($options['method']) == 'GET') {
- $_GET = $options['data'];
- $_POST = array();
- } else {
- $_POST = $options['data'];
- $_GET = array();
+ if (is_array($options['data'])) {
+ if (strtoupper($options['method']) == 'GET') {
+ $_GET = $options['data'];
+ $_POST = array();
+ } else {
+ $_POST = $options['data'];
+ $_GET = array();
+ }
+ }
+ $request = $this->getMock('CakeRequest', array('_readInput'), array($url));
+
+ if (is_string($options['data'])) {
+ $request->expects($this->any())
+ ->method('_readInput')
+ ->will($this->returnValue($options['data']));
}
- $request = new CakeRequest($url);
+
$Dispatch = new ControllerTestDispatcher();
foreach (Router::$routes as $route) {
if ($route instanceof RedirectRoute) {

0 comments on commit 493ce3a

Please sign in to comment.