2010-03-18 07:48:03 +00:00
< ? php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package .
2011-01-15 13:29:43 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-03-18 07:48:03 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2010-08-20 22:09:55 +01:00
namespace Symfony\Tests\Component\Console\Input ;
2010-03-18 07:48:03 +00:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\Console\Input\ArgvInput ;
use Symfony\Component\Console\Input\InputDefinition ;
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputOption ;
2010-03-18 07:48:03 +00:00
class ArgvInputTest extends \PHPUnit_Framework_TestCase
{
2010-05-06 12:25:53 +01:00
public function testConstructor ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$_SERVER [ 'argv' ] = array ( 'cli.php' , 'foo' );
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ();
$r = new \ReflectionObject ( $input );
$p = $r -> getProperty ( 'tokens' );
$p -> setAccessible ( true );
$this -> assertEquals ( array ( 'foo' ), $p -> getValue ( $input ), '__construct() automatically get its input from the argv server variable' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testParser ()
2010-03-18 07:48:03 +00:00
{
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , 'foo' ));
2010-05-06 12:25:53 +01:00
$input -> bind ( new InputDefinition ( array ( new InputArgument ( 'name' ))));
$this -> assertEquals ( array ( 'name' => 'foo' ), $input -> getArguments (), '->parse() parses required arguments' );
$input -> bind ( new InputDefinition ( array ( new InputArgument ( 'name' ))));
$this -> assertEquals ( array ( 'name' => 'foo' ), $input -> getArguments (), '->parse() is stateless' );
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '--foo' ));
2010-05-06 12:25:53 +01:00
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'foo' ))));
2010-11-27 16:56:55 +00:00
$this -> assertEquals ( array ( 'foo' => true ), $input -> getOptions (), '->parse() parses long options without a value' );
2010-05-06 12:25:53 +01:00
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '--foo=bar' ));
2010-11-27 16:56:55 +00:00
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'foo' , 'f' , InputOption :: VALUE_REQUIRED ))));
$this -> assertEquals ( array ( 'foo' => 'bar' ), $input -> getOptions (), '->parse() parses long options with a required value (with a = separator)' );
2010-05-06 12:25:53 +01:00
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '--foo' , 'bar' ));
2010-11-27 16:56:55 +00:00
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'foo' , 'f' , InputOption :: VALUE_REQUIRED ))));
$this -> assertEquals ( array ( 'foo' => 'bar' ), $input -> getOptions (), '->parse() parses long options with a required value (with a space separator)' );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '--foo' ));
2010-11-27 16:56:55 +00:00
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'foo' , 'f' , InputOption :: VALUE_REQUIRED ))));
$this -> fail ( '->parse() throws a \RuntimeException if no value is passed to an option when it is required' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-11-27 16:56:55 +00:00
$this -> assertInstanceOf ( '\RuntimeException' , $e , '->parse() throws a \RuntimeException if no value is passed to an option when it is required' );
$this -> assertEquals ( 'The "--foo" option requires a value.' , $e -> getMessage (), '->parse() throws a \RuntimeException if no value is passed to an option when it is required' );
2010-05-06 12:25:53 +01:00
}
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-f' ));
2010-05-06 12:25:53 +01:00
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'foo' , 'f' ))));
2010-11-27 16:56:55 +00:00
$this -> assertEquals ( array ( 'foo' => true ), $input -> getOptions (), '->parse() parses short options without a value' );
2010-05-06 12:25:53 +01:00
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-fbar' ));
2010-11-27 16:56:55 +00:00
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'foo' , 'f' , InputOption :: VALUE_REQUIRED ))));
$this -> assertEquals ( array ( 'foo' => 'bar' ), $input -> getOptions (), '->parse() parses short options with a required value (with no separator)' );
2010-05-06 12:25:53 +01:00
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-f' , 'bar' ));
2010-11-27 16:56:55 +00:00
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'foo' , 'f' , InputOption :: VALUE_REQUIRED ))));
$this -> assertEquals ( array ( 'foo' => 'bar' ), $input -> getOptions (), '->parse() parses short options with a required value (with a space separator)' );
2010-05-06 12:25:53 +01:00
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-f' , '-b' , 'foo' ));
2010-11-27 16:56:55 +00:00
$input -> bind ( new InputDefinition ( array ( new InputArgument ( 'name' ), new InputOption ( 'foo' , 'f' , InputOption :: VALUE_OPTIONAL ), new InputOption ( 'bar' , 'b' ))));
$this -> assertEquals ( array ( 'foo' => null , 'bar' => true ), $input -> getOptions (), '->parse() parses short options with an optional value which is not present' );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-f' ));
2010-11-27 16:56:55 +00:00
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'foo' , 'f' , InputOption :: VALUE_REQUIRED ))));
$this -> fail ( '->parse() throws a \RuntimeException if no value is passed to an option when it is required' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-11-27 16:56:55 +00:00
$this -> assertInstanceOf ( '\RuntimeException' , $e , '->parse() throws a \RuntimeException if no value is passed to an option when it is required' );
$this -> assertEquals ( 'The "--foo" option requires a value.' , $e -> getMessage (), '->parse() throws a \RuntimeException if no value is passed to an option when it is required' );
2010-05-06 12:25:53 +01:00
}
2010-05-07 15:09:11 +01:00
try {
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-ffoo' ));
2010-11-27 16:56:55 +00:00
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'foo' , 'f' , InputOption :: VALUE_NONE ))));
2010-05-06 12:25:53 +01:00
$this -> fail ( '->parse() throws a \RuntimeException if a value is passed to an option which does not take one' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\RuntimeException' , $e , '->parse() throws a \RuntimeException if a value is passed to an option which does not take one' );
$this -> assertEquals ( 'The "-o" option does not exist.' , $e -> getMessage (), '->parse() throws a \RuntimeException if a value is passed to an option which does not take one' );
}
2010-05-07 15:09:11 +01:00
try {
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , 'foo' , 'bar' ));
2010-05-06 12:25:53 +01:00
$input -> bind ( new InputDefinition ());
$this -> fail ( '->parse() throws a \RuntimeException if too many arguments are passed' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\RuntimeException' , $e , '->parse() throws a \RuntimeException if too many arguments are passed' );
$this -> assertEquals ( 'Too many arguments.' , $e -> getMessage (), '->parse() throws a \RuntimeException if too many arguments are passed' );
}
2010-05-07 15:09:11 +01:00
try {
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '--foo' ));
2010-05-06 12:25:53 +01:00
$input -> bind ( new InputDefinition ());
$this -> fail ( '->parse() throws a \RuntimeException if an unknown long option is passed' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\RuntimeException' , $e , '->parse() throws a \RuntimeException if an unknown long option is passed' );
$this -> assertEquals ( 'The "--foo" option does not exist.' , $e -> getMessage (), '->parse() throws a \RuntimeException if an unknown long option is passed' );
}
2010-05-07 15:09:11 +01:00
try {
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-f' ));
2010-05-06 12:25:53 +01:00
$input -> bind ( new InputDefinition ());
$this -> fail ( '->parse() throws a \RuntimeException if an unknown short option is passed' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\RuntimeException' , $e , '->parse() throws a \RuntimeException if an unknown short option is passed' );
$this -> assertEquals ( 'The "-f" option does not exist.' , $e -> getMessage (), '->parse() throws a \RuntimeException if an unknown short option is passed' );
}
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-fb' ));
2010-05-06 12:25:53 +01:00
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'foo' , 'f' ), new InputOption ( 'bar' , 'b' ))));
$this -> assertEquals ( array ( 'foo' => true , 'bar' => true ), $input -> getOptions (), '->parse() parses short options when they are aggregated as a single one' );
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-fb' , 'bar' ));
2010-11-27 16:56:55 +00:00
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'foo' , 'f' ), new InputOption ( 'bar' , 'b' , InputOption :: VALUE_REQUIRED ))));
$this -> assertEquals ( array ( 'foo' => true , 'bar' => 'bar' ), $input -> getOptions (), '->parse() parses short options when they are aggregated as a single one and the last one has a required value' );
2010-05-06 12:25:53 +01:00
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-fb' , 'bar' ));
2010-11-27 16:56:55 +00:00
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'foo' , 'f' ), new InputOption ( 'bar' , 'b' , InputOption :: VALUE_OPTIONAL ))));
$this -> assertEquals ( array ( 'foo' => true , 'bar' => 'bar' ), $input -> getOptions (), '->parse() parses short options when they are aggregated as a single one and the last one has an optional value' );
2010-05-06 12:25:53 +01:00
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-fbbar' ));
2010-11-27 16:56:55 +00:00
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'foo' , 'f' ), new InputOption ( 'bar' , 'b' , InputOption :: VALUE_OPTIONAL ))));
$this -> assertEquals ( array ( 'foo' => true , 'bar' => 'bar' ), $input -> getOptions (), '->parse() parses short options when they are aggregated as a single one and the last one has an optional value with no separator' );
2010-05-06 12:25:53 +01:00
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-fbbar' ));
2010-11-27 16:56:55 +00:00
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'foo' , 'f' , InputOption :: VALUE_OPTIONAL ), new InputOption ( 'bar' , 'b' , InputOption :: VALUE_OPTIONAL ))));
$this -> assertEquals ( array ( 'foo' => 'bbar' , 'bar' => null ), $input -> getOptions (), '->parse() parses short options when they are aggregated as a single one and one of them takes a value' );
2011-02-28 15:40:32 +00:00
try {
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , 'foo' , 'bar' , 'baz' , 'bat' ));
2011-02-28 15:40:32 +00:00
$input -> bind ( new InputDefinition ( array ( new InputArgument ( 'name' , InputArgument :: IS_ARRAY ))));
$this -> assertEquals ( array ( 'name' => array ( 'foo' , 'bar' , 'baz' , 'bat' )), $input -> getArguments (), '->parse() parses array arguments' );
} catch ( \RuntimeException $e ) {
$this -> assertNotEquals ( 'Too many arguments.' , $e -> getMessage (), '->parse() parses array arguments' );
}
2011-06-07 13:57:18 +01:00
$input = new ArgvInput ( array ( 'cli.php' , '--name=foo' , '--name=bar' , '--name=baz' ));
$input -> bind ( new InputDefinition ( array ( new InputOption ( 'name' , null , InputOption :: VALUE_OPTIONAL | InputOption :: VALUE_IS_ARRAY ))));
$this -> assertEquals ( array ( 'name' => array ( 'foo' , 'bar' , 'baz' )), $input -> getOptions ());
2012-03-16 19:28:57 +00:00
try {
$input = new ArgvInput ( array ( 'cli.php' , '-1' ));
$input -> bind ( new InputDefinition ( array ( new InputArgument ( 'number' ))));
$this -> fail ( '->parse() throws a \RuntimeException if an unknown option is passed' );
} catch ( \Exception $e ) {
$this -> assertInstanceOf ( '\RuntimeException' , $e , '->parse() parses arguments with leading dashes as options without having encountered a double-dash sequence' );
$this -> assertEquals ( 'The "-1" option does not exist.' , $e -> getMessage (), '->parse() parses arguments with leading dashes as options without having encountered a double-dash sequence' );
}
$input = new ArgvInput ( array ( 'cli.php' , '--' , '-1' ));
$input -> bind ( new InputDefinition ( array ( new InputArgument ( 'number' ))));
$this -> assertEquals ( array ( 'number' => '-1' ), $input -> getArguments (), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence' );
$input = new ArgvInput ( array ( 'cli.php' , '-f' , 'bar' , '--' , '-1' ));
$input -> bind ( new InputDefinition ( array ( new InputArgument ( 'number' ), new InputOption ( 'foo' , 'f' , InputOption :: VALUE_OPTIONAL ))));
$this -> assertEquals ( array ( 'foo' => 'bar' ), $input -> getOptions (), '->parse() parses arguments with leading dashes as options before having encountered a double-dash sequence' );
$this -> assertEquals ( array ( 'number' => '-1' ), $input -> getArguments (), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testGetFirstArgument ()
2010-03-18 07:48:03 +00:00
{
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-fbbar' ));
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( '' , $input -> getFirstArgument (), '->getFirstArgument() returns the first argument from the raw input' );
2010-03-18 07:48:03 +00:00
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-fbbar' , 'foo' ));
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'foo' , $input -> getFirstArgument (), '->getFirstArgument() returns the first argument from the raw input' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testHasParameterOption ()
2010-03-18 07:48:03 +00:00
{
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '-f' , 'foo' ));
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( $input -> hasParameterOption ( '-f' ), '->hasParameterOption() returns true if the given short option is in the raw input' );
2010-03-18 07:48:03 +00:00
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , '--foo' , 'foo' ));
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( $input -> hasParameterOption ( '--foo' ), '->hasParameterOption() returns true if the given short option is in the raw input' );
2010-03-18 07:48:03 +00:00
2011-03-11 11:53:42 +00:00
$input = new ArgvInput ( array ( 'cli.php' , 'foo' ));
2010-05-06 12:25:53 +01:00
$this -> assertFalse ( $input -> hasParameterOption ( '--foo' ), '->hasParameterOption() returns false if the given short option is not in the raw input' );
}
2011-02-11 00:53:18 +00:00
/**
* @ dataProvider provideGetParameterOptionValues
*/
public function testGetParameterOptionEqualSign ( $argv , $key , $expected )
{
$input = new ArgvInput ( $argv );
$this -> assertEquals ( $expected , $input -> getParameterOption ( $key ), '->getParameterOption() returns the expected value' );
}
public function provideGetParameterOptionValues ()
{
return array (
array ( array ( 'app/console' , 'foo:bar' , '-e' , 'dev' ), '-e' , 'dev' ),
array ( array ( 'app/console' , 'foo:bar' , '--env=dev' ), '--env' , 'dev' ),
array ( array ( 'app/console' , 'foo:bar' , '-e' , 'dev' ), array ( '-e' , '--env' ), 'dev' ),
array ( array ( 'app/console' , 'foo:bar' , '--env=dev' ), array ( '-e' , '--env' ), 'dev' ),
);
}
2010-03-18 07:48:03 +00:00
}