RouterDebugCommandTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand;
  13. use Symfony\Component\Console\Application;
  14. use Symfony\Component\Console\Tester\CommandTester;
  15. use Symfony\Component\Routing\Route;
  16. use Symfony\Component\Routing\RouteCollection;
  17. class RouterDebugCommandTest extends TestCase
  18. {
  19. public function testDebugAllRoutes()
  20. {
  21. $tester = $this->createCommandTester();
  22. $ret = $tester->execute(array('name' => null), array('decorated' => false));
  23. $this->assertEquals(0, $ret, 'Returns 0 in case of success');
  24. $this->assertContains('Name Method Scheme Host Path', $tester->getDisplay());
  25. }
  26. public function testDebugSingleRoute()
  27. {
  28. $tester = $this->createCommandTester();
  29. $ret = $tester->execute(array('name' => 'foo'), array('decorated' => false));
  30. $this->assertEquals(0, $ret, 'Returns 0 in case of success');
  31. $this->assertContains('Route Name | foo', $tester->getDisplay());
  32. }
  33. /**
  34. * @expectedException \InvalidArgumentException
  35. */
  36. public function testDebugInvalidRoute()
  37. {
  38. $this->createCommandTester()->execute(array('name' => 'test'));
  39. }
  40. /**
  41. * @return CommandTester
  42. */
  43. private function createCommandTester()
  44. {
  45. $application = new Application();
  46. $command = new RouterDebugCommand();
  47. $command->setContainer($this->getContainer());
  48. $application->add($command);
  49. return new CommandTester($application->find('debug:router'));
  50. }
  51. private function getContainer()
  52. {
  53. $routeCollection = new RouteCollection();
  54. $routeCollection->add('foo', new Route('foo'));
  55. $router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->getMock();
  56. $router
  57. ->expects($this->any())
  58. ->method('getRouteCollection')
  59. ->will($this->returnValue($routeCollection))
  60. ;
  61. $loader = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader')
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  65. $container
  66. ->expects($this->once())
  67. ->method('has')
  68. ->with('router')
  69. ->will($this->returnValue(true))
  70. ;
  71. $container
  72. ->method('get')
  73. ->will($this->returnValueMap(array(
  74. array('router', 1, $router),
  75. array('controller_name_converter', 1, $loader),
  76. )));
  77. return $container;
  78. }
  79. }