2010-03-18 07:48:03 +00:00
< ? php
/*
* This file is part of the symfony package .
* ( c ) Fabien Potencier < fabien . potencier @ symfony - project . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Tests\Components\Console\Input ;
use Symfony\Components\Console\Input\InputOption ;
use Symfony\Components\Console\Exception ;
class InputOptionTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor ()
{
$option = new InputOption ( 'foo' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( 'foo' , $option -> getName (), '__construct() takes a name as its first argument' );
2010-03-18 07:48:03 +00:00
$option = new InputOption ( '--foo' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( 'foo' , $option -> getName (), '__construct() removes the leading -- of the option name' );
2010-03-18 07:48:03 +00:00
try
{
$option = new InputOption ( 'foo' , 'f' , InputOption :: PARAMETER_IS_ARRAY );
$this -> fail ( '->setDefault() throws an Exception if PARAMETER_IS_ARRAY option is used when an option does not accept a value' );
}
catch ( \Exception $e )
{
2010-04-25 15:04:09 +01:00
$this -> assertInstanceOf ( '\Exception' , $e , '->setDefault() throws an Exception if PARAMETER_IS_ARRAY option is used when an option does not accept a value' );
2010-04-10 22:23:20 +01:00
$this -> assertEquals ( 'Impossible to have an option mode PARAMETER_IS_ARRAY if the option does not accept a parameter.' , $e -> getMessage ());
2010-03-18 07:48:03 +00:00
}
// shortcut argument
$option = new InputOption ( 'foo' , 'f' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( 'f' , $option -> getShortcut (), '__construct() can take a shortcut as its second argument' );
2010-03-18 07:48:03 +00:00
$option = new InputOption ( 'foo' , '-f' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( 'f' , $option -> getShortcut (), '__construct() removes the leading - of the shortcut' );
2010-03-18 07:48:03 +00:00
// mode argument
$option = new InputOption ( 'foo' , 'f' );
2010-04-10 22:23:20 +01:00
$this -> assertFalse ( $option -> acceptParameter (), '__construct() gives a "Option::PARAMETER_NONE" mode by default' );
$this -> assertFalse ( $option -> isParameterRequired (), '__construct() gives a "Option::PARAMETER_NONE" mode by default' );
$this -> assertFalse ( $option -> isParameterOptional (), '__construct() gives a "Option::PARAMETER_NONE" mode by default' );
2010-03-18 07:48:03 +00:00
$option = new InputOption ( 'foo' , 'f' , null );
2010-04-10 22:23:20 +01:00
$this -> assertFalse ( $option -> acceptParameter (), '__construct() can take "Option::PARAMETER_NONE" as its mode' );
$this -> assertFalse ( $option -> isParameterRequired (), '__construct() can take "Option::PARAMETER_NONE" as its mode' );
$this -> assertFalse ( $option -> isParameterOptional (), '__construct() can take "Option::PARAMETER_NONE" as its mode' );
2010-03-18 07:48:03 +00:00
$option = new InputOption ( 'foo' , 'f' , InputOption :: PARAMETER_NONE );
2010-04-10 22:23:20 +01:00
$this -> assertFalse ( $option -> acceptParameter (), '__construct() can take "Option::PARAMETER_NONE" as its mode' );
$this -> assertFalse ( $option -> isParameterRequired (), '__construct() can take "Option::PARAMETER_NONE" as its mode' );
$this -> assertFalse ( $option -> isParameterOptional (), '__construct() can take "Option::PARAMETER_NONE" as its mode' );
2010-03-18 07:48:03 +00:00
$option = new InputOption ( 'foo' , 'f' , InputOption :: PARAMETER_REQUIRED );
2010-04-10 22:23:20 +01:00
$this -> assertTrue ( $option -> acceptParameter (), '__construct() can take "Option::PARAMETER_REQUIRED" as its mode' );
$this -> assertTrue ( $option -> isParameterRequired (), '__construct() can take "Option::PARAMETER_REQUIRED" as its mode' );
$this -> assertFalse ( $option -> isParameterOptional (), '__construct() can take "Option::PARAMETER_REQUIRED" as its mode' );
2010-03-18 07:48:03 +00:00
$option = new InputOption ( 'foo' , 'f' , InputOption :: PARAMETER_OPTIONAL );
2010-04-10 22:23:20 +01:00
$this -> assertTrue ( $option -> acceptParameter (), '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode' );
$this -> assertFalse ( $option -> isParameterRequired (), '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode' );
$this -> assertTrue ( $option -> isParameterOptional (), '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode' );
2010-03-18 07:48:03 +00:00
try
{
$option = new InputOption ( 'foo' , 'f' , 'ANOTHER_ONE' );
$this -> fail ( '__construct() throws an Exception if the mode is not valid' );
}
catch ( \Exception $e )
{
2010-04-25 15:04:09 +01:00
$this -> assertInstanceOf ( '\Exception' , $e , '__construct() throws an Exception if the mode is not valid' );
2010-04-10 22:23:20 +01:00
$this -> assertEquals ( 'Option mode "ANOTHER_ONE" is not valid.' , $e -> getMessage ());
2010-03-18 07:48:03 +00:00
}
}
public function testIsArray ()
{
$option = new InputOption ( 'foo' , null , InputOption :: PARAMETER_OPTIONAL | InputOption :: PARAMETER_IS_ARRAY );
$this -> assertTrue ( $option -> isArray (), '->isArray() returns true if the option can be an array' );
$option = new InputOption ( 'foo' , null , InputOption :: PARAMETER_NONE );
2010-04-10 22:23:20 +01:00
$this -> assertFalse ( $option -> isArray (), '->isArray() returns false if the option can not be an array' );
2010-03-18 07:48:03 +00:00
}
public function testGetDescription ()
{
$option = new InputOption ( 'foo' , 'f' , null , 'Some description' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( 'Some description' , $option -> getDescription (), '->getDescription() returns the description message' );
2010-03-18 07:48:03 +00:00
}
public function testGetDefault ()
{
$option = new InputOption ( 'foo' , null , InputOption :: PARAMETER_OPTIONAL , '' , 'default' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( 'default' , $option -> getDefault (), '->getDefault() returns the default value' );
2010-03-18 07:48:03 +00:00
$option = new InputOption ( 'foo' , null , InputOption :: PARAMETER_REQUIRED , '' , 'default' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( 'default' , $option -> getDefault (), '->getDefault() returns the default value' );
2010-03-18 07:48:03 +00:00
$option = new InputOption ( 'foo' , null , InputOption :: PARAMETER_REQUIRED );
2010-04-10 22:23:20 +01:00
$this -> assertNull ( $option -> getDefault (), '->getDefault() returns null if no default value is configured' );
2010-03-18 07:48:03 +00:00
$option = new InputOption ( 'foo' , null , InputOption :: PARAMETER_OPTIONAL | InputOption :: PARAMETER_IS_ARRAY );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( array (), $option -> getDefault (), '->getDefault() returns an empty array if option is an array' );
2010-03-18 07:48:03 +00:00
$option = new InputOption ( 'foo' , null , InputOption :: PARAMETER_NONE );
2010-04-10 22:23:20 +01:00
$this -> assertFalse ( $option -> getDefault (), '->getDefault() returns false if the option does not take a parameter' );
2010-03-18 07:48:03 +00:00
}
public function testSetDefault ()
{
$option = new InputOption ( 'foo' , null , InputOption :: PARAMETER_REQUIRED , '' , 'default' );
$option -> setDefault ( null );
2010-04-10 22:23:20 +01:00
$this -> assertNull ( $option -> getDefault (), '->setDefault() can reset the default value by passing null' );
2010-03-18 07:48:03 +00:00
$option -> setDefault ( 'another' );
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( 'another' , $option -> getDefault (), '->setDefault() changes the default value' );
2010-03-18 07:48:03 +00:00
$option = new InputOption ( 'foo' , null , InputOption :: PARAMETER_REQUIRED | InputOption :: PARAMETER_IS_ARRAY );
$option -> setDefault ( array ( 1 , 2 ));
2010-03-19 14:04:37 +00:00
$this -> assertEquals ( array ( 1 , 2 ), $option -> getDefault (), '->setDefault() changes the default value' );
2010-03-18 07:48:03 +00:00
$option = new InputOption ( 'foo' , 'f' , InputOption :: PARAMETER_NONE );
try
{
$option -> setDefault ( 'default' );
$this -> fail ( '->setDefault() throws an Exception if you give a default value for a PARAMETER_NONE option' );
}
catch ( \Exception $e )
{
2010-04-25 15:04:09 +01:00
$this -> assertInstanceOf ( '\Exception' , $e , '->setDefault() throws an Exception if you give a default value for a PARAMETER_NONE option' );
2010-04-10 22:23:20 +01:00
$this -> assertEquals ( 'Cannot set a default value when using Option::PARAMETER_NONE mode.' , $e -> getMessage ());
2010-03-18 07:48:03 +00:00
}
$option = new InputOption ( 'foo' , 'f' , InputOption :: PARAMETER_OPTIONAL | InputOption :: PARAMETER_IS_ARRAY );
try
{
$option -> setDefault ( 'default' );
$this -> fail ( '->setDefault() throws an Exception if you give a default value which is not an array for a PARAMETER_IS_ARRAY option' );
}
catch ( \Exception $e )
{
2010-04-25 15:04:09 +01:00
$this -> assertInstanceOf ( '\Exception' , $e , '->setDefault() throws an Exception if you give a default value which is not an array for a PARAMETER_IS_ARRAY option' );
2010-04-10 22:23:20 +01:00
$this -> assertEquals ( 'A default value for an array option must be an array.' , $e -> getMessage ());
2010-03-18 07:48:03 +00:00
}
}
}