diff --git a/src/Console/ConsoleIo.php b/src/Console/ConsoleIo.php index abd0d8bedcf..9e9b8b70cb7 100644 --- a/src/Console/ConsoleIo.php +++ b/src/Console/ConsoleIo.php @@ -181,8 +181,7 @@ public function hr($newlines = 0, $width = 63) { * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::in */ public function ask($prompt, $default = null) { - $in = $this->_getInput($prompt, null, $default); - return $in; + return $this->_getInput($prompt, null, $default); } /** @@ -196,7 +195,6 @@ public function ask($prompt, $default = null) { */ public function askChoice($prompt, $options, $default = null) { $originalOptions = $options; - $in = $this->_getInput($prompt, $originalOptions, $default); if ($options && is_string($options)) { if (strpos($options, ',')) { @@ -207,15 +205,16 @@ public function askChoice($prompt, $options, $default = null) { $options = [$options]; } } - if (is_array($options)) { - $options = array_merge( - array_map('strtolower', $options), - array_map('strtoupper', $options), - $options - ); - while ($in === '' || !in_array($in, $options)) { - $in = $this->_getInput($prompt, $originalOptions, $default); - } + + $printOptions = '(' . implode('/', $options) . ')'; + $options = array_merge( + array_map('strtolower', $options), + array_map('strtoupper', $options), + $options + ); + $in = ''; + while ($in === '' || !in_array($in, $options)) { + $in = $this->_getInput($prompt, $printOptions, $default); } return $in; } @@ -229,17 +228,16 @@ public function askChoice($prompt, $options, $default = null) { * @return string Either the default value, or the user-provided input. */ protected function _getInput($prompt, $options, $default) { - if (!is_array($options)) { - $printOptions = ''; - } else { - $printOptions = '(' . implode('/', $options) . ')'; + $optionsText = ''; + if (isset($options)) { + $optionsText = " $options "; } - if ($default === null) { - $this->_out->write('' . $prompt . '' . " $printOptions \n" . '> ', 0); - } else { - $this->_out->write('' . $prompt . '' . " $printOptions \n" . "[$default] > ", 0); + $defaultText = ''; + if ($default !== null) { + $defaultText = "[$default] "; } + $this->_out->write('' . $prompt . "$optionsText\n$defaultText> ", 0); $result = $this->_in->read(); if ($result === false) { diff --git a/tests/TestCase/Console/ConsoleIoTest.php b/tests/TestCase/Console/ConsoleIoTest.php new file mode 100644 index 00000000000..5eda7b7e517 --- /dev/null +++ b/tests/TestCase/Console/ConsoleIoTest.php @@ -0,0 +1,119 @@ +out = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false); + $this->err = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false); + $this->in = $this->getMock('Cake\Console\ConsoleInput', [], [], '', false); + $this->io = new ConsoleIo($this->out, $this->err, $this->in); + } + +/** + * Provider for testing choice types. + * + * @return array + */ + public function choiceProvider() { + return [ + [['y', 'n']], + ['y,n'], + ['y/n'], + ['y'], + ]; + } + +/** + * test ask choices method + * + * @dataProvider choiceProvider + * @return void + */ + public function testAskChoices($choices) { + $this->in->expects($this->at(0)) + ->method('read') + ->will($this->returnValue('y')); + + $result = $this->io->askChoice('Just a test?', $choices); + $this->assertEquals('y', $result); + } + +/** + * test ask choices method + * + * @dataProvider choiceProvider + * @return void + */ + public function testAskChoicesInsensitive($choices) { + $this->in->expects($this->at(0)) + ->method('read') + ->will($this->returnValue('Y')); + + $result = $this->io->askChoice('Just a test?', $choices); + $this->assertEquals('Y', $result); + } + +/** + * Test ask method + * + * @return void + */ + public function testAsk() { + $this->out->expects($this->at(0)) + ->method('write') + ->with("Just a test?\n> "); + + $this->in->expects($this->at(0)) + ->method('read') + ->will($this->returnValue('y')); + + $result = $this->io->ask('Just a test?'); + $this->assertEquals('y', $result); + } + +/** + * Test ask method + * + * @return void + */ + public function testAskDefaultValue() { + $this->out->expects($this->at(0)) + ->method('write') + ->with("Just a test?\n[n] > "); + + $this->in->expects($this->at(0)) + ->method('read') + ->will($this->returnValue('')); + + $result = $this->io->ask('Just a test?', 'n'); + $this->assertEquals('n', $result); + } + +}