Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PhpMatcherDumperTest.php 20KB

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