[FrameworkBundle] feature: add the ability to search a route
This commit is contained in:
parent
9e82562948
commit
ef0df02afc
@ -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
|
||||
-----
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -1,109 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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;
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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);
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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(),
|
||||
);
|
@ -0,0 +1,2 @@
|
||||
imports:
|
||||
- { resource: ../config/default.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 }
|
Reference in New Issue
Block a user