PhpMatcherDumperTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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\Matcher\RedirectableUrlMatcherInterface;
  14. use Symfony\Component\Routing\Matcher\UrlMatcher;
  15. use Symfony\Component\Routing\RequestContext;
  16. use Symfony\Component\Routing\Route;
  17. use Symfony\Component\Routing\RouteCollection;
  18. class PhpMatcherDumperTest extends TestCase
  19. {
  20. /**
  21. * @var string
  22. */
  23. private $matcherClass;
  24. /**
  25. * @var string
  26. */
  27. private $dumpPath;
  28. protected function setUp()
  29. {
  30. parent::setUp();
  31. $this->matcherClass = uniqid('ProjectUrlMatcher');
  32. $this->dumpPath = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'php_matcher.'.$this->matcherClass.'.php';
  33. }
  34. protected function tearDown()
  35. {
  36. parent::tearDown();
  37. @unlink($this->dumpPath);
  38. }
  39. public function testRedirectPreservesUrlEncoding()
  40. {
  41. $collection = new RouteCollection();
  42. $collection->add('foo', new Route('/foo:bar/'));
  43. $class = $this->generateDumpedMatcher($collection, true);
  44. $matcher = $this->getMockBuilder($class)
  45. ->setMethods(['redirect'])
  46. ->setConstructorArgs([new RequestContext()])
  47. ->getMock();
  48. $matcher->expects($this->once())->method('redirect')->with('/foo%3Abar/', 'foo')->willReturn([]);
  49. $matcher->match('/foo%3Abar');
  50. }
  51. /**
  52. * @dataProvider getRouteCollections
  53. */
  54. public function testDump(RouteCollection $collection, $fixture, $options = [])
  55. {
  56. $basePath = __DIR__.'/../../Fixtures/dumper/';
  57. $dumper = new PhpMatcherDumper($collection);
  58. $this->assertStringEqualsFile($basePath.$fixture, $dumper->dump($options), '->dump() correctly dumps routes as optimized PHP code.');
  59. }
  60. public function getRouteCollections()
  61. {
  62. /* test case 1 */
  63. $collection = new RouteCollection();
  64. $collection->add('overridden', new Route('/overridden'));
  65. // defaults and requirements
  66. $collection->add('foo', new Route(
  67. '/foo/{bar}',
  68. ['def' => 'test'],
  69. ['bar' => 'baz|symfony']
  70. ));
  71. // method requirement
  72. $collection->add('bar', new Route(
  73. '/bar/{foo}',
  74. [],
  75. [],
  76. [],
  77. '',
  78. [],
  79. ['GET', 'head']
  80. ));
  81. // GET method requirement automatically adds HEAD as valid
  82. $collection->add('barhead', new Route(
  83. '/barhead/{foo}',
  84. [],
  85. [],
  86. [],
  87. '',
  88. [],
  89. ['GET']
  90. ));
  91. // simple
  92. $collection->add('baz', new Route(
  93. '/test/baz'
  94. ));
  95. // simple with extension
  96. $collection->add('baz2', new Route(
  97. '/test/baz.html'
  98. ));
  99. // trailing slash
  100. $collection->add('baz3', new Route(
  101. '/test/baz3/'
  102. ));
  103. // trailing slash with variable
  104. $collection->add('baz4', new Route(
  105. '/test/{foo}/'
  106. ));
  107. // trailing slash and method
  108. $collection->add('baz5', new Route(
  109. '/test/{foo}/',
  110. [],
  111. [],
  112. [],
  113. '',
  114. [],
  115. ['post']
  116. ));
  117. // complex name
  118. $collection->add('baz.baz6', new Route(
  119. '/test/{foo}/',
  120. [],
  121. [],
  122. [],
  123. '',
  124. [],
  125. ['put']
  126. ));
  127. // defaults without variable
  128. $collection->add('foofoo', new Route(
  129. '/foofoo',
  130. ['def' => 'test']
  131. ));
  132. // pattern with quotes
  133. $collection->add('quoter', new Route(
  134. '/{quoter}',
  135. [],
  136. ['quoter' => '[\']+']
  137. ));
  138. // space in pattern
  139. $collection->add('space', new Route(
  140. '/spa ce'
  141. ));
  142. // prefixes
  143. $collection1 = new RouteCollection();
  144. $collection1->add('overridden', new Route('/overridden1'));
  145. $collection1->add('foo1', (new Route('/{foo}'))->setMethods('PUT'));
  146. $collection1->add('bar1', new Route('/{bar}'));
  147. $collection1->addPrefix('/b\'b');
  148. $collection2 = new RouteCollection();
  149. $collection2->addCollection($collection1);
  150. $collection2->add('overridden', new Route('/{var}', [], ['var' => '.*']));
  151. $collection1 = new RouteCollection();
  152. $collection1->add('foo2', new Route('/{foo1}'));
  153. $collection1->add('bar2', new Route('/{bar1}'));
  154. $collection1->addPrefix('/b\'b');
  155. $collection2->addCollection($collection1);
  156. $collection2->addPrefix('/a');
  157. $collection->addCollection($collection2);
  158. // overridden through addCollection() and multiple sub-collections with no own prefix
  159. $collection1 = new RouteCollection();
  160. $collection1->add('overridden2', new Route('/old'));
  161. $collection1->add('helloWorld', new Route('/hello/{who}', ['who' => 'World!']));
  162. $collection2 = new RouteCollection();
  163. $collection3 = new RouteCollection();
  164. $collection3->add('overridden2', new Route('/new'));
  165. $collection3->add('hey', new Route('/hey/'));
  166. $collection2->addCollection($collection3);
  167. $collection1->addCollection($collection2);
  168. $collection1->addPrefix('/multi');
  169. $collection->addCollection($collection1);
  170. // "dynamic" prefix
  171. $collection1 = new RouteCollection();
  172. $collection1->add('foo3', new Route('/{foo}'));
  173. $collection1->add('bar3', new Route('/{bar}'));
  174. $collection1->addPrefix('/b');
  175. $collection1->addPrefix('{_locale}');
  176. $collection->addCollection($collection1);
  177. // route between collections
  178. $collection->add('ababa', new Route('/ababa'));
  179. // collection with static prefix but only one route
  180. $collection1 = new RouteCollection();
  181. $collection1->add('foo4', new Route('/{foo}'));
  182. $collection1->addPrefix('/aba');
  183. $collection->addCollection($collection1);
  184. // prefix and host
  185. $collection1 = new RouteCollection();
  186. $route1 = new Route('/route1', [], [], [], 'a.example.com');
  187. $collection1->add('route1', $route1);
  188. $route2 = new Route('/c2/route2', [], [], [], 'a.example.com');
  189. $collection1->add('route2', $route2);
  190. $route3 = new Route('/c2/route3', [], [], [], 'b.example.com');
  191. $collection1->add('route3', $route3);
  192. $route4 = new Route('/route4', [], [], [], 'a.example.com');
  193. $collection1->add('route4', $route4);
  194. $route5 = new Route('/route5', [], [], [], 'c.example.com');
  195. $collection1->add('route5', $route5);
  196. $route6 = new Route('/route6', [], [], [], null);
  197. $collection1->add('route6', $route6);
  198. $collection->addCollection($collection1);
  199. // host and variables
  200. $collection1 = new RouteCollection();
  201. $route11 = new Route('/route11', [], [], [], '{var1}.example.com');
  202. $collection1->add('route11', $route11);
  203. $route12 = new Route('/route12', ['var1' => 'val'], [], [], '{var1}.example.com');
  204. $collection1->add('route12', $route12);
  205. $route13 = new Route('/route13/{name}', [], [], [], '{var1}.example.com');
  206. $collection1->add('route13', $route13);
  207. $route14 = new Route('/route14/{name}', ['var1' => 'val'], [], [], '{var1}.example.com');
  208. $collection1->add('route14', $route14);
  209. $route15 = new Route('/route15/{name}', [], [], [], 'c.example.com');
  210. $collection1->add('route15', $route15);
  211. $route16 = new Route('/route16/{name}', ['var1' => 'val'], [], [], null);
  212. $collection1->add('route16', $route16);
  213. $route17 = new Route('/route17', [], [], [], null);
  214. $collection1->add('route17', $route17);
  215. $collection->addCollection($collection1);
  216. // multiple sub-collections with a single route and a prefix each
  217. $collection1 = new RouteCollection();
  218. $collection1->add('a', new Route('/a...'));
  219. $collection2 = new RouteCollection();
  220. $collection2->add('b', new Route('/{var}'));
  221. $collection3 = new RouteCollection();
  222. $collection3->add('c', new Route('/{var}'));
  223. $collection3->addPrefix('/c');
  224. $collection2->addCollection($collection3);
  225. $collection2->addPrefix('/b');
  226. $collection1->addCollection($collection2);
  227. $collection1->addPrefix('/a');
  228. $collection->addCollection($collection1);
  229. /* test case 2 */
  230. $redirectCollection = clone $collection;
  231. // force HTTPS redirection
  232. $redirectCollection->add('secure', new Route(
  233. '/secure',
  234. [],
  235. [],
  236. [],
  237. '',
  238. ['https']
  239. ));
  240. // force HTTP redirection
  241. $redirectCollection->add('nonsecure', new Route(
  242. '/nonsecure',
  243. [],
  244. [],
  245. [],
  246. '',
  247. ['http']
  248. ));
  249. /* test case 3 */
  250. $rootprefixCollection = new RouteCollection();
  251. $rootprefixCollection->add('static', new Route('/test'));
  252. $rootprefixCollection->add('dynamic', new Route('/{var}'));
  253. $rootprefixCollection->addPrefix('rootprefix');
  254. $route = new Route('/with-condition');
  255. $route->setCondition('context.getMethod() == "GET"');
  256. $rootprefixCollection->add('with-condition', $route);
  257. /* test case 4 */
  258. $headMatchCasesCollection = new RouteCollection();
  259. $headMatchCasesCollection->add('just_head', new Route(
  260. '/just_head',
  261. [],
  262. [],
  263. [],
  264. '',
  265. [],
  266. ['HEAD']
  267. ));
  268. $headMatchCasesCollection->add('head_and_get', new Route(
  269. '/head_and_get',
  270. [],
  271. [],
  272. [],
  273. '',
  274. [],
  275. ['HEAD', 'GET']
  276. ));
  277. $headMatchCasesCollection->add('get_and_head', new Route(
  278. '/get_and_head',
  279. [],
  280. [],
  281. [],
  282. '',
  283. [],
  284. ['GET', 'HEAD']
  285. ));
  286. $headMatchCasesCollection->add('post_and_head', new Route(
  287. '/post_and_head',
  288. [],
  289. [],
  290. [],
  291. '',
  292. [],
  293. ['POST', 'HEAD']
  294. ));
  295. $headMatchCasesCollection->add('put_and_post', new Route(
  296. '/put_and_post',
  297. [],
  298. [],
  299. [],
  300. '',
  301. [],
  302. ['PUT', 'POST']
  303. ));
  304. $headMatchCasesCollection->add('put_and_get_and_head', new Route(
  305. '/put_and_post',
  306. [],
  307. [],
  308. [],
  309. '',
  310. [],
  311. ['PUT', 'GET', 'HEAD']
  312. ));
  313. /* test case 5 */
  314. $groupOptimisedCollection = new RouteCollection();
  315. $groupOptimisedCollection->add('a_first', new Route('/a/11'));
  316. $groupOptimisedCollection->add('a_second', new Route('/a/22'));
  317. $groupOptimisedCollection->add('a_third', new Route('/a/333'));
  318. $groupOptimisedCollection->add('a_wildcard', new Route('/{param}'));
  319. $groupOptimisedCollection->add('a_fourth', new Route('/a/44/'));
  320. $groupOptimisedCollection->add('a_fifth', new Route('/a/55/'));
  321. $groupOptimisedCollection->add('a_sixth', new Route('/a/66/'));
  322. $groupOptimisedCollection->add('nested_wildcard', new Route('/nested/{param}'));
  323. $groupOptimisedCollection->add('nested_a', new Route('/nested/group/a/'));
  324. $groupOptimisedCollection->add('nested_b', new Route('/nested/group/b/'));
  325. $groupOptimisedCollection->add('nested_c', new Route('/nested/group/c/'));
  326. $groupOptimisedCollection->add('slashed_a', new Route('/slashed/group/'));
  327. $groupOptimisedCollection->add('slashed_b', new Route('/slashed/group/b/'));
  328. $groupOptimisedCollection->add('slashed_c', new Route('/slashed/group/c/'));
  329. /* test case 6 & 7 */
  330. $trailingSlashCollection = new RouteCollection();
  331. $trailingSlashCollection->add('simple_trailing_slash_no_methods', new Route('/trailing/simple/no-methods/', [], [], [], '', [], []));
  332. $trailingSlashCollection->add('simple_trailing_slash_GET_method', new Route('/trailing/simple/get-method/', [], [], [], '', [], ['GET']));
  333. $trailingSlashCollection->add('simple_trailing_slash_HEAD_method', new Route('/trailing/simple/head-method/', [], [], [], '', [], ['HEAD']));
  334. $trailingSlashCollection->add('simple_trailing_slash_POST_method', new Route('/trailing/simple/post-method/', [], [], [], '', [], ['POST']));
  335. $trailingSlashCollection->add('regex_trailing_slash_no_methods', new Route('/trailing/regex/no-methods/{param}/', [], [], [], '', [], []));
  336. $trailingSlashCollection->add('regex_trailing_slash_GET_method', new Route('/trailing/regex/get-method/{param}/', [], [], [], '', [], ['GET']));
  337. $trailingSlashCollection->add('regex_trailing_slash_HEAD_method', new Route('/trailing/regex/head-method/{param}/', [], [], [], '', [], ['HEAD']));
  338. $trailingSlashCollection->add('regex_trailing_slash_POST_method', new Route('/trailing/regex/post-method/{param}/', [], [], [], '', [], ['POST']));
  339. $trailingSlashCollection->add('simple_not_trailing_slash_no_methods', new Route('/not-trailing/simple/no-methods', [], [], [], '', [], []));
  340. $trailingSlashCollection->add('simple_not_trailing_slash_GET_method', new Route('/not-trailing/simple/get-method', [], [], [], '', [], ['GET']));
  341. $trailingSlashCollection->add('simple_not_trailing_slash_HEAD_method', new Route('/not-trailing/simple/head-method', [], [], [], '', [], ['HEAD']));
  342. $trailingSlashCollection->add('simple_not_trailing_slash_POST_method', new Route('/not-trailing/simple/post-method', [], [], [], '', [], ['POST']));
  343. $trailingSlashCollection->add('regex_not_trailing_slash_no_methods', new Route('/not-trailing/regex/no-methods/{param}', [], [], [], '', [], []));
  344. $trailingSlashCollection->add('regex_not_trailing_slash_GET_method', new Route('/not-trailing/regex/get-method/{param}', [], [], [], '', [], ['GET']));
  345. $trailingSlashCollection->add('regex_not_trailing_slash_HEAD_method', new Route('/not-trailing/regex/head-method/{param}', [], [], [], '', [], ['HEAD']));
  346. $trailingSlashCollection->add('regex_not_trailing_slash_POST_method', new Route('/not-trailing/regex/post-method/{param}', [], [], [], '', [], ['POST']));
  347. /* test case 8 */
  348. $unicodeCollection = new RouteCollection();
  349. $unicodeCollection->add('a', new Route('/{a}', [], ['a' => 'a'], ['utf8' => false]));
  350. $unicodeCollection->add('b', new Route('/{a}', [], ['a' => '.'], ['utf8' => true]));
  351. $unicodeCollection->add('c', new Route('/{a}', [], ['a' => '.'], ['utf8' => false]));
  352. /* test case 9 */
  353. $hostTreeCollection = new RouteCollection();
  354. $hostTreeCollection->add('a', (new Route('/'))->setHost('{d}.e.c.b.a'));
  355. $hostTreeCollection->add('b', (new Route('/'))->setHost('d.c.b.a'));
  356. $hostTreeCollection->add('c', (new Route('/'))->setHost('{e}.e.c.b.a'));
  357. /* test case 10 */
  358. $chunkedCollection = new RouteCollection();
  359. for ($i = 0; $i < 1000; ++$i) {
  360. $h = substr(md5($i), 0, 6);
  361. $chunkedCollection->add('_'.$i, new Route('/'.$h.'/{a}/{b}/{c}/'.$h));
  362. }
  363. /* test case 11 */
  364. $demoCollection = new RouteCollection();
  365. $demoCollection->add('a', new Route('/admin/post/'));
  366. $demoCollection->add('b', new Route('/admin/post/new'));
  367. $demoCollection->add('c', (new Route('/admin/post/{id}'))->setRequirements(['id' => '\d+']));
  368. $demoCollection->add('d', (new Route('/admin/post/{id}/edit'))->setRequirements(['id' => '\d+']));
  369. $demoCollection->add('e', (new Route('/admin/post/{id}/delete'))->setRequirements(['id' => '\d+']));
  370. $demoCollection->add('f', new Route('/blog/'));
  371. $demoCollection->add('g', new Route('/blog/rss.xml'));
  372. $demoCollection->add('h', (new Route('/blog/page/{page}'))->setRequirements(['id' => '\d+']));
  373. $demoCollection->add('i', (new Route('/blog/posts/{page}'))->setRequirements(['id' => '\d+']));
  374. $demoCollection->add('j', (new Route('/blog/comments/{id}/new'))->setRequirements(['id' => '\d+']));
  375. $demoCollection->add('k', new Route('/blog/search'));
  376. $demoCollection->add('l', new Route('/login'));
  377. $demoCollection->add('m', new Route('/logout'));
  378. $demoCollection->addPrefix('/{_locale}');
  379. $demoCollection->add('n', new Route('/{_locale}'));
  380. $demoCollection->addRequirements(['_locale' => 'en|fr']);
  381. $demoCollection->addDefaults(['_locale' => 'en']);
  382. /* test case 12 */
  383. $suffixCollection = new RouteCollection();
  384. $suffixCollection->add('r1', new Route('abc{foo}/1'));
  385. $suffixCollection->add('r2', new Route('abc{foo}/2'));
  386. $suffixCollection->add('r10', new Route('abc{foo}/10'));
  387. $suffixCollection->add('r20', new Route('abc{foo}/20'));
  388. $suffixCollection->add('r100', new Route('abc{foo}/100'));
  389. $suffixCollection->add('r200', new Route('abc{foo}/200'));
  390. /* test case 13 */
  391. $hostCollection = new RouteCollection();
  392. $hostCollection->add('r1', (new Route('abc{foo}'))->setHost('{foo}.exampple.com'));
  393. $hostCollection->add('r2', (new Route('abc{foo}'))->setHost('{foo}.exampple.com'));
  394. return [
  395. [new RouteCollection(), 'url_matcher0.php', []],
  396. [$collection, 'url_matcher1.php', []],
  397. [$redirectCollection, 'url_matcher2.php', ['base_class' => 'Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher']],
  398. [$rootprefixCollection, 'url_matcher3.php', []],
  399. [$headMatchCasesCollection, 'url_matcher4.php', []],
  400. [$groupOptimisedCollection, 'url_matcher5.php', ['base_class' => 'Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher']],
  401. [$trailingSlashCollection, 'url_matcher6.php', []],
  402. [$trailingSlashCollection, 'url_matcher7.php', ['base_class' => 'Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher']],
  403. [$unicodeCollection, 'url_matcher8.php', []],
  404. [$hostTreeCollection, 'url_matcher9.php', []],
  405. [$chunkedCollection, 'url_matcher10.php', []],
  406. [$demoCollection, 'url_matcher11.php', ['base_class' => 'Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher']],
  407. [$suffixCollection, 'url_matcher12.php', []],
  408. [$hostCollection, 'url_matcher13.php', []],
  409. ];
  410. }
  411. private function generateDumpedMatcher(RouteCollection $collection, $redirectableStub = false)
  412. {
  413. $options = ['class' => $this->matcherClass];
  414. if ($redirectableStub) {
  415. $options['base_class'] = '\Symfony\Component\Routing\Tests\Matcher\Dumper\RedirectableUrlMatcherStub';
  416. }
  417. $dumper = new PhpMatcherDumper($collection);
  418. $code = $dumper->dump($options);
  419. file_put_contents($this->dumpPath, $code);
  420. include $this->dumpPath;
  421. return $this->matcherClass;
  422. }
  423. /**
  424. * @expectedException \InvalidArgumentException
  425. * @expectedExceptionMessage Symfony\Component\Routing\Route cannot contain objects
  426. */
  427. public function testGenerateDumperMatcherWithObject()
  428. {
  429. $routeCollection = new RouteCollection();
  430. $routeCollection->add('_', new Route('/', [new \stdClass()]));
  431. $dumper = new PhpMatcherDumper($routeCollection);
  432. $dumper->dump();
  433. }
  434. }
  435. abstract class RedirectableUrlMatcherStub extends UrlMatcher implements RedirectableUrlMatcherInterface
  436. {
  437. public function redirect($path, $route, $scheme = null)
  438. {
  439. }
  440. }