@@ -11,15 +11,15 @@ helper set, which you can get by calling
11
11
12
12
$dialog = $this->getHelperSet()->get('dialog');
13
13
14
- All the methods inside the Dialog Helper have an
14
+ All the methods inside the Dialog Helper have an
15
15
:class: `Symfony\\ Component\\ Console\\ Output\\ OutputInterface ` as first the
16
16
argument, the question as the second argument and the default value as last
17
17
argument.
18
18
19
19
Asking the User for confirmation
20
20
--------------------------------
21
21
22
- Suppose you want to confirm an action before actually executing it. Add
22
+ Suppose you want to confirm an action before actually executing it. Add
23
23
the following to your command::
24
24
25
25
// ...
@@ -32,8 +32,8 @@ the following to your command::
32
32
}
33
33
34
34
In this case, the user will be asked "Continue with this action", and will return
35
- ``true `` if the user answers with ``y `` or false in any other case. The third
36
- argument to ``askConfirmation `` is the default value to return if the user doesn't
35
+ ``true `` if the user answers with ``y `` or false in any other case. The third
36
+ argument to ``askConfirmation `` is the default value to return if the user doesn't
37
37
enter any input.
38
38
39
39
Asking the User for Information
@@ -82,8 +82,8 @@ Validating the Answer
82
82
83
83
You can even validate the answer. For instance, in the last example you asked
84
84
for the bundle name. Following the Symfony2 naming conventions, it should
85
- be suffixed with ``Bundle ``. You can validate that by using the
86
- :method: `Symfony\\ Component\\ Console\\ Helper\\ DialogHelper::askAndValidate `
85
+ be suffixed with ``Bundle ``. You can validate that by using the
86
+ :method: `Symfony\\ Component\\ Console\\ Helper\\ DialogHelper::askAndValidate `
87
87
method::
88
88
89
89
// ...
@@ -105,16 +105,16 @@ method::
105
105
This methods has 2 new arguments, the full signature is::
106
106
107
107
askAndValidate(
108
- OutputInterface $output,
109
- string|array $question,
110
- callback $validator,
111
- integer $attempts = false,
108
+ OutputInterface $output,
109
+ string|array $question,
110
+ callback $validator,
111
+ integer $attempts = false,
112
112
string $default = null
113
113
)
114
114
115
115
The ``$validator `` is a callback which handles the validation. It should
116
116
throw an exception if there is something wrong. The exception message is displayed
117
- in the console, so it is a good practice to put some useful information in it. The callback
117
+ in the console, so it is a good practice to put some useful information in it. The callback
118
118
function should also return the value of the user's input if the validation was successful.
119
119
120
120
You can set the max number of times to ask in the ``$attempts `` argument.
@@ -162,24 +162,24 @@ could use the ``ask`` method described above or, to make sure the user
162
162
provided a correct answer, the ``askAndValidate `` method. Both have
163
163
the disadvantage that you need to handle incorrect values yourself.
164
164
165
- Instead, you can use the
165
+ Instead, you can use the
166
166
:method: `Symfony\\ Component\\ Console\\ Helper\\ DialogHelper::select `
167
167
method, which makes sure that the user can only enter a valid string
168
168
from a predefined list::
169
169
170
170
$dialog = $app->getHelperSet()->get('dialog');
171
171
$colors = array('red', 'blue', 'yellow');
172
-
172
+
173
173
$color = $dialog->select(
174
- $output,
175
- 'Please select your favorite color (default to red)',
176
- $colors,
174
+ $output,
175
+ 'Please select your favorite color (default to red)',
176
+ $colors,
177
177
0
178
178
);
179
179
$output->writeln('You have just selected: ' . $colors[$color]);
180
-
180
+
181
181
// ... do something with the color
182
-
182
+
183
183
The option which should be selected by default is provided with the fourth
184
184
parameter. The default is ``null ``, which means that no option is the default one.
185
185
@@ -197,23 +197,23 @@ from the command line, you need to overwrite the HelperSet used by the command::
197
197
198
198
use Symfony\Component\Console\Helper\DialogHelper;
199
199
use Symfony\Component\Console\Helper\HelperSet;
200
-
200
+
201
201
// ...
202
202
public function testExecute()
203
203
{
204
204
// ...
205
205
$commandTester = new CommandTester($command);
206
-
206
+
207
207
$dialog = $command->getHelper('dialog');
208
- $dialog->setInputStream($this->getInputStream('Test\n'));
208
+ $dialog->setInputStream($this->getInputStream('Test\n'));
209
209
// Equals to a user inputing "Test" and hitting ENTER
210
210
// If you need to enter a confirmation, "yes\n" will work
211
-
211
+
212
212
$commandTester->execute(array('command' => $command->getName()));
213
-
213
+
214
214
// $this->assertRegExp('/.../', $commandTester->getDisplay());
215
215
}
216
-
216
+
217
217
protected function getInputStream($input)
218
218
{
219
219
$stream = fopen('php://memory', 'r+', false);
@@ -222,7 +222,7 @@ from the command line, you need to overwrite the HelperSet used by the command::
222
222
223
223
return $stream;
224
224
}
225
-
225
+
226
226
By setting the inputStream of the ``DialogHelper ``, you imitate what the
227
227
console would do internally with all user input through the cli. This way
228
228
you can test any user interaction (even complex ones) by passing an appropriate
0 commit comments