Skip to content

Commit a94bd71

Browse files
peterrehmwouterj
authored andcommitted
Completed the needed context to successfully test commands
1 parent 3f3464f commit a94bd71

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

components/console/helpers/dialoghelper.rst

+10
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,18 @@ Testing a Command which Expects Input
253253
If you want to write a unit test for a command which expects some kind of input
254254
from the command line, you need to overwrite the HelperSet used by the command::
255255

256+
use Symfony\Component\Console\Application;
256257
use Symfony\Component\Console\Helper\DialogHelper;
257258
use Symfony\Component\Console\Helper\HelperSet;
259+
use Symfony\Component\Console\Tester\CommandTester;
258260

259261
// ...
260262
public function testExecute()
261263
{
262264
// ...
265+
$application = new Application();
266+
$application->add(new MyCommand());
267+
$command = $application->find('my:command:name');
263268
$commandTester = new CommandTester($command);
264269

265270
$dialog = $command->getHelper('dialog');
@@ -285,3 +290,8 @@ By setting the input stream of the ``DialogHelper``, you imitate what the
285290
console would do internally with all user input through the cli. This way
286291
you can test any user interaction (even complex ones) by passing an appropriate
287292
input stream.
293+
294+
.. seealso::
295+
296+
You find more information about testing commands in the console component
297+
docs about :ref:`testing console commands <component-console-testing-commands>`.

components/console/introduction.rst

+2
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ tools capable of helping you with different tasks:
377377
* :doc:`/components/console/helpers/progresshelper`: shows a progress bar
378378
* :doc:`/components/console/helpers/tablehelper`: displays tabular data as a table
379379

380+
.. _component-console-testing-commands:
381+
380382
Testing Commands
381383
----------------
382384

0 commit comments

Comments
 (0)