diff options
author | Andreas Gohr <andi@splitbrain.org> | 2012-06-25 00:37:21 +0200 |
---|---|---|
committer | Andreas Gohr <andi@splitbrain.org> | 2012-06-25 00:37:21 +0200 |
commit | 591acd873d64abb271864b581c48ca419aa5d329 (patch) | |
tree | 8d2e0511550c54620041c5aa9298d88cfd5e13cb | |
parent | bcc94b2c17efc51fd78a25db43058d10e685679d (diff) | |
download | rpg-591acd873d64abb271864b581c48ca419aa5d329.tar.gz rpg-591acd873d64abb271864b581c48ca419aa5d329.tar.bz2 |
some Input class fixes and unit tests
-rw-r--r-- | _test/tests/inc/input.test.php | 206 | ||||
-rw-r--r-- | inc/Input.class.php | 10 |
2 files changed, 210 insertions, 6 deletions
diff --git a/_test/tests/inc/input.test.php b/_test/tests/inc/input.test.php new file mode 100644 index 000000000..627af3a2b --- /dev/null +++ b/_test/tests/inc/input.test.php @@ -0,0 +1,206 @@ +<?php + +/** + * Tests for the Input class + */ +class input_test extends DokuWikiTest { + + private $data = array( + 'array' => array('foo', 'bar'), + 'string' => 'foo', + 'int' => '17', + 'zero' => '0', + 'one' => '1', + 'empty' => '', + 'emptya' => array() + ); + + public function test_str() { + $_REQUEST = $this->data; + $_POST = $this->data; + $_GET = $this->data; + $_GET['get'] = 1; + $_POST['post'] = 1; + $INPUT = new Input(); + + $this->assertSame('foo', $INPUT->str('string')); + $this->assertSame('', $INPUT->str('none')); + $this->assertSame('', $INPUT->str('empty')); + $this->assertSame('foo', $INPUT->str('none', 'foo')); + $this->assertSame('', $INPUT->str('empty', 'foo')); + $this->assertSame('foo', $INPUT->str('empty', 'foo', true)); + + $this->assertSame(false, $INPUT->str('get', false)); + $this->assertSame(false, $INPUT->str('post', false)); + + $this->assertSame('foo', $INPUT->post->str('string')); + $this->assertSame('', $INPUT->post->str('none')); + $this->assertSame('', $INPUT->post->str('empty')); + $this->assertSame('foo', $INPUT->post->str('none', 'foo')); + $this->assertSame('', $INPUT->post->str('empty', 'foo')); + $this->assertSame('foo', $INPUT->post->str('empty', 'foo', true)); + + $this->assertSame(false, $INPUT->post->str('get', false)); + $this->assertSame('1', $INPUT->post->str('post', false)); + + $this->assertSame('foo', $INPUT->get->str('string')); + $this->assertSame('', $INPUT->get->str('none')); + $this->assertSame('', $INPUT->get->str('empty')); + $this->assertSame('foo', $INPUT->get->str('none', 'foo')); + $this->assertSame('', $INPUT->get->str('empty', 'foo')); + $this->assertSame('foo', $INPUT->get->str('empty', 'foo', true)); + + $this->assertSame(false, $INPUT->get->str('post', false)); + $this->assertSame('1', $INPUT->get->str('get', false)); + + $this->assertSame('', $INPUT->str('array')); + } + + public function test_int() { + $_REQUEST = $this->data; + $_POST = $this->data; + $_GET = $this->data; + $_GET['get'] = 1; + $_POST['post'] = 1; + $INPUT = new Input(); + + $this->assertSame(17, $INPUT->int('int')); + $this->assertSame(0, $INPUT->int('none')); + $this->assertSame(0, $INPUT->int('empty')); + $this->assertSame(42, $INPUT->int('none', 42)); + $this->assertSame(0, $INPUT->int('zero', 42)); + $this->assertSame(42, $INPUT->int('zero', 42, true)); + + $this->assertSame(false, $INPUT->int('get', false)); + $this->assertSame(false, $INPUT->int('post', false)); + + $this->assertSame(17, $INPUT->post->int('int')); + $this->assertSame(0, $INPUT->post->int('none')); + $this->assertSame(0, $INPUT->post->int('empty')); + $this->assertSame(42, $INPUT->post->int('none', 42)); + $this->assertSame(0, $INPUT->post->int('zero', 42)); + $this->assertSame(42, $INPUT->post->int('zero', 42, true)); + + $this->assertSame(false, $INPUT->post->int('get', false)); + $this->assertSame(1, $INPUT->post->int('post', false)); + + $this->assertSame(17, $INPUT->post->int('int')); + $this->assertSame(0, $INPUT->post->int('none')); + $this->assertSame(0, $INPUT->post->int('empty')); + $this->assertSame(42, $INPUT->post->int('none', 42)); + $this->assertSame(0, $INPUT->post->int('zero', 42)); + $this->assertSame(42, $INPUT->post->int('zero', 42, true)); + + $this->assertSame(false, $INPUT->get->int('post', false)); + $this->assertSame(1, $INPUT->get->int('get', false)); + + $this->assertSame(0, $INPUT->int('array')); + } + + public function test_arr() { + $_REQUEST = $this->data; + $_POST = $this->data; + $_GET = $this->data; + $_GET['get'] = array(1, 2); + $_POST['post'] = array(1, 2); + $INPUT = new Input(); + + $this->assertSame(array('foo', 'bar'), $INPUT->arr('array')); + $this->assertSame(array(), $INPUT->arr('none')); + $this->assertSame(array(), $INPUT->arr('empty')); + $this->assertSame(array(1, 2), $INPUT->arr('none', array(1, 2))); + $this->assertSame(array(), $INPUT->arr('emptya', array(1, 2))); + $this->assertSame(array(1, 2), $INPUT->arr('emptya', array(1, 2), true)); + + $this->assertSame(false, $INPUT->arr('get', false)); + $this->assertSame(false, $INPUT->arr('post', false)); + + $this->assertSame(array('foo', 'bar'), $INPUT->post->arr('array')); + $this->assertSame(array(), $INPUT->post->arr('none')); + $this->assertSame(array(), $INPUT->post->arr('empty')); + $this->assertSame(array(1, 2), $INPUT->post->arr('none', array(1, 2))); + $this->assertSame(array(), $INPUT->post->arr('emptya', array(1, 2))); + $this->assertSame(array(1, 2), $INPUT->post->arr('emptya', array(1, 2), true)); + + $this->assertSame(false, $INPUT->post->arr('get', false)); + $this->assertSame(array(1, 2), $INPUT->post->arr('post', false)); + + $this->assertSame(array('foo', 'bar'), $INPUT->get->arr('array')); + $this->assertSame(array(), $INPUT->get->arr('none')); + $this->assertSame(array(), $INPUT->get->arr('empty')); + $this->assertSame(array(1, 2), $INPUT->get->arr('none', array(1, 2))); + $this->assertSame(array(), $INPUT->get->arr('emptya', array(1, 2))); + $this->assertSame(array(1, 2), $INPUT->get->arr('emptya', array(1, 2), true)); + + $this->assertSame(array(1, 2), $INPUT->get->arr('get', false)); + $this->assertSame(false, $INPUT->get->arr('post', false)); + } + + public function test_bool() { + $_REQUEST = $this->data; + $_POST = $this->data; + $_GET = $this->data; + $_GET['get'] = '1'; + $_POST['post'] = '1'; + $INPUT = new Input(); + + $this->assertSame(true, $INPUT->bool('one')); + $this->assertSame(false, $INPUT->bool('zero')); + + $this->assertSame(false, $INPUT->bool('get')); + $this->assertSame(false, $INPUT->bool('post')); + + $this->assertSame(true, $INPUT->post->bool('one')); + $this->assertSame(false, $INPUT->post->bool('zero')); + + $this->assertSame(false, $INPUT->post->bool('get')); + $this->assertSame(true, $INPUT->post->bool('post')); + } + + public function test_remove() { + $_REQUEST = $this->data; + $_POST = $this->data; + $_GET = $this->data; + $INPUT = new Input(); + + $INPUT->remove('string'); + $this->assertNull($_REQUEST['string']); + $this->assertNull($_POST['string']); + $this->assertNull($_GET['string']); + + $INPUT->post->remove('int'); + $this->assertNull($_POST['int']); + $this->assertEquals(17, $_GET['int']); + $this->assertEquals(17, $_REQUEST['int']); + } + + public function test_set(){ + $_REQUEST = $this->data; + $_POST = $this->data; + $_GET = $this->data; + $INPUT = new Input(); + + $INPUT->set('test','foo'); + $this->assertEquals('foo',$_REQUEST['test']); + $this->assertNull($_POST['test']); + $this->assertNull($_GET['test']); + + $INPUT->get->set('test2','foo'); + $this->assertEquals('foo',$_GET['test2']); + $this->assertEquals('foo',$_REQUEST['test2']); + $this->assertNull($_POST['test']); + } + + public function test_ref(){ + $_REQUEST = $this->data; + $_POST = $this->data; + $_GET = $this->data; + $INPUT = new Input(); + + $test = &$INPUT->ref('string'); + $this->assertEquals('foo',$test); + $_REQUEST['string'] = 'bla'; + $this->assertEquals('bla',$test); + } + +}
\ No newline at end of file diff --git a/inc/Input.class.php b/inc/Input.class.php index 62c2688c8..1ea5e031f 100644 --- a/inc/Input.class.php +++ b/inc/Input.class.php @@ -104,8 +104,7 @@ class Input { $this->set($name, $default); } - $ref = &$this->access[$name]; - return $ref; + return $this->access[$name]; } /** @@ -143,6 +142,8 @@ class Input { /** * Access a request parameter as bool * + * Note: $nonempty is here for interface consistency and makes not much sense for booleans + * * @param string $name Parameter name * @param mixed $default Default to return if parameter isn't set * @param bool $nonempty Return $default if parameter is set but empty() @@ -165,6 +166,7 @@ class Input { */ public function arr($name, $default = array(), $nonempty = false) { if(!isset($this->access[$name])) return $default; + if(!is_array($this->access[$name])) return $default; if($nonempty && empty($this->access[$name])) return $default; return (array) $this->access[$name]; @@ -183,8 +185,6 @@ class PostInput extends Input { */ function __construct() { $this->access = &$_POST; - unset ($this->post); - unset ($this->get); } /** @@ -211,8 +211,6 @@ class GetInput extends Input { */ function __construct() { $this->access = &$_GET; - unset ($this->post); - unset ($this->get); } /** |