Skip to content

Commit

Permalink
Fix and add tests for ask() and askChoice()
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Apr 10, 2014
1 parent 83961d5 commit 1fb57b1
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 20 deletions.
38 changes: 18 additions & 20 deletions src/Console/ConsoleIo.php
Expand Up @@ -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);
}

/**
Expand All @@ -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, ',')) {
Expand All @@ -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;
}
Expand All @@ -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('<question>' . $prompt . '</question>' . " $printOptions \n" . '> ', 0);
} else {
$this->_out->write('<question>' . $prompt . '</question>' . " $printOptions \n" . "[$default] > ", 0);
$defaultText = '';
if ($default !== null) {
$defaultText = "[$default] ";
}
$this->_out->write('<question>' . $prompt . "</question>$optionsText\n$defaultText> ", 0);
$result = $this->_in->read();

if ($result === false) {
Expand Down
119 changes: 119 additions & 0 deletions tests/TestCase/Console/ConsoleIoTest.php
@@ -0,0 +1,119 @@
<?php
/**
* CakePHP : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Console;

use Cake\Console\ConsoleIo;
use Cake\TestSuite\TestCase;

/**
* ConsoleIo test.
*/
class ConsoleIoTest extends TestCase {

/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();

$this->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("<question>Just a test?</question>\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("<question>Just a test?</question>\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);
}

}

0 comments on commit 1fb57b1

Please sign in to comment.