From debbf98bc5405ae54cd3a59ad1eca4305211805c Mon Sep 17 00:00:00 2001 From: Sebastian Goettschkes Date: Sat, 30 Mar 2013 10:40:01 +0100 Subject: [PATCH 1/3] Adding description of how to test a command which expects user input --- components/console/helpers/dialoghelper.rst | 47 ++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/components/console/helpers/dialoghelper.rst b/components/console/helpers/dialoghelper.rst index 70a0bcd60d1..63a0fa51d1b 100644 --- a/components/console/helpers/dialoghelper.rst +++ b/components/console/helpers/dialoghelper.rst @@ -97,4 +97,49 @@ You can set the max number of times to ask in the ``$attempts`` argument. If you reach this max number it will use the default value, which is given in the last argument. Using ``false`` means the amount of attempts is infinite. The user will be asked as long as he provides an invalid answer and will only -be able to proceed if her input is valid. \ No newline at end of file +be able to proceed if her input is valid. + +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\Helper\DialogHelper; + use Symfony\Component\Console\Helper\HelperSet; + + // ... + + public function testExecute() + { + + // .. + + $commandTester = new CommandTester($command); + + $dialog = new DialogHelper(); + $dialog->setInputStream($this->getInputStream('Test\n')); + // Equals to a user inputing "Test" and hitting ENTER + // If you need to enter a confirmation, "yes\n" will work + + $command->setHelperSet(new HelperSet(array($dialog))); + + $commandTester->execute(array('command' => $command->getName())); + + // assert + + } + + protected function getInputStream($input) + { + $stream = fopen('php://memory', 'r+', false); + fputs($stream, $input); + rewind($stream); + + return $stream; + } + +By setting the inputStream of the `DialogHelper`, you do the same 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. \ No newline at end of file From 5ac4466d2f196196f1ff9efb66083620f4ded90f Mon Sep 17 00:00:00 2001 From: Sebastian Goettschkes Date: Sat, 30 Mar 2013 11:03:28 +0100 Subject: [PATCH 2/3] Minor improvements suggested by @WouterJ --- components/console/helpers/dialoghelper.rst | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/components/console/helpers/dialoghelper.rst b/components/console/helpers/dialoghelper.rst index 63a0fa51d1b..1d7f551cc7c 100644 --- a/components/console/helpers/dialoghelper.rst +++ b/components/console/helpers/dialoghelper.rst @@ -109,12 +109,9 @@ from the command line, you need to overwrite the HelperSet used by the command:: use Symfony\Component\Console\Helper\HelperSet; // ... - public function testExecute() { - - // .. - + // ... $commandTester = new CommandTester($command); $dialog = new DialogHelper(); @@ -126,8 +123,7 @@ from the command line, you need to overwrite the HelperSet used by the command:: $commandTester->execute(array('command' => $command->getName())); - // assert - + // $this->assertRegExp('/.../', $commandTester->getDisplay()); } protected function getInputStream($input) From b79b9cf7b9c1e09b39a8cfb1df71456c0c261cf0 Mon Sep 17 00:00:00 2001 From: Sebastian Goettschkes Date: Sat, 30 Mar 2013 20:10:20 +0100 Subject: [PATCH 3/3] Refactoring example code for the dialog helper command test --- components/console/helpers/dialoghelper.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/components/console/helpers/dialoghelper.rst b/components/console/helpers/dialoghelper.rst index 1d7f551cc7c..6025332e148 100644 --- a/components/console/helpers/dialoghelper.rst +++ b/components/console/helpers/dialoghelper.rst @@ -114,13 +114,11 @@ from the command line, you need to overwrite the HelperSet used by the command:: // ... $commandTester = new CommandTester($command); - $dialog = new DialogHelper(); + $dialog = $command->getHelper('dialog'); $dialog->setInputStream($this->getInputStream('Test\n')); // Equals to a user inputing "Test" and hitting ENTER // If you need to enter a confirmation, "yes\n" will work - $command->setHelperSet(new HelperSet(array($dialog))); - $commandTester->execute(array('command' => $command->getName())); // $this->assertRegExp('/.../', $commandTester->getDisplay());