From 4eef2c19f0e505f81b673ff997ae88610a520958 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 2 May 2010 01:22:34 -0400 Subject: [PATCH] Making CakeRequest::addParams() chainable. Adding CakeRequest::addPaths() to allow easy settings of path variables, its also chainable. Tests added. --- cake/libs/cake_request.php | 18 ++++++++++++++- cake/tests/cases/libs/cake_request.test.php | 25 ++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/cake/libs/cake_request.php b/cake/libs/cake_request.php index edafbf65740..b1300afd190 100644 --- a/cake/libs/cake_request.php +++ b/cake/libs/cake_request.php @@ -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; } /** diff --git a/cake/tests/cases/libs/cake_request.test.php b/cake/tests/cases/libs/cake_request.test.php index c06f9539b6d..27bf25bb82f 100644 --- a/cake/tests/cases/libs/cake_request.test.php +++ b/cake/tests/cases/libs/cake_request.test.php @@ -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. *