diff --git a/src/Symfony/Component/Console/Command/Command.php b/src/Symfony/Component/Console/Command/Command.php index 033a95c7630a6..4cee43a93c58b 100644 --- a/src/Symfony/Component/Console/Command/Command.php +++ b/src/Symfony/Component/Console/Command/Command.php @@ -45,8 +45,6 @@ class Command * * @param string $name The name of the command * - * @throws \LogicException When the command name is empty - * * @api */ public function __construct($name = null) @@ -61,10 +59,6 @@ public function __construct($name = null) } $this->configure(); - - if (!$this->name) { - throw new \LogicException('The command name cannot be empty.'); - } } /** @@ -82,10 +76,16 @@ public function ignoreValidationErrors() * * @param Application $application An Application instance * + * @throws \LogicException When the command name is empty + * * @api */ public function setApplication(Application $application = null) { + if (!$this->name) { + throw new \LogicException('The command name cannot be empty.'); + } + $this->application = $application; if ($application) { $this->setHelperSet($application->getHelperSet()); diff --git a/src/Symfony/Component/Console/Tests/Command/CommandTest.php b/src/Symfony/Component/Console/Tests/Command/CommandTest.php index 40a34658edfb5..f2b962a0e3437 100644 --- a/src/Symfony/Component/Console/Tests/Command/CommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CommandTest.php @@ -35,13 +35,6 @@ static public function setUpBeforeClass() public function testConstructor() { - try { - $command = new Command(); - $this->fail('__construct() throws a \LogicException if the name is null'); - } catch (\Exception $e) { - $this->assertInstanceOf('\LogicException', $e, '__construct() throws a \LogicException if the name is null'); - $this->assertEquals('The command name cannot be empty.', $e->getMessage(), '__construct() throws a \LogicException if the name is null'); - } $command = new Command('foo:bar'); $this->assertEquals('foo:bar', $command->getName(), '__construct() takes the command name as its first argument'); } @@ -52,6 +45,17 @@ public function testSetApplication() $command = new \TestCommand(); $command->setApplication($application); $this->assertEquals($application, $command->getApplication(), '->setApplication() sets the current application'); + + $application = new Application(); + $command = new Command(); + + try { + $command->setApplication($application); + $this->fail('__construct() throws a \LogicException if the command name is null or empty'); + } catch (\Exception $e) { + $this->assertInstanceOf('\LogicException', $e, '->setApplication() throws a \LogicException if the command name is null or empty'); + $this->assertEquals('The command name cannot be empty.', $e->getMessage(), '->setApplication() throws a \LogicException if the command name is null or empty'); + } } public function testSetGetDefinition()