From a94bd718bc0c19b92f6b955b0e92b8daf1b33ce4 Mon Sep 17 00:00:00 2001 From: Peter Rehm Date: Wed, 3 Dec 2014 22:38:27 +0100 Subject: [PATCH] Completed the needed context to successfully test commands --- components/console/helpers/dialoghelper.rst | 10 ++++++++++ components/console/introduction.rst | 2 ++ 2 files changed, 12 insertions(+) diff --git a/components/console/helpers/dialoghelper.rst b/components/console/helpers/dialoghelper.rst index b6544139a26..96c5c880d09 100644 --- a/components/console/helpers/dialoghelper.rst +++ b/components/console/helpers/dialoghelper.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 `. diff --git a/components/console/introduction.rst b/components/console/introduction.rst index 3461eefebc0..2b80d439c5a 100644 --- a/components/console/introduction.rst +++ b/components/console/introduction.rst @@ -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 ----------------