diff --git a/src/Symfony/Component/Console/Input/InputOption.php b/src/Symfony/Component/Console/Input/InputOption.php index dbfcb216adc0..e194b322e9e0 100644 --- a/src/Symfony/Component/Console/Input/InputOption.php +++ b/src/Symfony/Component/Console/Input/InputOption.php @@ -50,6 +50,10 @@ public function __construct($name, $shortcut = null, $mode = null, $description $name = substr($name, 2); } + if (empty($name)) { + throw new \InvalidArgumentException('An option name cannot be empty.'); + } + if (empty($shortcut)) { $shortcut = null; } @@ -60,7 +64,7 @@ public function __construct($name, $shortcut = null, $mode = null, $description } if (empty($shortcut)) { - $shortcut = null; + throw new \InvalidArgumentException('An option shortcut cannot be empty.'); } } diff --git a/src/Symfony/Component/Console/Tests/Input/InputOptionTest.php b/src/Symfony/Component/Console/Tests/Input/InputOptionTest.php index 125629fac08c..b5d8b4336a2e 100644 --- a/src/Symfony/Component/Console/Tests/Input/InputOptionTest.php +++ b/src/Symfony/Component/Console/Tests/Input/InputOptionTest.php @@ -37,8 +37,6 @@ public function testConstructor() $this->assertEquals('f', $option->getShortcut(), '__construct() removes the leading - of the shortcut'); $option = new InputOption('foo'); $this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default'); - $option = new InputOption('foo', '-'); - $this->assertNull($option->getShortcut(), '__construct() makes the shortcut null if a single dash is specified as its name'); // mode argument $option = new InputOption('foo', 'f'); @@ -82,6 +80,30 @@ public function testConstructor() } } + /** + * @expectedException \InvalidArgumentException + */ + public function testEmptyNameIsInvalid() + { + new InputOption(''); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testDoubleDashNameIsInvalid() + { + new InputOption('--'); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testSingleDashOptionIsInvalid() + { + new InputOption('foo', '-'); + } + public function testIsArray() { $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);