1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441:
<?php
namespace Symfony\Component\Console\Tests\Input;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
class InputDefinitionTest extends \PHPUnit_Framework_TestCase
{
protected static $fixtures;
protected $foo;
protected $bar;
protected $foo1;
protected $foo2;
public static function setUpBeforeClass()
{
self::$fixtures = __DIR__.'/../Fixtures/';
}
public function testConstructorArguments()
{
$this->initializeArguments();
$definition = new InputDefinition();
$this->assertEquals(array(), $definition->getArguments(), '__construct() creates a new InputDefinition object');
$definition = new InputDefinition(array($this->foo, $this->bar));
$this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '__construct() takes an array of InputArgument objects as its first argument');
}
public function testConstructorOptions()
{
$this->initializeOptions();
$definition = new InputDefinition();
$this->assertEquals(array(), $definition->getOptions(), '__construct() creates a new InputDefinition object');
$definition = new InputDefinition(array($this->foo, $this->bar));
$this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '__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(array('foo' => $this->foo), $definition->getArguments(), '->setArguments() sets the array of InputArgument objects');
$definition->setArguments(array($this->bar));
$this->assertEquals(array('bar' => $this->bar), $definition->getArguments(), '->setArguments() clears all InputArgument objects');
}
public function testAddArguments()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArguments(array($this->foo));
$this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->addArguments() adds an array of InputArgument objects');
$definition->addArguments(array($this->bar));
$this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '->addArguments() does not clear existing InputArgument objects');
}
public function testAddArgument()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArgument($this->foo);
$this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->addArgument() adds a InputArgument object');
$definition->addArgument($this->bar);
$this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '->addArgument() adds a InputArgument object');
}
public function testArgumentsMustHaveDifferentNames()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArgument($this->foo);
$definition->addArgument($this->foo1);
}
public function testArrayArgumentHasToBeLast()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArgument(new InputArgument('fooarray', InputArgument::IS_ARRAY));
$definition->addArgument(new InputArgument('anotherbar'));
}
public function testRequiredArgumentCannotFollowAnOptionalOne()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArgument($this->foo);
$definition->addArgument($this->foo2);
}
public function testGetArgument()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArguments(array($this->foo));
$this->assertEquals($this->foo, $definition->getArgument('foo'), '->getArgument() returns a InputArgument by its name');
}
public function testGetInvalidArgument()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArguments(array($this->foo));
$definition->getArgument('bar');
}
public function testHasArgument()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArguments(array($this->foo));
$this->assertTrue($definition->hasArgument('foo'), '->hasArgument() returns true if a InputArgument exists for the given name');
$this->assertFalse($definition->hasArgument('bar'), '->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(1, $definition->getArgumentRequiredCount(), '->getArgumentRequiredCount() returns the number of required arguments');
$definition->addArgument($this->foo);
$this->assertEquals(1, $definition->getArgumentRequiredCount(), '->getArgumentRequiredCount() returns the number of required arguments');
}
public function testGetArgumentCount()
{
$this->initializeArguments();
$definition = new InputDefinition();
$definition->addArgument($this->foo2);
$this->assertEquals(1, $definition->getArgumentCount(), '->getArgumentCount() returns the number of arguments');
$definition->addArgument($this->foo);
$this->assertEquals(2, $definition->getArgumentCount(), '->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),
));
$this->assertEquals(array('foo1' => null, 'foo2' => 'default', 'foo3' => array()), $definition->getArgumentDefaults(), '->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(array('foo4' => array(1, 2)), $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
}
public function testSetOptions()
{
$this->initializeOptions();
$definition = new InputDefinition(array($this->foo));
$this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->setOptions() sets the array of InputOption objects');
$definition->setOptions(array($this->bar));
$this->assertEquals(array('bar' => $this->bar), $definition->getOptions(), '->setOptions() clears all InputOption objects');
}
public function testSetOptionsClearsOptions()
{
$this->initializeOptions();
$definition = new InputDefinition(array($this->foo));
$definition->setOptions(array($this->bar));
$definition->getOptionForShortcut('f');
}
public function testAddOptions()
{
$this->initializeOptions();
$definition = new InputDefinition(array($this->foo));
$this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->addOptions() adds an array of InputOption objects');
$definition->addOptions(array($this->bar));
$this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '->addOptions() does not clear existing InputOption objects');
}
public function testAddOption()
{
$this->initializeOptions();
$definition = new InputDefinition();
$definition->addOption($this->foo);
$this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->addOption() adds a InputOption object');
$definition->addOption($this->bar);
$this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '->addOption() adds a InputOption object');
}
public function testAddDuplicateOption()
{
$this->initializeOptions();
$definition = new InputDefinition();
$definition->addOption($this->foo);
$definition->addOption($this->foo2);
}
public function testAddDuplicateShortcutOption()
{
$this->initializeOptions();
$definition = new InputDefinition();
$definition->addOption($this->foo);
$definition->addOption($this->foo1);
}
public function testGetOption()
{
$this->initializeOptions();
$definition = new InputDefinition(array($this->foo));
$this->assertEquals($this->foo, $definition->getOption('foo'), '->getOption() returns a InputOption by its name');
}
public function testGetInvalidOption()
{
$this->initializeOptions();
$definition = new InputDefinition(array($this->foo));
$definition->getOption('bar');
}
public function testHasOption()
{
$this->initializeOptions();
$definition = new InputDefinition(array($this->foo));
$this->assertTrue($definition->hasOption('foo'), '->hasOption() returns true if a InputOption exists for the given name');
$this->assertFalse($definition->hasOption('bar'), '->hasOption() returns false if a InputOption exists for the given name');
}
public function testHasShortcut()
{
$this->initializeOptions();
$definition = new InputDefinition(array($this->foo));
$this->assertTrue($definition->hasShortcut('f'), '->hasShortcut() returns true if a InputOption exists for the given shortcut');
$this->assertFalse($definition->hasShortcut('b'), '->hasShortcut() returns false if a InputOption exists for the given shortcut');
}
public function testGetOptionForShortcut()
{
$this->initializeOptions();
$definition = new InputDefinition(array($this->foo));
$this->assertEquals($this->foo, $definition->getOptionForShortcut('f'), '->getOptionForShortcut() returns a InputOption by its shortcut');
}
public function testGetOptionForMultiShortcut()
{
$this->initializeOptions();
$definition = new InputDefinition(array($this->multi));
$this->assertEquals($this->multi, $definition->getOptionForShortcut('m'), '->getOptionForShortcut() returns a InputOption by its shortcut');
$this->assertEquals($this->multi, $definition->getOptionForShortcut('mmm'), '->getOptionForShortcut() returns a InputOption by its shortcut');
}
public function testGetOptionForInvalidShortcut()
{
$this->initializeOptions();
$definition = new InputDefinition(array($this->foo));
$definition->getOptionForShortcut('l');
}
public function testGetOptionDefaults()
{
$definition = new InputDefinition(array(
new InputOption('foo1', null, InputOption::VALUE_NONE),
new InputOption('foo2', null, InputOption::VALUE_REQUIRED),
new InputOption('foo3', null, InputOption::VALUE_REQUIRED, '', 'default'),
new InputOption('foo4', null, InputOption::VALUE_OPTIONAL),
new InputOption('foo5', null, InputOption::VALUE_OPTIONAL, '', 'default'),
new InputOption('foo6', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY),
new InputOption('foo7', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, '', array(1, 2)),
));
$defaults = array(
'foo1' => false,
'foo2' => null,
'foo3' => 'default',
'foo4' => null,
'foo5' => 'default',
'foo6' => array(),
'foo7' => array(1, 2),
);
$this->assertSame($defaults, $definition->getOptionDefaults(), '->getOptionDefaults() returns the default values for all options');
}
public function testGetSynopsis(InputDefinition $definition, $expectedSynopsis, $message = null)
{
$this->assertEquals($expectedSynopsis, $definition->getSynopsis(), $message ? '->getSynopsis() '.$message : '');
}
public function getGetSynopsisData()
{
return array(
array(new InputDefinition(array(new InputOption('foo'))), '[--foo]', 'puts optional options in square brackets'),
array(new InputDefinition(array(new InputOption('foo', 'f'))), '[-f|--foo]', 'separates shortcut with a pipe'),
array(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))), '[-f|--foo FOO]', 'uses shortcut as value placeholder'),
array(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))), '[-f|--foo [FOO]]', 'puts optional values in square brackets'),
array(new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED))), '<foo>', 'puts arguments in angle brackets'),
array(new InputDefinition(array(new InputArgument('foo'))), '[<foo>]', 'puts optional arguments in square brackets'),
array(new InputDefinition(array(new InputArgument('foo', InputArgument::IS_ARRAY))), '[<foo>]...', 'uses an ellipsis for array arguments'),
array(new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY))), '<foo> (<foo>)...', 'uses parenthesis and ellipsis for required array arguments'),
array(new InputDefinition(array(new InputOption('foo'), new InputArgument('foo', InputArgument::REQUIRED))), '[--foo] [--] <foo>', 'puts [--] between options and arguments'),
);
}
public function testGetShortSynopsis()
{
$definition = new InputDefinition(array(new InputOption('foo'), new InputOption('bar'), new InputArgument('cat')));
$this->assertEquals('[options] [--] [<cat>]', $definition->getSynopsis(true), '->getSynopsis(true) groups options in [options]');
}
public function testLegacyAsText()
{
$definition = new InputDefinition(array(
new InputArgument('foo', InputArgument::OPTIONAL, 'The foo argument'),
new InputArgument('baz', InputArgument::OPTIONAL, 'The baz argument', true),
new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The bar argument', array('http://foo.com/')),
new InputOption('foo', 'f', InputOption::VALUE_REQUIRED, 'The foo option'),
new InputOption('baz', null, InputOption::VALUE_OPTIONAL, 'The baz option', false),
new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL, 'The bar option', 'bar'),
new InputOption('qux', '', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The qux option', array('http://foo.com/', 'bar')),
new InputOption('qux2', '', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The qux2 option', array('foo' => 'bar')),
));
$this->assertStringEqualsFile(self::$fixtures.'/definition_astext.txt', $definition->asText(), '->asText() returns a textual representation of the InputDefinition');
}
public function testLegacyAsXml()
{
$definition = new InputDefinition(array(
new InputArgument('foo', InputArgument::OPTIONAL, 'The foo argument'),
new InputArgument('baz', InputArgument::OPTIONAL, 'The baz argument', true),
new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The bar argument', array('bar')),
new InputOption('foo', 'f', InputOption::VALUE_REQUIRED, 'The foo option'),
new InputOption('baz', null, InputOption::VALUE_OPTIONAL, 'The baz option', false),
new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL, 'The bar option', 'bar'),
));
$this->assertXmlStringEqualsXmlFile(self::$fixtures.'/definition_asxml.txt', $definition->asXml(), '->asXml() returns an XML 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');
$this->multi = new InputOption('multi', 'm|mm|mmm');
}
}