Skip to content

Commit

Permalink
implementing CakeRequest::addDetector() and adding test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Apr 30, 2010
1 parent 4fbed67 commit 6dcc680
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
7 changes: 6 additions & 1 deletion cake/libs/cake_request.php
Expand Up @@ -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.
Expand Down Expand Up @@ -308,7 +310,10 @@ public function is($type) {
* @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;
}

/**
Expand Down
40 changes: 35 additions & 5 deletions cake/tests/cases/libs/cake_request.test.php
Expand Up @@ -304,7 +304,7 @@ function testFILESParsing() {
)
);
$this->assertEqual($request->data, $expected);

$_FILES = array(
'something' => array(
'name' => 'something.txt',
Expand All @@ -316,7 +316,7 @@ function testFILESParsing() {
);
$request = new CakeRequest();
$this->assertEqual($request->params['form'], $_FILES);

}

/**
Expand Down Expand Up @@ -458,7 +458,7 @@ function testIsSsl() {

$_SERVER['HTTPS'] = 1;
$this->assertTrue($request->is('ssl'));

$_SERVER['HTTPS'] = 'on';
$this->assertTrue($request->is('ssl'));

Expand Down Expand Up @@ -504,9 +504,9 @@ function test__get() {
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');
Expand All @@ -520,4 +520,34 @@ function testArrayAccess() {
$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());
}
}

0 comments on commit 6dcc680

Please sign in to comment.