RouteBuilderTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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\Routing\RouteBuilder;
  17. use Cake\Routing\RouteCollection;
  18. use Cake\Routing\Router;
  19. use Cake\Routing\Route\InflectedRoute;
  20. use Cake\Routing\Route\RedirectRoute;
  21. use Cake\Routing\Route\Route;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * RouteBuilder test case
  25. */
  26. class RouteBuilderTest extends TestCase
  27. {
  28. /**
  29. * Setup method
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. $this->collection = new RouteCollection();
  37. }
  38. /**
  39. * Test path()
  40. *
  41. * @return void
  42. */
  43. public function testPath()
  44. {
  45. $routes = new RouteBuilder($this->collection, '/some/path');
  46. $this->assertEquals('/some/path', $routes->path());
  47. $routes = new RouteBuilder($this->collection, '/:book_id');
  48. $this->assertEquals('/', $routes->path());
  49. $routes = new RouteBuilder($this->collection, '/path/:book_id');
  50. $this->assertEquals('/path/', $routes->path());
  51. $routes = new RouteBuilder($this->collection, '/path/book:book_id');
  52. $this->assertEquals('/path/book', $routes->path());
  53. }
  54. /**
  55. * Test params()
  56. *
  57. * @return void
  58. */
  59. public function testParams()
  60. {
  61. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  62. $this->assertEquals(['prefix' => 'api'], $routes->params());
  63. }
  64. /**
  65. * Test getting connected routes.
  66. *
  67. * @return void
  68. */
  69. public function testRoutes()
  70. {
  71. $routes = new RouteBuilder($this->collection, '/l');
  72. $routes->connect('/:controller', ['action' => 'index']);
  73. $routes->connect('/:controller/:action/*');
  74. $all = $this->collection->routes();
  75. $this->assertCount(2, $all);
  76. $this->assertInstanceOf(Route::class, $all[0]);
  77. $this->assertInstanceOf(Route::class, $all[1]);
  78. }
  79. /**
  80. * Test setting default route class
  81. *
  82. * @return void
  83. */
  84. public function testRouteClass()
  85. {
  86. $routes = new RouteBuilder(
  87. $this->collection,
  88. '/l',
  89. [],
  90. ['routeClass' => 'InflectedRoute']
  91. );
  92. $routes->connect('/:controller', ['action' => 'index']);
  93. $routes->connect('/:controller/:action/*');
  94. $all = $this->collection->routes();
  95. $this->assertInstanceOf(InflectedRoute::class, $all[0]);
  96. $this->assertInstanceOf(InflectedRoute::class, $all[1]);
  97. $this->collection = new RouteCollection();
  98. $routes = new RouteBuilder($this->collection, '/l');
  99. $routes->routeClass('TestApp\Routing\Route\DashedRoute');
  100. $routes->connect('/:controller', ['action' => 'index']);
  101. $all = $this->collection->routes();
  102. $this->assertInstanceOf('TestApp\Routing\Route\DashedRoute', $all[0]);
  103. }
  104. /**
  105. * Test connecting an instance routes.
  106. *
  107. * @return void
  108. */
  109. public function testConnectInstance()
  110. {
  111. $routes = new RouteBuilder($this->collection, '/l', ['prefix' => 'api']);
  112. $route = new Route('/:controller');
  113. $this->assertNull($routes->connect($route));
  114. $result = $this->collection->routes()[0];
  115. $this->assertSame($route, $result);
  116. }
  117. /**
  118. * Test connecting basic routes.
  119. *
  120. * @return void
  121. */
  122. public function testConnectBasic()
  123. {
  124. $routes = new RouteBuilder($this->collection, '/l', ['prefix' => 'api']);
  125. $this->assertNull($routes->connect('/:controller'));
  126. $route = $this->collection->routes()[0];
  127. $this->assertInstanceOf(Route::class, $route);
  128. $this->assertEquals('/l/:controller', $route->template);
  129. $expected = ['prefix' => 'api', 'action' => 'index', 'plugin' => null];
  130. $this->assertEquals($expected, $route->defaults);
  131. }
  132. /**
  133. * Test that compiling a route results in an trailing / optional pattern.
  134. *
  135. * @return void
  136. */
  137. public function testConnectTrimTrailingSlash()
  138. {
  139. $routes = new RouteBuilder($this->collection, '/articles', ['controller' => 'Articles']);
  140. $routes->connect('/', ['action' => 'index']);
  141. $expected = [
  142. 'plugin' => null,
  143. 'controller' => 'Articles',
  144. 'action' => 'index',
  145. 'pass' => [],
  146. '_matchedRoute' => '/articles',
  147. ];
  148. $this->assertEquals($expected, $this->collection->parse('/articles'));
  149. $this->assertEquals($expected, $this->collection->parse('/articles/'));
  150. }
  151. /**
  152. * Test if a route name already exist
  153. *
  154. * @return void
  155. */
  156. public function testNameExists()
  157. {
  158. $routes = new RouteBuilder($this->collection, '/l', ['prefix' => 'api']);
  159. $this->assertFalse($routes->nameExists('myRouteName'));
  160. $routes->connect('myRouteUrl', ['action' => 'index'], ['_name' => 'myRouteName']);
  161. $this->assertTrue($routes->nameExists('myRouteName'));
  162. }
  163. /**
  164. * Test extensions being connected to routes.
  165. *
  166. * @return void
  167. */
  168. public function testConnectExtensions()
  169. {
  170. $routes = new RouteBuilder(
  171. $this->collection,
  172. '/l',
  173. [],
  174. ['extensions' => ['json']]
  175. );
  176. $this->assertEquals(['json'], $routes->extensions());
  177. $routes->connect('/:controller');
  178. $route = $this->collection->routes()[0];
  179. $this->assertEquals(['json'], $route->options['_ext']);
  180. $routes->extensions(['xml', 'json']);
  181. $routes->connect('/:controller/:action');
  182. $new = $this->collection->routes()[1];
  183. $this->assertEquals(['json'], $route->options['_ext']);
  184. $this->assertEquals(['xml', 'json'], $new->options['_ext']);
  185. }
  186. /**
  187. * Test adding additional extensions will be merged with current.
  188. *
  189. * @return void
  190. */
  191. public function testConnectExtensionsAdd()
  192. {
  193. $routes = new RouteBuilder(
  194. $this->collection,
  195. '/l',
  196. [],
  197. ['extensions' => ['json']]
  198. );
  199. $this->assertEquals(['json'], $routes->extensions());
  200. $routes->addExtensions(['xml']);
  201. $this->assertEquals(['json', 'xml'], $routes->extensions());
  202. $routes->addExtensions('csv');
  203. $this->assertEquals(['json', 'xml', 'csv'], $routes->extensions());
  204. }
  205. /**
  206. * test that extensions() accepts a string.
  207. *
  208. * @return void
  209. */
  210. public function testExtensionsString()
  211. {
  212. $routes = new RouteBuilder($this->collection, '/l');
  213. $routes->extensions('json');
  214. $this->assertEquals(['json'], $routes->extensions());
  215. }
  216. /**
  217. * Test error on invalid route class
  218. *
  219. * @expectedException \InvalidArgumentException
  220. * @expectedExceptionMessage Route class not found, or route class is not a subclass of
  221. * @return void
  222. */
  223. public function testConnectErrorInvalidRouteClass()
  224. {
  225. $routes = new RouteBuilder(
  226. $this->collection,
  227. '/l',
  228. [],
  229. ['extensions' => ['json']]
  230. );
  231. $routes->connect('/:controller', [], ['routeClass' => '\StdClass']);
  232. }
  233. /**
  234. * Test conflicting parameters raises an exception.
  235. *
  236. * @expectedException \BadMethodCallException
  237. * @expectedExceptionMessage You cannot define routes that conflict with the scope.
  238. * @return void
  239. */
  240. public function testConnectConflictingParameters()
  241. {
  242. $routes = new RouteBuilder($this->collection, '/admin', ['plugin' => 'TestPlugin']);
  243. $routes->connect('/', ['plugin' => 'TestPlugin2', 'controller' => 'Dashboard', 'action' => 'view']);
  244. }
  245. /**
  246. * Test connecting redirect routes.
  247. *
  248. * @return void
  249. */
  250. public function testRedirect()
  251. {
  252. $routes = new RouteBuilder($this->collection, '/');
  253. $routes->redirect('/p/:id', ['controller' => 'posts', 'action' => 'view'], ['status' => 301]);
  254. $route = $this->collection->routes()[0];
  255. $this->assertInstanceOf(RedirectRoute::class, $route);
  256. $routes->redirect('/old', '/forums', ['status' => 301]);
  257. $route = $this->collection->routes()[1];
  258. $this->assertInstanceOf(RedirectRoute::class, $route);
  259. $this->assertEquals('/forums', $route->redirect[0]);
  260. }
  261. /**
  262. * Test creating sub-scopes with prefix()
  263. *
  264. * @return void
  265. */
  266. public function testPrefix()
  267. {
  268. $routes = new RouteBuilder($this->collection, '/path', ['key' => 'value']);
  269. $res = $routes->prefix('admin', ['param' => 'value'], function ($r) {
  270. $this->assertInstanceOf(RouteBuilder::class, $r);
  271. $this->assertCount(0, $this->collection->routes());
  272. $this->assertEquals('/path/admin', $r->path());
  273. $this->assertEquals(['prefix' => 'admin', 'key' => 'value', 'param' => 'value'], $r->params());
  274. });
  275. $this->assertNull($res);
  276. }
  277. /**
  278. * Test creating sub-scopes with prefix()
  279. *
  280. * @return void
  281. */
  282. public function testPrefixWithNoParams()
  283. {
  284. $routes = new RouteBuilder($this->collection, '/path', ['key' => 'value']);
  285. $res = $routes->prefix('admin', function ($r) {
  286. $this->assertInstanceOf(RouteBuilder::class, $r);
  287. $this->assertCount(0, $this->collection->routes());
  288. $this->assertEquals('/path/admin', $r->path());
  289. $this->assertEquals(['prefix' => 'admin', 'key' => 'value'], $r->params());
  290. });
  291. $this->assertNull($res);
  292. }
  293. /**
  294. * Test creating sub-scopes with prefix()
  295. *
  296. * @return void
  297. */
  298. public function testNestedPrefix()
  299. {
  300. $routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'admin']);
  301. $res = $routes->prefix('api', ['_namePrefix' => 'api:'], function ($r) {
  302. $this->assertEquals('/admin/api', $r->path());
  303. $this->assertEquals(['prefix' => 'admin/api'], $r->params());
  304. $this->assertEquals('api:', $r->namePrefix());
  305. });
  306. $this->assertNull($res);
  307. }
  308. /**
  309. * Test creating sub-scopes with prefix()
  310. *
  311. * @return void
  312. */
  313. public function testPathWithDotInPrefix()
  314. {
  315. $routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'admin']);
  316. $res = $routes->prefix('api', function ($r) {
  317. $r->prefix('v10', ['path' => '/v1.0'], function ($r2) {
  318. $this->assertEquals('/admin/api/v1.0', $r2->path());
  319. $this->assertEquals(['prefix' => 'admin/api/v10'], $r2->params());
  320. $r2->prefix('b1', ['path' => '/beta.1'], function ($r3) {
  321. $this->assertEquals('/admin/api/v1.0/beta.1', $r3->path());
  322. $this->assertEquals(['prefix' => 'admin/api/v10/b1'], $r3->params());
  323. });
  324. });
  325. });
  326. $this->assertNull($res);
  327. }
  328. /**
  329. * Test creating sub-scopes with plugin()
  330. *
  331. * @return void
  332. */
  333. public function testNestedPlugin()
  334. {
  335. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  336. $res = $routes->plugin('Contacts', function ($r) {
  337. $this->assertEquals('/b/contacts', $r->path());
  338. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  339. $r->connect('/:controller');
  340. $route = $this->collection->routes()[0];
  341. $this->assertEquals(
  342. ['key' => 'value', 'plugin' => 'Contacts', 'action' => 'index'],
  343. $route->defaults
  344. );
  345. });
  346. $this->assertNull($res);
  347. }
  348. /**
  349. * Test creating sub-scopes with plugin() + path option
  350. *
  351. * @return void
  352. */
  353. public function testNestedPluginPathOption()
  354. {
  355. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  356. $routes->plugin('Contacts', ['path' => '/people'], function ($r) {
  357. $this->assertEquals('/b/people', $r->path());
  358. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  359. });
  360. }
  361. /**
  362. * Test connecting resources.
  363. *
  364. * @return void
  365. */
  366. public function testResources()
  367. {
  368. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  369. $routes->resources('Articles', ['_ext' => 'json']);
  370. $all = $this->collection->routes();
  371. $this->assertCount(5, $all);
  372. $this->assertEquals('/api/articles', $all[0]->template);
  373. $this->assertEquals(
  374. ['controller', 'action', '_method', 'prefix', 'plugin'],
  375. array_keys($all[0]->defaults)
  376. );
  377. $this->assertEquals('json', $all[0]->options['_ext']);
  378. $this->assertEquals('Articles', $all[0]->defaults['controller']);
  379. }
  380. /**
  381. * Test connecting resources with a prefix
  382. *
  383. * @return void
  384. */
  385. public function testResourcesPrefix()
  386. {
  387. $routes = new RouteBuilder($this->collection, '/api');
  388. $routes->resources('Articles', ['prefix' => 'rest']);
  389. $all = $this->collection->routes();
  390. $this->assertEquals('rest', $all[0]->defaults['prefix']);
  391. }
  392. /**
  393. * Test that resource prefixes work within a prefixed scope.
  394. *
  395. * @return void
  396. */
  397. public function testResourcesNestedPrefix()
  398. {
  399. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  400. $routes->resources('Articles', ['prefix' => 'rest']);
  401. $all = $this->collection->routes();
  402. $this->assertCount(5, $all);
  403. $this->assertEquals('/api/articles', $all[0]->template);
  404. foreach ($all as $route) {
  405. $this->assertEquals('api/rest', $route->defaults['prefix']);
  406. $this->assertEquals('Articles', $route->defaults['controller']);
  407. }
  408. }
  409. /**
  410. * Test connecting resources with the inflection option
  411. *
  412. * @return void
  413. */
  414. public function testResourcesInflection()
  415. {
  416. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  417. $routes->resources('BlogPosts', ['_ext' => 'json', 'inflect' => 'dasherize']);
  418. $all = $this->collection->routes();
  419. $this->assertCount(5, $all);
  420. $this->assertEquals('/api/blog-posts', $all[0]->template);
  421. $this->assertEquals(
  422. ['controller', 'action', '_method', 'prefix', 'plugin'],
  423. array_keys($all[0]->defaults)
  424. );
  425. $this->assertEquals('BlogPosts', $all[0]->defaults['controller']);
  426. }
  427. /**
  428. * Test connecting nested resources with the inflection option
  429. *
  430. * @return void
  431. */
  432. public function testResourcesNestedInflection()
  433. {
  434. $routes = new RouteBuilder($this->collection, '/api');
  435. $routes->resources(
  436. 'NetworkObjects',
  437. ['inflect' => 'dasherize'],
  438. function ($routes) {
  439. $routes->resources('Attributes');
  440. }
  441. );
  442. $all = $this->collection->routes();
  443. $this->assertCount(10, $all);
  444. $this->assertEquals('/api/network-objects', $all[0]->template);
  445. $this->assertEquals('/api/network-objects/:id', $all[2]->template);
  446. $this->assertEquals('/api/network-objects/:network_object_id/attributes', $all[5]->template);
  447. }
  448. /**
  449. * Test connecting resources with additional mappings
  450. *
  451. * @return void
  452. */
  453. public function testResourcesMappings()
  454. {
  455. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  456. $routes->resources('Articles', [
  457. '_ext' => 'json',
  458. 'map' => [
  459. 'delete_all' => ['action' => 'deleteAll', 'method' => 'DELETE'],
  460. 'update_many' => ['action' => 'updateAll', 'method' => 'DELETE', 'path' => '/updateAll'],
  461. ]
  462. ]);
  463. $all = $this->collection->routes();
  464. $this->assertCount(7, $all);
  465. $this->assertEquals('/api/articles/delete_all', $all[5]->template, 'Path defaults to key name.');
  466. $this->assertEquals(
  467. ['controller', 'action', '_method', 'prefix', 'plugin'],
  468. array_keys($all[5]->defaults)
  469. );
  470. $this->assertEquals('Articles', $all[5]->defaults['controller']);
  471. $this->assertEquals('deleteAll', $all[5]->defaults['action']);
  472. $this->assertEquals('/api/articles/updateAll', $all[6]->template, 'Explicit path option');
  473. $this->assertEquals(
  474. ['controller', 'action', '_method', 'prefix', 'plugin'],
  475. array_keys($all[6]->defaults)
  476. );
  477. $this->assertEquals('Articles', $all[6]->defaults['controller']);
  478. $this->assertEquals('updateAll', $all[6]->defaults['action']);
  479. }
  480. /**
  481. * Test connecting resources.
  482. *
  483. * @return void
  484. */
  485. public function testResourcesInScope()
  486. {
  487. Router::scope('/api', ['prefix' => 'api'], function ($routes) {
  488. $routes->extensions(['json']);
  489. $routes->resources('Articles');
  490. });
  491. $url = Router::url([
  492. 'prefix' => 'api',
  493. 'controller' => 'Articles',
  494. 'action' => 'edit',
  495. '_method' => 'PUT',
  496. 'id' => 99
  497. ]);
  498. $this->assertEquals('/api/articles/99', $url);
  499. $url = Router::url([
  500. 'prefix' => 'api',
  501. 'controller' => 'Articles',
  502. 'action' => 'edit',
  503. '_method' => 'PUT',
  504. '_ext' => 'json',
  505. 'id' => 99
  506. ]);
  507. $this->assertEquals('/api/articles/99.json', $url);
  508. }
  509. /**
  510. * Test resource parsing.
  511. *
  512. * @return void
  513. */
  514. public function testResourcesParsing()
  515. {
  516. $routes = new RouteBuilder($this->collection, '/');
  517. $routes->resources('Articles');
  518. $_SERVER['REQUEST_METHOD'] = 'GET';
  519. $result = $this->collection->parse('/articles');
  520. $this->assertEquals('Articles', $result['controller']);
  521. $this->assertEquals('index', $result['action']);
  522. $this->assertEquals([], $result['pass']);
  523. $result = $this->collection->parse('/articles/1');
  524. $this->assertEquals('Articles', $result['controller']);
  525. $this->assertEquals('view', $result['action']);
  526. $this->assertEquals([1], $result['pass']);
  527. $_SERVER['REQUEST_METHOD'] = 'POST';
  528. $result = $this->collection->parse('/articles');
  529. $this->assertEquals('Articles', $result['controller']);
  530. $this->assertEquals('add', $result['action']);
  531. $this->assertEquals([], $result['pass']);
  532. $_SERVER['REQUEST_METHOD'] = 'PUT';
  533. $result = $this->collection->parse('/articles/1');
  534. $this->assertEquals('Articles', $result['controller']);
  535. $this->assertEquals('edit', $result['action']);
  536. $this->assertEquals([1], $result['pass']);
  537. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  538. $result = $this->collection->parse('/articles/1');
  539. $this->assertEquals('Articles', $result['controller']);
  540. $this->assertEquals('delete', $result['action']);
  541. $this->assertEquals([1], $result['pass']);
  542. }
  543. /**
  544. * Test the only option of RouteBuilder.
  545. *
  546. * @return void
  547. */
  548. public function testResourcesOnlyString()
  549. {
  550. $routes = new RouteBuilder($this->collection, '/');
  551. $routes->resources('Articles', ['only' => 'index']);
  552. $result = $this->collection->routes();
  553. $this->assertCount(1, $result);
  554. $this->assertEquals('/articles', $result[0]->template);
  555. }
  556. /**
  557. * Test the only option of RouteBuilder.
  558. *
  559. * @return void
  560. */
  561. public function testResourcesOnlyArray()
  562. {
  563. $routes = new RouteBuilder($this->collection, '/');
  564. $routes->resources('Articles', ['only' => ['index', 'delete']]);
  565. $result = $this->collection->routes();
  566. $this->assertCount(2, $result);
  567. $this->assertEquals('/articles', $result[0]->template);
  568. $this->assertEquals('index', $result[0]->defaults['action']);
  569. $this->assertEquals('GET', $result[0]->defaults['_method']);
  570. $this->assertEquals('/articles/:id', $result[1]->template);
  571. $this->assertEquals('delete', $result[1]->defaults['action']);
  572. $this->assertEquals('DELETE', $result[1]->defaults['_method']);
  573. }
  574. /**
  575. * Test the actions option of RouteBuilder.
  576. *
  577. * @return void
  578. */
  579. public function testResourcesActions()
  580. {
  581. $routes = new RouteBuilder($this->collection, '/');
  582. $routes->resources('Articles', [
  583. 'only' => ['index', 'delete'],
  584. 'actions' => ['index' => 'showList']
  585. ]);
  586. $result = $this->collection->routes();
  587. $this->assertCount(2, $result);
  588. $this->assertEquals('/articles', $result[0]->template);
  589. $this->assertEquals('showList', $result[0]->defaults['action']);
  590. $this->assertEquals('/articles/:id', $result[1]->template);
  591. $this->assertEquals('delete', $result[1]->defaults['action']);
  592. }
  593. /**
  594. * Test nesting resources
  595. *
  596. * @return void
  597. */
  598. public function testResourcesNested()
  599. {
  600. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  601. $routes->resources('Articles', function ($routes) {
  602. $this->assertEquals('/api/articles/', $routes->path());
  603. $this->assertEquals(['prefix' => 'api'], $routes->params());
  604. $routes->resources('Comments');
  605. $route = $this->collection->routes()[6];
  606. $this->assertEquals('/api/articles/:article_id/comments', $route->template);
  607. });
  608. }
  609. /**
  610. * Test connecting fallback routes.
  611. *
  612. * @return void
  613. */
  614. public function testFallbacks()
  615. {
  616. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  617. $routes->fallbacks();
  618. $all = $this->collection->routes();
  619. $this->assertEquals('/api/:controller', $all[0]->template);
  620. $this->assertEquals('/api/:controller/:action/*', $all[1]->template);
  621. $this->assertInstanceOf(Route::class, $all[0]);
  622. }
  623. /**
  624. * Test connecting fallback routes with specific route class
  625. *
  626. * @return void
  627. */
  628. public function testFallbacksWithClass()
  629. {
  630. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  631. $routes->fallbacks('InflectedRoute');
  632. $all = $this->collection->routes();
  633. $this->assertEquals('/api/:controller', $all[0]->template);
  634. $this->assertEquals('/api/:controller/:action/*', $all[1]->template);
  635. $this->assertInstanceOf(InflectedRoute::class, $all[0]);
  636. }
  637. /**
  638. * Test connecting fallback routes after setting default route class.
  639. *
  640. * @return void
  641. */
  642. public function testDefaultRouteClassFallbacks()
  643. {
  644. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  645. $routes->routeClass('TestApp\Routing\Route\DashedRoute');
  646. $routes->fallbacks();
  647. $all = $this->collection->routes();
  648. $this->assertInstanceOf('TestApp\Routing\Route\DashedRoute', $all[0]);
  649. }
  650. /**
  651. * Test adding a scope.
  652. *
  653. * @return void
  654. */
  655. public function testScope()
  656. {
  657. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  658. $routes->scope('/v1', ['version' => 1], function ($routes) {
  659. $this->assertEquals('/api/v1', $routes->path());
  660. $this->assertEquals(['prefix' => 'api', 'version' => 1], $routes->params());
  661. });
  662. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  663. $routes->scope('/v1', function ($routes) {
  664. $this->assertEquals('/api/v1', $routes->path());
  665. $this->assertEquals(['prefix' => 'api'], $routes->params());
  666. });
  667. }
  668. /**
  669. * Test using name prefixes.
  670. *
  671. * @return void
  672. */
  673. public function testNamePrefixes()
  674. {
  675. $routes = new RouteBuilder($this->collection, '/api', [], ['namePrefix' => 'api:']);
  676. $routes->scope('/v1', ['version' => 1, '_namePrefix' => 'v1:'], function ($routes) {
  677. $this->assertEquals('api:v1:', $routes->namePrefix());
  678. $routes->connect('/ping', ['controller' => 'Pings'], ['_name' => 'ping']);
  679. $routes->namePrefix('web:');
  680. $routes->connect('/pong', ['controller' => 'Pongs'], ['_name' => 'pong']);
  681. });
  682. $all = $this->collection->named();
  683. $this->assertArrayHasKey('api:v1:ping', $all);
  684. $this->assertArrayHasKey('web:pong', $all);
  685. }
  686. }