Skip to content
Permalink
Browse files

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

  • Loading branch information...
markstory committed Apr 30, 2010
1 parent 4fbed67 commit 6dcc680c1ad3b87e6c0653cd40ccec659d9e1c6e
Showing with 41 additions and 6 deletions.
  1. +6 −1 cake/libs/cake_request.php
  2. +35 −5 cake/tests/cases/libs/cake_request.test.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 @@ 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;
}
/**
@@ -304,7 +304,7 @@ function testFILESParsing() {
)
);
$this->assertEqual($request->data, $expected);
$_FILES = array(
'something' => array(
'name' => 'something.txt',
@@ -316,7 +316,7 @@ function testFILESParsing() {
);
$request = new CakeRequest();
$this->assertEqual($request->params['form'], $_FILES);
}
/**
@@ -458,7 +458,7 @@ function testIsSsl() {
$_SERVER['HTTPS'] = 1;
$this->assertTrue($request->is('ssl'));
$_SERVER['HTTPS'] = 'on';
$this->assertTrue($request->is('ssl'));
@@ -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');
@@ -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.
You can’t perform that action at this time.