implementing CakeRequest::addDetector() and adding test cases.

This commit is contained in:
Mark Story 2010-04-29 23:08:01 -04:00
parent 4fbed678cf
commit 6dcc680c1a
2 changed files with 41 additions and 6 deletions

View file

@ -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;
}
/**

View file

@ -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());
}
}