369 lines
15 KiB
PHP
369 lines
15 KiB
PHP
<?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;
|
|
|
|
require_once __DIR__.'/../../../bootstrap.php';
|
|
|
|
use Symfony\Components\Console\Input\InputDefinition;
|
|
use Symfony\Components\Console\Input\InputArgument;
|
|
use Symfony\Components\Console\Input\InputOption;
|
|
use Symfony\Components\Console\Exception;
|
|
|
|
class InputDefinitionTest extends \PHPUnit_Framework_TestCase
|
|
{
|
|
static protected $fixtures;
|
|
|
|
protected $foo, $bar, $foo1, $foo2;
|
|
|
|
static public function setUpBeforeClass()
|
|
{
|
|
self::$fixtures = __DIR__.'/../../../../../fixtures/Symfony/Components/Console';
|
|
}
|
|
|
|
public function testConstructor()
|
|
{
|
|
$this->initializeArguments();
|
|
|
|
$definition = new InputDefinition();
|
|
$this->assertEquals($definition->getArguments(), array(), '__construct() creates a new InputDefinition object');
|
|
|
|
$definition = new InputDefinition(array($this->foo, $this->bar));
|
|
$this->assertEquals($definition->getArguments(), array('foo' => $this->foo, 'bar' => $this->bar), '__construct() takes an array of InputArgument objects as its first argument');
|
|
|
|
$this->initializeOptions();
|
|
|
|
$definition = new InputDefinition();
|
|
$this->assertEquals($definition->getOptions(), array(), '__construct() creates a new InputDefinition object');
|
|
|
|
$definition = new InputDefinition(array($this->foo, $this->bar));
|
|
$this->assertEquals($definition->getOptions(), array('foo' => $this->foo, 'bar' => $this->bar), '__construct() takes an array of InputOption objects as its first argument');
|
|
}
|
|
|
|
public function testSetArguments()
|
|
{
|
|
$this->initializeArguments();
|
|
|
|
$definition = new InputDefinition();
|
|
$definition->setArguments(array($this->foo));
|
|
$this->assertEquals($definition->getArguments(), array('foo' => $this->foo), '->setArguments() sets the array of InputArgument objects');
|
|
$definition->setArguments(array($this->bar));
|
|
|
|
$this->assertEquals($definition->getArguments(), array('bar' => $this->bar), '->setArguments() clears all InputArgument objects');
|
|
}
|
|
|
|
public function testAddArguments()
|
|
{
|
|
$this->initializeArguments();
|
|
|
|
$definition = new InputDefinition();
|
|
$definition->addArguments(array($this->foo));
|
|
$this->assertEquals($definition->getArguments(), array('foo' => $this->foo), '->addArguments() adds an array of InputArgument objects');
|
|
$definition->addArguments(array($this->bar));
|
|
$this->assertEquals($definition->getArguments(), array('foo' => $this->foo, 'bar' => $this->bar), '->addArguments() does not clear existing InputArgument objects');
|
|
}
|
|
|
|
public function testAddArgument()
|
|
{
|
|
$this->initializeArguments();
|
|
|
|
$definition = new InputDefinition();
|
|
$definition->addArgument($this->foo);
|
|
$this->assertEquals($definition->getArguments(), array('foo' => $this->foo), '->addArgument() adds a InputArgument object');
|
|
$definition->addArgument($this->bar);
|
|
$this->assertEquals($definition->getArguments(), array('foo' => $this->foo, 'bar' => $this->bar), '->addArgument() adds a InputArgument object');
|
|
|
|
// arguments must have different names
|
|
try
|
|
{
|
|
$definition->addArgument($this->foo1);
|
|
$this->fail('->addArgument() throws a Exception if another argument is already registered with the same name');
|
|
}
|
|
catch (\Exception $e)
|
|
{
|
|
}
|
|
|
|
// cannot add a parameter after an array parameter
|
|
$definition->addArgument(new InputArgument('fooarray', InputArgument::IS_ARRAY));
|
|
try
|
|
{
|
|
$definition->addArgument(new InputArgument('anotherbar'));
|
|
$this->fail('->addArgument() throws a Exception if there is an array parameter already registered');
|
|
}
|
|
catch (\Exception $e)
|
|
{
|
|
}
|
|
|
|
// cannot add a required argument after an optional one
|
|
$definition = new InputDefinition();
|
|
$definition->addArgument($this->foo);
|
|
try
|
|
{
|
|
$definition->addArgument($this->foo2);
|
|
$this->fail('->addArgument() throws an exception if you try to add a required argument after an optional one');
|
|
}
|
|
catch (\Exception $e)
|
|
{
|
|
}
|
|
}
|
|
|
|
public function testGetArgument()
|
|
{
|
|
$this->initializeArguments();
|
|
|
|
$definition = new InputDefinition();
|
|
$definition->addArguments(array($this->foo));
|
|
$this->assertEquals($definition->getArgument('foo'), $this->foo, '->getArgument() returns a InputArgument by its name');
|
|
try
|
|
{
|
|
$definition->getArgument('bar');
|
|
$this->fail('->getArgument() throws an exception if the InputArgument name does not exist');
|
|
}
|
|
catch (\Exception $e)
|
|
{
|
|
}
|
|
}
|
|
|
|
public function testHasArgument()
|
|
{
|
|
$this->initializeArguments();
|
|
|
|
$definition = new InputDefinition();
|
|
$definition->addArguments(array($this->foo));
|
|
$this->assertEquals($definition->hasArgument('foo'), true, '->hasArgument() returns true if a InputArgument exists for the given name');
|
|
$this->assertEquals($definition->hasArgument('bar'), false, '->hasArgument() returns false if a InputArgument exists for the given name');
|
|
}
|
|
|
|
public function testGetArgumentRequiredCount()
|
|
{
|
|
$this->initializeArguments();
|
|
|
|
$definition = new InputDefinition();
|
|
$definition->addArgument($this->foo2);
|
|
$this->assertEquals($definition->getArgumentRequiredCount(), 1, '->getArgumentRequiredCount() returns the number of required arguments');
|
|
$definition->addArgument($this->foo);
|
|
$this->assertEquals($definition->getArgumentRequiredCount(), 1, '->getArgumentRequiredCount() returns the number of required arguments');
|
|
}
|
|
|
|
public function testGetArgumentCount()
|
|
{
|
|
$this->initializeArguments();
|
|
|
|
$definition = new InputDefinition();
|
|
$definition->addArgument($this->foo2);
|
|
$this->assertEquals($definition->getArgumentCount(), 1, '->getArgumentCount() returns the number of arguments');
|
|
$definition->addArgument($this->foo);
|
|
$this->assertEquals($definition->getArgumentCount(), 2, '->getArgumentCount() returns the number of arguments');
|
|
}
|
|
|
|
public function testGetArgumentDefaults()
|
|
{
|
|
$definition = new InputDefinition(array(
|
|
new InputArgument('foo1', InputArgument::OPTIONAL),
|
|
new InputArgument('foo2', InputArgument::OPTIONAL, '', 'default'),
|
|
new InputArgument('foo3', InputArgument::OPTIONAL | InputArgument::IS_ARRAY),
|
|
// new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)),
|
|
));
|
|
$this->assertEquals($definition->getArgumentDefaults(), array('foo1' => null, 'foo2' => 'default', 'foo3' => array()), '->getArgumentDefaults() return the default values for each argument');
|
|
|
|
$definition = new InputDefinition(array(
|
|
new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)),
|
|
));
|
|
$this->assertEquals($definition->getArgumentDefaults(), array('foo4' => array(1, 2)), '->getArgumentDefaults() return the default values for each argument');
|
|
}
|
|
|
|
public function testSetOptions()
|
|
{
|
|
$this->initializeOptions();
|
|
|
|
$definition = new InputDefinition(array($this->foo));
|
|
$this->assertEquals($definition->getOptions(), array('foo' => $this->foo), '->setOptions() sets the array of InputOption objects');
|
|
$definition->setOptions(array($this->bar));
|
|
$this->assertEquals($definition->getOptions(), array('bar' => $this->bar), '->setOptions() clears all InputOption objects');
|
|
try
|
|
{
|
|
$definition->getOptionForShortcut('f');
|
|
$this->fail('->setOptions() clears all InputOption objects');
|
|
}
|
|
catch (\Exception $e)
|
|
{
|
|
}
|
|
}
|
|
|
|
public function testAddOptions()
|
|
{
|
|
$this->initializeOptions();
|
|
|
|
$definition = new InputDefinition(array($this->foo));
|
|
$this->assertEquals($definition->getOptions(), array('foo' => $this->foo), '->addOptions() adds an array of InputOption objects');
|
|
$definition->addOptions(array($this->bar));
|
|
$this->assertEquals($definition->getOptions(), array('foo' => $this->foo, 'bar' => $this->bar), '->addOptions() does not clear existing InputOption objects');
|
|
}
|
|
|
|
public function testAddOption()
|
|
{
|
|
$this->initializeOptions();
|
|
|
|
$definition = new InputDefinition();
|
|
$definition->addOption($this->foo);
|
|
$this->assertEquals($definition->getOptions(), array('foo' => $this->foo), '->addOption() adds a InputOption object');
|
|
$definition->addOption($this->bar);
|
|
$this->assertEquals($definition->getOptions(), array('foo' => $this->foo, 'bar' => $this->bar), '->addOption() adds a InputOption object');
|
|
try
|
|
{
|
|
$definition->addOption($this->foo2);
|
|
$this->fail('->addOption() throws a Exception if the another option is already registered with the same name');
|
|
}
|
|
catch (\Exception $e)
|
|
{
|
|
}
|
|
try
|
|
{
|
|
$definition->addOption($this->foo1);
|
|
$this->fail('->addOption() throws a Exception if the another option is already registered with the same shortcut');
|
|
}
|
|
catch (\Exception $e)
|
|
{
|
|
}
|
|
}
|
|
|
|
public function testGetOption()
|
|
{
|
|
$this->initializeOptions();
|
|
|
|
$definition = new InputDefinition(array($this->foo));
|
|
$this->assertEquals($definition->getOption('foo'), $this->foo, '->getOption() returns a InputOption by its name');
|
|
try
|
|
{
|
|
$definition->getOption('bar');
|
|
$this->fail('->getOption() throws an exception if the option name does not exist');
|
|
}
|
|
catch (\Exception $e)
|
|
{
|
|
}
|
|
}
|
|
|
|
public function testHasOption()
|
|
{
|
|
$this->initializeOptions();
|
|
|
|
$definition = new InputDefinition(array($this->foo));
|
|
$this->assertEquals($definition->hasOption('foo'), true, '->hasOption() returns true if a InputOption exists for the given name');
|
|
$this->assertEquals($definition->hasOption('bar'), false, '->hasOption() returns false if a InputOption exists for the given name');
|
|
}
|
|
|
|
public function testHasShortcut()
|
|
{
|
|
$this->initializeOptions();
|
|
|
|
$definition = new InputDefinition(array($this->foo));
|
|
$this->assertEquals($definition->hasShortcut('f'), true, '->hasShortcut() returns true if a InputOption exists for the given shortcut');
|
|
$this->assertEquals($definition->hasShortcut('b'), false, '->hasShortcut() returns false if a InputOption exists for the given shortcut');
|
|
}
|
|
|
|
public function testGetOptionForShortcut()
|
|
{
|
|
$this->initializeOptions();
|
|
|
|
$definition = new InputDefinition(array($this->foo));
|
|
$this->assertEquals($definition->getOptionForShortcut('f'), $this->foo, '->getOptionForShortcut() returns a InputOption by its shortcut');
|
|
try
|
|
{
|
|
$definition->getOptionForShortcut('l');
|
|
$this->fail('->getOption() throws an exception if the shortcut does not exist');
|
|
}
|
|
catch (\Exception $e)
|
|
{
|
|
}
|
|
}
|
|
|
|
public function testGetOptionDefaults()
|
|
{
|
|
$definition = new InputDefinition(array(
|
|
new InputOption('foo1', null, InputOption::PARAMETER_NONE),
|
|
new InputOption('foo2', null, InputOption::PARAMETER_REQUIRED),
|
|
new InputOption('foo3', null, InputOption::PARAMETER_REQUIRED, '', 'default'),
|
|
new InputOption('foo4', null, InputOption::PARAMETER_OPTIONAL),
|
|
new InputOption('foo5', null, InputOption::PARAMETER_OPTIONAL, '', 'default'),
|
|
new InputOption('foo6', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY),
|
|
new InputOption('foo7', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY, '', array(1, 2)),
|
|
));
|
|
$defaults = array(
|
|
'foo1' => null,
|
|
'foo2' => null,
|
|
'foo3' => 'default',
|
|
'foo4' => null,
|
|
'foo5' => 'default',
|
|
'foo6' => array(),
|
|
'foo7' => array(1, 2),
|
|
);
|
|
$this->assertEquals($definition->getOptionDefaults(), $defaults, '->getOptionDefaults() returns the default values for all options');
|
|
}
|
|
|
|
public function testGetSynopsis()
|
|
{
|
|
$definition = new InputDefinition(array(new InputOption('foo')));
|
|
$this->assertEquals($definition->getSynopsis(), '[--foo]', '->getSynopsis() returns a synopsis of arguments and options');
|
|
$definition = new InputDefinition(array(new InputOption('foo', 'f')));
|
|
$this->assertEquals($definition->getSynopsis(), '[-f|--foo]', '->getSynopsis() returns a synopsis of arguments and options');
|
|
$definition = new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)));
|
|
$this->assertEquals($definition->getSynopsis(), '[-f|--foo="..."]', '->getSynopsis() returns a synopsis of arguments and options');
|
|
$definition = new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL)));
|
|
$this->assertEquals($definition->getSynopsis(), '[-f|--foo[="..."]]', '->getSynopsis() returns a synopsis of arguments and options');
|
|
|
|
$definition = new InputDefinition(array(new InputArgument('foo')));
|
|
$this->assertEquals($definition->getSynopsis(), '[foo]', '->getSynopsis() returns a synopsis of arguments and options');
|
|
$definition = new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED)));
|
|
$this->assertEquals($definition->getSynopsis(), 'foo', '->getSynopsis() returns a synopsis of arguments and options');
|
|
$definition = new InputDefinition(array(new InputArgument('foo', InputArgument::IS_ARRAY)));
|
|
$this->assertEquals($definition->getSynopsis(), '[foo1] ... [fooN]', '->getSynopsis() returns a synopsis of arguments and options');
|
|
$definition = new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY)));
|
|
$this->assertEquals($definition->getSynopsis(), 'foo1 ... [fooN]', '->getSynopsis() returns a synopsis of arguments and options');
|
|
}
|
|
|
|
public function testAsText()
|
|
{
|
|
$definition = new InputDefinition(array(
|
|
new InputArgument('foo', InputArgument::OPTIONAL, 'The bar argument'),
|
|
new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The foo argument', array('bar')),
|
|
new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED, 'The foo option'),
|
|
new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL, 'The foo option', 'bar'),
|
|
));
|
|
$this->assertEquals($definition->asText(), file_get_contents(self::$fixtures.'/definition_astext.txt'), '->asText() returns a textual representation of the InputDefinition');
|
|
}
|
|
|
|
public function testAsXml()
|
|
{
|
|
$definition = new InputDefinition(array(
|
|
new InputArgument('foo', InputArgument::OPTIONAL, 'The bar argument'),
|
|
new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The foo argument', array('bar')),
|
|
new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED, 'The foo option'),
|
|
new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL, 'The foo option', 'bar'),
|
|
));
|
|
$this->assertEquals($definition->asXml(), file_get_contents(self::$fixtures.'/definition_asxml.txt'), '->asText() returns a textual representation of the InputDefinition');
|
|
}
|
|
|
|
protected function initializeArguments()
|
|
{
|
|
$this->foo = new InputArgument('foo');
|
|
$this->bar = new InputArgument('bar');
|
|
$this->foo1 = new InputArgument('foo');
|
|
$this->foo2 = new InputArgument('foo2', InputArgument::REQUIRED);
|
|
}
|
|
|
|
protected function initializeOptions()
|
|
{
|
|
$this->foo = new InputOption('foo', 'f');
|
|
$this->bar = new InputOption('bar', 'b');
|
|
$this->foo1 = new InputOption('fooBis', 'f');
|
|
$this->foo2 = new InputOption('foo', 'p');
|
|
}
|
|
}
|