diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 71beae1e84..b03567d68e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -17,6 +17,7 @@ CHANGELOG is either the service ID or the FQCN of the controller. * Deprecated `Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser` * The `container.service_locator` tag of `ServiceLocator`s is now autoconfigured. + * Add the ability to search a route in `debug:router`. 4.0.0 ----- diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php index da232b2264..498eab8d6c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php @@ -19,6 +19,7 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Routing\RouterInterface; +use Symfony\Component\Routing\RouteCollection; /** * A console command for retrieving information about routes. @@ -76,7 +77,13 @@ EOF $routes = $this->router->getRouteCollection(); if ($name) { - if (!$route = $routes->get($name)) { + if (!($route = $routes->get($name)) && $matchingRoutes = $this->findRouteNameContaining($name, $routes)) { + $default = 1 === count($matchingRoutes) ? $matchingRoutes[0] : null; + $name = $io->choice('Select one of the matching routes', $matchingRoutes, $default); + $route = $routes->get($name); + } + + if (!$route) { throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name)); } @@ -95,4 +102,16 @@ EOF )); } } + + private function findRouteNameContaining(string $name, RouteCollection $routes): array + { + $foundRoutesNames = array(); + foreach ($routes as $routeName => $route) { + if (false !== stripos($routeName, $name)) { + $foundRoutesNames[] = $routeName; + } + } + + return $foundRoutesNames; + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/RouterDebugCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/RouterDebugCommandTest.php deleted file mode 100644 index 54fb8db8c6..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/RouterDebugCommandTest.php +++ /dev/null @@ -1,109 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Bundle\FrameworkBundle\Tests\Command; - -use PHPUnit\Framework\TestCase; -use Symfony\Bundle\FrameworkBundle\Console\Application; -use Symfony\Component\Console\Tester\CommandTester; -use Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand; -use Symfony\Component\HttpKernel\KernelInterface; -use Symfony\Component\Routing\Route; -use Symfony\Component\Routing\RouteCollection; - -class RouterDebugCommandTest extends TestCase -{ - public function testDebugAllRoutes() - { - $tester = $this->createCommandTester(); - $ret = $tester->execute(array('name' => null), array('decorated' => false)); - - $this->assertEquals(0, $ret, 'Returns 0 in case of success'); - $this->assertContains('Name Method Scheme Host Path', $tester->getDisplay()); - } - - public function testDebugSingleRoute() - { - $tester = $this->createCommandTester(); - $ret = $tester->execute(array('name' => 'foo'), array('decorated' => false)); - - $this->assertEquals(0, $ret, 'Returns 0 in case of success'); - $this->assertContains('Route Name | foo', $tester->getDisplay()); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testDebugInvalidRoute() - { - $this->createCommandTester()->execute(array('name' => 'test')); - } - - /** - * @return CommandTester - */ - private function createCommandTester() - { - $application = new Application($this->getKernel()); - $application->add(new RouterDebugCommand($this->getRouter())); - - return new CommandTester($application->find('debug:router')); - } - - private function getRouter() - { - $routeCollection = new RouteCollection(); - $routeCollection->add('foo', new Route('foo')); - $router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->getMock(); - $router - ->expects($this->any()) - ->method('getRouteCollection') - ->will($this->returnValue($routeCollection)); - - return $router; - } - - private function getKernel() - { - $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); - $container - ->expects($this->atLeastOnce()) - ->method('has') - ->will($this->returnCallback(function ($id) { - if ('console.command_loader' === $id) { - return false; - } - - return true; - })) - ; - $container - ->expects($this->any()) - ->method('get') - ->with('router') - ->willReturn($this->getRouter()) - ; - - $kernel = $this->getMockBuilder(KernelInterface::class)->getMock(); - $kernel - ->expects($this->any()) - ->method('getContainer') - ->willReturn($container) - ; - $kernel - ->expects($this->once()) - ->method('getBundles') - ->willReturn(array()) - ; - - return $kernel; - } -} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/RouterDebugCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/RouterDebugCommandTest.php new file mode 100644 index 0000000000..26f6916e9f --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/RouterDebugCommandTest.php @@ -0,0 +1,80 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\Functional; + +use Symfony\Bundle\FrameworkBundle\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; + +/** + * @group functional + */ +class RouterDebugCommandTest extends WebTestCase +{ + private $application; + + protected function setUp() + { + $kernel = static::createKernel(array('test_case' => 'RouterDebug', 'root_config' => 'config.yml')); + $this->application = new Application($kernel); + } + + public function testDumpAllRoutes() + { + $tester = $this->createCommandTester(); + $ret = $tester->execute(array()); + $display = $tester->getDisplay(); + + $this->assertSame(0, $ret, 'Returns 0 in case of success'); + $this->assertContains('routerdebug_test', $display); + $this->assertContains('/test', $display); + $this->assertContains('/session', $display); + } + + public function testDumpOneRoute() + { + $tester = $this->createCommandTester(); + $ret = $tester->execute(array('name' => 'routerdebug_session_welcome')); + + $this->assertSame(0, $ret, 'Returns 0 in case of success'); + $this->assertContains('routerdebug_session_welcome', $tester->getDisplay()); + $this->assertContains('/session', $tester->getDisplay()); + } + + public function testSearchMultipleRoutes() + { + $tester = $this->createCommandTester(); + $tester->setInputs(array(3)); + $ret = $tester->execute(array('name' => 'routerdebug'), array('interactive' => true)); + + $this->assertSame(0, $ret, 'Returns 0 in case of success'); + $this->assertContains('Select one of the matching routes:', $tester->getDisplay()); + $this->assertContains('routerdebug_test', $tester->getDisplay()); + $this->assertContains('/test', $tester->getDisplay()); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The route "gerard" does not exist. + */ + public function testSearchWithThrow() + { + $tester = $this->createCommandTester(); + $tester->execute(array('name' => 'gerard'), array('interactive' => true)); + } + + private function createCommandTester(): CommandTester + { + $command = $this->application->get('debug:router'); + + return new CommandTester($command); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/RouterDebug/bundles.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/RouterDebug/bundles.php new file mode 100644 index 0000000000..a73987bcc9 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/RouterDebug/bundles.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle; +use Symfony\Bundle\FrameworkBundle\FrameworkBundle; + +return array( + new FrameworkBundle(), + new TestBundle(), +); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/RouterDebug/config.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/RouterDebug/config.yml new file mode 100644 index 0000000000..377d3e7852 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/RouterDebug/config.yml @@ -0,0 +1,2 @@ +imports: + - { resource: ../config/default.yml } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/RouterDebug/routing.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/RouterDebug/routing.yml new file mode 100644 index 0000000000..1b9a6c2725 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/RouterDebug/routing.yml @@ -0,0 +1,15 @@ +routerdebug_session_welcome: + path: /session + defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction } + +routerdebug_session_welcome_name: + path: /session/{name} + defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction } + +routerdebug_session_logout: + path: /session_logout + defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::logoutAction } + +routerdebug_test: + path: /test + defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction }