2017-04-10 20:21:53 +00:00
< ? 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 .
*/
2018-11-12 14:23:32 +00:00
namespace Composer\Test\Question ;
2017-04-10 20:21:53 +00:00
use Composer\Question\StrictConfirmationQuestion ;
2020-02-07 03:18:45 +00:00
use Composer\Test\TestCase ;
2017-04-10 20:21:53 +00:00
use Symfony\Component\Console\Exception\InvalidArgumentException ;
use Symfony\Component\Console\Helper\QuestionHelper ;
2017-11-06 15:32:29 +00:00
use Symfony\Component\Console\Input\ArrayInput ;
use Symfony\Component\Console\Input\StreamableInputInterface ;
2017-04-10 20:21:53 +00:00
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
{
public function getAskConfirmationBadData ()
{
return array (
array ( 'not correct' ),
array ( 'no more' ),
array ( 'yes please' ),
array ( 'yellow' ),
);
}
/**
* @ dataProvider getAskConfirmationBadData
*/
public function testAskConfirmationBadAnswer ( $answer )
{
2017-11-06 15:32:29 +00:00
list ( $input , $dialog ) = $this -> createInput ( $answer . " \n " );
2020-09-10 15:21:11 +00:00
$this -> setExpectedException ( 'InvalidArgumentException' , 'Please answer yes, y, no, or n.' );
2017-04-10 20:21:53 +00:00
$question = new StrictConfirmationQuestion ( 'Do you like French fries?' );
$question -> setMaxAttempts ( 1 );
2017-11-06 15:32:29 +00:00
$dialog -> ask ( $input , $this -> createOutputInterface (), $question );
2017-04-10 20:21:53 +00:00
}
/**
* @ dataProvider getAskConfirmationData
*/
public function testAskConfirmation ( $question , $expected , $default = true )
{
2017-11-06 15:32:29 +00:00
list ( $input , $dialog ) = $this -> createInput ( $question . " \n " );
2017-04-10 20:21:53 +00:00
$question = new StrictConfirmationQuestion ( 'Do you like French fries?' , $default );
2017-11-06 15:32:29 +00:00
$this -> assertEquals ( $expected , $dialog -> ask ( $input , $this -> createOutputInterface (), $question ), 'confirmation question should ' . ( $expected ? 'pass' : 'cancel' ));
2017-04-10 20:21:53 +00:00
}
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' );
2017-11-06 15:32:29 +00:00
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 ));
2017-04-10 20:21:53 +00:00
}
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 ));
}
2017-11-06 15:32:29 +00:00
protected function createInput ( $entry )
2017-04-10 20:21:53 +00:00
{
2017-11-06 15:32:29 +00:00
$stream = $this -> getInputStream ( $entry );
$input = new ArrayInput ( array ( '--no-interaction' ));
$dialog = new QuestionHelper ();
if ( method_exists ( $dialog , 'setInputStream' )) {
$dialog -> setInputStream ( $stream );
}
if ( $input instanceof StreamableInputInterface ) {
$input -> setStream ( $stream );
}
2017-04-10 20:21:53 +00:00
2017-11-06 15:32:29 +00:00
return array ( $input , $dialog );
2017-04-10 20:21:53 +00:00
}
}