From 6dcc680c1ad3b87e6c0653cd40ccec659d9e1c6e Mon Sep 17 00:00:00 2001 From: Mark Story Date: Thu, 29 Apr 2010 23:08:01 -0400 Subject: [PATCH] implementing CakeRequest::addDetector() and adding test cases. --- cake/libs/cake_request.php | 7 +++- cake/tests/cases/libs/cake_request.test.php | 40 ++++++++++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/cake/libs/cake_request.php b/cake/libs/cake_request.php index c569ab789..7d78fc1d6 100644 --- a/cake/libs/cake_request.php +++ b/cake/libs/cake_request.php @@ -17,6 +17,8 @@ * @since CakePHP(tm) v 2.0 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ +App::import('Core', 'Set'); + class CakeRequest implements ArrayAccess { /** * Array of parameters parsed from the url. @@ -308,7 +310,10 @@ class CakeRequest implements ArrayAccess { * @return void */ public function addDetector($name, $options) { - + if (isset($this->_detectors[$name]) && isset($options['options'])) { + $options = Set::merge($this->_detectors[$name], $options); + } + $this->_detectors[$name] = $options; } /** diff --git a/cake/tests/cases/libs/cake_request.test.php b/cake/tests/cases/libs/cake_request.test.php index b97fe9e53..c9be99201 100644 --- a/cake/tests/cases/libs/cake_request.test.php +++ b/cake/tests/cases/libs/cake_request.test.php @@ -304,7 +304,7 @@ class CakeRequestTestCase extends CakeTestCase { ) ); $this->assertEqual($request->data, $expected); - + $_FILES = array( 'something' => array( 'name' => 'something.txt', @@ -316,7 +316,7 @@ class CakeRequestTestCase extends CakeTestCase { ); $request = new CakeRequest(); $this->assertEqual($request->params['form'], $_FILES); - + } /** @@ -458,7 +458,7 @@ class CakeRequestTestCase extends CakeTestCase { $_SERVER['HTTPS'] = 1; $this->assertTrue($request->is('ssl')); - + $_SERVER['HTTPS'] = 'on'; $this->assertTrue($request->is('ssl')); @@ -504,9 +504,9 @@ class CakeRequestTestCase extends CakeTestCase { function testArrayAccess() { $request = new CakeRequest(); $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs'); - + $this->assertEqual($request['controller'], 'posts'); - + $request['slug'] = 'speedy-slug'; $this->assertEqual($request->slug, 'speedy-slug'); $this->assertEqual($request['slug'], 'speedy-slug'); @@ -520,4 +520,34 @@ class CakeRequestTestCase extends CakeTestCase { $this->assertNull($request['plugin']); $this->assertNull($request->plugin); } + +/** + * test adding detectors and having them work. + * + * @return void + */ + function testAddDetector() { + $request = new CakeRequest(); + $request->addDetector('compare', array('env' => 'TEST_VAR', 'value' => 'something')); + + $_SERVER['TEST_VAR'] = 'something'; + $this->assertTrue($request->is('compare'), 'Value match failed %s.'); + + $_SERVER['TEST_VAR'] = 'wrong'; + $this->assertFalse($request->is('compare'), 'Value mis-match failed %s.'); + + $request->addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/')); + $_SERVER['TEST_VAR'] = 'banana'; + $this->assertTrue($request->isBanana()); + + $_SERVER['TEST_VAR'] = 'wrong value'; + $this->assertFalse($request->isBanana()); + + $request->addDetector('mobile', array('options' => array('Imagination'))); + $_SERVER['HTTP_USER_AGENT'] = 'Imagination land'; + $this->assertTrue($request->isMobile()); + + $_SERVER['HTTP_USER_AGENT'] = 'iPhone 3.0'; + $this->assertTrue($request->isMobile()); + } } \ No newline at end of file