Browse files

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).


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.


a94bd71 Completed the needed context to successfully test commands
  • Loading branch information...
wouterj committed Dec 28, 2014
2 parents 3f3464f + a94bd71 commit cad4d3f88bc3edffab2133f0689d03d4297ddeb4
Showing with 12 additions and 0 deletions.
  1. +10 −0 components/console/helpers/dialoghelper.rst
  2. +2 −0 components/console/introduction.rst
@@ -253,13 +253,18 @@ Testing a Command which Expects Input
If you want to write a unit test for a command which expects some kind of input
from the command line, you need to overwrite the HelperSet used by the command::
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Tester\CommandTester;
// ...
public function testExecute()
// ...
$application = new Application();
$application->add(new MyCommand());
$command = $application->find('my:command:name');
$commandTester = new CommandTester($command);
$dialog = $command->getHelper('dialog');
@@ -285,3 +290,8 @@ By setting the input stream of the ``DialogHelper``, you imitate what the
console would do internally with all user input through the cli. This way
you can test any user interaction (even complex ones) by passing an appropriate
input stream.
.. seealso::
You find more information about testing commands in the console component
docs about :ref:`testing console commands <component-console-testing-commands>`.
@@ -377,6 +377,8 @@ tools capable of helping you with different tasks:
* :doc:`/components/console/helpers/progresshelper`: shows a progress bar
* :doc:`/components/console/helpers/tablehelper`: displays tabular data as a table
.. _component-console-testing-commands:
Testing Commands

0 comments on commit cad4d3f

Please sign in to comment.