10000 merged branch fabpot/console-fix (PR #8599) · symfony/symfony@70bbf96 · GitHub
[go: up one dir, main page]

Skip to content

Commit 70bbf96

Browse files
committed
merged branch fabpot/console-fix (PR #8599)
This PR was merged into the master branch. Discussion ---------- [FrameworkBundle] fixed regression where the command might have the wrong container if the application is reused several times | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a This fixes a regression introduced in #8242. Before this PR, the commands were always getting the current container registered in the kernel. But as the commands are now registered only once, if the container changes, the commands were not aware of this change. That happens quite frequently in functional tests where the Kernel is booted and shutdown several times in the same process. Commits ------- 1bd45b3 [FrameworkBundle] fixed regression where the command might have the wrong container if the application is reused several times
2 parents 7786fe4 + 1bd45b3 commit 70bbf96

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/Console/Application.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Console;
1313

14+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
1415
use Symfony\Component\Console\Application as BaseApplication;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Input\InputOption;
@@ -66,13 +67,23 @@ public function getKernel()
6667
*/
6768
public function doRun(InputInterface $input, OutputInterface $output)
6869
{
70+
$this->kernel->boot();
71+
6972
if (!$this->commandsRegistered) {
7073
$this->registerCommands();
7174

7275
$this->commandsRegistered = true;
7376
}
7477

75-
$this->setDispatcher($this->kernel->getContainer()->get('event_dispatcher'));
78+
$container = $this->kernel->getContainer();
79+
80+
foreach ($this->all() as $command) {
81+
if ($command instanceof ContainerAwareCommand) {
82+
$command->setContainer($container);
83+
}
84+
}
85+
86+
$this->setDispatcher($container->get('event_dispatcher'));
7687

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

8899
protected function registerCommands()
89100
{
90-
$this->kernel->boot();
91-
92101
foreach ($this->kernel->getBundles() as $bundle) {
93102
if ($bundle instanceof Bundle) {
94103
$bundle->registerCommands($this);

src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bundle\FrameworkBundle\Console\Application;
1616
use Symfony\Component\Console\Input\ArrayInput;
1717
use Symfony\Component\Console\Output\NullOutput;
18+
use Symfony\Component\Console\Tester\ApplicationTester;
1819

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

43+
public function testBundleCommandsHaveRightContainer()
44+
{
45+
$command = $this->getMockForAbstractClass('Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand', array('foo'), '', true, true, true, array('setContainer'));
46+
$command->setCode(function () {});
47+
$command->expects($this->exactly(2))->method('setContainer');
48+
49+
$application = new Application($this->getKernel(array()));
50+
$application->setAutoExit(false);
51+
$application->setCatchExceptions(false);
52+
$application->add($command);
53+
$tester = new ApplicationTester($application);
54+
55+
// set container is called here
56+
$tester->run(array('command' => 'foo'));
57+
58+
// as the container might have change between two runs, setContainer must called again
59+
$tester->run(array('command' => 'foo'));
60+
}
61+
4262
private function getKernel(array $bundles)
4363
{
4464
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
@@ -49,7 +69,7 @@ private function getKernel(array $bundles)
4969

5070
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
5171
$container
52-
->expects($this->once())
72+
->expects($this->atLeastOnce())
5373
->method('get')
5474
->with($this->equalTo('event_dispatcher'))
5575
->will($this->returnValue($dispatcher))

0 commit comments

Comments
 (0)
0