Skip to content

Commit

Permalink
merged branch fabpot/console-fix (PR #8600)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.3 branch.

Discussion
----------

[FrameworkBundle] fixed regression where the command might have the wrong container if the application is reused several times

Commits
-------

1bd45b3 [FrameworkBundle] fixed regression where the command might have the wrong container if the application is reused several times
  • Loading branch information
fabpot committed Jul 29, 2013
2 parents 9d53905 + 1bd45b3 commit bd30a5c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/Symfony/Bundle/FrameworkBundle/Console/Application.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\Console;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -66,13 +67,23 @@ public function getKernel()
*/
public function doRun(InputInterface $input, OutputInterface $output)
{
$this->kernel->boot();

if (!$this->commandsRegistered) {
$this->registerCommands();

$this->commandsRegistered = true;
}

$this->setDispatcher($this->kernel->getContainer()->get('event_dispatcher'));
$container = $this->kernel->getContainer();

foreach ($this->all() as $command) {
if ($command instanceof ContainerAwareCommand) {
$command->setContainer($container);
}
}

$this->setDispatcher($container->get('event_dispatcher'));

if (true === $input->hasParameterOption(array('--shell', '-s'))) {
$shell = new Shell($this);
Expand All @@ -87,8 +98,6 @@ public function doRun(InputInterface $input, OutputInterface $output)

protected function registerCommands()
{
$this->kernel->boot();

foreach ($this->kernel->getBundles() as $bundle) {
if ($bundle instanceof Bundle) {
$bundle->registerCommands($this);
Expand Down
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Tester\ApplicationTester;

class ApplicationTest extends TestCase
{
Expand All @@ -39,6 +40,25 @@ public function testBundleCommandsAreRegistered()
$application->doRun(new ArrayInput(array('list')), new NullOutput());
}

public function testBundleCommandsHaveRightContainer()
{
$command = $this->getMockForAbstractClass('Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand', array('foo'), '', true, true, true, array('setContainer'));
$command->setCode(function () {});
$command->expects($this->exactly(2))->method('setContainer');

$application = new Application($this->getKernel(array()));
$application->setAutoExit(false);
$application->setCatchExceptions(false);
$application->add($command);
$tester = new ApplicationTester($application);

// set container is called here
$tester->run(array('command' => 'foo'));

// as the container might have change between two runs, setContainer must called again
$tester->run(array('command' => 'foo'));
}

private function getKernel(array $bundles)
{
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
Expand All @@ -49,7 +69,7 @@ private function getKernel(array $bundles)

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container
->expects($this->once())
->expects($this->atLeastOnce())
->method('get')
->with($this->equalTo('event_dispatcher'))
->will($this->returnValue($dispatcher))
Expand Down

0 comments on commit bd30a5c

Please sign in to comment.