Fix #19853: Added support for default value for \yii\helpers\Console::select()

This commit is contained in:
rhertogh
2023-06-09 18:36:21 +02:00
committed by GitHub
parent 643d45a454
commit 3cebbdad2e
5 changed files with 83 additions and 47 deletions

View File

@ -6,6 +6,7 @@ Yii Framework 2 Change Log
- Bug #18859: Fix `yii\web\Controller::bindInjectedParams()` to not throw error when argument of `ReflectionUnionType` type is passed (bizley) - Bug #18859: Fix `yii\web\Controller::bindInjectedParams()` to not throw error when argument of `ReflectionUnionType` type is passed (bizley)
- Enh #19841: Allow jQuery 3.7 to be installed (wouter90) - Enh #19841: Allow jQuery 3.7 to be installed (wouter90)
- Enh #19853: Added support for default value for `\yii\helpers\Console::select()` (rhertogh)
2.0.48.1 May 24, 2023 2.0.48.1 May 24, 2023

View File

@ -52,6 +52,17 @@ version B between A and C, you need to follow the instructions
for both A and B. for both A and B.
Upgrade from Yii 2.0.48
-----------------------
* Since Yii 2.0.49 the `yii\console\Controller::select()` function supports a default value and respects
the `yii\console\Controller::$interactive` setting. Before the user was always prompted to select an option
regardless of the `$interactive` setting. Now the `$default` value is automatically returned when `$interactive` is
`false`.
* The function signature for `yii\console\Controller::select()` and `yii\helpers\BaseConsole::select()` have changed.
They now have an additional `$default = null` parameter. In case those methods are overwritten you will need to
update your child classes accordingly.
Upgrade from Yii 2.0.46 Upgrade from Yii 2.0.46
----------------------- -----------------------

View File

@ -400,12 +400,19 @@ class Controller extends \yii\base\Controller
* *
* @param string $prompt the prompt message * @param string $prompt the prompt message
* @param array $options Key-value array of options to choose from * @param array $options Key-value array of options to choose from
* @param string|null $default value to use when the user doesn't provide an option.
* If the default is `null`, the user is required to select an option.
* *
* @return string An option character the user chose * @return string An option character the user chose
* @since 2.0.49 Added the $default argument
*/ */
public function select($prompt, $options = []) public function select($prompt, $options = [], $default = null)
{ {
return Console::select($prompt, $options); if ($this->interactive) {
return Console::select($prompt, $options, $default);
}
return $default;
} }
/** /**

View File

@ -948,13 +948,17 @@ class BaseConsole
* @param string $prompt the prompt message * @param string $prompt the prompt message
* @param array $options Key-value array of options to choose from. Key is what is inputed and used, value is * @param array $options Key-value array of options to choose from. Key is what is inputed and used, value is
* what's displayed to end user by help command. * what's displayed to end user by help command.
* @param string|null $default value to use when the user doesn't provide an option.
* If the default is `null`, the user is required to select an option.
* *
* @return string An option character the user chose * @return string An option character the user chose
* @since 2.0.49 Added the $default argument
*/ */
public static function select($prompt, $options = []) public static function select($prompt, $options = [], $default = null)
{ {
top: top:
static::stdout("$prompt [" . implode(',', array_keys($options)) . ',?]: '); static::stdout("$prompt (" . implode(',', array_keys($options)) . ',?)'
. ($default !== null ? '[' . $default . ']' : '') . ': ');
$input = static::stdin(); $input = static::stdin();
if ($input === '?') { if ($input === '?') {
foreach ($options as $key => $value) { foreach ($options as $key => $value) {
@ -962,6 +966,8 @@ class BaseConsole
} }
static::output(' ? - Show help'); static::output(' ? - Show help');
goto top; goto top;
} elseif ($default !== null && $input === '') {
return $default;
} elseif (!array_key_exists($input, $options)) { } elseif (!array_key_exists($input, $options)) {
goto top; goto top;
} }

View File

@ -216,27 +216,6 @@ class ConsoleTest extends TestCase
$expectedHtml = "Error message. Here are some chars: < >\nError message. Here are even more chars: \"\""; $expectedHtml = "Error message. Here are some chars: < >\nError message. Here are even more chars: \"\"";
$this->assertEqualsWithoutLE($expectedHtml, Console::errorSummary($model, $options)); $this->assertEqualsWithoutLE($expectedHtml, Console::errorSummary($model, $options));
} }
}
/**
* @property string name
* @property array types
* @property string description
*/
class TestConsoleModel extends DynamicModel
{
public function rules()
{
return [
['name', 'required'],
['name', 'string', 'max' => 100]
];
}
public function init()
{
$this->defineAttribute('name');
}
/** /**
* @covers \yii\helpers\BaseConsole::input() * @covers \yii\helpers\BaseConsole::input()
@ -390,16 +369,16 @@ class TestConsoleModel extends DynamicModel
$this->truncateStreams(); $this->truncateStreams();
foreach ([ foreach ([
'y' => true, 'y' => true,
'Y' => true, 'Y' => true,
'yes' => true, 'yes' => true,
'YeS' => true, 'YeS' => true,
'n' => false, 'n' => false,
'N' => false, 'N' => false,
'no' => false, 'no' => false,
'NO' => false, 'NO' => false,
'WHAT?!' . PHP_EOL . 'yes' => true, 'WHAT?!' . PHP_EOL . 'yes' => true,
] as $currInput => $currAssertion) { ] as $currInput => $currAssertion) {
$this->sendInput($currInput); $this->sendInput($currInput);
$result = ConsoleStub::confirm('Are you sure?'); $result = ConsoleStub::confirm('Are you sure?');
$this->assertEquals($currAssertion, $result, $currInput); $this->assertEquals($currAssertion, $result, $currInput);
@ -420,31 +399,63 @@ class TestConsoleModel extends DynamicModel
$this->sendInput('c'); $this->sendInput('c');
$result = ConsoleStub::select('Usual behavior', $options); $result = ConsoleStub::select('Usual behavior', $options);
$this->assertEquals('Usual behavior [c,d,m,?]: ', $this->readOutput()); $this->assertEquals('Usual behavior (c,d,m,?): ', $this->readOutput());
$this->assertEquals('c', $result); $this->assertEquals('c', $result);
$this->truncateStreams(); $this->truncateStreams();
$this->sendInput('x', 'd'); $this->sendInput('x', 'd');
$result = ConsoleStub::select('Wrong character', $options); $result = ConsoleStub::select('Wrong character', $options);
$this->assertEquals('Wrong character [c,d,m,?]: Wrong character [c,d,m,?]: ', $this->readOutput()); $this->assertEquals('Wrong character (c,d,m,?): Wrong character (c,d,m,?): ', $this->readOutput());
$this->assertEquals('d', $result); $this->assertEquals('d', $result);
$this->truncateStreams(); $this->truncateStreams();
$this->sendInput('?', 'm'); $this->sendInput('?', 'm');
$result = ConsoleStub::select('Using help', $options); $result = ConsoleStub::select('Using help', $options);
$this->assertEquals( $this->assertEquals(
'Using help [c,d,m,?]: ' 'Using help (c,d,m,?): '
. ' c - cat' . ' c - cat'
. PHP_EOL . PHP_EOL
. ' d - dog' . ' d - dog'
. PHP_EOL . PHP_EOL
. ' m - mouse' . ' m - mouse'
. PHP_EOL . PHP_EOL
. ' ? - Show help' . ' ? - Show help'
. PHP_EOL . PHP_EOL
. 'Using help [c,d,m,?]: ', . 'Using help (c,d,m,?): ',
$this->readOutput() $this->readOutput()
); );
$this->truncateStreams(); $this->truncateStreams();
$this->sendInput('');
$result = ConsoleStub::select('Use Default', $options, 'm');
$this->assertEquals('m', $result);
$this->truncateStreams();
$this->sendInput('', 'd');
$result = ConsoleStub::select('Empty without Default', $options);
$this->assertEquals('Empty without Default (c,d,m,?): Empty without Default (c,d,m,?): ', $this->readOutput());
$this->assertEquals('d', $result);
$this->truncateStreams();
}
}
/**
* @property string name
* @property array types
* @property string description
*/
class TestConsoleModel extends DynamicModel
{
public function rules()
{
return [
['name', 'required'],
['name', 'string', 'max' => 100]
];
}
public function init()
{
$this->defineAttribute('name');
} }
} }