From 7e5c9011c9e9f51f8631b75d258d4efe83ac1fbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Tue, 1 Oct 2013 12:23:25 +0200 Subject: [PATCH] [Console] Throw an exception if the command does not contain aliases It can only happend if the constructor has been overridden --- src/Symfony/Component/Console/Application.php | 4 ++++ .../Component/Console/Tests/ApplicationTest.php | 11 +++++++++++ .../Component/Console/Tests/Fixtures/Foo5Command.php | 10 ++++++++++ 3 files changed, 25 insertions(+) create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/Foo5Command.php diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index f6453ce6b0..eb6b669cf8 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -407,6 +407,10 @@ class Application return; } + if (null === $command->getAliases()) { + throw new \InvalidArgumentException(sprintf('You must call the parent constructor in "%s::__construct()"', get_class($command))); + } + $this->commands[$command->getName()] = $command; foreach ($command->getAliases() as $alias) { diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index 44632ec8e4..065f83474d 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -40,6 +40,7 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase require_once self::$fixturesPath.'/Foo2Command.php'; require_once self::$fixturesPath.'/Foo3Command.php'; require_once self::$fixturesPath.'/Foo4Command.php'; + require_once self::$fixturesPath.'/Foo5Command.php'; require_once self::$fixturesPath.'/FoobarCommand.php'; } @@ -125,6 +126,16 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array($foo, $foo1), array($commands['foo:bar'], $commands['foo:bar1']), '->addCommands() registers an array of commands'); } + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage You must call the parent constructor in "Foo5Command::__construct()" + */ + public function testAddCommandWithEmptyContructor() + { + $application = new Application(); + $application->add($foo = new \Foo5Command()); + } + public function testHasGet() { $application = new Application(); diff --git a/src/Symfony/Component/Console/Tests/Fixtures/Foo5Command.php b/src/Symfony/Component/Console/Tests/Fixtures/Foo5Command.php new file mode 100644 index 0000000000..a1c60827a5 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/Foo5Command.php @@ -0,0 +1,10 @@ +