RouteBuilderTest.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Routing;
  16. use Cake\Core\Plugin;
  17. use Cake\Routing\RouteBuilder;
  18. use Cake\Routing\RouteCollection;
  19. use Cake\Routing\Router;
  20. use Cake\Routing\Route\InflectedRoute;
  21. use Cake\Routing\Route\RedirectRoute;
  22. use Cake\Routing\Route\Route;
  23. use Cake\TestSuite\TestCase;
  24. use RuntimeException;
  25. /**
  26. * RouteBuilder test case
  27. */
  28. class RouteBuilderTest extends TestCase
  29. {
  30. /**
  31. * Setup method
  32. *
  33. * @return void
  34. */
  35. public function setUp()
  36. {
  37. parent::setUp();
  38. $this->collection = new RouteCollection();
  39. }
  40. /**
  41. * Teardown method
  42. *
  43. * @return void
  44. */
  45. public function tearDown()
  46. {
  47. parent::tearDown();
  48. $this->clearPlugins();
  49. }
  50. /**
  51. * Test path()
  52. *
  53. * @return void
  54. */
  55. public function testPath()
  56. {
  57. $routes = new RouteBuilder($this->collection, '/some/path');
  58. $this->assertEquals('/some/path', $routes->path());
  59. $routes = new RouteBuilder($this->collection, '/:book_id');
  60. $this->assertEquals('/', $routes->path());
  61. $routes = new RouteBuilder($this->collection, '/path/:book_id');
  62. $this->assertEquals('/path/', $routes->path());
  63. $routes = new RouteBuilder($this->collection, '/path/book:book_id');
  64. $this->assertEquals('/path/book', $routes->path());
  65. }
  66. /**
  67. * Test params()
  68. *
  69. * @return void
  70. */
  71. public function testParams()
  72. {
  73. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  74. $this->assertEquals(['prefix' => 'api'], $routes->params());
  75. }
  76. /**
  77. * Test getting connected routes.
  78. *
  79. * @return void
  80. */
  81. public function testRoutes()
  82. {
  83. $routes = new RouteBuilder($this->collection, '/l');
  84. $routes->connect('/:controller', ['action' => 'index']);
  85. $routes->connect('/:controller/:action/*');
  86. $all = $this->collection->routes();
  87. $this->assertCount(2, $all);
  88. $this->assertInstanceOf(Route::class, $all[0]);
  89. $this->assertInstanceOf(Route::class, $all[1]);
  90. }
  91. /**
  92. * Test setting default route class
  93. *
  94. * @return void
  95. */
  96. public function testRouteClass()
  97. {
  98. $routes = new RouteBuilder(
  99. $this->collection,
  100. '/l',
  101. [],
  102. ['routeClass' => 'InflectedRoute']
  103. );
  104. $routes->connect('/:controller', ['action' => 'index']);
  105. $routes->connect('/:controller/:action/*');
  106. $all = $this->collection->routes();
  107. $this->assertInstanceOf(InflectedRoute::class, $all[0]);
  108. $this->assertInstanceOf(InflectedRoute::class, $all[1]);
  109. $this->collection = new RouteCollection();
  110. $routes = new RouteBuilder($this->collection, '/l');
  111. $this->assertSame($routes, $routes->setRouteClass('TestApp\Routing\Route\DashedRoute'));
  112. $this->assertSame('TestApp\Routing\Route\DashedRoute', $routes->getRouteClass());
  113. $routes->connect('/:controller', ['action' => 'index']);
  114. $all = $this->collection->routes();
  115. $this->assertInstanceOf('TestApp\Routing\Route\DashedRoute', $all[0]);
  116. }
  117. /**
  118. * Test connecting an instance routes.
  119. *
  120. * @return void
  121. */
  122. public function testConnectInstance()
  123. {
  124. $routes = new RouteBuilder($this->collection, '/l', ['prefix' => 'api']);
  125. $route = new Route('/:controller');
  126. $this->assertSame($route, $routes->connect($route));
  127. $result = $this->collection->routes()[0];
  128. $this->assertSame($route, $result);
  129. }
  130. /**
  131. * Test connecting basic routes.
  132. *
  133. * @return void
  134. */
  135. public function testConnectBasic()
  136. {
  137. $routes = new RouteBuilder($this->collection, '/l', ['prefix' => 'api']);
  138. $route = $routes->connect('/:controller');
  139. $this->assertInstanceOf(Route::class, $route);
  140. $this->assertSame($route, $this->collection->routes()[0]);
  141. $this->assertEquals('/l/:controller', $route->template);
  142. $expected = ['prefix' => 'api', 'action' => 'index', 'plugin' => null];
  143. $this->assertEquals($expected, $route->defaults);
  144. }
  145. /**
  146. * Test that compiling a route results in an trailing / optional pattern.
  147. *
  148. * @return void
  149. */
  150. public function testConnectTrimTrailingSlash()
  151. {
  152. $routes = new RouteBuilder($this->collection, '/articles', ['controller' => 'Articles']);
  153. $routes->connect('/', ['action' => 'index']);
  154. $expected = [
  155. 'plugin' => null,
  156. 'controller' => 'Articles',
  157. 'action' => 'index',
  158. 'pass' => [],
  159. '_matchedRoute' => '/articles',
  160. ];
  161. $this->assertEquals($expected, $this->collection->parse('/articles'));
  162. $this->assertEquals($expected, $this->collection->parse('/articles/'));
  163. }
  164. /**
  165. * Test connect() with short string syntax
  166. *
  167. * @return void
  168. */
  169. public function testConnectShortStringInvalid()
  170. {
  171. $this->expectException(RuntimeException::class);
  172. $routes = new RouteBuilder($this->collection, '/');
  173. $routes->connect('/my-articles/view', 'Articles:no');
  174. }
  175. /**
  176. * Test connect() with short string syntax
  177. *
  178. * @return void
  179. */
  180. public function testConnectShortString()
  181. {
  182. $routes = new RouteBuilder($this->collection, '/');
  183. $routes->connect('/my-articles/view', 'Articles::view');
  184. $expected = [
  185. 'pass' => [],
  186. 'controller' => 'Articles',
  187. 'action' => 'view',
  188. 'plugin' => null,
  189. '_matchedRoute' => '/my-articles/view',
  190. ];
  191. $this->assertEquals($expected, $this->collection->parse('/my-articles/view'));
  192. $url = $expected['_matchedRoute'];
  193. unset($expected['_matchedRoute']);
  194. $this->assertEquals($url, '/' . $this->collection->match($expected, []));
  195. }
  196. /**
  197. * Test connect() with short string syntax
  198. *
  199. * @return void
  200. */
  201. public function testConnectShortStringPrefix()
  202. {
  203. $routes = new RouteBuilder($this->collection, '/');
  204. $routes->connect('/admin/bookmarks', 'Admin/Bookmarks::index');
  205. $expected = [
  206. 'pass' => [],
  207. 'plugin' => null,
  208. 'prefix' => 'admin',
  209. 'controller' => 'Bookmarks',
  210. 'action' => 'index',
  211. '_matchedRoute' => '/admin/bookmarks',
  212. ];
  213. $this->assertEquals($expected, $this->collection->parse('/admin/bookmarks'));
  214. $url = $expected['_matchedRoute'];
  215. unset($expected['_matchedRoute']);
  216. $this->assertEquals($url, '/' . $this->collection->match($expected, []));
  217. }
  218. /**
  219. * Test connect() with short string syntax
  220. *
  221. * @return void
  222. */
  223. public function testConnectShortStringPlugin()
  224. {
  225. $routes = new RouteBuilder($this->collection, '/');
  226. $routes->connect('/blog/articles/view', 'Blog.Articles::view');
  227. $expected = [
  228. 'pass' => [],
  229. 'plugin' => 'Blog',
  230. 'controller' => 'Articles',
  231. 'action' => 'view',
  232. '_matchedRoute' => '/blog/articles/view',
  233. ];
  234. $this->assertEquals($expected, $this->collection->parse('/blog/articles/view'));
  235. $url = $expected['_matchedRoute'];
  236. unset($expected['_matchedRoute']);
  237. $this->assertEquals($url, '/' . $this->collection->match($expected, []));
  238. }
  239. /**
  240. * Test connect() with short string syntax
  241. *
  242. * @return void
  243. */
  244. public function testConnectShortStringPluginPrefix()
  245. {
  246. $routes = new RouteBuilder($this->collection, '/');
  247. $routes->connect('/admin/blog/articles/view', 'Vendor/Blog.Management/Admin/Articles::view');
  248. $expected = [
  249. 'pass' => [],
  250. 'plugin' => 'Vendor/Blog',
  251. 'prefix' => 'management/admin',
  252. 'controller' => 'Articles',
  253. 'action' => 'view',
  254. '_matchedRoute' => '/admin/blog/articles/view',
  255. ];
  256. $this->assertEquals($expected, $this->collection->parse('/admin/blog/articles/view'));
  257. $url = $expected['_matchedRoute'];
  258. unset($expected['_matchedRoute']);
  259. $this->assertEquals($url, '/' . $this->collection->match($expected, []));
  260. }
  261. /**
  262. * Test if a route name already exist
  263. *
  264. * @return void
  265. */
  266. public function testNameExists()
  267. {
  268. $routes = new RouteBuilder($this->collection, '/l', ['prefix' => 'api']);
  269. $this->assertFalse($routes->nameExists('myRouteName'));
  270. $routes->connect('myRouteUrl', ['action' => 'index'], ['_name' => 'myRouteName']);
  271. $this->assertTrue($routes->nameExists('myRouteName'));
  272. }
  273. /**
  274. * Test setExtensions() and getExtensions().
  275. *
  276. * @return void
  277. */
  278. public function testExtensions()
  279. {
  280. $routes = new RouteBuilder($this->collection, '/l');
  281. $this->assertSame($routes, $routes->setExtensions(['html']));
  282. $this->assertSame(['html'], $routes->getExtensions());
  283. }
  284. /**
  285. * Test extensions being connected to routes.
  286. *
  287. * @return void
  288. */
  289. public function testConnectExtensions()
  290. {
  291. $routes = new RouteBuilder(
  292. $this->collection,
  293. '/l',
  294. [],
  295. ['extensions' => ['json']]
  296. );
  297. $this->assertEquals(['json'], $routes->getExtensions());
  298. $routes->connect('/:controller');
  299. $route = $this->collection->routes()[0];
  300. $this->assertEquals(['json'], $route->options['_ext']);
  301. $routes->setExtensions(['xml', 'json']);
  302. $routes->connect('/:controller/:action');
  303. $new = $this->collection->routes()[1];
  304. $this->assertEquals(['json'], $route->options['_ext']);
  305. $this->assertEquals(['xml', 'json'], $new->options['_ext']);
  306. }
  307. /**
  308. * Test adding additional extensions will be merged with current.
  309. *
  310. * @return void
  311. */
  312. public function testConnectExtensionsAdd()
  313. {
  314. $routes = new RouteBuilder(
  315. $this->collection,
  316. '/l',
  317. [],
  318. ['extensions' => ['json']]
  319. );
  320. $this->assertEquals(['json'], $routes->getExtensions());
  321. $routes->addExtensions(['xml']);
  322. $this->assertEquals(['json', 'xml'], $routes->getExtensions());
  323. $routes->addExtensions('csv');
  324. $this->assertEquals(['json', 'xml', 'csv'], $routes->getExtensions());
  325. }
  326. /**
  327. * test that setExtensions() accepts a string.
  328. *
  329. * @return void
  330. */
  331. public function testExtensionsString()
  332. {
  333. $routes = new RouteBuilder($this->collection, '/l');
  334. $routes->setExtensions('json');
  335. $this->assertEquals(['json'], $routes->getExtensions());
  336. }
  337. /**
  338. * Test error on invalid route class
  339. *
  340. * @return void
  341. */
  342. public function testConnectErrorInvalidRouteClass()
  343. {
  344. $this->expectException(\InvalidArgumentException::class);
  345. $this->expectExceptionMessage('Route class not found, or route class is not a subclass of');
  346. $routes = new RouteBuilder(
  347. $this->collection,
  348. '/l',
  349. [],
  350. ['extensions' => ['json']]
  351. );
  352. $routes->connect('/:controller', [], ['routeClass' => '\StdClass']);
  353. }
  354. /**
  355. * Test conflicting parameters raises an exception.
  356. *
  357. * @return void
  358. */
  359. public function testConnectConflictingParameters()
  360. {
  361. $this->expectException(\BadMethodCallException::class);
  362. $this->expectExceptionMessage('You cannot define routes that conflict with the scope.');
  363. $routes = new RouteBuilder($this->collection, '/admin', ['plugin' => 'TestPlugin']);
  364. $routes->connect('/', ['plugin' => 'TestPlugin2', 'controller' => 'Dashboard', 'action' => 'view']);
  365. }
  366. /**
  367. * Test connecting redirect routes.
  368. *
  369. * @return void
  370. */
  371. public function testRedirect()
  372. {
  373. $routes = new RouteBuilder($this->collection, '/');
  374. $routes->redirect('/p/:id', ['controller' => 'posts', 'action' => 'view'], ['status' => 301]);
  375. $route = $this->collection->routes()[0];
  376. $this->assertInstanceOf(RedirectRoute::class, $route);
  377. $routes->redirect('/old', '/forums', ['status' => 301]);
  378. $route = $this->collection->routes()[1];
  379. $this->assertInstanceOf(RedirectRoute::class, $route);
  380. $this->assertEquals('/forums', $route->redirect[0]);
  381. $route = $routes->redirect('/old', '/forums');
  382. $this->assertInstanceOf(RedirectRoute::class, $route);
  383. $this->assertSame($route, $this->collection->routes()[2]);
  384. }
  385. /**
  386. * Test using a custom route class for redirect routes.
  387. *
  388. * @return void
  389. */
  390. public function testRedirectWithCustomRouteClass()
  391. {
  392. $routes = new RouteBuilder($this->collection, '/');
  393. $routes->redirect('/old', '/forums', ['status' => 301, 'routeClass' => 'InflectedRoute']);
  394. $route = $this->collection->routes()[0];
  395. $this->assertInstanceOf(InflectedRoute::class, $route);
  396. }
  397. /**
  398. * Test creating sub-scopes with prefix()
  399. *
  400. * @return void
  401. */
  402. public function testPrefix()
  403. {
  404. $routes = new RouteBuilder($this->collection, '/path', ['key' => 'value']);
  405. $res = $routes->prefix('admin', ['param' => 'value'], function ($r) {
  406. $this->assertInstanceOf(RouteBuilder::class, $r);
  407. $this->assertCount(0, $this->collection->routes());
  408. $this->assertEquals('/path/admin', $r->path());
  409. $this->assertEquals(['prefix' => 'admin', 'key' => 'value', 'param' => 'value'], $r->params());
  410. });
  411. $this->assertNull($res);
  412. }
  413. /**
  414. * Test creating sub-scopes with prefix()
  415. *
  416. * @return void
  417. */
  418. public function testPrefixWithNoParams()
  419. {
  420. $routes = new RouteBuilder($this->collection, '/path', ['key' => 'value']);
  421. $res = $routes->prefix('admin', function ($r) {
  422. $this->assertInstanceOf(RouteBuilder::class, $r);
  423. $this->assertCount(0, $this->collection->routes());
  424. $this->assertEquals('/path/admin', $r->path());
  425. $this->assertEquals(['prefix' => 'admin', 'key' => 'value'], $r->params());
  426. });
  427. $this->assertNull($res);
  428. }
  429. /**
  430. * Test creating sub-scopes with prefix()
  431. *
  432. * @return void
  433. */
  434. public function testNestedPrefix()
  435. {
  436. $routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'admin']);
  437. $res = $routes->prefix('api', ['_namePrefix' => 'api:'], function ($r) {
  438. $this->assertEquals('/admin/api', $r->path());
  439. $this->assertEquals(['prefix' => 'admin/api'], $r->params());
  440. $this->assertEquals('api:', $r->namePrefix());
  441. });
  442. $this->assertNull($res);
  443. }
  444. /**
  445. * Test creating sub-scopes with prefix()
  446. *
  447. * @return void
  448. */
  449. public function testPathWithDotInPrefix()
  450. {
  451. $routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'admin']);
  452. $res = $routes->prefix('api', function ($r) {
  453. $r->prefix('v10', ['path' => '/v1.0'], function ($r2) {
  454. $this->assertEquals('/admin/api/v1.0', $r2->path());
  455. $this->assertEquals(['prefix' => 'admin/api/v10'], $r2->params());
  456. $r2->prefix('b1', ['path' => '/beta.1'], function ($r3) {
  457. $this->assertEquals('/admin/api/v1.0/beta.1', $r3->path());
  458. $this->assertEquals(['prefix' => 'admin/api/v10/b1'], $r3->params());
  459. });
  460. });
  461. });
  462. $this->assertNull($res);
  463. }
  464. /**
  465. * Test creating sub-scopes with plugin()
  466. *
  467. * @return void
  468. */
  469. public function testPlugin()
  470. {
  471. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  472. $res = $routes->plugin('Contacts', function ($r) {
  473. $this->assertEquals('/b/contacts', $r->path());
  474. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  475. $r->connect('/:controller');
  476. $route = $this->collection->routes()[0];
  477. $this->assertEquals(
  478. ['key' => 'value', 'plugin' => 'Contacts', 'action' => 'index'],
  479. $route->defaults
  480. );
  481. });
  482. $this->assertNull($res);
  483. }
  484. /**
  485. * Test creating sub-scopes with plugin() + path option
  486. *
  487. * @return void
  488. */
  489. public function testPluginPathOption()
  490. {
  491. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  492. $routes->plugin('Contacts', ['path' => '/people'], function ($r) {
  493. $this->assertEquals('/b/people', $r->path());
  494. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  495. });
  496. }
  497. /**
  498. * Test creating sub-scopes with plugin() + namePrefix option
  499. *
  500. * @return void
  501. */
  502. public function testPluginNamePrefix()
  503. {
  504. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  505. $routes->plugin('Contacts', ['_namePrefix' => 'contacts.'], function (RouteBuilder $r) {
  506. $this->assertEquals('contacts.', $r->namePrefix());
  507. });
  508. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  509. $routes->namePrefix('default.');
  510. $routes->plugin('Blog', ['_namePrefix' => 'blog.'], function (RouteBuilder $r) {
  511. $this->assertEquals('default.blog.', $r->namePrefix(), 'Should combine nameprefix');
  512. });
  513. }
  514. /**
  515. * Test connecting resources.
  516. *
  517. * @return void
  518. */
  519. public function testResources()
  520. {
  521. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  522. $routes->resources('Articles', ['_ext' => 'json']);
  523. $all = $this->collection->routes();
  524. $this->assertCount(5, $all);
  525. $this->assertEquals('/api/articles', $all[0]->template);
  526. $this->assertEquals(
  527. ['controller', 'action', '_method', 'prefix', 'plugin'],
  528. array_keys($all[0]->defaults)
  529. );
  530. $this->assertEquals('json', $all[0]->options['_ext']);
  531. $this->assertEquals('Articles', $all[0]->defaults['controller']);
  532. }
  533. /**
  534. * Test connecting resources with a path
  535. *
  536. * @return void
  537. */
  538. public function testResourcesPathOption()
  539. {
  540. $routes = new RouteBuilder($this->collection, '/api');
  541. $routes->resources('Articles', ['path' => 'posts'], function (RouteBuilder $routes) {
  542. $routes->resources('Comments');
  543. });
  544. $all = $this->collection->routes();
  545. $this->assertEquals('Articles', $all[0]->defaults['controller']);
  546. $this->assertEquals('/api/posts', $all[0]->template);
  547. $this->assertEquals('/api/posts/:id', $all[2]->template);
  548. $this->assertEquals(
  549. '/api/posts/:article_id/comments',
  550. $all[6]->template,
  551. 'parameter name should reflect resource name'
  552. );
  553. }
  554. /**
  555. * Test connecting resources with a prefix
  556. *
  557. * @return void
  558. */
  559. public function testResourcesPrefix()
  560. {
  561. $routes = new RouteBuilder($this->collection, '/api');
  562. $routes->resources('Articles', ['prefix' => 'rest']);
  563. $all = $this->collection->routes();
  564. $this->assertEquals('rest', $all[0]->defaults['prefix']);
  565. }
  566. /**
  567. * Test that resource prefixes work within a prefixed scope.
  568. *
  569. * @return void
  570. */
  571. public function testResourcesNestedPrefix()
  572. {
  573. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  574. $routes->resources('Articles', ['prefix' => 'rest']);
  575. $all = $this->collection->routes();
  576. $this->assertCount(5, $all);
  577. $this->assertEquals('/api/articles', $all[0]->template);
  578. foreach ($all as $route) {
  579. $this->assertEquals('api/rest', $route->defaults['prefix']);
  580. $this->assertEquals('Articles', $route->defaults['controller']);
  581. }
  582. }
  583. /**
  584. * Test connecting resources with the inflection option
  585. *
  586. * @return void
  587. */
  588. public function testResourcesInflection()
  589. {
  590. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  591. $routes->resources('BlogPosts', ['_ext' => 'json', 'inflect' => 'dasherize']);
  592. $all = $this->collection->routes();
  593. $this->assertCount(5, $all);
  594. $this->assertEquals('/api/blog-posts', $all[0]->template);
  595. $this->assertEquals(
  596. ['controller', 'action', '_method', 'prefix', 'plugin'],
  597. array_keys($all[0]->defaults)
  598. );
  599. $this->assertEquals('BlogPosts', $all[0]->defaults['controller']);
  600. }
  601. /**
  602. * Test connecting nested resources with the inflection option
  603. *
  604. * @return void
  605. */
  606. public function testResourcesNestedInflection()
  607. {
  608. $routes = new RouteBuilder($this->collection, '/api');
  609. $routes->resources(
  610. 'NetworkObjects',
  611. ['inflect' => 'dasherize'],
  612. function (RouteBuilder $routes) {
  613. $routes->resources('Attributes');
  614. }
  615. );
  616. $all = $this->collection->routes();
  617. $this->assertCount(10, $all);
  618. $this->assertEquals('/api/network-objects', $all[0]->template);
  619. $this->assertEquals('/api/network-objects/:id', $all[2]->template);
  620. $this->assertEquals('/api/network-objects/:network_object_id/attributes', $all[5]->template);
  621. }
  622. /**
  623. * Test connecting resources with additional mappings
  624. *
  625. * @return void
  626. */
  627. public function testResourcesMappings()
  628. {
  629. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  630. $routes->resources('Articles', [
  631. '_ext' => 'json',
  632. 'map' => [
  633. 'delete_all' => ['action' => 'deleteAll', 'method' => 'DELETE'],
  634. 'update_many' => ['action' => 'updateAll', 'method' => 'DELETE', 'path' => '/updateAll'],
  635. ],
  636. ]);
  637. $all = $this->collection->routes();
  638. $this->assertCount(7, $all);
  639. $this->assertEquals('/api/articles/delete_all', $all[5]->template, 'Path defaults to key name.');
  640. $this->assertEquals(
  641. ['controller', 'action', '_method', 'prefix', 'plugin'],
  642. array_keys($all[5]->defaults)
  643. );
  644. $this->assertEquals('Articles', $all[5]->defaults['controller']);
  645. $this->assertEquals('deleteAll', $all[5]->defaults['action']);
  646. $this->assertEquals('/api/articles/updateAll', $all[6]->template, 'Explicit path option');
  647. $this->assertEquals(
  648. ['controller', 'action', '_method', 'prefix', 'plugin'],
  649. array_keys($all[6]->defaults)
  650. );
  651. $this->assertEquals('Articles', $all[6]->defaults['controller']);
  652. $this->assertEquals('updateAll', $all[6]->defaults['action']);
  653. }
  654. /**
  655. * Test connecting resources.
  656. *
  657. * @return void
  658. */
  659. public function testResourcesInScope()
  660. {
  661. Router::scope('/api', ['prefix' => 'api'], function (RouteBuilder $routes) {
  662. $routes->setExtensions(['json']);
  663. $routes->resources('Articles');
  664. });
  665. $url = Router::url([
  666. 'prefix' => 'api',
  667. 'controller' => 'Articles',
  668. 'action' => 'edit',
  669. '_method' => 'PUT',
  670. 'id' => 99,
  671. ]);
  672. $this->assertEquals('/api/articles/99', $url);
  673. $url = Router::url([
  674. 'prefix' => 'api',
  675. 'controller' => 'Articles',
  676. 'action' => 'edit',
  677. '_method' => 'PUT',
  678. '_ext' => 'json',
  679. 'id' => 99,
  680. ]);
  681. $this->assertEquals('/api/articles/99.json', $url);
  682. }
  683. /**
  684. * Test resource parsing.
  685. *
  686. * @group deprecated
  687. * @return void
  688. */
  689. public function testResourcesParsingReadGlobals()
  690. {
  691. $this->deprecated(function () {
  692. $routes = new RouteBuilder($this->collection, '/');
  693. $routes->resources('Articles');
  694. $_SERVER['REQUEST_METHOD'] = 'GET';
  695. $result = $this->collection->parse('/articles');
  696. $this->assertEquals('Articles', $result['controller']);
  697. $this->assertEquals('index', $result['action']);
  698. $this->assertEquals([], $result['pass']);
  699. $_SERVER['REQUEST_METHOD'] = 'POST';
  700. $result = $this->collection->parse('/articles');
  701. $this->assertEquals('Articles', $result['controller']);
  702. $this->assertEquals('add', $result['action']);
  703. $this->assertEquals([], $result['pass']);
  704. });
  705. }
  706. /**
  707. * Test resource parsing.
  708. *
  709. * @return void
  710. */
  711. public function testResourcesParsing()
  712. {
  713. $routes = new RouteBuilder($this->collection, '/');
  714. $routes->resources('Articles');
  715. $result = $this->collection->parse('/articles', 'GET');
  716. $this->assertEquals('Articles', $result['controller']);
  717. $this->assertEquals('index', $result['action']);
  718. $this->assertEquals([], $result['pass']);
  719. $result = $this->collection->parse('/articles/1', 'GET');
  720. $this->assertEquals('Articles', $result['controller']);
  721. $this->assertEquals('view', $result['action']);
  722. $this->assertEquals([1], $result['pass']);
  723. $result = $this->collection->parse('/articles', 'POST');
  724. $this->assertEquals('Articles', $result['controller']);
  725. $this->assertEquals('add', $result['action']);
  726. $this->assertEquals([], $result['pass']);
  727. $result = $this->collection->parse('/articles/1', 'PUT');
  728. $this->assertEquals('Articles', $result['controller']);
  729. $this->assertEquals('edit', $result['action']);
  730. $this->assertEquals([1], $result['pass']);
  731. $result = $this->collection->parse('/articles/1', 'DELETE');
  732. $this->assertEquals('Articles', $result['controller']);
  733. $this->assertEquals('delete', $result['action']);
  734. $this->assertEquals([1], $result['pass']);
  735. }
  736. /**
  737. * Test the only option of RouteBuilder.
  738. *
  739. * @return void
  740. */
  741. public function testResourcesOnlyString()
  742. {
  743. $routes = new RouteBuilder($this->collection, '/');
  744. $routes->resources('Articles', ['only' => 'index']);
  745. $result = $this->collection->routes();
  746. $this->assertCount(1, $result);
  747. $this->assertEquals('/articles', $result[0]->template);
  748. }
  749. /**
  750. * Test the only option of RouteBuilder.
  751. *
  752. * @return void
  753. */
  754. public function testResourcesOnlyArray()
  755. {
  756. $routes = new RouteBuilder($this->collection, '/');
  757. $routes->resources('Articles', ['only' => ['index', 'delete']]);
  758. $result = $this->collection->routes();
  759. $this->assertCount(2, $result);
  760. $this->assertEquals('/articles', $result[0]->template);
  761. $this->assertEquals('index', $result[0]->defaults['action']);
  762. $this->assertEquals('GET', $result[0]->defaults['_method']);
  763. $this->assertEquals('/articles/:id', $result[1]->template);
  764. $this->assertEquals('delete', $result[1]->defaults['action']);
  765. $this->assertEquals('DELETE', $result[1]->defaults['_method']);
  766. }
  767. /**
  768. * Test the actions option of RouteBuilder.
  769. *
  770. * @return void
  771. */
  772. public function testResourcesActions()
  773. {
  774. $routes = new RouteBuilder($this->collection, '/');
  775. $routes->resources('Articles', [
  776. 'only' => ['index', 'delete'],
  777. 'actions' => ['index' => 'showList'],
  778. ]);
  779. $result = $this->collection->routes();
  780. $this->assertCount(2, $result);
  781. $this->assertEquals('/articles', $result[0]->template);
  782. $this->assertEquals('showList', $result[0]->defaults['action']);
  783. $this->assertEquals('/articles/:id', $result[1]->template);
  784. $this->assertEquals('delete', $result[1]->defaults['action']);
  785. }
  786. /**
  787. * Test nesting resources
  788. *
  789. * @return void
  790. */
  791. public function testResourcesNested()
  792. {
  793. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  794. $routes->resources('Articles', function (RouteBuilder $routes) {
  795. $this->assertEquals('/api/articles/', $routes->path());
  796. $this->assertEquals(['prefix' => 'api'], $routes->params());
  797. $routes->resources('Comments');
  798. $route = $this->collection->routes()[6];
  799. $this->assertEquals('/api/articles/:article_id/comments', $route->template);
  800. });
  801. }
  802. /**
  803. * Test connecting fallback routes.
  804. *
  805. * @return void
  806. */
  807. public function testFallbacks()
  808. {
  809. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  810. $routes->fallbacks();
  811. $all = $this->collection->routes();
  812. $this->assertSame('/api/{controller}', $all[0]->template);
  813. $this->assertSame('/api/{controller}/{action}/*', $all[1]->template);
  814. $this->assertInstanceOf(Route::class, $all[0]);
  815. }
  816. /**
  817. * Test connecting fallback routes with specific route class
  818. *
  819. * @return void
  820. */
  821. public function testFallbacksWithClass()
  822. {
  823. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  824. $routes->fallbacks('InflectedRoute');
  825. $all = $this->collection->routes();
  826. $this->assertSame('/api/{controller}', $all[0]->template);
  827. $this->assertSame('/api/{controller}/{action}/*', $all[1]->template);
  828. $this->assertInstanceOf(InflectedRoute::class, $all[0]);
  829. }
  830. /**
  831. * Test connecting fallback routes after setting default route class.
  832. *
  833. * @return void
  834. */
  835. public function testDefaultRouteClassFallbacks()
  836. {
  837. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  838. $routes->setRouteClass('TestApp\Routing\Route\DashedRoute');
  839. $routes->fallbacks();
  840. $all = $this->collection->routes();
  841. $this->assertInstanceOf('TestApp\Routing\Route\DashedRoute', $all[0]);
  842. }
  843. /**
  844. * Test adding a scope.
  845. *
  846. * @return void
  847. */
  848. public function testScope()
  849. {
  850. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  851. $routes->scope('/v1', ['version' => 1], function (RouteBuilder $routes) {
  852. $this->assertEquals('/api/v1', $routes->path());
  853. $this->assertEquals(['prefix' => 'api', 'version' => 1], $routes->params());
  854. });
  855. }
  856. /**
  857. * Test adding a scope with action in the scope
  858. *
  859. * @return void
  860. */
  861. public function testScopeWithAction()
  862. {
  863. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  864. $routes->scope('/prices', ['controller' => 'Prices', 'action' => 'view'], function (RouteBuilder $routes) {
  865. $routes->connect('/shared', ['shared' => true]);
  866. $routes->get('/exclusive', ['exclusive' => true]);
  867. });
  868. $all = $this->collection->routes();
  869. $this->assertCount(2, $all);
  870. $this->assertSame('view', $all[0]->defaults['action']);
  871. $this->assertArrayHasKey('shared', $all[0]->defaults);
  872. $this->assertSame('view', $all[1]->defaults['action']);
  873. $this->assertArrayHasKey('exclusive', $all[1]->defaults);
  874. }
  875. /**
  876. * Test that nested scopes inherit middleware.
  877. *
  878. * @return void
  879. */
  880. public function testScopeInheritMiddleware()
  881. {
  882. $routes = new RouteBuilder(
  883. $this->collection,
  884. '/api',
  885. ['prefix' => 'api'],
  886. ['middleware' => ['auth']]
  887. );
  888. $routes->scope('/v1', function (RouteBuilder $routes) {
  889. $this->assertAttributeEquals(['auth'], 'middleware', $routes, 'Should inherit middleware');
  890. $this->assertEquals('/api/v1', $routes->path());
  891. $this->assertEquals(['prefix' => 'api'], $routes->params());
  892. });
  893. }
  894. /**
  895. * Test using name prefixes.
  896. *
  897. * @return void
  898. */
  899. public function testNamePrefixes()
  900. {
  901. $routes = new RouteBuilder($this->collection, '/api', [], ['namePrefix' => 'api:']);
  902. $routes->scope('/v1', ['version' => 1, '_namePrefix' => 'v1:'], function (RouteBuilder $routes) {
  903. $this->assertEquals('api:v1:', $routes->namePrefix());
  904. $routes->connect('/ping', ['controller' => 'Pings'], ['_name' => 'ping']);
  905. $routes->namePrefix('web:');
  906. $routes->connect('/pong', ['controller' => 'Pongs'], ['_name' => 'pong']);
  907. });
  908. $all = $this->collection->named();
  909. $this->assertArrayHasKey('api:v1:ping', $all);
  910. $this->assertArrayHasKey('web:pong', $all);
  911. }
  912. /**
  913. * Test adding middleware to the collection.
  914. *
  915. * @return void
  916. */
  917. public function testRegisterMiddleware()
  918. {
  919. $func = function () {
  920. };
  921. $routes = new RouteBuilder($this->collection, '/api');
  922. $result = $routes->registerMiddleware('test', $func);
  923. $this->assertSame($result, $routes);
  924. $this->assertTrue($this->collection->hasMiddleware('test'));
  925. $this->assertTrue($this->collection->middlewareExists('test'));
  926. }
  927. /**
  928. * Test middleware group
  929. *
  930. * @return void
  931. */
  932. public function testMiddlewareGroup()
  933. {
  934. $func = function () {
  935. };
  936. $routes = new RouteBuilder($this->collection, '/api');
  937. $routes->registerMiddleware('test', $func);
  938. $routes->registerMiddleware('test_two', $func);
  939. $result = $routes->middlewareGroup('group', ['test', 'test_two']);
  940. $this->assertSame($result, $routes);
  941. $this->assertTrue($this->collection->hasMiddlewareGroup('group'));
  942. $this->assertTrue($this->collection->middlewareExists('group'));
  943. }
  944. /**
  945. * Test overlap between middleware name and group name
  946. *
  947. * @return void
  948. */
  949. public function testMiddlewareGroupOverlap()
  950. {
  951. $this->expectException(\RuntimeException::class);
  952. $this->expectExceptionMessage('Cannot add middleware group \'test\'. A middleware by this name has already been registered.');
  953. $func = function () {
  954. };
  955. $routes = new RouteBuilder($this->collection, '/api');
  956. $routes->registerMiddleware('test', $func);
  957. $result = $routes->middlewareGroup('test', ['test']);
  958. }
  959. /**
  960. * Test applying middleware to a scope when it doesn't exist
  961. *
  962. * @return void
  963. */
  964. public function testApplyMiddlewareInvalidName()
  965. {
  966. $this->expectException(\RuntimeException::class);
  967. $this->expectExceptionMessage('Cannot apply \'bad\' middleware or middleware group. Use registerMiddleware() to register middleware');
  968. $routes = new RouteBuilder($this->collection, '/api');
  969. $routes->applyMiddleware('bad');
  970. }
  971. /**
  972. * Test applying middleware to a scope
  973. *
  974. * @return void
  975. */
  976. public function testApplyMiddleware()
  977. {
  978. $func = function () {
  979. };
  980. $routes = new RouteBuilder($this->collection, '/api');
  981. $routes->registerMiddleware('test', $func)
  982. ->registerMiddleware('test2', $func);
  983. $result = $routes->applyMiddleware('test', 'test2');
  984. $this->assertSame($result, $routes);
  985. }
  986. /**
  987. * Test that applyMiddleware() merges with previous data.
  988. *
  989. * @return void
  990. */
  991. public function testApplyMiddlewareMerges()
  992. {
  993. $func = function () {
  994. };
  995. $routes = new RouteBuilder($this->collection, '/api');
  996. $routes->registerMiddleware('test', $func)
  997. ->registerMiddleware('test2', $func);
  998. $routes->applyMiddleware('test');
  999. $routes->applyMiddleware('test2');
  1000. $this->assertAttributeEquals(['test', 'test2'], 'middleware', $routes);
  1001. }
  1002. /**
  1003. * Test that applyMiddleware() uses unique middleware set
  1004. *
  1005. * @return void
  1006. */
  1007. public function testApplyMiddlewareUnique()
  1008. {
  1009. $func = function () {
  1010. };
  1011. $routes = new RouteBuilder($this->collection, '/api');
  1012. $routes->registerMiddleware('test', $func)
  1013. ->registerMiddleware('test2', $func);
  1014. $routes->applyMiddleware('test', 'test2');
  1015. $routes->applyMiddleware('test2', 'test');
  1016. $this->assertAttributeEquals(['test', 'test2'], 'middleware', $routes);
  1017. }
  1018. /**
  1019. * Test applying middleware results in middleware attached to the route.
  1020. *
  1021. * @return void
  1022. */
  1023. public function testApplyMiddlewareAttachToRoutes()
  1024. {
  1025. $func = function () {
  1026. };
  1027. $routes = new RouteBuilder($this->collection, '/api');
  1028. $routes->registerMiddleware('test', $func)
  1029. ->registerMiddleware('test2', $func);
  1030. $routes->applyMiddleware('test', 'test2');
  1031. $route = $routes->get('/docs', ['controller' => 'Docs']);
  1032. $this->assertSame(['test', 'test2'], $route->getMiddleware());
  1033. }
  1034. /**
  1035. * @return array
  1036. */
  1037. public static function httpMethodProvider()
  1038. {
  1039. return [
  1040. ['GET'],
  1041. ['POST'],
  1042. ['PUT'],
  1043. ['PATCH'],
  1044. ['DELETE'],
  1045. ['OPTIONS'],
  1046. ['HEAD'],
  1047. ];
  1048. }
  1049. /**
  1050. * Test that the HTTP method helpers create the right kind of routes.
  1051. *
  1052. * @dataProvider httpMethodProvider
  1053. * @return void
  1054. */
  1055. public function testHttpMethods($method)
  1056. {
  1057. $routes = new RouteBuilder($this->collection, '/', [], ['namePrefix' => 'app:']);
  1058. $route = $routes->{strtolower($method)}(
  1059. '/bookmarks/:id',
  1060. ['controller' => 'Bookmarks', 'action' => 'view'],
  1061. 'route-name'
  1062. );
  1063. $this->assertInstanceOf(Route::class, $route, 'Should return a route');
  1064. $this->assertSame($method, $route->defaults['_method']);
  1065. $this->assertSame('app:route-name', $route->options['_name']);
  1066. $this->assertSame('/bookmarks/:id', $route->template);
  1067. $this->assertEquals(
  1068. ['plugin' => null, 'controller' => 'Bookmarks', 'action' => 'view', '_method' => $method],
  1069. $route->defaults
  1070. );
  1071. }
  1072. /**
  1073. * Test that the HTTP method helpers create the right kind of routes.
  1074. *
  1075. * @dataProvider httpMethodProvider
  1076. * @return void
  1077. */
  1078. public function testHttpMethodsStringTarget($method)
  1079. {
  1080. $routes = new RouteBuilder($this->collection, '/', [], ['namePrefix' => 'app:']);
  1081. $route = $routes->{strtolower($method)}(
  1082. '/bookmarks/:id',
  1083. 'Bookmarks::view',
  1084. 'route-name'
  1085. );
  1086. $this->assertInstanceOf(Route::class, $route, 'Should return a route');
  1087. $this->assertSame($method, $route->defaults['_method']);
  1088. $this->assertSame('app:route-name', $route->options['_name']);
  1089. $this->assertSame('/bookmarks/:id', $route->template);
  1090. $this->assertEquals(
  1091. ['plugin' => null, 'controller' => 'Bookmarks', 'action' => 'view', '_method' => $method],
  1092. $route->defaults
  1093. );
  1094. }
  1095. /**
  1096. * Integration test for http method helpers and route fluent method
  1097. *
  1098. * @return void
  1099. */
  1100. public function testHttpMethodIntegration()
  1101. {
  1102. $routes = new RouteBuilder($this->collection, '/');
  1103. $routes->scope('/', function (RouteBuilder $routes) {
  1104. $routes->get('/faq/:page', ['controller' => 'Pages', 'action' => 'faq'], 'faq')
  1105. ->setPatterns(['page' => '[a-z0-9_]+'])
  1106. ->setHost('docs.example.com');
  1107. $routes->post('/articles/:id', ['controller' => 'Articles', 'action' => 'update'], 'article:update')
  1108. ->setPatterns(['id' => '[0-9]+'])
  1109. ->setPass(['id']);
  1110. });
  1111. $this->assertCount(2, $this->collection->routes());
  1112. $this->assertEquals(['faq', 'article:update'], array_keys($this->collection->named()));
  1113. $this->assertNotEmpty($this->collection->parse('/faq/things_you_know', 'GET'));
  1114. $result = $this->collection->parse('/articles/123', 'POST');
  1115. $this->assertEquals(['123'], $result['pass']);
  1116. }
  1117. /**
  1118. * Test loading routes from a missing plugin
  1119. *
  1120. * @return void
  1121. */
  1122. public function testLoadPluginBadPlugin()
  1123. {
  1124. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  1125. $routes = new RouteBuilder($this->collection, '/');
  1126. $routes->loadPlugin('Nope');
  1127. }
  1128. /**
  1129. * Test loading routes from a missing file
  1130. *
  1131. * @return void
  1132. */
  1133. public function testLoadPluginBadFile()
  1134. {
  1135. $this->deprecated(function () {
  1136. $this->expectException(\InvalidArgumentException::class);
  1137. $this->expectExceptionMessage('Cannot load routes for the plugin named TestPlugin.');
  1138. $this->loadPlugins(['TestPlugin']);
  1139. $routes = new RouteBuilder($this->collection, '/');
  1140. $routes->loadPlugin('TestPlugin', 'nope.php');
  1141. });
  1142. }
  1143. /**
  1144. * Test loading routes with success
  1145. *
  1146. * @return void
  1147. */
  1148. public function testLoadPlugin()
  1149. {
  1150. $this->loadPlugins(['TestPlugin']);
  1151. $routes = new RouteBuilder($this->collection, '/');
  1152. $routes->loadPlugin('TestPlugin');
  1153. $this->assertCount(1, $this->collection->routes());
  1154. $this->assertNotEmpty($this->collection->parse('/test_plugin', 'GET'));
  1155. $plugin = Plugin::getCollection()->get('TestPlugin');
  1156. $this->assertFalse($plugin->isEnabled('routes'), 'Hook should be disabled preventing duplicate routes');
  1157. }
  1158. /**
  1159. * Test routeClass() still works.
  1160. *
  1161. * @group deprecated
  1162. * @return void
  1163. */
  1164. public function testRouteClassBackwardCompat()
  1165. {
  1166. $this->deprecated(function () {
  1167. $routes = new RouteBuilder($this->collection, '/l');
  1168. $this->assertNull($routes->routeClass('TestApp\Routing\Route\DashedRoute'));
  1169. $this->assertSame('TestApp\Routing\Route\DashedRoute', $routes->routeClass());
  1170. $this->assertSame('TestApp\Routing\Route\DashedRoute', $routes->getRouteClass());
  1171. });
  1172. }
  1173. /**
  1174. * Test extensions() still works.
  1175. *
  1176. * @group deprecated
  1177. * @return void
  1178. */
  1179. public function testExtensionsBackwardCompat()
  1180. {
  1181. $this->deprecated(function () {
  1182. $routes = new RouteBuilder($this->collection, '/l');
  1183. $this->assertNull($routes->extensions(['html']));
  1184. $this->assertSame(['html'], $routes->extensions());
  1185. $this->assertSame(['html'], $routes->getExtensions());
  1186. $this->assertNull($routes->extensions('json'));
  1187. $this->assertSame(['json'], $routes->extensions());
  1188. $this->assertSame(['json'], $routes->getExtensions());
  1189. });
  1190. }
  1191. }