This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/unit/Symfony/Components/Console/Input/InputDefinitionTest.php

306 lines
13 KiB
PHP
Raw Normal View History

2010-01-04 14:42:28 +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.
*/
require_once __DIR__.'/../../../../bootstrap.php';
2010-01-09 11:57:15 +00:00
use Symfony\Components\Console\Input\InputDefinition;
use Symfony\Components\Console\Input\InputArgument;
use Symfony\Components\Console\Input\InputOption;
use Symfony\Components\Console\Exception;
2010-01-04 14:42:28 +00:00
2010-01-09 11:57:15 +00:00
$fixtures = __DIR__.'/../../../../../fixtures/Symfony/Components/Console';
2010-01-04 14:42:28 +00:00
$t = new LimeTest(51);
$foo = new InputArgument('foo');
$bar = new InputArgument('bar');
$foo1 = new InputArgument('foo');
$foo2 = new InputArgument('foo2', InputArgument::REQUIRED);
2010-01-04 14:42:28 +00:00
// __construct()
$t->diag('__construct()');
$definition = new InputDefinition();
$t->is($definition->getArguments(), array(), '__construct() creates a new InputDefinition object');
2010-01-04 14:42:28 +00:00
$definition = new InputDefinition(array($foo, $bar));
$t->is($definition->getArguments(), array('foo' => $foo, 'bar' => $bar), '__construct() takes an array of InputArgument objects as its first argument');
2010-01-04 14:42:28 +00:00
// ->setArguments()
$t->diag('->setArguments()');
$definition = new InputDefinition();
2010-01-04 14:42:28 +00:00
$definition->setArguments(array($foo));
$t->is($definition->getArguments(), array('foo' => $foo), '->setArguments() sets the array of InputArgument objects');
2010-01-04 14:42:28 +00:00
$definition->setArguments(array($bar));
$t->is($definition->getArguments(), array('bar' => $bar), '->setArguments() clears all InputArgument objects');
2010-01-04 14:42:28 +00:00
// ->addArguments()
$t->diag('->addArguments()');
$definition = new InputDefinition();
2010-01-04 14:42:28 +00:00
$definition->addArguments(array($foo));
$t->is($definition->getArguments(), array('foo' => $foo), '->addArguments() adds an array of InputArgument objects');
2010-01-04 14:42:28 +00:00
$definition->addArguments(array($bar));
$t->is($definition->getArguments(), array('foo' => $foo, 'bar' => $bar), '->addArguments() does not clear existing InputArgument objects');
2010-01-04 14:42:28 +00:00
// ->addArgument()
$t->diag('->addArgument()');
$definition = new InputDefinition();
2010-01-04 14:42:28 +00:00
$definition->addArgument($foo);
$t->is($definition->getArguments(), array('foo' => $foo), '->addArgument() adds a InputArgument object');
2010-01-04 14:42:28 +00:00
$definition->addArgument($bar);
$t->is($definition->getArguments(), array('foo' => $foo, 'bar' => $bar), '->addArgument() adds a InputArgument object');
2010-01-04 14:42:28 +00:00
// arguments must have different names
try
{
$definition->addArgument($foo1);
$t->fail('->addArgument() throws a Exception if another argument is already registered with the same name');
}
catch (\Exception $e)
{
$t->pass('->addArgument() throws a Exception if another argument is already registered with the same name');
}
// cannot add a parameter after an array parameter
$definition->addArgument(new InputArgument('fooarray', InputArgument::IS_ARRAY));
2010-01-04 14:42:28 +00:00
try
{
$definition->addArgument(new InputArgument('anotherbar'));
2010-01-04 14:42:28 +00:00
$t->fail('->addArgument() throws a Exception if there is an array parameter already registered');
}
catch (\Exception $e)
{
$t->pass('->addArgument() throws a Exception if there is an array parameter already registered');
}
// cannot add a required argument after an optional one
$definition = new InputDefinition();
2010-01-04 14:42:28 +00:00
$definition->addArgument($foo);
try
{
$definition->addArgument($foo2);
$t->fail('->addArgument() throws an exception if you try to add a required argument after an optional one');
}
catch (\Exception $e)
{
$t->pass('->addArgument() throws an exception if you try to add a required argument after an optional one');
}
// ->getArgument()
$t->diag('->getArgument()');
$definition = new InputDefinition();
2010-01-04 14:42:28 +00:00
$definition->addArguments(array($foo));
$t->is($definition->getArgument('foo'), $foo, '->getArgument() returns a InputArgument by its name');
2010-01-04 14:42:28 +00:00
try
{
$definition->getArgument('bar');
$t->fail('->getArgument() throws an exception if the InputArgument name does not exist');
2010-01-04 14:42:28 +00:00
}
catch (\Exception $e)
{
$t->pass('->getArgument() throws an exception if the InputArgument name does not exist');
2010-01-04 14:42:28 +00:00
}
// ->hasArgument()
$t->diag('->hasArgument()');
$definition = new InputDefinition();
2010-01-04 14:42:28 +00:00
$definition->addArguments(array($foo));
$t->is($definition->hasArgument('foo'), true, '->hasArgument() returns true if a InputArgument exists for the given name');
$t->is($definition->hasArgument('bar'), false, '->hasArgument() returns false if a InputArgument exists for the given name');
2010-01-04 14:42:28 +00:00
// ->getArgumentRequiredCount()
$t->diag('->getArgumentRequiredCount()');
$definition = new InputDefinition();
2010-01-04 14:42:28 +00:00
$definition->addArgument($foo2);
$t->is($definition->getArgumentRequiredCount(), 1, '->getArgumentRequiredCount() returns the number of required arguments');
$definition->addArgument($foo);
$t->is($definition->getArgumentRequiredCount(), 1, '->getArgumentRequiredCount() returns the number of required arguments');
// ->getArgumentCount()
$t->diag('->getArgumentCount()');
$definition = new InputDefinition();
2010-01-04 14:42:28 +00:00
$definition->addArgument($foo2);
$t->is($definition->getArgumentCount(), 1, '->getArgumentCount() returns the number of arguments');
$definition->addArgument($foo);
$t->is($definition->getArgumentCount(), 2, '->getArgumentCount() returns the number of arguments');
// ->getArgumentDefaults()
$t->diag('->getArgumentDefaults()');
$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)),
2010-01-04 14:42:28 +00:00
));
$t->is($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)),
2010-01-04 14:42:28 +00:00
));
$t->is($definition->getArgumentDefaults(), array('foo4' => array(1, 2)), '->getArgumentDefaults() return the default values for each argument');
$foo = new InputOption('foo', 'f');
$bar = new InputOption('bar', 'b');
$foo1 = new InputOption('fooBis', 'f');
$foo2 = new InputOption('foo', 'p');
2010-01-04 14:42:28 +00:00
// __construct()
$t->diag('__construct()');
$definition = new InputDefinition();
$t->is($definition->getOptions(), array(), '__construct() creates a new InputDefinition object');
2010-01-04 14:42:28 +00:00
$definition = new InputDefinition(array($foo, $bar));
$t->is($definition->getOptions(), array('foo' => $foo, 'bar' => $bar), '__construct() takes an array of InputOption objects as its first argument');
2010-01-04 14:42:28 +00:00
// ->setOptions()
$t->diag('->setOptions()');
$definition = new InputDefinition(array($foo));
$t->is($definition->getOptions(), array('foo' => $foo), '->setOptions() sets the array of InputOption objects');
2010-01-04 14:42:28 +00:00
$definition->setOptions(array($bar));
$t->is($definition->getOptions(), array('bar' => $bar), '->setOptions() clears all InputOption objects');
2010-01-04 14:42:28 +00:00
try
{
$definition->getOptionForShortcut('f');
$t->fail('->setOptions() clears all InputOption objects');
2010-01-04 14:42:28 +00:00
}
catch (\Exception $e)
{
$t->pass('->setOptions() clears all InputOption objects');
2010-01-04 14:42:28 +00:00
}
// ->addOptions()
$t->diag('->addOptions()');
$definition = new InputDefinition(array($foo));
$t->is($definition->getOptions(), array('foo' => $foo), '->addOptions() adds an array of InputOption objects');
2010-01-04 14:42:28 +00:00
$definition->addOptions(array($bar));
$t->is($definition->getOptions(), array('foo' => $foo, 'bar' => $bar), '->addOptions() does not clear existing InputOption objects');
2010-01-04 14:42:28 +00:00
// ->addOption()
$t->diag('->addOption()');
$definition = new InputDefinition();
2010-01-04 14:42:28 +00:00
$definition->addOption($foo);
$t->is($definition->getOptions(), array('foo' => $foo), '->addOption() adds a InputOption object');
2010-01-04 14:42:28 +00:00
$definition->addOption($bar);
$t->is($definition->getOptions(), array('foo' => $foo, 'bar' => $bar), '->addOption() adds a InputOption object');
2010-01-04 14:42:28 +00:00
try
{
$definition->addOption($foo2);
$t->fail('->addOption() throws a Exception if the another option is already registered with the same name');
}
catch (\Exception $e)
{
$t->pass('->addOption() throws a Exception if the another option is already registered with the same name');
}
try
{
$definition->addOption($foo1);
$t->fail('->addOption() throws a Exception if the another option is already registered with the same shortcut');
}
catch (\Exception $e)
{
$t->pass('->addOption() throws a Exception if the another option is already registered with the same shortcut');
}
// ->getOption()
$t->diag('->getOption()');
$definition = new InputDefinition(array($foo));
$t->is($definition->getOption('foo'), $foo, '->getOption() returns a InputOption by its name');
2010-01-04 14:42:28 +00:00
try
{
$definition->getOption('bar');
$t->fail('->getOption() throws an exception if the option name does not exist');
}
catch (\Exception $e)
{
$t->pass('->getOption() throws an exception if the option name does not exist');
}
// ->hasOption()
$t->diag('->hasOption()');
$definition = new InputDefinition(array($foo));
$t->is($definition->hasOption('foo'), true, '->hasOption() returns true if a InputOption exists for the given name');
$t->is($definition->hasOption('bar'), false, '->hasOption() returns false if a InputOption exists for the given name');
2010-01-04 14:42:28 +00:00
// ->hasShortcut()
$t->diag('->hasShortcut()');
$definition = new InputDefinition(array($foo));
$t->is($definition->hasShortcut('f'), true, '->hasShortcut() returns true if a InputOption exists for the given shortcut');
$t->is($definition->hasShortcut('b'), false, '->hasShortcut() returns false if a InputOption exists for the given shortcut');
2010-01-04 14:42:28 +00:00
// ->getOptionForShortcut()
$t->diag('->getOptionForShortcut()');
$definition = new InputDefinition(array($foo));
$t->is($definition->getOptionForShortcut('f'), $foo, '->getOptionForShortcut() returns a InputOption by its shortcut');
2010-01-04 14:42:28 +00:00
try
{
$definition->getOptionForShortcut('l');
$t->fail('->getOption() throws an exception if the shortcut does not exist');
}
catch (\Exception $e)
{
$t->pass('->getOption() throws an exception if the shortcut does not exist');
}
// ->getOptionDefaults()
$t->diag('->getOptionDefaults()');
$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)),
2010-01-04 14:42:28 +00:00
));
$defaults = array(
'foo1' => null,
'foo2' => null,
'foo3' => 'default',
'foo4' => null,
'foo5' => 'default',
'foo6' => array(),
'foo7' => array(1, 2),
);
$t->is($definition->getOptionDefaults(), $defaults, '->getOptionDefaults() returns the default values for all options');
// ->getSynopsis()
$t->diag('->getSynopsis()');
$definition = new InputDefinition(array(new InputOption('foo')));
2010-01-04 14:42:28 +00:00
$t->is($definition->getSynopsis(), '[--foo]', '->getSynopsis() returns a synopsis of arguments and options');
$definition = new InputDefinition(array(new InputOption('foo', 'f')));
2010-01-04 14:42:28 +00:00
$t->is($definition->getSynopsis(), '[-f|--foo]', '->getSynopsis() returns a synopsis of arguments and options');
$definition = new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)));
2010-01-04 14:42:28 +00:00
$t->is($definition->getSynopsis(), '[-f|--foo="..."]', '->getSynopsis() returns a synopsis of arguments and options');
$definition = new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL)));
2010-01-04 14:42:28 +00:00
$t->is($definition->getSynopsis(), '[-f|--foo[="..."]]', '->getSynopsis() returns a synopsis of arguments and options');
$definition = new InputDefinition(array(new InputArgument('foo')));
2010-01-04 14:42:28 +00:00
$t->is($definition->getSynopsis(), '[foo]', '->getSynopsis() returns a synopsis of arguments and options');
$definition = new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED)));
2010-01-04 14:42:28 +00:00
$t->is($definition->getSynopsis(), 'foo', '->getSynopsis() returns a synopsis of arguments and options');
$definition = new InputDefinition(array(new InputArgument('foo', InputArgument::IS_ARRAY)));
2010-01-04 14:42:28 +00:00
$t->is($definition->getSynopsis(), '[foo1] ... [fooN]', '->getSynopsis() returns a synopsis of arguments and options');
$definition = new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY)));
2010-01-04 14:42:28 +00:00
$t->is($definition->getSynopsis(), 'foo1 ... [fooN]', '->getSynopsis() returns a synopsis of arguments and options');
// ->asText()
$t->diag('->asText()');
$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'),
2010-01-04 14:42:28 +00:00
));
$t->is($definition->asText(), file_get_contents($fixtures.'/definition_astext.txt'), '->asText() returns a textual representation of the InputDefinition');
2010-01-04 14:42:28 +00:00
// ->asXml()
$t->diag('->asXml()');
$t->is($definition->asXml(), file_get_contents($fixtures.'/definition_asxml.txt'), '->asText() returns a textual representation of the InputDefinition');