Skip to content

Commit cad4d3f

Browse files
committed
bug #4582 Completed the needed context to successfully test commands with Helpers (peterrehm)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #4582). Discussion ---------- Completed the needed context to successfully test commands with Helpers | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | all | Fixed tickets | #4581 As mentioned in symfony/symfony#12798 the explanation about the initialization is not clear. Therefore I have added the needed context and a reference to the related cookbook article. Commits ------- a94bd71 Completed the needed context to successfully test commands
2 parents 3f3464f + a94bd71 commit cad4d3f

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)