Skip to content

Commit

Permalink
Making CakeRequest::addParams() chainable. Adding CakeRequest::addPat…
Browse files Browse the repository at this point in the history
…hs() to allow easy settings of path variables, its also chainable. Tests added.
  • Loading branch information
markstory committed May 2, 2010
1 parent bd1365f commit 4eef2c1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
18 changes: 17 additions & 1 deletion cake/libs/cake_request.php
Expand Up @@ -503,10 +503,26 @@ public function addDetector($name, $options) {
* 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;
}

/**
Expand Down
25 changes: 24 additions & 1 deletion cake/tests/cases/libs/cake_request.test.php
Expand Up @@ -72,13 +72,36 @@ function testQueryStringParsingFromInputUrl() {
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.
*
Expand Down

0 comments on commit 4eef2c1

Please sign in to comment.