From 1c56c723f5e21ba1abf8977ba070ec97217382a5 Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Thu, 25 Jun 2015 00:52:33 +0200 Subject: [PATCH] Backport param() access. --- lib/Cake/Console/Shell.php | 13 +++++++ lib/Cake/Test/Case/Console/ShellTest.php | 45 ++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/lib/Cake/Console/Shell.php b/lib/Cake/Console/Shell.php index 35b1bb5e4..830181949 100644 --- a/lib/Cake/Console/Shell.php +++ b/lib/Cake/Console/Shell.php @@ -499,6 +499,19 @@ class Shell extends Object { 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/lib/Cake/Test/Case/Console/ShellTest.php b/lib/Cake/Test/Case/Console/ShellTest.php index bfd8c9e4e..e15705439 100644 --- a/lib/Cake/Test/Case/Console/ShellTest.php +++ b/lib/Cake/Test/Case/Console/ShellTest.php @@ -855,6 +855,51 @@ TEXT; $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', + 'value', + ), + array( + 'help', + false, + ), + array( + 'emptykey', + '', + ), + array( + 'truthy', + true, + ), + array( + 'does_not_exist', + null, + ) + ); + } + /** * Test that option parsers are created with the correct name/command. *