Dashboard sipadu mbip
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CompiledUrlMatcherDumperTest.php 19KB

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