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 .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\Console\Tests\Input ;
2010-03-18 07:48:03 +00:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\Console\Input\InputOption ;
2010-03-18 07:48:03 +00:00
class InputOptionTest 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
$option = new InputOption ( 'foo' );
$this -> assertEquals ( 'foo' , $option -> getName (), '__construct() takes a name as its first argument' );
$option = new InputOption ( '--foo' );
$this -> assertEquals ( 'foo' , $option -> getName (), '__construct() removes the leading -- of the option name' );
2010-05-07 15:09:11 +01:00
try {
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , 'f' , InputOption :: VALUE_IS_ARRAY );
2012-09-23 15:17:15 +01:00
$this -> fail ( '__construct() throws an \InvalidArgumentException if VALUE_IS_ARRAY option is used when an option does not accept a value' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2012-09-23 15:17:15 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '__construct() throws an \InvalidArgumentException if VALUE_IS_ARRAY option is used when an option does not accept a value' );
2010-11-27 16:56:55 +00:00
$this -> assertEquals ( 'Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.' , $e -> getMessage ());
2010-05-06 12:25:53 +01:00
}
// shortcut argument
$option = new InputOption ( 'foo' , 'f' );
$this -> assertEquals ( 'f' , $option -> getShortcut (), '__construct() can take a shortcut as its second argument' );
$option = new InputOption ( 'foo' , '-f' );
$this -> assertEquals ( 'f' , $option -> getShortcut (), '__construct() removes the leading - of the shortcut' );
2012-05-01 22:44:56 +01:00
$option = new InputOption ( 'foo' );
$this -> assertNull ( $option -> getShortcut (), '__construct() makes the shortcut null by default' );
2010-05-06 12:25:53 +01:00
// mode argument
$option = new InputOption ( 'foo' , 'f' );
2010-11-27 16:56:55 +00:00
$this -> assertFalse ( $option -> acceptValue (), '__construct() gives a "InputOption::VALUE_NONE" mode by default' );
$this -> assertFalse ( $option -> isValueRequired (), '__construct() gives a "InputOption::VALUE_NONE" mode by default' );
$this -> assertFalse ( $option -> isValueOptional (), '__construct() gives a "InputOption::VALUE_NONE" mode by default' );
2010-05-06 12:25:53 +01:00
$option = new InputOption ( 'foo' , 'f' , null );
2010-11-27 16:56:55 +00:00
$this -> assertFalse ( $option -> acceptValue (), '__construct() can take "InputOption::VALUE_NONE" as its mode' );
$this -> assertFalse ( $option -> isValueRequired (), '__construct() can take "InputOption::VALUE_NONE" as its mode' );
$this -> assertFalse ( $option -> isValueOptional (), '__construct() can take "InputOption::VALUE_NONE" as its mode' );
2010-05-06 12:25:53 +01:00
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , 'f' , InputOption :: VALUE_NONE );
$this -> assertFalse ( $option -> acceptValue (), '__construct() can take "InputOption::VALUE_NONE" as its mode' );
$this -> assertFalse ( $option -> isValueRequired (), '__construct() can take "InputOption::VALUE_NONE" as its mode' );
$this -> assertFalse ( $option -> isValueOptional (), '__construct() can take "InputOption::VALUE_NONE" as its mode' );
2010-05-06 12:25:53 +01:00
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , 'f' , InputOption :: VALUE_REQUIRED );
$this -> assertTrue ( $option -> acceptValue (), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode' );
$this -> assertTrue ( $option -> isValueRequired (), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode' );
$this -> assertFalse ( $option -> isValueOptional (), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode' );
2010-05-06 12:25:53 +01:00
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , 'f' , InputOption :: VALUE_OPTIONAL );
$this -> assertTrue ( $option -> acceptValue (), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode' );
$this -> assertFalse ( $option -> isValueRequired (), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode' );
$this -> assertTrue ( $option -> isValueOptional (), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode' );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$option = new InputOption ( 'foo' , 'f' , 'ANOTHER_ONE' );
2012-09-23 15:17:15 +01:00
$this -> fail ( '__construct() throws an \InvalidArgumentException if the mode is not valid' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2012-09-23 15:17:15 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '__construct() throws an \InvalidArgumentException if the mode is not valid' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'Option mode "ANOTHER_ONE" is not valid.' , $e -> getMessage ());
}
2011-08-30 12:45:17 +01:00
try {
$option = new InputOption ( 'foo' , 'f' , - 1 );
2012-09-23 15:17:15 +01:00
$this -> fail ( '__construct() throws an \InvalidArgumentException if the mode is not valid' );
2011-08-30 12:45:17 +01:00
} catch ( \Exception $e ) {
2012-09-23 15:17:15 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '__construct() throws an \InvalidArgumentException if the mode is not valid' );
2011-08-30 12:45:17 +01:00
$this -> assertEquals ( 'Option mode "-1" is not valid.' , $e -> getMessage ());
}
2010-03-18 07:48:03 +00:00
}
2012-05-22 11:35:53 +01:00
/**
* @ expectedException \InvalidArgumentException
*/
public function testEmptyNameIsInvalid ()
{
new InputOption ( '' );
}
/**
* @ expectedException \InvalidArgumentException
*/
public function testDoubleDashNameIsInvalid ()
{
new InputOption ( '--' );
}
/**
* @ expectedException \InvalidArgumentException
*/
public function testSingleDashOptionIsInvalid ()
{
new InputOption ( 'foo' , '-' );
}
2010-05-06 12:25:53 +01:00
public function testIsArray ()
2010-03-18 07:48:03 +00:00
{
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , null , InputOption :: VALUE_OPTIONAL | InputOption :: VALUE_IS_ARRAY );
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( $option -> isArray (), '->isArray() returns true if the option can be an array' );
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , null , InputOption :: VALUE_NONE );
2010-05-06 12:25:53 +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
}
2010-05-06 12:25:53 +01:00
public function testGetDescription ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$option = new InputOption ( 'foo' , 'f' , null , 'Some description' );
$this -> assertEquals ( 'Some description' , $option -> getDescription (), '->getDescription() returns the description message' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testGetDefault ()
2010-03-18 07:48:03 +00:00
{
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , null , InputOption :: VALUE_OPTIONAL , '' , 'default' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'default' , $option -> getDefault (), '->getDefault() returns the default value' );
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , null , InputOption :: VALUE_REQUIRED , '' , 'default' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'default' , $option -> getDefault (), '->getDefault() returns the default value' );
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , null , InputOption :: VALUE_REQUIRED );
2010-05-06 12:25:53 +01:00
$this -> assertNull ( $option -> getDefault (), '->getDefault() returns null if no default value is configured' );
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , null , InputOption :: VALUE_OPTIONAL | InputOption :: VALUE_IS_ARRAY );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( array (), $option -> getDefault (), '->getDefault() returns an empty array if option is an array' );
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , null , InputOption :: VALUE_NONE );
$this -> assertFalse ( $option -> getDefault (), '->getDefault() returns false if the option does not take a value' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testSetDefault ()
2010-03-18 07:48:03 +00:00
{
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , null , InputOption :: VALUE_REQUIRED , '' , 'default' );
2010-05-06 12:25:53 +01:00
$option -> setDefault ( null );
$this -> assertNull ( $option -> getDefault (), '->setDefault() can reset the default value by passing null' );
$option -> setDefault ( 'another' );
$this -> assertEquals ( 'another' , $option -> getDefault (), '->setDefault() changes the default value' );
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , null , InputOption :: VALUE_REQUIRED | InputOption :: VALUE_IS_ARRAY );
2010-05-06 12:25:53 +01:00
$option -> setDefault ( array ( 1 , 2 ));
$this -> assertEquals ( array ( 1 , 2 ), $option -> getDefault (), '->setDefault() changes the default value' );
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , 'f' , InputOption :: VALUE_NONE );
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$option -> setDefault ( 'default' );
2012-09-23 15:17:15 +01:00
$this -> fail ( '->setDefault() throws a \LogicException if you give a default value for a VALUE_NONE option' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2012-09-23 15:17:15 +01:00
$this -> assertInstanceOf ( '\LogicException' , $e , '->setDefault() throws a \LogicException if you give a default value for a VALUE_NONE option' );
2010-11-27 16:56:55 +00:00
$this -> assertEquals ( 'Cannot set a default value when using Option::VALUE_NONE mode.' , $e -> getMessage ());
2010-05-06 12:25:53 +01:00
}
2010-11-27 16:56:55 +00:00
$option = new InputOption ( 'foo' , 'f' , InputOption :: VALUE_OPTIONAL | InputOption :: VALUE_IS_ARRAY );
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$option -> setDefault ( 'default' );
2012-09-23 15:17:15 +01:00
$this -> fail ( '->setDefault() throws a \LogicException if you give a default value which is not an array for a VALUE_IS_ARRAY option' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2012-09-23 15:17:15 +01:00
$this -> assertInstanceOf ( '\LogicException' , $e , '->setDefault() throws a \LogicException if you give a default value which is not an array for a VALUE_IS_ARRAY option' );
2010-05-06 12:25:53 +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
}
2012-01-08 23:12:02 +00:00
public function testEquals ()
{
$option = new InputOption ( 'foo' , 'f' , null , 'Some description' );
$option2 = new InputOption ( 'foo' , 'f' , null , 'Alternative description' );
$this -> assertTrue ( $option -> equals ( $option2 ));
$option = new InputOption ( 'foo' , 'f' , InputOption :: VALUE_OPTIONAL , 'Some description' );
$option2 = new InputOption ( 'foo' , 'f' , InputOption :: VALUE_OPTIONAL , 'Some description' , true );
$this -> assertFalse ( $option -> equals ( $option2 ));
$option = new InputOption ( 'foo' , 'f' , null , 'Some description' );
$option2 = new InputOption ( 'bar' , 'f' , null , 'Some description' );
$this -> assertFalse ( $option -> equals ( $option2 ));
$option = new InputOption ( 'foo' , 'f' , null , 'Some description' );
$option2 = new InputOption ( 'foo' , '' , null , 'Some description' );
$this -> assertFalse ( $option -> equals ( $option2 ));
$option = new InputOption ( 'foo' , 'f' , null , 'Some description' );
$option2 = new InputOption ( 'foo' , 'f' , InputOption :: VALUE_OPTIONAL , 'Some description' );
$this -> assertFalse ( $option -> equals ( $option2 ));
}
2010-03-18 07:48:03 +00:00
}