RouterTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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\Routing;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\FrameworkBundle\Routing\Router;
  13. use Symfony\Component\Routing\Route;
  14. use Symfony\Component\Routing\RouteCollection;
  15. class RouterTest extends TestCase
  16. {
  17. public function testGenerateWithServiceParam()
  18. {
  19. $routes = new RouteCollection();
  20. $routes->add('foo', new Route(
  21. ' /{_locale}',
  22. array(
  23. '_locale' => '%locale%',
  24. ),
  25. array(
  26. '_locale' => 'en|es',
  27. ), array(), '', array(), array(), '"%foo%" == "bar"'
  28. ));
  29. $sc = $this->getServiceContainer($routes);
  30. $sc->setParameter('locale', 'es');
  31. $sc->setParameter('foo', 'bar');
  32. $router = new Router($sc, 'foo');
  33. $this->assertSame('/en', $router->generate('foo', array('_locale' => 'en')));
  34. $this->assertSame('/', $router->generate('foo', array('_locale' => 'es')));
  35. $this->assertSame('"bar" == "bar"', $router->getRouteCollection()->get('foo')->getCondition());
  36. }
  37. public function testDefaultsPlaceholders()
  38. {
  39. $routes = new RouteCollection();
  40. $routes->add('foo', new Route(
  41. '/foo',
  42. array(
  43. 'foo' => 'before_%parameter.foo%',
  44. 'bar' => '%parameter.bar%_after',
  45. 'baz' => '%%escaped%%',
  46. 'boo' => array('%parameter%', '%%escaped_parameter%%', array('%bee_parameter%', 'bee')),
  47. 'bee' => array('bee', 'bee'),
  48. ),
  49. array(
  50. )
  51. ));
  52. $sc = $this->getServiceContainer($routes);
  53. $sc->setParameter('parameter.foo', 'foo');
  54. $sc->setParameter('parameter.bar', 'bar');
  55. $sc->setParameter('parameter', 'boo');
  56. $sc->setParameter('bee_parameter', 'foo_bee');
  57. $router = new Router($sc, 'foo');
  58. $route = $router->getRouteCollection()->get('foo');
  59. $this->assertEquals(
  60. array(
  61. 'foo' => 'before_foo',
  62. 'bar' => 'bar_after',
  63. 'baz' => '%escaped%',
  64. 'boo' => array('boo', '%escaped_parameter%', array('foo_bee', 'bee')),
  65. 'bee' => array('bee', 'bee'),
  66. ),
  67. $route->getDefaults()
  68. );
  69. }
  70. public function testRequirementsPlaceholders()
  71. {
  72. $routes = new RouteCollection();
  73. $routes->add('foo', new Route(
  74. '/foo',
  75. array(
  76. ),
  77. array(
  78. 'foo' => 'before_%parameter.foo%',
  79. 'bar' => '%parameter.bar%_after',
  80. 'baz' => '%%escaped%%',
  81. )
  82. ));
  83. $sc = $this->getServiceContainer($routes);
  84. $sc->setParameter('parameter.foo', 'foo');
  85. $sc->setParameter('parameter.bar', 'bar');
  86. $router = new Router($sc, 'foo');
  87. $route = $router->getRouteCollection()->get('foo');
  88. $this->assertEquals(
  89. array(
  90. 'foo' => 'before_foo',
  91. 'bar' => 'bar_after',
  92. 'baz' => '%escaped%',
  93. ),
  94. $route->getRequirements()
  95. );
  96. }
  97. public function testPatternPlaceholders()
  98. {
  99. $routes = new RouteCollection();
  100. $routes->add('foo', new Route('/before/%parameter.foo%/after/%%escaped%%'));
  101. $sc = $this->getServiceContainer($routes);
  102. $sc->setParameter('parameter.foo', 'foo');
  103. $router = new Router($sc, 'foo');
  104. $route = $router->getRouteCollection()->get('foo');
  105. $this->assertEquals(
  106. '/before/foo/after/%escaped%',
  107. $route->getPath()
  108. );
  109. }
  110. public function testHostPlaceholders()
  111. {
  112. $routes = new RouteCollection();
  113. $route = new Route('foo');
  114. $route->setHost('/before/%parameter.foo%/after/%%escaped%%');
  115. $routes->add('foo', $route);
  116. $sc = $this->getServiceContainer($routes);
  117. $sc->setParameter('parameter.foo', 'foo');
  118. $router = new Router($sc, 'foo');
  119. $route = $router->getRouteCollection()->get('foo');
  120. $this->assertEquals(
  121. '/before/foo/after/%escaped%',
  122. $route->getHost()
  123. );
  124. }
  125. /**
  126. * @expectedException \Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException
  127. * @expectedExceptionMessage You have requested a non-existent parameter "nope".
  128. */
  129. public function testExceptionOnNonExistentParameter()
  130. {
  131. $routes = new RouteCollection();
  132. $routes->add('foo', new Route('/%nope%'));
  133. $sc = $this->getServiceContainer($routes);
  134. $router = new Router($sc, 'foo');
  135. $router->getRouteCollection()->get('foo');
  136. }
  137. /**
  138. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  139. * @expectedExceptionMessage The container parameter "object", used in the route configuration value "/%object%", must be a string or numeric, but it is of type object.
  140. */
  141. public function testExceptionOnNonStringParameter()
  142. {
  143. $routes = new RouteCollection();
  144. $routes->add('foo', new Route('/%object%'));
  145. $sc = $this->getServiceContainer($routes);
  146. $sc->setParameter('object', new \stdClass());
  147. $router = new Router($sc, 'foo');
  148. $router->getRouteCollection()->get('foo');
  149. }
  150. /**
  151. * @dataProvider getNonStringValues
  152. */
  153. public function testDefaultValuesAsNonStrings($value)
  154. {
  155. $routes = new RouteCollection();
  156. $routes->add('foo', new Route('foo', array('foo' => $value), array('foo' => '\d+')));
  157. $sc = $this->getServiceContainer($routes);
  158. $router = new Router($sc, 'foo');
  159. $route = $router->getRouteCollection()->get('foo');
  160. $this->assertSame($value, $route->getDefault('foo'));
  161. }
  162. public function getNonStringValues()
  163. {
  164. return array(array(null), array(false), array(true), array(new \stdClass()), array(array('foo', 'bar')), array(array(array())));
  165. }
  166. /**
  167. * @return \Symfony\Component\DependencyInjection\Container
  168. */
  169. private function getServiceContainer(RouteCollection $routes)
  170. {
  171. $loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
  172. $loader
  173. ->expects($this->any())
  174. ->method('load')
  175. ->will($this->returnValue($routes))
  176. ;
  177. $sc = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\Container')->setMethods(array('get'))->getMock();
  178. $sc
  179. ->expects($this->once())
  180. ->method('get')
  181. ->will($this->returnValue($loader))
  182. ;
  183. return $sc;
  184. }
  185. }