Fix bundle commands are not available via find()

This commit is contained in:
Julien Falque 2016-11-07 20:38:43 +01:00
parent 40dc73aa28
commit dd69b8875d
No known key found for this signature in database
GPG Key ID: 6B13BB4B40DBD0E9
2 changed files with 36 additions and 20 deletions

View File

@ -90,6 +90,16 @@ class Application extends BaseApplication
return parent::doRun($input, $output); return parent::doRun($input, $output);
} }
/**
* {@inheritdoc}
*/
public function find($name)
{
$this->registerCommands();
return parent::find($name);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -32,8 +32,7 @@ class ApplicationTest extends TestCase
public function testBundleCommandsAreRegistered() public function testBundleCommandsAreRegistered()
{ {
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle'); $bundle = $this->createBundleMock(array());
$bundle->expects($this->once())->method('registerCommands');
$kernel = $this->getKernel(array($bundle), true); $kernel = $this->getKernel(array($bundle), true);
@ -46,8 +45,7 @@ class ApplicationTest extends TestCase
public function testBundleCommandsAreRetrievable() public function testBundleCommandsAreRetrievable()
{ {
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle'); $bundle = $this->createBundleMock(array());
$bundle->expects($this->once())->method('registerCommands');
$kernel = $this->getKernel(array($bundle)); $kernel = $this->getKernel(array($bundle));
@ -60,47 +58,41 @@ class ApplicationTest extends TestCase
public function testBundleSingleCommandIsRetrievable() public function testBundleSingleCommandIsRetrievable()
{ {
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle'); $command = new Command('example');
$bundle->expects($this->once())->method('registerCommands');
$bundle = $this->createBundleMock(array($command));
$kernel = $this->getKernel(array($bundle)); $kernel = $this->getKernel(array($bundle));
$application = new Application($kernel); $application = new Application($kernel);
$command = new Command('example');
$application->add($command);
$this->assertSame($command, $application->get('example')); $this->assertSame($command, $application->get('example'));
} }
public function testBundleCommandCanBeFound() public function testBundleCommandCanBeFound()
{ {
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle'); $command = new Command('example');
$bundle->expects($this->once())->method('registerCommands');
$bundle = $this->createBundleMock(array($command));
$kernel = $this->getKernel(array($bundle)); $kernel = $this->getKernel(array($bundle));
$application = new Application($kernel); $application = new Application($kernel);
$command = new Command('example');
$application->add($command);
$this->assertSame($command, $application->find('example')); $this->assertSame($command, $application->find('example'));
} }
public function testBundleCommandCanBeFoundByAlias() public function testBundleCommandCanBeFoundByAlias()
{ {
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle'); $command = new Command('example');
$bundle->expects($this->once())->method('registerCommands'); $command->setAliases(array('alias'));
$bundle = $this->createBundleMock(array($command));
$kernel = $this->getKernel(array($bundle)); $kernel = $this->getKernel(array($bundle));
$application = new Application($kernel); $application = new Application($kernel);
$command = new Command('example');
$command->setAliases(array('alias'));
$application->add($command);
$this->assertSame($command, $application->find('alias')); $this->assertSame($command, $application->find('alias'));
} }
@ -167,4 +159,18 @@ class ApplicationTest extends TestCase
return $kernel; return $kernel;
} }
private function createBundleMock(array $commands)
{
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
$bundle
->expects($this->once())
->method('registerCommands')
->will($this->returnCallback(function (Application $application) use ($commands) {
$application->addCommands($commands);
}))
;
return $bundle;
}
} }