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

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