123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace Composer\Question\Test;
- use Composer\Question\StrictConfirmationQuestion;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\Console\Exception\InvalidArgumentException;
- use Symfony\Component\Console\Helper\QuestionHelper;
- use Symfony\Component\Console\Output\StreamOutput;
- class StrictConfirmationQuestionTest extends TestCase
- {
- public function getAskConfirmationBadData()
- {
- return array(
- array('not correct'),
- array('no more'),
- array('yes please'),
- array('yellow'),
- );
- }
-
- public function testAskConfirmationBadAnswer($answer)
- {
- $dialog = new QuestionHelper();
- $dialog->setInputStream($this->getInputStream($answer."\n"));
- $question = new StrictConfirmationQuestion('Do you like French fries?');
- $question->setMaxAttempts(1);
- $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
- }
-
- public function testAskConfirmation($question, $expected, $default = true)
- {
- $dialog = new QuestionHelper();
- $dialog->setInputStream($this->getInputStream($question."\n"));
- $question = new StrictConfirmationQuestion('Do you like French fries?', $default);
- $this->assertEquals($expected, $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question), 'confirmation question should '.($expected ? 'pass' : 'cancel'));
- }
- public function getAskConfirmationData()
- {
- return array(
- array('', true),
- array('', false, false),
- array('y', true),
- array('yes', true),
- array('n', false),
- array('no', false),
- );
- }
- public function testAskConfirmationWithCustomTrueAndFalseAnswer()
- {
- $dialog = new QuestionHelper();
- $question = new StrictConfirmationQuestion('Do you like French fries?', false, '/^ja$/i', '/^nein$/i');
- $dialog->setInputStream($this->getInputStream("ja\n"));
- $this->assertTrue($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
- $dialog->setInputStream($this->getInputStream("nein\n"));
- $this->assertFalse($dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
- }
- protected function getInputStream($input)
- {
- $stream = fopen('php://memory', 'r+', false);
- fwrite($stream, $input);
- rewind($stream);
- return $stream;
- }
- protected function createOutputInterface()
- {
- return new StreamOutput(fopen('php://memory', 'r+', false));
- }
- protected function createInputInterfaceMock($interactive = true)
- {
- $mock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
- $mock->expects($this->any())
- ->method('isInteractive')
- ->will($this->returnValue($interactive));
- return $mock;
- }
- }
|