From 0bcc61ce66cba413728f76736cd4fcc8dccc9b68 Mon Sep 17 00:00:00 2001 From: Luis Cordova Date: Thu, 27 Jun 2013 01:04:16 -0500 Subject: [PATCH 1/3] add input to the arguments of dialog helper --- .../Component/Console/Helper/DialogHelper.php | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/DialogHelper.php b/src/Symfony/Component/Console/Helper/DialogHelper.php index 7fc2b3a7397ae..c340842bab8e9 100644 --- a/src/Symfony/Component/Console/Helper/DialogHelper.php +++ b/src/Symfony/Component/Console/Helper/DialogHelper.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Console\Helper; +use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Formatter\OutputFormatterStyle; @@ -28,6 +29,7 @@ class DialogHelper extends Helper /** * Asks the user to select a value. * + * @param InputInterface $input An Input instance * @param OutputInterface $output An Output instance * @param string|array $question The question to ask * @param array $choices List of choices to pick from @@ -40,7 +42,7 @@ class DialogHelper extends Helper * * @throws \InvalidArgumentException */ - public function select(OutputInterface $output, $question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false) + public function select(InputInterface $input, OutputInterface $output, $question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false) { $width = max(array_map('strlen', array_keys($choices))); @@ -51,7 +53,7 @@ public function select(OutputInterface $output, $question, $choices, $default = $output->writeln($messages); - $result = $this->askAndValidate($output, '> ', function ($picked) use ($choices, $errorMessage, $multiselect) { + $result = $this->askAndValidate($input, $output, '> ', function ($picked) use ($choices, $errorMessage, $multiselect) { // Collapse all spaces. $selectedChoices = str_replace(" ", "", $picked); @@ -87,6 +89,7 @@ public function select(OutputInterface $output, $question, $choices, $default = /** * Asks a question to the user. * + * @param InputInterface $input An Input instance * @param OutputInterface $output An Output instance * @param string|array $question The question to ask * @param string $default The default answer if none is given by the user @@ -96,10 +99,14 @@ public function select(OutputInterface $output, $question, $choices, $default = * * @throws \RuntimeException If there is no data to read in the input stream */ - public function ask(OutputInterface $output, $question, $default = null, array $autocomplete = null) + public function ask(InputInterface $input, OutputInterface $output, $question, $default = null, array $autocomplete = null) { $output->write($question); + if (!$input->isInteractive()) { + return $default; + } + $inputStream = $this->inputStream ?: STDIN; if (null === $autocomplete || !$this->hasSttyAvailable()) { @@ -221,17 +228,18 @@ public function ask(OutputInterface $output, $question, $default = null, array $ * * The question will be asked until the user answers by nothing, yes, or no. * + * @param InputInterface $input An Input instance * @param OutputInterface $output An Output instance * @param string|array $question The question to ask * @param Boolean $default The default answer if the user enters nothing * * @return Boolean true if the user has confirmed, false otherwise */ - public function askConfirmation(OutputInterface $output, $question, $default = true) + public function askConfirmation(InputInterface $input, OutputInterface $output, $question, $default = true) { $answer = 'z'; while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) { - $answer = $this->ask($output, $question); + $answer = $this->ask($input, $output, $question); } if (false === $default) { @@ -252,7 +260,7 @@ public function askConfirmation(OutputInterface $output, $question, $default = t * * @throws \RuntimeException In case the fallback is deactivated and the response can not be hidden */ - public function askHiddenResponse(OutputInterface $output, $question, $fallback = true) + public function askHiddenResponse(InputInterface $input, OutputInterface $output, $question, $fallback = true) { if (defined('PHP_WINDOWS_VERSION_BUILD')) { $exe = __DIR__.'/../Resources/bin/hiddeninput.exe'; @@ -305,7 +313,7 @@ public function askHiddenResponse(OutputInterface $output, $question, $fallback } if ($fallback) { - return $this->ask($output, $question); + return $this->ask($input, $output, $question); } throw new \RuntimeException('Unable to hide the response'); @@ -318,6 +326,7 @@ public function askHiddenResponse(OutputInterface $output, $question, $fallback * validated data when the data is valid and throw an exception * otherwise. * + * @param InputInterface $input An Input instance * @param OutputInterface $output An Output instance * @param string|array $question The question to ask * @param callable $validator A PHP callback @@ -329,12 +338,12 @@ public function askHiddenResponse(OutputInterface $output, $question, $fallback * * @throws \Exception When any of the validators return an error */ - public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $default = null, array $autocomplete = null) + public function askAndValidate(InputInterface $input, OutputInterface $output, $question, $validator, $attempts = false, $default = null, array $autocomplete = null) { $that = $this; - $interviewer = function() use ($output, $question, $default, $autocomplete, $that) { - return $that->ask($output, $question, $default, $autocomplete); + $interviewer = function() use ($input, $output, $question, $default, $autocomplete, $that) { + return $that->ask($input, $output, $question, $default, $autocomplete); }; return $this->validateAttempts($interviewer, $output, $validator, $attempts); @@ -347,6 +356,7 @@ public function askAndValidate(OutputInterface $output, $question, $validator, $ * validated data when the data is valid and throw an exception * otherwise. * + * @param InputInterface $input An Input instance * @param OutputInterface $output An Output instance * @param string|array $question The question to ask * @param callable $validator A PHP callback @@ -359,12 +369,12 @@ public function askAndValidate(OutputInterface $output, $question, $validator, $ * @throws \RuntimeException In case the fallback is deactivated and the response can not be hidden * */ - public function askHiddenResponseAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $fallback = true) + public function askHiddenResponseAndValidate(InputInterface $input, OutputInterface $output, $question, $validator, $attempts = false, $fallback = true) { $that = $this; - $interviewer = function() use ($output, $question, $fallback, $that) { - return $that->askHiddenResponse($output, $question, $fallback); + $interviewer = function() use ($input, $output, $question, $fallback, $that) { + return $that->askHiddenResponse($input, $output, $question, $fallback); }; return $this->validateAttempts($interviewer, $output, $validator, $attempts); From 6f1da7103c12a6ee29cf60f8906aa6e609ba50a0 Mon Sep 17 00:00:00 2001 From: Luis Cordova Date: Thu, 27 Jun 2013 01:20:34 -0500 Subject: [PATCH 2/3] fix tests that got broken because of new argument for helper dialog --- .../Console/Tests/Helper/DialogHelperTest.php | 71 ++++++++++--------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php index 108f709bf4db8..00870bd24e900 100644 --- a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php @@ -14,6 +14,7 @@ use Symfony\Component\Console\Helper\DialogHelper; use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Helper\FormatterHelper; +use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\StreamOutput; class DialogHelperTest extends \PHPUnit_Framework_TestCase @@ -26,28 +27,29 @@ public function testSelect() $dialog->setHelperSet($helperSet); $heroes = array('Superman', 'Batman', 'Spiderman'); - $dialog->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n")); - $this->assertEquals('2', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, '2')); - $this->assertEquals('1', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes)); - $this->assertEquals('1', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes)); - $this->assertEquals('1', $dialog->select($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', false)); + $input = new ArrayInput(array()); + + $this->assertEquals('2', $dialog->select($input, $this->getOutputStream(), 'What is your favorite superhero?', $heroes, '2')); + $this->assertEquals('1', $dialog->select($input, $this->getOutputStream(), 'What is your favorite superhero?', $heroes)); + $this->assertEquals('1', $dialog->select($input, $this->getOutputStream(), 'What is your favorite superhero?', $heroes)); + $this->assertEquals('1', $dialog->select($input, $output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', false)); rewind($output->getStream()); $this->assertContains('Input "Fabien" is not a superhero!', stream_get_contents($output->getStream())); try { - $this->assertEquals('1', $dialog->select($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, 1)); + $this->assertEquals('1', $dialog->select($input, $output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, 1)); $this->fail(); } catch (\InvalidArgumentException $e) { $this->assertEquals('Value "Fabien" is invalid', $e->getMessage()); } - $this->assertEquals(array('1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true)); - $this->assertEquals(array('0', '2'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true)); - $this->assertEquals(array('0', '2'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true)); - $this->assertEquals(array('0', '1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, '0,1', false, 'Input "%s" is not a superhero!', true)); - $this->assertEquals(array('0', '1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, ' 0 , 1 ', false, 'Input "%s" is not a superhero!', true)); + $this->assertEquals(array('1'), $dialog->select($input, $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true)); + $this->assertEquals(array('0', '2'), $dialog->select($input, $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true)); + $this->assertEquals(array('0', '2'), $dialog->select($input, $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true)); + $this->assertEquals(array('0', '1'), $dialog->select($input, $this->getOutputStream(), 'What is your favorite superhero?', $heroes, '0,1', false, 'Input "%s" is not a superhero!', true)); + $this->assertEquals(array('0', '1'), $dialog->select($input, $this->getOutputStream(), 'What is your favorite superhero?', $heroes, ' 0 , 1 ', false, 'Input "%s" is not a superhero!', true)); } public function testAsk() @@ -55,9 +57,10 @@ public function testAsk() $dialog = new DialogHelper(); $dialog->setInputStream($this->getInputStream("\n8AM\n")); + $input = new ArrayInput(array()); - $this->assertEquals('2PM', $dialog->ask($this->getOutputStream(), 'What time is it?', '2PM')); - $this->assertEquals('8AM', $dialog->ask($output = $this->getOutputStream(), 'What time is it?', '2PM')); + $this->assertEquals('2PM', $dialog->ask($input, $this->getOutputStream(), 'What time is it?', '2PM')); + $this->assertEquals('8AM', $dialog->ask($input, $output = $this->getOutputStream(), 'What time is it?', '2PM')); rewind($output->getStream()); $this->assertEquals('What time is it?', stream_get_contents($output->getStream())); @@ -83,15 +86,16 @@ public function testAskWithAutocomplete() $dialog->setInputStream($inputStream); $bundles = array('AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle'); - - $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); - $this->assertEquals('AsseticBundleTest', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); - $this->assertEquals('FrameworkBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); - $this->assertEquals('SecurityBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); - $this->assertEquals('FooBundleTest', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); - $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); - $this->assertEquals('AsseticBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); - $this->assertEquals('FooBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); + $input = new ArrayInput(array()); + + $this->assertEquals('AcmeDemoBundle', $dialog->ask($input, $this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); + $this->assertEquals('AsseticBundleTest', $dialog->ask($input, $this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); + $this->assertEquals('FrameworkBundle', $dialog->ask($input, $this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); + $this->assertEquals('SecurityBundle', $dialog->ask($input, $this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); + $this->assertEquals('FooBundleTest', $dialog->ask($input, $this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); + $this->assertEquals('AcmeDemoBundle', $dialog->ask($input, $this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); + $this->assertEquals('AsseticBundle', $dialog->ask($input, $this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); + $this->assertEquals('FooBundle', $dialog->ask($input, $this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles)); } public function testAskHiddenResponse() @@ -103,8 +107,9 @@ public function testAskHiddenResponse() $dialog = new DialogHelper(); $dialog->setInputStream($this->getInputStream("8AM\n")); + $input = new ArrayInput(array()); - $this->assertEquals('8AM', $dialog->askHiddenResponse($this->getOutputStream(), 'What time is it?')); + $this->assertEquals('8AM', $dialog->askHiddenResponse($input, $this->getOutputStream(), 'What time is it?')); } public function testAskConfirmation() @@ -112,16 +117,17 @@ public function testAskConfirmation() $dialog = new DialogHelper(); $dialog->setInputStream($this->getInputStream("\n\n")); - $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?')); - $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false)); + $input = new ArrayInput(array()); + $this->assertTrue($dialog->askConfirmation($input, $this->getOutputStream(), 'Do you like French fries?')); + $this->assertFalse($dialog->askConfirmation($input, $this->getOutputStream(), 'Do you like French fries?', false)); $dialog->setInputStream($this->getInputStream("y\nyes\n")); - $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false)); - $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false)); + $this->assertTrue($dialog->askConfirmation($input, $this->getOutputStream(), 'Do you like French fries?', false)); + $this->assertTrue($dialog->askConfirmation($input, $this->getOutputStream(), 'Do you like French fries?', false)); $dialog->setInputStream($this->getInputStream("n\nno\n")); - $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true)); - $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true)); + $this->assertFalse($dialog->askConfirmation($input, $this->getOutputStream(), 'Do you like French fries?', true)); + $this->assertFalse($dialog->askConfirmation($input, $this->getOutputStream(), 'Do you like French fries?', true)); } public function testAskAndValidate() @@ -141,12 +147,13 @@ public function testAskAndValidate() }; $dialog->setInputStream($this->getInputStream("\nblack\n")); - $this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white')); - $this->assertEquals('black', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white')); + $input = new ArrayInput(array()); + $this->assertEquals('white', $dialog->askAndValidate($input, $this->getOutputStream(), $question, $validator, 2, 'white')); + $this->assertEquals('black', $dialog->askAndValidate($input, $this->getOutputStream(), $question, $validator, 2, 'white')); $dialog->setInputStream($this->getInputStream("green\nyellow\norange\n")); try { - $this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white')); + $this->assertEquals('white', $dialog->askAndValidate($input, $this->getOutputStream(), $question, $validator, 2, 'white')); $this->fail(); } catch (\InvalidArgumentException $e) { $this->assertEquals($error, $e->getMessage()); From 1a458271645bb24ed3cb8466dd85589cd47f89aa Mon Sep 17 00:00:00 2001 From: Luis Cordova Date: Thu, 27 Jun 2013 01:28:12 -0500 Subject: [PATCH 3/3] add test for no interaction aka -n --- .../Component/Console/Tests/Helper/DialogHelperTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php index 00870bd24e900..30afa3fb04be9 100644 --- a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php @@ -160,6 +160,14 @@ public function testAskAndValidate() } } + public function testNoInteraction() + { + $dialog = new DialogHelper(); + $input = new ArrayInput(array()); + $input->setInteractive(false); + $this->assertEquals('not yet', $dialog->ask($input, $this->getOutputStream(), 'Do you have a job?', 'not yet')); + } + protected function getInputStream($input) { $stream = fopen('php://memory', 'r+', false);