2022-02-23 15:58:18 +00:00
< ? php declare ( strict_types = 1 );
2017-04-10 20:21:53 +00:00
/*
* 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\Helper\QuestionHelper ;
2017-11-06 15:32:29 +00:00
use Symfony\Component\Console\Input\ArrayInput ;
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
{
2021-10-26 19:17:28 +00:00
/**
* @ return string [][]
*
* @ phpstan - return list < array { non - empty - string } >
*/
2022-11-24 13:39:08 +00:00
public static function getAskConfirmationBadData () : array
2017-04-10 20:21:53 +00:00
{
2022-08-17 12:20:07 +00:00
return [
[ 'not correct' ],
[ 'no more' ],
[ 'yes please' ],
[ 'yellow' ],
];
2017-04-10 20:21:53 +00:00
}
/**
2021-10-26 19:17:28 +00:00
* @ dataProvider getAskConfirmationBadData
2017-04-10 20:21:53 +00:00
*/
2022-02-22 15:47:09 +00:00
public function testAskConfirmationBadAnswer ( string $answer ) : void
2017-04-10 20:21:53 +00:00
{
2022-08-17 12:20:07 +00:00
[ $input , $dialog ] = $this -> createInput ( $answer . " \n " );
2017-11-06 15:32:29 +00:00
2021-12-09 19:55:26 +00:00
self :: expectException ( 'InvalidArgumentException' );
self :: expectExceptionMessage ( 'Please answer yes, y, no, or n.' );
2020-09-10 15:21:11 +00:00
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
*/
2022-02-22 15:47:09 +00:00
public function testAskConfirmation ( string $question , bool $expected , bool $default = true ) : void
2017-04-10 20:21:53 +00:00
{
2022-08-17 12:20:07 +00:00
[ $input , $dialog ] = $this -> createInput ( $question . " \n " );
2017-04-10 20:21:53 +00:00
$question = new StrictConfirmationQuestion ( 'Do you like French fries?' , $default );
2024-05-29 21:12:06 +00:00
self :: assertEquals ( $expected , $dialog -> ask ( $input , $this -> createOutputInterface (), $question ), 'confirmation question should ' . ( $expected ? 'pass' : 'cancel' ));
2017-04-10 20:21:53 +00:00
}
2021-10-26 19:17:28 +00:00
/**
* @ return mixed [][]
*
* @ phpstan - return list < array { string , bool } >| list < array { string , bool , bool } >
*/
2022-11-24 13:39:08 +00:00
public static function getAskConfirmationData () : array
2017-04-10 20:21:53 +00:00
{
2022-08-17 12:20:07 +00:00
return [
[ '' , true ],
[ '' , false , false ],
[ 'y' , true ],
[ 'yes' , true ],
[ 'n' , false ],
[ 'no' , false ],
];
2017-04-10 20:21:53 +00:00
}
2022-02-18 09:38:54 +00:00
public function testAskConfirmationWithCustomTrueAndFalseAnswer () : void
2017-04-10 20:21:53 +00:00
{
$question = new StrictConfirmationQuestion ( 'Do you like French fries?' , false , '/^ja$/i' , '/^nein$/i' );
2017-11-06 15:32:29 +00:00
2022-08-17 12:20:07 +00:00
[ $input , $dialog ] = $this -> createInput ( " ja \n " );
2024-05-29 21:12:06 +00:00
self :: assertTrue ( $dialog -> ask ( $input , $this -> createOutputInterface (), $question ));
2017-11-06 15:32:29 +00:00
2022-08-17 12:20:07 +00:00
[ $input , $dialog ] = $this -> createInput ( " nein \n " );
2024-05-29 21:12:06 +00:00
self :: assertFalse ( $dialog -> ask ( $input , $this -> createOutputInterface (), $question ));
2017-04-10 20:21:53 +00:00
}
2021-10-26 19:17:28 +00:00
/**
* @ return resource
*/
2022-02-22 15:47:09 +00:00
protected function getInputStream ( string $input )
2017-04-10 20:21:53 +00:00
{
$stream = fopen ( 'php://memory' , 'r+' , false );
2024-05-29 21:12:06 +00:00
self :: assertNotFalse ( $stream );
2021-10-26 19:17:28 +00:00
2017-04-10 20:21:53 +00:00
fwrite ( $stream , $input );
rewind ( $stream );
return $stream ;
}
2022-02-18 10:22:01 +00:00
protected function createOutputInterface () : StreamOutput
2017-04-10 20:21:53 +00:00
{
return new StreamOutput ( fopen ( 'php://memory' , 'r+' , false ));
}
2021-10-26 19:17:28 +00:00
/**
* @ return object []
*
* @ phpstan - return array { ArrayInput , QuestionHelper }
*/
2022-02-22 15:47:09 +00:00
protected function createInput ( string $entry ) : array
2017-04-10 20:21:53 +00:00
{
2022-08-17 12:20:07 +00:00
$input = new ArrayInput ([ '--no-interaction' ]);
2022-01-04 09:41:33 +00:00
$input -> setStream ( $this -> getInputStream ( $entry ));
2017-11-06 15:32:29 +00:00
2022-01-04 09:41:33 +00:00
$dialog = new QuestionHelper ();
2017-04-10 20:21:53 +00:00
2022-08-17 12:20:07 +00:00
return [ $input , $dialog ];
2017-04-10 20:21:53 +00:00
}
}