Making CakeRequest::addParams() chainable. Adding CakeRequest::addPaths() to allow easy settings of path variables, its also chainable. Tests added.

This commit is contained in:
Mark Story 2010-05-02 01:22:34 -04:00
parent bd1365f8ae
commit 4eef2c19f0
2 changed files with 41 additions and 2 deletions

View file

@ -503,10 +503,26 @@ class CakeRequest implements ArrayAccess {
* Add parameters to the request's parsed parameter set.
*
* @param array $params Array of parameters to merge in
* @return void
* @return The current object, you can chain this method.
*/
public function addParams($params) {
$this->params = array_merge($this->params, $params);
return $this;
}
/**
* Add paths to the requests' paths vars
*
* @param array $paths Array of paths to merge in
* @return the current object, you can chain this method.
*/
public function addPaths($paths) {
foreach (array('webroot', 'here', 'base') as $element) {
if (isset($paths[$element])) {
$this->{$element} = $paths[$element];
}
}
return $this;
}
/**

View file

@ -72,13 +72,36 @@ class CakeRequestTestCase extends CakeTestCase {
function testAddParams() {
$request = new CakeRequest('some/path');
$request->params = array('controller' => 'posts', 'action' => 'view');
$request->addParams(array('plugin' => null, 'action' => 'index'));
$result = $request->addParams(array('plugin' => null, 'action' => 'index'));
$this->assertIdentical($result, $request, 'Method did not return itself. %s');
$this->assertEqual($request->controller, 'posts');
$this->assertEqual($request->action, 'index');
$this->assertEqual($request->plugin, null);
}
/**
* test splicing in paths.
*
* @return void
*/
function testAddPaths() {
$request = new CakeRequest('some/path');
$request->webroot = '/some/path/going/here/';
$result = $request->addPaths(array(
'random' => '/something', 'webroot' => '/', 'here' => '/', 'base' => '/base_dir'
));
$this->assertIdentical($result, $request, 'Method did not return itself. %s');
$this->assertEqual($request->webroot, '/');
$this->assertEqual($request->base, '/base_dir');
$this->assertEqual($request->here, '/');
$this->assertFalse(isset($request->random));
}
/**
* test parsing POST data into the object.
*