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\ArrayInput ;
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 ArrayInputTest extends \PHPUnit_Framework_TestCase
{
2010-05-06 12:25:53 +01:00
public function testGetFirstArgument ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$input = new ArrayInput ( array ());
$this -> assertNull ( $input -> getFirstArgument (), '->getFirstArgument() returns null if no argument were passed' );
$input = new ArrayInput ( array ( 'name' => 'Fabien' ));
$this -> assertEquals ( 'Fabien' , $input -> getFirstArgument (), '->getFirstArgument() returns the first passed argument' );
$input = new ArrayInput ( array ( '--foo' => 'bar' , 'name' => 'Fabien' ));
$this -> assertEquals ( 'Fabien' , $input -> getFirstArgument (), '->getFirstArgument() returns the first passed argument' );
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
{
2010-05-06 12:25:53 +01:00
$input = new ArrayInput ( array ( 'name' => 'Fabien' , '--foo' => 'bar' ));
$this -> assertTrue ( $input -> hasParameterOption ( '--foo' ), '->hasParameterOption() returns true if an option is present in the passed parameters' );
$this -> assertFalse ( $input -> hasParameterOption ( '--bar' ), '->hasParameterOption() returns false if an option is not present in the passed parameters' );
$input = new ArrayInput ( array ( '--foo' ));
$this -> assertTrue ( $input -> hasParameterOption ( '--foo' ), '->hasParameterOption() returns true if an option is present in the passed parameters' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testParse ()
{
$input = new ArrayInput ( array ( 'name' => 'foo' ), new InputDefinition ( array ( new InputArgument ( 'name' ))));
$this -> assertEquals ( array ( 'name' => 'foo' ), $input -> getArguments (), '->parse() parses required arguments' );
2010-03-18 07:48:03 +00:00
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$input = new ArrayInput ( array ( 'foo' => 'foo' ), new InputDefinition ( array ( new InputArgument ( 'name' ))));
$this -> fail ( '->parse() throws an \InvalidArgumentException exception if an invalid argument is passed' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->parse() throws an \InvalidArgumentException exception if an invalid argument is passed' );
$this -> assertEquals ( 'The "foo" argument does not exist.' , $e -> getMessage (), '->parse() throws an \InvalidArgumentException exception if an invalid argument is passed' );
}
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
$input = new ArrayInput ( array ( '--foo' => 'bar' ), new InputDefinition ( array ( new InputOption ( 'foo' ))));
$this -> assertEquals ( array ( 'foo' => 'bar' ), $input -> getOptions (), '->parse() parses long options' );
2010-03-18 07:48:03 +00:00
2010-11-27 16:56:55 +00:00
$input = new ArrayInput ( array ( '--foo' => 'bar' ), new InputDefinition ( array ( new InputOption ( 'foo' , 'f' , InputOption :: VALUE_OPTIONAL , '' , 'default' ))));
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( array ( 'foo' => 'bar' ), $input -> getOptions (), '->parse() parses long options with a default value' );
2010-03-18 07:48:03 +00:00
2010-11-27 16:56:55 +00:00
$input = new ArrayInput ( array ( '--foo' => null ), new InputDefinition ( array ( new InputOption ( 'foo' , 'f' , InputOption :: VALUE_OPTIONAL , '' , 'default' ))));
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( array ( 'foo' => 'default' ), $input -> getOptions (), '->parse() parses long options with a default value' );
2010-03-18 07:48:03 +00:00
2010-05-07 15:09:11 +01:00
try {
2010-11-27 16:56:55 +00:00
$input = new ArrayInput ( array ( '--foo' => null ), new InputDefinition ( array ( new InputOption ( 'foo' , 'f' , InputOption :: VALUE_REQUIRED ))));
2010-05-06 12:25:53 +01:00
$this -> fail ( '->parse() throws an \InvalidArgumentException exception if a required option is passed without a value' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->parse() throws an \InvalidArgumentException exception if a required option is passed without a value' );
$this -> assertEquals ( 'The "--foo" option requires a value.' , $e -> getMessage (), '->parse() throws an \InvalidArgumentException exception if a required option is passed without a value' );
}
2010-03-18 07:48:03 +00:00
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$input = new ArrayInput ( array ( '--foo' => 'foo' ), new InputDefinition ());
$this -> fail ( '->parse() throws an \InvalidArgumentException exception if an invalid option is passed' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->parse() throws an \InvalidArgumentException exception if an invalid option is passed' );
$this -> assertEquals ( 'The "--foo" option does not exist.' , $e -> getMessage (), '->parse() throws an \InvalidArgumentException exception if an invalid option is passed' );
}
$input = new ArrayInput ( array ( '-f' => 'bar' ), new InputDefinition ( array ( new InputOption ( 'foo' , 'f' ))));
$this -> assertEquals ( array ( 'foo' => 'bar' ), $input -> getOptions (), '->parse() parses short options' );
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$input = new ArrayInput ( array ( '-o' => 'foo' ), new InputDefinition ());
$this -> fail ( '->parse() throws an \InvalidArgumentException exception if an invalid option is passed' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->parse() throws an \InvalidArgumentException exception if an invalid option is passed' );
$this -> assertEquals ( 'The "-o" option does not exist.' , $e -> getMessage (), '->parse() throws an \InvalidArgumentException exception if an invalid option is passed' );
}
2010-03-18 07:48:03 +00:00
}
}