1
0
Fork 0
composer/tests/Composer/Test/Question/StrictConfirmationQuestionT...

144 lines
3.9 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Test\Question;
use Composer\Question\StrictConfirmationQuestion;
2020-02-07 03:18:45 +00:00
use Composer\Test\TestCase;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\StreamOutput;
/**
* based on Symfony\Component\Console\Tests\Helper\QuestionHelperTest
*
* @author Theo Tonge <theo@theotonge.co.uk>
*/
class StrictConfirmationQuestionTest extends TestCase
{
2021-10-26 19:17:28 +00:00
/**
* @return string[][]
*
* @phpstan-return list<array{non-empty-string}>
*/
public function getAskConfirmationBadData()
{
return array(
array('not correct'),
array('no more'),
array('yes please'),
array('yellow'),
);
}
/**
2021-10-26 19:17:28 +00:00
* @dataProvider getAskConfirmationBadData
*
* @param string $answer
*/
public function testAskConfirmationBadAnswer($answer)
{
list($input, $dialog) = $this->createInput($answer."\n");
2021-12-09 19:55:26 +00:00
self::expectException('InvalidArgumentException');
self::expectExceptionMessage('Please answer yes, y, no, or n.');
$question = new StrictConfirmationQuestion('Do you like French fries?');
$question->setMaxAttempts(1);
$dialog->ask($input, $this->createOutputInterface(), $question);
}
/**
* @dataProvider getAskConfirmationData
2021-10-26 19:17:28 +00:00
*
* @param string $question
* @param bool $expected
* @param bool $default
*/
public function testAskConfirmation($question, $expected, $default = true)
{
list($input, $dialog) = $this->createInput($question."\n");
$question = new StrictConfirmationQuestion('Do you like French fries?', $default);
$this->assertEquals($expected, $dialog->ask($input, $this->createOutputInterface(), $question), 'confirmation question should '.($expected ? 'pass' : 'cancel'));
}
2021-10-26 19:17:28 +00:00
/**
* @return mixed[][]
*
* @phpstan-return list<array{string, bool}>|list<array{string, bool, bool}>
*/
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()
{
$question = new StrictConfirmationQuestion('Do you like French fries?', false, '/^ja$/i', '/^nein$/i');
list($input, $dialog) = $this->createInput("ja\n");
$this->assertTrue($dialog->ask($input, $this->createOutputInterface(), $question));
list($input, $dialog) = $this->createInput("nein\n");
$this->assertFalse($dialog->ask($input, $this->createOutputInterface(), $question));
}
2021-10-26 19:17:28 +00:00
/**
* @param string $input
*
* @return resource
*/
protected function getInputStream($input)
{
$stream = fopen('php://memory', 'r+', false);
2021-10-26 19:17:28 +00:00
$this->assertNotFalse($stream);
fwrite($stream, $input);
rewind($stream);
return $stream;
}
2021-10-26 19:17:28 +00:00
/**
* @return StreamOutput
*/
protected function createOutputInterface()
{
return new StreamOutput(fopen('php://memory', 'r+', false));
}
2021-10-26 19:17:28 +00:00
/**
* @param string $entry
*
* @return object[]
*
* @phpstan-return array{ArrayInput, QuestionHelper}
*/
protected function createInput($entry)
{
$input = new ArrayInput(array('--no-interaction'));
$input->setStream($this->getInputStream($entry));
$dialog = new QuestionHelper();
return array($input, $dialog);
}
}