[Console] Throw an exception if the command does not contain aliases

It can only happend if the constructor has been overridden
This commit is contained in:
Grégoire Pineau 2013-10-01 12:23:25 +02:00
parent 554d57b399
commit 7e5c9011c9
3 changed files with 25 additions and 0 deletions

View File

@ -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) {

View File

@ -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();

View File

@ -0,0 +1,10 @@
<?php
use Symfony\Component\Console\Command\Command;
class Foo5Command extends Command
{
public function __construct()
{
}
}