RouterMatchCommandTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\Bundle\FrameworkBundle\Command\RouterMatchCommand;
  14. use Symfony\Component\Console\Application;
  15. use Symfony\Component\Console\Tester\CommandTester;
  16. use Symfony\Component\Routing\RequestContext;
  17. use Symfony\Component\Routing\Route;
  18. use Symfony\Component\Routing\RouteCollection;
  19. class RouterMatchCommandTest extends TestCase
  20. {
  21. public function testWithMatchPath()
  22. {
  23. $tester = $this->createCommandTester();
  24. $ret = $tester->execute(array('path_info' => '/foo', 'foo'), array('decorated' => false));
  25. $this->assertEquals(0, $ret, 'Returns 0 in case of success');
  26. $this->assertContains('Route Name | foo', $tester->getDisplay());
  27. }
  28. public function testWithNotMatchPath()
  29. {
  30. $tester = $this->createCommandTester();
  31. $ret = $tester->execute(array('path_info' => '/test', 'foo'), array('decorated' => false));
  32. $this->assertEquals(1, $ret, 'Returns 1 in case of failure');
  33. $this->assertContains('None of the routes match the path "/test"', $tester->getDisplay());
  34. }
  35. /**
  36. * @return CommandTester
  37. */
  38. private function createCommandTester()
  39. {
  40. $application = new Application();
  41. $command = new RouterMatchCommand();
  42. $command->setContainer($this->getContainer());
  43. $application->add($command);
  44. $command = new RouterDebugCommand();
  45. $command->setContainer($this->getContainer());
  46. $application->add($command);
  47. return new CommandTester($application->find('router:match'));
  48. }
  49. private function getContainer()
  50. {
  51. $routeCollection = new RouteCollection();
  52. $routeCollection->add('foo', new Route('foo'));
  53. $requestContext = new RequestContext();
  54. $router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->getMock();
  55. $router
  56. ->expects($this->any())
  57. ->method('getRouteCollection')
  58. ->will($this->returnValue($routeCollection))
  59. ;
  60. $router
  61. ->expects($this->any())
  62. ->method('getContext')
  63. ->will($this->returnValue($requestContext))
  64. ;
  65. $loader = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader')
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  69. $container
  70. ->expects($this->once())
  71. ->method('has')
  72. ->with('router')
  73. ->will($this->returnValue(true));
  74. $container->method('get')
  75. ->will($this->returnValueMap(array(
  76. array('router', 1, $router),
  77. array('controller_name_converter', 1, $loader),
  78. )));
  79. return $container;
  80. }
  81. }