Skip to content

Commit

Permalink
[Console] Throw an exception if the command does not contain aliases
Browse files Browse the repository at this point in the history
It can only happend if the constructor has been overridden
  • Loading branch information
lyrixx committed Oct 1, 2013
1 parent 554d57b commit 7e5c901
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Symfony/Component/Console/Application.php
Expand Up @@ -407,6 +407,10 @@ public function add(Command $command)
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) {
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Expand Up @@ -40,6 +40,7 @@ public static function setUpBeforeClass()
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';
}

Expand Down Expand Up @@ -125,6 +126,16 @@ public function testAdd()
$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();
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Console/Tests/Fixtures/Foo5Command.php
@@ -0,0 +1,10 @@
<?php

use Symfony\Component\Console\Command\Command;

class Foo5Command extends Command
{
public function __construct()
{
}
}

0 comments on commit 7e5c901

Please sign in to comment.