Skip to content

Commit a6a781c

Browse files
Merge branch '3.4' into 4.1
* 3.4: [Console] fix test using deprecated code [travis] build libsodium only if it's not already enabled Add a test case for stringifying of Process arguments Add a missing English translation [Console] Fixes multiselect choice question in interactive mode with default values [Validator] Add a missing Polish translation Allow integers as default console option value Setting missing default paths under BC layer Changed "epost-adress" to "e-postadress" Fix for race condition in console output stream write reverse transform RFC 3339 formatted dates
2 parents 7e91f51 + 3cb827c commit a6a781c

File tree

4 files changed

+82
-11
lines changed

4 files changed

+82
-11
lines changed

Helper/QuestionHelper.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,23 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
4747
}
4848

4949
if (!$input->isInteractive()) {
50-
if ($question instanceof ChoiceQuestion) {
50+
$default = $question->getDefault();
51+
52+
if (null !== $default && $question instanceof ChoiceQuestion) {
5153
$choices = $question->getChoices();
5254

53-
return $choices[$question->getDefault()];
55+
if (!$question->isMultiselect()) {
56+
return isset($choices[$default]) ? $choices[$default] : $default;
57+
}
58+
59+
$default = explode(',', $default);
60+
foreach ($default as $k => $v) {
61+
$v = trim($v);
62+
$default[$k] = isset($choices[$v]) ? $choices[$v] : $v;
63+
}
5464
}
5565

56-
return $question->getDefault();
66+
return $default;
5767
}
5868

5969
if ($input instanceof StreamableInputInterface && $stream = $input->getStream()) {

Input/InputOption.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class InputOption
3333
private $description;
3434

3535
/**
36-
* @param string $name The option name
37-
* @param string|array $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
38-
* @param int|null $mode The option mode: One of the VALUE_* constants
39-
* @param string $description A description text
40-
* @param string|string[]|bool|null $default The default value (must be null for self::VALUE_NONE)
36+
* @param string $name The option name
37+
* @param string|array $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
38+
* @param int|null $mode The option mode: One of the VALUE_* constants
39+
* @param string $description A description text
40+
* @param string|string[]|int|bool|null $default The default value (must be null for self::VALUE_NONE)
4141
*
4242
* @throws InvalidArgumentException If option mode is invalid or incompatible
4343
*/
@@ -149,7 +149,7 @@ public function isArray()
149149
/**
150150
* Sets the default value.
151151
*
152-
* @param string|string[]|bool|null $default The default value
152+
* @param string|string[]|int|bool|null $default The default value
153153
*
154154
* @throws LogicException When incorrect default value is given
155155
*/
@@ -173,7 +173,7 @@ public function setDefault($default = null)
173173
/**
174174
* Returns the default value.
175175
*
176-
* @return string|string[]|bool|null The default value
176+
* @return string|string[]|int|bool|null The default value
177177
*/
178178
public function getDefault()
179179
{

Output/StreamOutput.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ public function getStream()
7070
*/
7171
protected function doWrite($message, $newline)
7272
{
73-
if (false === @fwrite($this->stream, $message) || ($newline && (false === @fwrite($this->stream, PHP_EOL)))) {
73+
if ($newline) {
74+
$message .= PHP_EOL;
75+
}
76+
77+
if (false === @fwrite($this->stream, $message)) {
7478
// should never happen
7579
throw new RuntimeException('Unable to write output.');
7680
}

Tests/Helper/QuestionHelperTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,63 @@ public function testAskChoice()
8989
$this->assertEquals('Superman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, true), $this->createOutputInterface(), $question));
9090
}
9191

92+
public function testAskChoiceNonInteractive()
93+
{
94+
$questionHelper = new QuestionHelper();
95+
96+
$helperSet = new HelperSet(array(new FormatterHelper()));
97+
$questionHelper->setHelperSet($helperSet);
98+
$inputStream = $this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n");
99+
100+
$heroes = array('Superman', 'Batman', 'Spiderman');
101+
102+
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0');
103+
104+
$this->assertSame('Superman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
105+
106+
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, 'Batman');
107+
$this->assertSame('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
108+
109+
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
110+
$this->assertNull($questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
111+
112+
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0');
113+
$question->setValidator(null);
114+
$this->assertSame('Superman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
115+
116+
try {
117+
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
118+
$questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question);
119+
} catch (\InvalidArgumentException $e) {
120+
$this->assertSame('Value "" is invalid', $e->getMessage());
121+
}
122+
123+
$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, 1');
124+
$question->setMultiselect(true);
125+
$this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
126+
127+
$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, 1');
128+
$question->setMultiselect(true);
129+
$question->setValidator(null);
130+
$this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
131+
132+
$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '0, Batman');
133+
$question->setMultiselect(true);
134+
$this->assertSame(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
135+
136+
$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, null);
137+
$question->setMultiselect(true);
138+
$this->assertNull($questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question));
139+
140+
try {
141+
$question = new ChoiceQuestion('Who are your favorite superheros?', $heroes, '');
142+
$question->setMultiselect(true);
143+
$questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream, false), $this->createOutputInterface(), $question);
144+
} catch (\InvalidArgumentException $e) {
145+
$this->assertSame('Value "" is invalid', $e->getMessage());
146+
}
147+
}
148+
92149
public function testAsk()
93150
{
94151
$dialog = new QuestionHelper();

0 commit comments

Comments
 (0)