Skip to content

Commit

Permalink
bug #4582 Completed the needed context to successfully test commands …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
wouterj committed Dec 28, 2014
2 parents 3f3464f + a94bd71 commit cad4d3f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions components/console/helpers/dialoghelper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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>`.
2 changes: 2 additions & 0 deletions components/console/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------

Expand Down

0 comments on commit cad4d3f

Please sign in to comment.