Browse Source

Fix and add tests for ask() and askChoice()

mark_story 12 years ago
parent
commit
1fb57b149e
2 changed files with 137 additions and 20 deletions
  1. 18 20
      src/Console/ConsoleIo.php
  2. 119 0
      tests/TestCase/Console/ConsoleIoTest.php

+ 18 - 20
src/Console/ConsoleIo.php

@@ -181,8 +181,7 @@ class ConsoleIo {
  * @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 @@ class ConsoleIo {
  */
 	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 @@ class ConsoleIo {
 				$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 @@ class ConsoleIo {
  * @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) {

+ 119 - 0
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);
+	}
+
+}