Skip to content

Commit

Permalink
Add Shell::param()
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed Aug 19, 2014
1 parent 07d979d commit fc9b288
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/Console/Shell.php
Expand Up @@ -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.
*
Expand Down
46 changes: 46 additions & 0 deletions tests/TestCase/Console/ShellTest.php
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Network/RequestTest.php
Expand Up @@ -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));
}

/**
Expand Down

0 comments on commit fc9b288

Please sign in to comment.