DialogHelperTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Console\Helper;
  11. use Symfony\Component\Console\Helper\DialogHelper;
  12. use Symfony\Component\Console\Helper\HelperSet;
  13. use Symfony\Component\Console\Helper\FormatterHelper;
  14. use Symfony\Component\Console\Output\StreamOutput;
  15. class DialogHelperTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testAsk()
  18. {
  19. $dialog = new DialogHelper();
  20. $dialog->setInputStream($this->getInputStream("\n8AM\n"));
  21. $this->assertEquals('2PM', $dialog->ask($this->getOutputStream(), 'What time is it?', '2PM'));
  22. $this->assertEquals('8AM', $dialog->ask($output = $this->getOutputStream(), 'What time is it?', '2PM'));
  23. rewind($output->getStream());
  24. $this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
  25. }
  26. public function testAskConfirmation()
  27. {
  28. $dialog = new DialogHelper();
  29. $dialog->setInputStream($this->getInputStream("\n\n"));
  30. $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?'));
  31. $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
  32. $dialog->setInputStream($this->getInputStream("y\nyes\n"));
  33. $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
  34. $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
  35. $dialog->setInputStream($this->getInputStream("n\nno\n"));
  36. $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
  37. $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
  38. }
  39. public function testAskAndValidate()
  40. {
  41. $dialog = new DialogHelper();
  42. $helperSet = new HelperSet(array(new FormatterHelper()));
  43. $dialog->setHelperSet($helperSet);
  44. $question ='What color was the white horse of Henry IV?';
  45. $error = 'This is not a color!';
  46. $validator = function ($color) use ($error) {
  47. if (!in_array($color, array('white', 'black'))) {
  48. throw new \InvalidArgumentException($error);
  49. }
  50. return $color;
  51. };
  52. $dialog->setInputStream($this->getInputStream("\nblack\n"));
  53. $this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
  54. $this->assertEquals('black', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
  55. $dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
  56. try {
  57. $this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
  58. $this->fail();
  59. } catch (\InvalidArgumentException $e) {
  60. $this->assertEquals($error, $e->getMessage());
  61. }
  62. }
  63. protected function getInputStream($input)
  64. {
  65. $stream = fopen('php://memory', 'r+', false);
  66. fputs($stream, $input);
  67. rewind($stream);
  68. return $stream;
  69. }
  70. protected function getOutputStream()
  71. {
  72. return new StreamOutput(fopen('php://memory', 'r+', false));
  73. }
  74. }