From decb2bca1535136caafd61d436d0a18ddae0e167 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 8b0730ee3e5..c9b9f640388 100644 --- a/components/console/helpers/dialoghelper.rst +++ b/components/console/helpers/dialoghelper.rst @@ -247,13 +247,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'); @@ -279,3 +284,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 d698af4a96c..03a82709658 100644 --- a/components/console/introduction.rst +++ b/components/console/introduction.rst @@ -411,6 +411,8 @@ tools capable of helping you with different tasks: * :doc:`/components/console/helpers/tablehelper`: displays tabular data as a table * :doc:`/components/console/helpers/dialoghelper`: (deprecated) interactively ask the user for information +.. _component-console-testing-commands: + Testing Commands ----------------