StrictConfirmationQuestionTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\Question\Test;
  12. use Composer\Question\StrictConfirmationQuestion;
  13. use PHPUnit\Framework\TestCase;
  14. use Symfony\Component\Console\Exception\InvalidArgumentException;
  15. use Symfony\Component\Console\Helper\QuestionHelper;
  16. use Symfony\Component\Console\Output\StreamOutput;
  17. /**
  18. * based on Symfony\Component\Console\Tests\Helper\QuestionHelperTest
  19. *
  20. * @author Theo Tonge <theo@theotonge.co.uk>
  21. */
  22. class StrictConfirmationQuestionTest extends TestCase
  23. {
  24. public function getAskConfirmationBadData()
  25. {
  26. return array(
  27. array('not correct'),
  28. array('no more'),
  29. array('yes please'),
  30. array('yellow'),
  31. );
  32. }
  33. /**
  34. * @expectedException InvalidArgumentException
  35. * @expectedExceptionMessage Please answer yes, y, no, or n.
  36. * @dataProvider getAskConfirmationBadData
  37. */
  38. public function testAskConfirmationBadAnswer($answer)
  39. {
  40. $dialog = new QuestionHelper();
  41. $dialog->setInputStream($this->getInputStream($answer."\n"));
  42. $question = new StrictConfirmationQuestion('Do you like French fries?');
  43. $question->setMaxAttempts(1);
  44. $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
  45. }
  46. /**
  47. * @dataProvider getAskConfirmationData
  48. */
  49. public function testAskConfirmation($question, $expected, $default = true)
  50. {
  51. $dialog = new QuestionHelper();
  52. $dialog->setInputStream($this->getInputStream($question."\n"));
  53. $question = new StrictConfirmationQuestion('Do you like French fries?', $default);
  54. $this->assertEquals($expected, $dialog->ask($this->createInputInterfaceMock(), $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. $dialog = new QuestionHelper();
  70. $question = new StrictConfirmationQuestion('Do you like French fries?', false, '/^ja$/i', '/^nein$/i');
  71. $dialog->setInputStream($this->getInputStream("ja\n"));
  72. $this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  73. $dialog->setInputStream($this->getInputStream("nein\n"));
  74. $this->assertFalse($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  75. }
  76. protected function getInputStream($input)
  77. {
  78. $stream = fopen('php://memory', 'r+', false);
  79. fwrite($stream, $input);
  80. rewind($stream);
  81. return $stream;
  82. }
  83. protected function createOutputInterface()
  84. {
  85. return new StreamOutput(fopen('php://memory', 'r+', false));
  86. }
  87. protected function createInputInterfaceMock($interactive = true)
  88. {
  89. $mock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  90. $mock->expects($this->any())
  91. ->method('isInteractive')
  92. ->will($this->returnValue($interactive));
  93. return $mock;
  94. }
  95. }