Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Throw an exception if the Command __construct() method is not called #9186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
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)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InvalidArgumentException seems inappropriate here. UnexpectedValueException would be better.

}

$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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Symfony\Component\Console\Command\Command;

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