Skip to content

Commit 4177ee8

Browse files
author
Robin Chalas
committed
Don't access private services from container aware commands (deprecated)
1 parent a2df0ee commit 4177ee8

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

Command/RouterDebugCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

1414
use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
15+
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
1516
use Symfony\Component\Console\Input\InputArgument;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Input\InputOption;
@@ -109,7 +110,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
109110

110111
private function convertController(Route $route)
111112
{
112-
$nameParser = $this->getContainer()->get('controller_name_converter');
113+
$nameParser = new ControllerNameParser($this->getApplication()->getKernel());
113114
if ($route->hasDefault('_controller')) {
114115
try {
115116
$route->setDefault('_controller', $nameParser->build($route->getDefault('_controller')));

Tests/Command/RouterDebugCommandTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\Console\Application;
15+
use Symfony\Bundle\FrameworkBundle\Console\Application;
1616
use Symfony\Component\Console\Tester\CommandTester;
1717
use Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand;
18+
use Symfony\Component\HttpKernel\KernelInterface;
1819
use Symfony\Component\Routing\Route;
1920
use Symfony\Component\Routing\RouteCollection;
2021

@@ -51,16 +52,15 @@ public function testDebugInvalidRoute()
5152
*/
5253
private function createCommandTester()
5354
{
54-
$application = new Application();
55+
$application = new Application($this->getKernel());
5556

5657
$command = new RouterDebugCommand();
57-
$command->setContainer($this->getContainer());
5858
$application->add($command);
5959

6060
return new CommandTester($application->find('debug:router'));
6161
}
6262

63-
private function getContainer()
63+
private function getKernel()
6464
{
6565
$routeCollection = new RouteCollection();
6666
$routeCollection->add('foo', new Route('foo'));
@@ -82,14 +82,25 @@ private function getContainer()
8282
->with('router')
8383
->will($this->returnValue(true))
8484
;
85-
8685
$container
86+
->expects($this->any())
8787
->method('get')
88-
->will($this->returnValueMap(array(
89-
array('router', 1, $router),
90-
array('controller_name_converter', 1, $loader),
91-
)));
88+
->with('router')
89+
->willReturn($router)
90+
;
91+
92+
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
93+
$kernel
94+
->expects($this->any())
95+
->method('getContainer')
96+
->willReturn($container)
97+
;
98+
$kernel
99+
->expects($this->once())
100+
->method('getBundles')
101+
->willReturn(array())
102+
;
92103

93-
return $container;
104+
return $kernel;
94105
}
95106
}

Tests/Command/RouterMatchCommandTest.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\Console\Application;
15+
use Symfony\Bundle\FrameworkBundle\Console\Application;
1616
use Symfony\Component\Console\Tester\CommandTester;
1717
use Symfony\Bundle\FrameworkBundle\Command\RouterMatchCommand;
1818
use Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand;
19+
use Symfony\Component\HttpKernel\KernelInterface;
1920
use Symfony\Component\Routing\Route;
2021
use Symfony\Component\Routing\RouteCollection;
2122
use Symfony\Component\Routing\RequestContext;
@@ -45,20 +46,14 @@ public function testWithNotMatchPath()
4546
*/
4647
private function createCommandTester()
4748
{
48-
$application = new Application();
49-
50-
$command = new RouterMatchCommand();
51-
$command->setContainer($this->getContainer());
52-
$application->add($command);
53-
54-
$command = new RouterDebugCommand();
55-
$command->setContainer($this->getContainer());
56-
$application->add($command);
49+
$application = new Application($this->getKernel());
50+
$application->add(new RouterMatchCommand());
51+
$application->add(new RouterDebugCommand());
5752

5853
return new CommandTester($application->find('router:match'));
5954
}
6055

61-
private function getContainer()
56+
private function getKernel()
6257
{
6358
$routeCollection = new RouteCollection();
6459
$routeCollection->add('foo', new Route('foo'));
@@ -81,16 +76,27 @@ private function getContainer()
8176

8277
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
8378
$container
84-
->expects($this->once())
79+
->expects($this->atLeastOnce())
8580
->method('has')
8681
->with('router')
8782
->will($this->returnValue(true));
88-
$container->method('get')
89-
->will($this->returnValueMap(array(
90-
array('router', 1, $router),
91-
array('controller_name_converter', 1, $loader),
92-
)));
83+
$container
84+
->expects($this->any())
85+
->method('get')
86+
->willReturn($router);
87+
88+
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
89+
$kernel
90+
->expects($this->any())
91+
->method('getContainer')
92+
->willReturn($container)
93+
;
94+
$kernel
95+
->expects($this->once())
96+
->method('getBundles')
97+
->willReturn(array())
98+
;
9399

94-
return $container;
100+
return $kernel;
95101
}
96102
}

0 commit comments

Comments
 (0)