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\InputArgument;
|
|
|
|
use Symfony\Components\Console\Exception;
|
|
|
|
|
|
|
|
class InputArgumentTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testConstructor()
|
|
|
|
{
|
|
|
|
$argument = new InputArgument('foo');
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals('foo', $argument->getName(), '__construct() takes a name as its first argument');
|
2010-03-18 07:48:03 +00:00
|
|
|
|
|
|
|
// mode argument
|
|
|
|
$argument = new InputArgument('foo');
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals(false, $argument->isRequired(), '__construct() gives a "Argument::OPTIONAL" mode by default');
|
2010-03-18 07:48:03 +00:00
|
|
|
|
|
|
|
$argument = new InputArgument('foo', null);
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals(false, $argument->isRequired(), '__construct() can take "Argument::OPTIONAL" as its mode');
|
2010-03-18 07:48:03 +00:00
|
|
|
|
|
|
|
$argument = new InputArgument('foo', InputArgument::OPTIONAL);
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals(false, $argument->isRequired(), '__construct() can take "Argument::PARAMETER_OPTIONAL" as its mode');
|
2010-03-18 07:48:03 +00:00
|
|
|
|
|
|
|
$argument = new InputArgument('foo', InputArgument::REQUIRED);
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals(true, $argument->isRequired(), '__construct() can take "Argument::PARAMETER_REQUIRED" as its mode');
|
2010-03-18 07:48:03 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$argument = new InputArgument('foo', 'ANOTHER_ONE');
|
|
|
|
$this->fail('__construct() throws an Exception if the mode is not valid');
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsArray()
|
|
|
|
{
|
|
|
|
$argument = new InputArgument('foo', InputArgument::IS_ARRAY);
|
|
|
|
$this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
|
|
|
|
$argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
|
|
|
|
$this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
|
|
|
|
$argument = new InputArgument('foo', InputArgument::OPTIONAL);
|
|
|
|
$this->assertTrue(!$argument->isArray(), '->isArray() returns false if the argument can not be an array');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetDescription()
|
|
|
|
{
|
|
|
|
$argument = new InputArgument('foo', null, 'Some description');
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals('Some description', $argument->getDescription(), '->getDescription() return the message description');
|
2010-03-18 07:48:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetDefault()
|
|
|
|
{
|
|
|
|
$argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals('default', $argument->getDefault(), '->getDefault() return the default value');
|
2010-03-18 07:48:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetDefault()
|
|
|
|
{
|
|
|
|
$argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
|
|
|
|
$argument->setDefault(null);
|
|
|
|
$this->assertTrue(is_null($argument->getDefault()), '->setDefault() can reset the default value by passing null');
|
|
|
|
$argument->setDefault('another');
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals('another', $argument->getDefault(), '->setDefault() changes the default value');
|
2010-03-18 07:48:03 +00:00
|
|
|
|
|
|
|
$argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
|
|
|
|
$argument->setDefault(array(1, 2));
|
2010-03-19 14:04:37 +00:00
|
|
|
$this->assertEquals(array(1, 2), $argument->getDefault(), '->setDefault() changes the default value');
|
2010-03-18 07:48:03 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$argument = new InputArgument('foo', InputArgument::REQUIRED);
|
|
|
|
$argument->setDefault('default');
|
|
|
|
$this->fail('->setDefault() throws an Exception if you give a default value for a required argument');
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$argument = new InputArgument('foo', InputArgument::IS_ARRAY);
|
|
|
|
$argument->setDefault('default');
|
|
|
|
$this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|