PhpMatcherDumperTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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\Component\Routing\Tests\Matcher\Dumper;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
  13. use Symfony\Component\Routing\Route;
  14. use Symfony\Component\Routing\RouteCollection;
  15. class PhpMatcherDumperTest extends TestCase
  16. {
  17. /**
  18. * @expectedException \LogicException
  19. */
  20. public function testDumpWhenSchemeIsUsedWithoutAProperDumper()
  21. {
  22. $collection = new RouteCollection();
  23. $collection->add('secure', new Route(
  24. '/secure',
  25. array(),
  26. array(),
  27. array(),
  28. '',
  29. array('https')
  30. ));
  31. $dumper = new PhpMatcherDumper($collection);
  32. $dumper->dump();
  33. }
  34. /**
  35. * @dataProvider getRouteCollections
  36. */
  37. public function testDump(RouteCollection $collection, $fixture, $options = array())
  38. {
  39. $basePath = __DIR__.'/../../Fixtures/dumper/';
  40. $dumper = new PhpMatcherDumper($collection);
  41. $this->assertStringEqualsFile($basePath.$fixture, $dumper->dump($options), '->dump() correctly dumps routes as optimized PHP code.');
  42. }
  43. public function getRouteCollections()
  44. {
  45. /* test case 1 */
  46. $collection = new RouteCollection();
  47. $collection->add('overridden', new Route('/overridden'));
  48. // defaults and requirements
  49. $collection->add('foo', new Route(
  50. '/foo/{bar}',
  51. array('def' => 'test'),
  52. array('bar' => 'baz|symfony')
  53. ));
  54. // method requirement
  55. $collection->add('bar', new Route(
  56. '/bar/{foo}',
  57. array(),
  58. array(),
  59. array(),
  60. '',
  61. array(),
  62. array('GET', 'head')
  63. ));
  64. // GET method requirement automatically adds HEAD as valid
  65. $collection->add('barhead', new Route(
  66. '/barhead/{foo}',
  67. array(),
  68. array(),
  69. array(),
  70. '',
  71. array(),
  72. array('GET')
  73. ));
  74. // simple
  75. $collection->add('baz', new Route(
  76. '/test/baz'
  77. ));
  78. // simple with extension
  79. $collection->add('baz2', new Route(
  80. '/test/baz.html'
  81. ));
  82. // trailing slash
  83. $collection->add('baz3', new Route(
  84. '/test/baz3/'
  85. ));
  86. // trailing slash with variable
  87. $collection->add('baz4', new Route(
  88. '/test/{foo}/'
  89. ));
  90. // trailing slash and method
  91. $collection->add('baz5', new Route(
  92. '/test/{foo}/',
  93. array(),
  94. array(),
  95. array(),
  96. '',
  97. array(),
  98. array('post')
  99. ));
  100. // complex name
  101. $collection->add('baz.baz6', new Route(
  102. '/test/{foo}/',
  103. array(),
  104. array(),
  105. array(),
  106. '',
  107. array(),
  108. array('put')
  109. ));
  110. // defaults without variable
  111. $collection->add('foofoo', new Route(
  112. '/foofoo',
  113. array('def' => 'test')
  114. ));
  115. // pattern with quotes
  116. $collection->add('quoter', new Route(
  117. '/{quoter}',
  118. array(),
  119. array('quoter' => '[\']+')
  120. ));
  121. // space in pattern
  122. $collection->add('space', new Route(
  123. '/spa ce'
  124. ));
  125. // prefixes
  126. $collection1 = new RouteCollection();
  127. $collection1->add('overridden', new Route('/overridden1'));
  128. $collection1->add('foo1', new Route('/{foo}'));
  129. $collection1->add('bar1', new Route('/{bar}'));
  130. $collection1->addPrefix('/b\'b');
  131. $collection2 = new RouteCollection();
  132. $collection2->addCollection($collection1);
  133. $collection2->add('overridden', new Route('/{var}', array(), array('var' => '.*')));
  134. $collection1 = new RouteCollection();
  135. $collection1->add('foo2', new Route('/{foo1}'));
  136. $collection1->add('bar2', new Route('/{bar1}'));
  137. $collection1->addPrefix('/b\'b');
  138. $collection2->addCollection($collection1);
  139. $collection2->addPrefix('/a');
  140. $collection->addCollection($collection2);
  141. // overridden through addCollection() and multiple sub-collections with no own prefix
  142. $collection1 = new RouteCollection();
  143. $collection1->add('overridden2', new Route('/old'));
  144. $collection1->add('helloWorld', new Route('/hello/{who}', array('who' => 'World!')));
  145. $collection2 = new RouteCollection();
  146. $collection3 = new RouteCollection();
  147. $collection3->add('overridden2', new Route('/new'));
  148. $collection3->add('hey', new Route('/hey/'));
  149. $collection2->addCollection($collection3);
  150. $collection1->addCollection($collection2);
  151. $collection1->addPrefix('/multi');
  152. $collection->addCollection($collection1);
  153. // "dynamic" prefix
  154. $collection1 = new RouteCollection();
  155. $collection1->add('foo3', new Route('/{foo}'));
  156. $collection1->add('bar3', new Route('/{bar}'));
  157. $collection1->addPrefix('/b');
  158. $collection1->addPrefix('{_locale}');
  159. $collection->addCollection($collection1);
  160. // route between collections
  161. $collection->add('ababa', new Route('/ababa'));
  162. // collection with static prefix but only one route
  163. $collection1 = new RouteCollection();
  164. $collection1->add('foo4', new Route('/{foo}'));
  165. $collection1->addPrefix('/aba');
  166. $collection->addCollection($collection1);
  167. // prefix and host
  168. $collection1 = new RouteCollection();
  169. $route1 = new Route('/route1', array(), array(), array(), 'a.example.com');
  170. $collection1->add('route1', $route1);
  171. $route2 = new Route('/c2/route2', array(), array(), array(), 'a.example.com');
  172. $collection1->add('route2', $route2);
  173. $route3 = new Route('/c2/route3', array(), array(), array(), 'b.example.com');
  174. $collection1->add('route3', $route3);
  175. $route4 = new Route('/route4', array(), array(), array(), 'a.example.com');
  176. $collection1->add('route4', $route4);
  177. $route5 = new Route('/route5', array(), array(), array(), 'c.example.com');
  178. $collection1->add('route5', $route5);
  179. $route6 = new Route('/route6', array(), array(), array(), null);
  180. $collection1->add('route6', $route6);
  181. $collection->addCollection($collection1);
  182. // host and variables
  183. $collection1 = new RouteCollection();
  184. $route11 = new Route('/route11', array(), array(), array(), '{var1}.example.com');
  185. $collection1->add('route11', $route11);
  186. $route12 = new Route('/route12', array('var1' => 'val'), array(), array(), '{var1}.example.com');
  187. $collection1->add('route12', $route12);
  188. $route13 = new Route('/route13/{name}', array(), array(), array(), '{var1}.example.com');
  189. $collection1->add('route13', $route13);
  190. $route14 = new Route('/route14/{name}', array('var1' => 'val'), array(), array(), '{var1}.example.com');
  191. $collection1->add('route14', $route14);
  192. $route15 = new Route('/route15/{name}', array(), array(), array(), 'c.example.com');
  193. $collection1->add('route15', $route15);
  194. $route16 = new Route('/route16/{name}', array('var1' => 'val'), array(), array(), null);
  195. $collection1->add('route16', $route16);
  196. $route17 = new Route('/route17', array(), array(), array(), null);
  197. $collection1->add('route17', $route17);
  198. $collection->addCollection($collection1);
  199. // multiple sub-collections with a single route and a prefix each
  200. $collection1 = new RouteCollection();
  201. $collection1->add('a', new Route('/a...'));
  202. $collection2 = new RouteCollection();
  203. $collection2->add('b', new Route('/{var}'));
  204. $collection3 = new RouteCollection();
  205. $collection3->add('c', new Route('/{var}'));
  206. $collection3->addPrefix('/c');
  207. $collection2->addCollection($collection3);
  208. $collection2->addPrefix('/b');
  209. $collection1->addCollection($collection2);
  210. $collection1->addPrefix('/a');
  211. $collection->addCollection($collection1);
  212. /* test case 2 */
  213. $redirectCollection = clone $collection;
  214. // force HTTPS redirection
  215. $redirectCollection->add('secure', new Route(
  216. '/secure',
  217. array(),
  218. array(),
  219. array(),
  220. '',
  221. array('https')
  222. ));
  223. // force HTTP redirection
  224. $redirectCollection->add('nonsecure', new Route(
  225. '/nonsecure',
  226. array(),
  227. array(),
  228. array(),
  229. '',
  230. array('http')
  231. ));
  232. /* test case 3 */
  233. $rootprefixCollection = new RouteCollection();
  234. $rootprefixCollection->add('static', new Route('/test'));
  235. $rootprefixCollection->add('dynamic', new Route('/{var}'));
  236. $rootprefixCollection->addPrefix('rootprefix');
  237. $route = new Route('/with-condition');
  238. $route->setCondition('context.getMethod() == "GET"');
  239. $rootprefixCollection->add('with-condition', $route);
  240. /* test case 4 */
  241. $headMatchCasesCollection = new RouteCollection();
  242. $headMatchCasesCollection->add('just_head', new Route(
  243. '/just_head',
  244. array(),
  245. array(),
  246. array(),
  247. '',
  248. array(),
  249. array('HEAD')
  250. ));
  251. $headMatchCasesCollection->add('head_and_get', new Route(
  252. '/head_and_get',
  253. array(),
  254. array(),
  255. array(),
  256. '',
  257. array(),
  258. array('GET', 'HEAD')
  259. ));
  260. $headMatchCasesCollection->add('post_and_head', new Route(
  261. '/post_and_get',
  262. array(),
  263. array(),
  264. array(),
  265. '',
  266. array(),
  267. array('POST', 'HEAD')
  268. ));
  269. $headMatchCasesCollection->add('put_and_post', new Route(
  270. '/put_and_post',
  271. array(),
  272. array(),
  273. array(),
  274. '',
  275. array(),
  276. array('PUT', 'POST')
  277. ));
  278. $headMatchCasesCollection->add('put_and_get_and_head', new Route(
  279. '/put_and_post',
  280. array(),
  281. array(),
  282. array(),
  283. '',
  284. array(),
  285. array('PUT', 'GET', 'HEAD')
  286. ));
  287. /* test case 5 */
  288. $groupOptimisedCollection = new RouteCollection();
  289. $groupOptimisedCollection->add('a_first', new Route('/a/11'));
  290. $groupOptimisedCollection->add('a_second', new Route('/a/22'));
  291. $groupOptimisedCollection->add('a_third', new Route('/a/333'));
  292. $groupOptimisedCollection->add('a_wildcard', new Route('/{param}'));
  293. $groupOptimisedCollection->add('a_fourth', new Route('/a/44/'));
  294. $groupOptimisedCollection->add('a_fifth', new Route('/a/55/'));
  295. $groupOptimisedCollection->add('a_sixth', new Route('/a/66/'));
  296. $groupOptimisedCollection->add('nested_wildcard', new Route('/nested/{param}'));
  297. $groupOptimisedCollection->add('nested_a', new Route('/nested/group/a/'));
  298. $groupOptimisedCollection->add('nested_b', new Route('/nested/group/b/'));
  299. $groupOptimisedCollection->add('nested_c', new Route('/nested/group/c/'));
  300. $groupOptimisedCollection->add('slashed_a', new Route('/slashed/group/'));
  301. $groupOptimisedCollection->add('slashed_b', new Route('/slashed/group/b/'));
  302. $groupOptimisedCollection->add('slashed_c', new Route('/slashed/group/c/'));
  303. $trailingSlashCollection = new RouteCollection();
  304. $trailingSlashCollection->add('simple_trailing_slash_no_methods', new Route('/trailing/simple/no-methods/', array(), array(), array(), '', array(), array()));
  305. $trailingSlashCollection->add('simple_trailing_slash_GET_method', new Route('/trailing/simple/get-method/', array(), array(), array(), '', array(), array('GET')));
  306. $trailingSlashCollection->add('simple_trailing_slash_HEAD_method', new Route('/trailing/simple/head-method/', array(), array(), array(), '', array(), array('HEAD')));
  307. $trailingSlashCollection->add('simple_trailing_slash_POST_method', new Route('/trailing/simple/post-method/', array(), array(), array(), '', array(), array('POST')));
  308. $trailingSlashCollection->add('regex_trailing_slash_no_methods', new Route('/trailing/regex/no-methods/{param}/', array(), array(), array(), '', array(), array()));
  309. $trailingSlashCollection->add('regex_trailing_slash_GET_method', new Route('/trailing/regex/get-method/{param}/', array(), array(), array(), '', array(), array('GET')));
  310. $trailingSlashCollection->add('regex_trailing_slash_HEAD_method', new Route('/trailing/regex/head-method/{param}/', array(), array(), array(), '', array(), array('HEAD')));
  311. $trailingSlashCollection->add('regex_trailing_slash_POST_method', new Route('/trailing/regex/post-method/{param}/', array(), array(), array(), '', array(), array('POST')));
  312. $trailingSlashCollection->add('simple_not_trailing_slash_no_methods', new Route('/not-trailing/simple/no-methods', array(), array(), array(), '', array(), array()));
  313. $trailingSlashCollection->add('simple_not_trailing_slash_GET_method', new Route('/not-trailing/simple/get-method', array(), array(), array(), '', array(), array('GET')));
  314. $trailingSlashCollection->add('simple_not_trailing_slash_HEAD_method', new Route('/not-trailing/simple/head-method', array(), array(), array(), '', array(), array('HEAD')));
  315. $trailingSlashCollection->add('simple_not_trailing_slash_POST_method', new Route('/not-trailing/simple/post-method', array(), array(), array(), '', array(), array('POST')));
  316. $trailingSlashCollection->add('regex_not_trailing_slash_no_methods', new Route('/not-trailing/regex/no-methods/{param}', array(), array(), array(), '', array(), array()));
  317. $trailingSlashCollection->add('regex_not_trailing_slash_GET_method', new Route('/not-trailing/regex/get-method/{param}', array(), array(), array(), '', array(), array('GET')));
  318. $trailingSlashCollection->add('regex_not_trailing_slash_HEAD_method', new Route('/not-trailing/regex/head-method/{param}', array(), array(), array(), '', array(), array('HEAD')));
  319. $trailingSlashCollection->add('regex_not_trailing_slash_POST_method', new Route('/not-trailing/regex/post-method/{param}', array(), array(), array(), '', array(), array('POST')));
  320. return array(
  321. array($collection, 'url_matcher1.php', array()),
  322. array($redirectCollection, 'url_matcher2.php', array('base_class' => 'Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher')),
  323. array($rootprefixCollection, 'url_matcher3.php', array()),
  324. array($headMatchCasesCollection, 'url_matcher4.php', array()),
  325. array($groupOptimisedCollection, 'url_matcher5.php', array('base_class' => 'Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher')),
  326. array($trailingSlashCollection, 'url_matcher6.php', array()),
  327. array($trailingSlashCollection, 'url_matcher7.php', array('base_class' => 'Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher')),
  328. );
  329. }
  330. }