Fix issue where REST actions were not easily testable.

Fixes #2198
This commit is contained in:
mark_story 2011-11-11 21:26:46 -05:00
parent a5d497068b
commit 493ce3a442
2 changed files with 36 additions and 9 deletions

View file

@ -425,6 +425,21 @@ class ControllerTestCaseTest extends CakeTestCase {
$this->assertTrue(isset($query['blue'])); $this->assertTrue(isset($query['blue']));
} }
/**
* 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 * Tests autoMock ability
*/ */

View file

@ -187,11 +187,14 @@ abstract class ControllerTestCase extends CakeTestCase {
} }
/** /**
* Tests a controller action. * Lets you do functional tests of a controller action.
* *
* ### Options: * ### 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. * - `method` POST or GET. Defaults to POST.
* - `return` Specify the return type you want. Choose from: * - `return` Specify the return type you want. Choose from:
* - `vars` Get the set view variables. * - `vars` Get the set view variables.
@ -213,14 +216,23 @@ abstract class ControllerTestCase extends CakeTestCase {
), $options); ), $options);
$_SERVER['REQUEST_METHOD'] = strtoupper($options['method']); $_SERVER['REQUEST_METHOD'] = strtoupper($options['method']);
if (strtoupper($options['method']) == 'GET') { if (is_array($options['data'])) {
$_GET = $options['data']; if (strtoupper($options['method']) == 'GET') {
$_POST = array(); $_GET = $options['data'];
} else { $_POST = array();
$_POST = $options['data']; } else {
$_GET = array(); $_POST = $options['data'];
$_GET = array();
}
} }
$request = new CakeRequest($url); $request = $this->getMock('CakeRequest', array('_readInput'), array($url));
if (is_string($options['data'])) {
$request->expects($this->any())
->method('_readInput')
->will($this->returnValue($options['data']));
}
$Dispatch = new ControllerTestDispatcher(); $Dispatch = new ControllerTestDispatcher();
foreach (Router::$routes as $route) { foreach (Router::$routes as $route) {
if ($route instanceof RedirectRoute) { if ($route instanceof RedirectRoute) {