diff --git a/src/Console/Shell.php b/src/Console/Shell.php index f86faabc545..54e32aa917d 100644 --- a/src/Console/Shell.php +++ b/src/Console/Shell.php @@ -428,6 +428,19 @@ public function __get($name) { return $this->{$name}; } +/** + * Safely access the values in $this->params. + * + * @param string $name The name of the parameter to get. + * @return string|bool|null Value. Will return null if it doesn't exist. + */ + public function param($name) { + if (!isset($this->params[$name])) { + return null; + } + return $this->params[$name]; + } + /** * Prompts the user for input, and returns it. * diff --git a/tests/TestCase/Console/ShellTest.php b/tests/TestCase/Console/ShellTest.php index d57fedb5f2f..f9318e0f004 100644 --- a/tests/TestCase/Console/ShellTest.php +++ b/tests/TestCase/Console/ShellTest.php @@ -788,6 +788,52 @@ public function testShellNaming() { $this->assertEquals($expected, $this->Shell->TestApple->name); } + +/** + * Test reading params + * + * @dataProvider paramReadingDataProvider + */ + public function testParamReading($toRead, $expected) { + $this->Shell->params = array( + 'key' => 'value', + 'help' => false, + 'emptykey' => '', + 'truthy' => true + ); + $this->assertSame($expected, $this->Shell->param($toRead)); + } + +/** + * Data provider for testing reading values with Shell::param() + * + * @return array + */ + public function paramReadingDataProvider() { + return array( + array( + 'key', + 'index', + ), + array( + 'help', + false, + ), + array( + 'emptykey', + '', + ), + array( + 'truthy', + true, + ), + array( + 'does_not_exist', + null, + ) + ); + } + /** * Test that option parsers are created with the correct name/command. * diff --git a/tests/TestCase/Network/RequestTest.php b/tests/TestCase/Network/RequestTest.php index 3c3b6508faa..7c40351dfe7 100644 --- a/tests/TestCase/Network/RequestTest.php +++ b/tests/TestCase/Network/RequestTest.php @@ -1927,7 +1927,7 @@ public function testParamReading($toRead, $expected) { 'truthy' => 1, 'zero' => '0', )); - $this->assertEquals($expected, $request->param($toRead)); + $this->assertSame($expected, $request->param($toRead)); } /**