[Console] added an exception when an option name or shortcut is invalid (refs #4346)

This commit is contained in:
Fabien Potencier 2012-05-22 12:35:53 +02:00
parent c1e868f994
commit 517ae43fe9
2 changed files with 29 additions and 3 deletions

View File

@ -50,6 +50,10 @@ class InputOption
$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 @@ class InputOption
}
if (empty($shortcut)) {
$shortcut = null;
throw new \InvalidArgumentException('An option shortcut cannot be empty.');
}
}

View File

@ -37,8 +37,6 @@ class InputOptionTest extends \PHPUnit_Framework_TestCase
$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 @@ class InputOptionTest extends \PHPUnit_Framework_TestCase
}
}
/**
* @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);