StrictConfirmationQuestionTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Question;
  12. use Composer\Question\StrictConfirmationQuestion;
  13. use Composer\Test\TestCase;
  14. use Symfony\Component\Console\Exception\InvalidArgumentException;
  15. use Symfony\Component\Console\Helper\QuestionHelper;
  16. use Symfony\Component\Console\Input\ArrayInput;
  17. use Symfony\Component\Console\Input\StreamableInputInterface;
  18. use Symfony\Component\Console\Output\StreamOutput;
  19. /**
  20. * based on Symfony\Component\Console\Tests\Helper\QuestionHelperTest
  21. *
  22. * @author Theo Tonge <theo@theotonge.co.uk>
  23. */
  24. class StrictConfirmationQuestionTest extends TestCase
  25. {
  26. public function getAskConfirmationBadData()
  27. {
  28. return array(
  29. array('not correct'),
  30. array('no more'),
  31. array('yes please'),
  32. array('yellow'),
  33. );
  34. }
  35. /**
  36. * @expectedException InvalidArgumentException
  37. * @expectedExceptionMessage Please answer yes, y, no, or n.
  38. * @dataProvider getAskConfirmationBadData
  39. */
  40. public function testAskConfirmationBadAnswer($answer)
  41. {
  42. list($input, $dialog) = $this->createInput($answer."\n");
  43. $question = new StrictConfirmationQuestion('Do you like French fries?');
  44. $question->setMaxAttempts(1);
  45. $dialog->ask($input, $this->createOutputInterface(), $question);
  46. }
  47. /**
  48. * @dataProvider getAskConfirmationData
  49. */
  50. public function testAskConfirmation($question, $expected, $default = true)
  51. {
  52. list($input, $dialog) = $this->createInput($question."\n");
  53. $question = new StrictConfirmationQuestion('Do you like French fries?', $default);
  54. $this->assertEquals($expected, $dialog->ask($input, $this->createOutputInterface(), $question), 'confirmation question should '.($expected ? 'pass' : 'cancel'));
  55. }
  56. public function getAskConfirmationData()
  57. {
  58. return array(
  59. array('', true),
  60. array('', false, false),
  61. array('y', true),
  62. array('yes', true),
  63. array('n', false),
  64. array('no', false),
  65. );
  66. }
  67. public function testAskConfirmationWithCustomTrueAndFalseAnswer()
  68. {
  69. $question = new StrictConfirmationQuestion('Do you like French fries?', false, '/^ja$/i', '/^nein$/i');
  70. list($input, $dialog) = $this->createInput("ja\n");
  71. $this->assertTrue($dialog->ask($input, $this->createOutputInterface(), $question));
  72. list($input, $dialog) = $this->createInput("nein\n");
  73. $this->assertFalse($dialog->ask($input, $this->createOutputInterface(), $question));
  74. }
  75. protected function getInputStream($input)
  76. {
  77. $stream = fopen('php://memory', 'r+', false);
  78. fwrite($stream, $input);
  79. rewind($stream);
  80. return $stream;
  81. }
  82. protected function createOutputInterface()
  83. {
  84. return new StreamOutput(fopen('php://memory', 'r+', false));
  85. }
  86. protected function createInput($entry)
  87. {
  88. $stream = $this->getInputStream($entry);
  89. $input = new ArrayInput(array('--no-interaction'));
  90. $dialog = new QuestionHelper();
  91. if (method_exists($dialog, 'setInputStream')) {
  92. $dialog->setInputStream($stream);
  93. }
  94. if ($input instanceof StreamableInputInterface) {
  95. $input->setStream($stream);
  96. }
  97. return array($input, $dialog);
  98. }
  99. }