2011-06-20 18:05:56 +01:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\Console\Tests\Helper ;
2011-06-20 18:05:56 +01:00
use Symfony\Component\Console\Helper\DialogHelper ;
use Symfony\Component\Console\Helper\HelperSet ;
use Symfony\Component\Console\Helper\FormatterHelper ;
use Symfony\Component\Console\Output\StreamOutput ;
class DialogHelperTest extends \PHPUnit_Framework_TestCase
{
2012-09-02 20:35:58 +01:00
public function testSelect ()
{
$dialog = new DialogHelper ();
$helperSet = new HelperSet ( array ( new FormatterHelper ()));
$dialog -> setHelperSet ( $helperSet );
2012-12-14 07:48:31 +00:00
$heroes = array ( 'Superman' , 'Batman' , 'Spiderman' );
2013-04-21 08:53:05 +01:00
$dialog -> setInputStream ( $this -> getInputStream ( " \n 1 \n 1 \n Fabien \n 1 \n Fabien \n 1 \n 0,2 \n 0 , 2 \n \n \n " ));
2012-12-14 07:48:31 +00:00
$this -> assertEquals ( '2' , $dialog -> select ( $this -> getOutputStream (), 'What is your favorite superhero?' , $heroes , '2' ));
$this -> assertEquals ( '1' , $dialog -> select ( $this -> getOutputStream (), 'What is your favorite superhero?' , $heroes ));
2013-04-21 08:53:05 +01:00
$this -> assertEquals ( '1' , $dialog -> select ( $this -> getOutputStream (), 'What is your favorite superhero?' , $heroes ));
2013-04-08 14:09:07 +01:00
$this -> assertEquals ( '1' , $dialog -> select ( $output = $this -> getOutputStream (), 'What is your favorite superhero?' , $heroes , null , false , 'Input "%s" is not a superhero!' , false ));
2012-12-14 07:48:31 +00:00
rewind ( $output -> getStream ());
$this -> assertContains ( 'Input "Fabien" is not a superhero!' , stream_get_contents ( $output -> getStream ()));
try {
$this -> assertEquals ( '1' , $dialog -> select ( $output = $this -> getOutputStream (), 'What is your favorite superhero?' , $heroes , null , 1 ));
$this -> fail ();
} catch ( \InvalidArgumentException $e ) {
$this -> assertEquals ( 'Value "Fabien" is invalid' , $e -> getMessage ());
}
2013-04-08 14:09:07 +01:00
$this -> assertEquals ( array ( '1' ), $dialog -> select ( $this -> getOutputStream (), 'What is your favorite superhero?' , $heroes , null , false , 'Input "%s" is not a superhero!' , true ));
$this -> assertEquals ( array ( '0' , '2' ), $dialog -> select ( $this -> getOutputStream (), 'What is your favorite superhero?' , $heroes , null , false , 'Input "%s" is not a superhero!' , true ));
2013-04-21 08:53:05 +01:00
$this -> assertEquals ( array ( '0' , '2' ), $dialog -> select ( $this -> getOutputStream (), 'What is your favorite superhero?' , $heroes , null , false , 'Input "%s" is not a superhero!' , true ));
2013-04-08 14:09:07 +01:00
$this -> assertEquals ( array ( '0' , '1' ), $dialog -> select ( $this -> getOutputStream (), 'What is your favorite superhero?' , $heroes , '0,1' , false , 'Input "%s" is not a superhero!' , true ));
2013-04-21 08:53:05 +01:00
$this -> assertEquals ( array ( '0' , '1' ), $dialog -> select ( $this -> getOutputStream (), 'What is your favorite superhero?' , $heroes , ' 0 , 1 ' , false , 'Input "%s" is not a superhero!' , true ));
2012-09-02 20:35:58 +01:00
}
2011-06-20 18:05:56 +01:00
public function testAsk ()
{
$dialog = new DialogHelper ();
$dialog -> setInputStream ( $this -> getInputStream ( " \n 8AM \n " ));
$this -> assertEquals ( '2PM' , $dialog -> ask ( $this -> getOutputStream (), 'What time is it?' , '2PM' ));
$this -> assertEquals ( '8AM' , $dialog -> ask ( $output = $this -> getOutputStream (), 'What time is it?' , '2PM' ));
rewind ( $output -> getStream ());
$this -> assertEquals ( 'What time is it?' , stream_get_contents ( $output -> getStream ()));
2013-01-08 11:54:42 +00:00
}
2012-12-17 02:58:21 +00:00
2013-01-08 11:54:42 +00:00
public function testAskWithAutocomplete ()
{
if ( ! $this -> hasSttyAvailable ()) {
$this -> markTestSkipped ( '`stty` is required to test autocomplete functionality' );
}
2012-12-17 02:58:21 +00:00
// Acm<NEWLINE>
// Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
// <NEWLINE>
2013-01-06 04:05:03 +00:00
// <UP ARROW><UP ARROW><NEWLINE>
2013-01-08 03:50:00 +00:00
// <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
2013-01-06 04:05:03 +00:00
// <DOWN ARROW><NEWLINE>
2013-01-08 03:50:00 +00:00
// S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
2013-04-10 23:42:15 +01:00
// F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
$inputStream = $this -> getInputStream ( " Acm \n Ac \177 \177 s \t Test \n \n \033 [A \033 [A \n \033 [A \033 [A \033 [A \033 [A \033 [A \t Test \n \033 [B \n S \177 \177 \033 [B \033 [B \n F00 \177 \177 oo \t \n " );
2013-01-08 11:54:42 +00:00
$dialog = new DialogHelper ();
2012-12-17 02:58:21 +00:00
$dialog -> setInputStream ( $inputStream );
2013-01-08 11:54:42 +00:00
$bundles = array ( 'AcmeDemoBundle' , 'AsseticBundle' , 'SecurityBundle' , 'FooBundle' );
$this -> assertEquals ( 'AcmeDemoBundle' , $dialog -> ask ( $this -> getOutputStream (), 'Please select a bundle' , 'FrameworkBundle' , $bundles ));
$this -> assertEquals ( 'AsseticBundleTest' , $dialog -> ask ( $this -> getOutputStream (), 'Please select a bundle' , 'FrameworkBundle' , $bundles ));
$this -> assertEquals ( 'FrameworkBundle' , $dialog -> ask ( $this -> getOutputStream (), 'Please select a bundle' , 'FrameworkBundle' , $bundles ));
$this -> assertEquals ( 'SecurityBundle' , $dialog -> ask ( $this -> getOutputStream (), 'Please select a bundle' , 'FrameworkBundle' , $bundles ));
$this -> assertEquals ( 'FooBundleTest' , $dialog -> ask ( $this -> getOutputStream (), 'Please select a bundle' , 'FrameworkBundle' , $bundles ));
$this -> assertEquals ( 'AcmeDemoBundle' , $dialog -> ask ( $this -> getOutputStream (), 'Please select a bundle' , 'FrameworkBundle' , $bundles ));
2013-01-08 03:50:00 +00:00
$this -> assertEquals ( 'AsseticBundle' , $dialog -> ask ( $this -> getOutputStream (), 'Please select a bundle' , 'FrameworkBundle' , $bundles ));
2013-04-10 23:42:15 +01:00
$this -> assertEquals ( 'FooBundle' , $dialog -> ask ( $this -> getOutputStream (), 'Please select a bundle' , 'FrameworkBundle' , $bundles ));
2011-06-20 18:05:56 +01:00
}
2013-09-12 22:41:34 +01:00
/**
* @ group tty
*/
2012-10-10 15:55:03 +01:00
public function testAskHiddenResponse ()
{
2012-10-13 23:10:38 +01:00
if ( defined ( 'PHP_WINDOWS_VERSION_BUILD' )) {
$this -> markTestSkipped ( 'This test is not supported on Windows' );
}
2012-10-10 15:55:03 +01:00
$dialog = new DialogHelper ();
$dialog -> setInputStream ( $this -> getInputStream ( " 8AM \n " ));
$this -> assertEquals ( '8AM' , $dialog -> askHiddenResponse ( $this -> getOutputStream (), 'What time is it?' ));
}
2011-06-20 18:05:56 +01:00
public function testAskConfirmation ()
{
$dialog = new DialogHelper ();
$dialog -> setInputStream ( $this -> getInputStream ( " \n \n " ));
$this -> assertTrue ( $dialog -> askConfirmation ( $this -> getOutputStream (), 'Do you like French fries?' ));
$this -> assertFalse ( $dialog -> askConfirmation ( $this -> getOutputStream (), 'Do you like French fries?' , false ));
$dialog -> setInputStream ( $this -> getInputStream ( " y \n yes \n " ));
$this -> assertTrue ( $dialog -> askConfirmation ( $this -> getOutputStream (), 'Do you like French fries?' , false ));
$this -> assertTrue ( $dialog -> askConfirmation ( $this -> getOutputStream (), 'Do you like French fries?' , false ));
$dialog -> setInputStream ( $this -> getInputStream ( " n \n no \n " ));
$this -> assertFalse ( $dialog -> askConfirmation ( $this -> getOutputStream (), 'Do you like French fries?' , true ));
$this -> assertFalse ( $dialog -> askConfirmation ( $this -> getOutputStream (), 'Do you like French fries?' , true ));
}
public function testAskAndValidate ()
{
$dialog = new DialogHelper ();
$helperSet = new HelperSet ( array ( new FormatterHelper ()));
$dialog -> setHelperSet ( $helperSet );
$question = 'What color was the white horse of Henry IV?' ;
$error = 'This is not a color!' ;
$validator = function ( $color ) use ( $error ) {
if ( ! in_array ( $color , array ( 'white' , 'black' ))) {
throw new \InvalidArgumentException ( $error );
}
return $color ;
};
$dialog -> setInputStream ( $this -> getInputStream ( " \n black \n " ));
$this -> assertEquals ( 'white' , $dialog -> askAndValidate ( $this -> getOutputStream (), $question , $validator , 2 , 'white' ));
$this -> assertEquals ( 'black' , $dialog -> askAndValidate ( $this -> getOutputStream (), $question , $validator , 2 , 'white' ));
$dialog -> setInputStream ( $this -> getInputStream ( " green \n yellow \n orange \n " ));
try {
$this -> assertEquals ( 'white' , $dialog -> askAndValidate ( $this -> getOutputStream (), $question , $validator , 2 , 'white' ));
$this -> fail ();
} catch ( \InvalidArgumentException $e ) {
$this -> assertEquals ( $error , $e -> getMessage ());
}
}
protected function getInputStream ( $input )
{
$stream = fopen ( 'php://memory' , 'r+' , false );
fputs ( $stream , $input );
rewind ( $stream );
return $stream ;
}
protected function getOutputStream ()
{
return new StreamOutput ( fopen ( 'php://memory' , 'r+' , false ));
}
2012-12-17 02:58:21 +00:00
private function hasSttyAvailable ()
{
exec ( 'stty 2>&1' , $output , $exitcode );
return $exitcode === 0 ;
}
2011-06-20 18:05:56 +01:00
}