Skip to content

Commit

Permalink
[Console] added an exception when an option name or shortcut is inval…
Browse files Browse the repository at this point in the history
…id (refs #4346)
  • Loading branch information
fabpot committed May 22, 2012
1 parent c1e868f commit 517ae43
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/Symfony/Component/Console/Input/InputOption.php
Expand Up @@ -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;
}
Expand All @@ -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.');
}
}

Expand Down
26 changes: 24 additions & 2 deletions src/Symfony/Component/Console/Tests/Input/InputOptionTest.php
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 517ae43

Please sign in to comment.