RouteBuilderTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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 plugin()
  310. *
  311. * @return void
  312. */
  313. public function testNestedPlugin()
  314. {
  315. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  316. $res = $routes->plugin('Contacts', function ($r) {
  317. $this->assertEquals('/b/contacts', $r->path());
  318. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  319. $r->connect('/:controller');
  320. $route = $this->collection->routes()[0];
  321. $this->assertEquals(
  322. ['key' => 'value', 'plugin' => 'Contacts', 'action' => 'index'],
  323. $route->defaults
  324. );
  325. });
  326. $this->assertNull($res);
  327. }
  328. /**
  329. * Test creating sub-scopes with plugin() + path option
  330. *
  331. * @return void
  332. */
  333. public function testNestedPluginPathOption()
  334. {
  335. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  336. $routes->plugin('Contacts', ['path' => '/people'], function ($r) {
  337. $this->assertEquals('/b/people', $r->path());
  338. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  339. });
  340. }
  341. /**
  342. * Test connecting resources.
  343. *
  344. * @return void
  345. */
  346. public function testResources()
  347. {
  348. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  349. $routes->resources('Articles', ['_ext' => 'json']);
  350. $all = $this->collection->routes();
  351. $this->assertCount(5, $all);
  352. $this->assertEquals('/api/articles', $all[0]->template);
  353. $this->assertEquals(
  354. ['controller', 'action', '_method', 'prefix', 'plugin'],
  355. array_keys($all[0]->defaults)
  356. );
  357. $this->assertEquals('json', $all[0]->options['_ext']);
  358. $this->assertEquals('Articles', $all[0]->defaults['controller']);
  359. }
  360. /**
  361. * Test connecting resources with a controller
  362. *
  363. * @return void
  364. */
  365. public function testResourcesController()
  366. {
  367. $routes = new RouteBuilder($this->collection, '/api');
  368. $routes->resources('Articles', ['controller' => 'Posts']);
  369. $all = $this->collection->routes();
  370. $this->assertEquals('Posts', $all[0]->defaults['controller']);
  371. }
  372. /**
  373. * Test connecting resources with a prefix
  374. *
  375. * @return void
  376. */
  377. public function testResourcesPrefix()
  378. {
  379. $routes = new RouteBuilder($this->collection, '/api');
  380. $routes->resources('Articles', ['prefix' => 'rest']);
  381. $all = $this->collection->routes();
  382. $this->assertEquals('rest', $all[0]->defaults['prefix']);
  383. }
  384. /**
  385. * Test that resource prefixes work within a prefixed scope.
  386. *
  387. * @return void
  388. */
  389. public function testResourcesNestedPrefix()
  390. {
  391. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  392. $routes->resources('Articles', ['prefix' => 'rest']);
  393. $all = $this->collection->routes();
  394. $this->assertCount(5, $all);
  395. $this->assertEquals('/api/articles', $all[0]->template);
  396. foreach ($all as $route) {
  397. $this->assertEquals('api/rest', $route->defaults['prefix']);
  398. $this->assertEquals('Articles', $route->defaults['controller']);
  399. }
  400. }
  401. /**
  402. * Test connecting resources with the inflection option
  403. *
  404. * @return void
  405. */
  406. public function testResourcesInflection()
  407. {
  408. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  409. $routes->resources('BlogPosts', ['_ext' => 'json', 'inflect' => 'dasherize']);
  410. $all = $this->collection->routes();
  411. $this->assertCount(5, $all);
  412. $this->assertEquals('/api/blog-posts', $all[0]->template);
  413. $this->assertEquals(
  414. ['controller', 'action', '_method', 'prefix', 'plugin'],
  415. array_keys($all[0]->defaults)
  416. );
  417. $this->assertEquals('BlogPosts', $all[0]->defaults['controller']);
  418. }
  419. /**
  420. * Test connecting nested resources with the inflection option
  421. *
  422. * @return void
  423. */
  424. public function testResourcesNestedInflection()
  425. {
  426. $routes = new RouteBuilder($this->collection, '/api');
  427. $routes->resources(
  428. 'NetworkObjects',
  429. ['inflect' => 'dasherize'],
  430. function ($routes) {
  431. $routes->resources('Attributes');
  432. }
  433. );
  434. $all = $this->collection->routes();
  435. $this->assertCount(10, $all);
  436. $this->assertEquals('/api/network-objects', $all[0]->template);
  437. $this->assertEquals('/api/network-objects/:id', $all[2]->template);
  438. $this->assertEquals('/api/network-objects/:network_object_id/attributes', $all[5]->template);
  439. }
  440. /**
  441. * Test connecting resources with additional mappings
  442. *
  443. * @return void
  444. */
  445. public function testResourcesMappings()
  446. {
  447. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  448. $routes->resources('Articles', [
  449. '_ext' => 'json',
  450. 'map' => [
  451. 'delete_all' => ['action' => 'deleteAll', 'method' => 'DELETE'],
  452. 'update_many' => ['action' => 'updateAll', 'method' => 'DELETE', 'path' => '/updateAll'],
  453. ]
  454. ]);
  455. $all = $this->collection->routes();
  456. $this->assertCount(7, $all);
  457. $this->assertEquals('/api/articles/delete_all', $all[5]->template, 'Path defaults to key name.');
  458. $this->assertEquals(
  459. ['controller', 'action', '_method', 'prefix', 'plugin'],
  460. array_keys($all[5]->defaults)
  461. );
  462. $this->assertEquals('Articles', $all[5]->defaults['controller']);
  463. $this->assertEquals('deleteAll', $all[5]->defaults['action']);
  464. $this->assertEquals('/api/articles/updateAll', $all[6]->template, 'Explicit path option');
  465. $this->assertEquals(
  466. ['controller', 'action', '_method', 'prefix', 'plugin'],
  467. array_keys($all[6]->defaults)
  468. );
  469. $this->assertEquals('Articles', $all[6]->defaults['controller']);
  470. $this->assertEquals('updateAll', $all[6]->defaults['action']);
  471. }
  472. /**
  473. * Test connecting resources.
  474. *
  475. * @return void
  476. */
  477. public function testResourcesInScope()
  478. {
  479. Router::scope('/api', ['prefix' => 'api'], function ($routes) {
  480. $routes->extensions(['json']);
  481. $routes->resources('Articles');
  482. });
  483. $url = Router::url([
  484. 'prefix' => 'api',
  485. 'controller' => 'Articles',
  486. 'action' => 'edit',
  487. '_method' => 'PUT',
  488. 'id' => 99
  489. ]);
  490. $this->assertEquals('/api/articles/99', $url);
  491. $url = Router::url([
  492. 'prefix' => 'api',
  493. 'controller' => 'Articles',
  494. 'action' => 'edit',
  495. '_method' => 'PUT',
  496. '_ext' => 'json',
  497. 'id' => 99
  498. ]);
  499. $this->assertEquals('/api/articles/99.json', $url);
  500. }
  501. /**
  502. * Test resource parsing.
  503. *
  504. * @return void
  505. */
  506. public function testResourcesParsing()
  507. {
  508. $routes = new RouteBuilder($this->collection, '/');
  509. $routes->resources('Articles');
  510. $_SERVER['REQUEST_METHOD'] = 'GET';
  511. $result = $this->collection->parse('/articles');
  512. $this->assertEquals('Articles', $result['controller']);
  513. $this->assertEquals('index', $result['action']);
  514. $this->assertEquals([], $result['pass']);
  515. $result = $this->collection->parse('/articles/1');
  516. $this->assertEquals('Articles', $result['controller']);
  517. $this->assertEquals('view', $result['action']);
  518. $this->assertEquals([1], $result['pass']);
  519. $_SERVER['REQUEST_METHOD'] = 'POST';
  520. $result = $this->collection->parse('/articles');
  521. $this->assertEquals('Articles', $result['controller']);
  522. $this->assertEquals('add', $result['action']);
  523. $this->assertEquals([], $result['pass']);
  524. $_SERVER['REQUEST_METHOD'] = 'PUT';
  525. $result = $this->collection->parse('/articles/1');
  526. $this->assertEquals('Articles', $result['controller']);
  527. $this->assertEquals('edit', $result['action']);
  528. $this->assertEquals([1], $result['pass']);
  529. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  530. $result = $this->collection->parse('/articles/1');
  531. $this->assertEquals('Articles', $result['controller']);
  532. $this->assertEquals('delete', $result['action']);
  533. $this->assertEquals([1], $result['pass']);
  534. }
  535. /**
  536. * Test the only option of RouteBuilder.
  537. *
  538. * @return void
  539. */
  540. public function testResourcesOnlyString()
  541. {
  542. $routes = new RouteBuilder($this->collection, '/');
  543. $routes->resources('Articles', ['only' => 'index']);
  544. $result = $this->collection->routes();
  545. $this->assertCount(1, $result);
  546. $this->assertEquals('/articles', $result[0]->template);
  547. }
  548. /**
  549. * Test the only option of RouteBuilder.
  550. *
  551. * @return void
  552. */
  553. public function testResourcesOnlyArray()
  554. {
  555. $routes = new RouteBuilder($this->collection, '/');
  556. $routes->resources('Articles', ['only' => ['index', 'delete']]);
  557. $result = $this->collection->routes();
  558. $this->assertCount(2, $result);
  559. $this->assertEquals('/articles', $result[0]->template);
  560. $this->assertEquals('index', $result[0]->defaults['action']);
  561. $this->assertEquals('GET', $result[0]->defaults['_method']);
  562. $this->assertEquals('/articles/:id', $result[1]->template);
  563. $this->assertEquals('delete', $result[1]->defaults['action']);
  564. $this->assertEquals('DELETE', $result[1]->defaults['_method']);
  565. }
  566. /**
  567. * Test the actions option of RouteBuilder.
  568. *
  569. * @return void
  570. */
  571. public function testResourcesActions()
  572. {
  573. $routes = new RouteBuilder($this->collection, '/');
  574. $routes->resources('Articles', [
  575. 'only' => ['index', 'delete'],
  576. 'actions' => ['index' => 'showList']
  577. ]);
  578. $result = $this->collection->routes();
  579. $this->assertCount(2, $result);
  580. $this->assertEquals('/articles', $result[0]->template);
  581. $this->assertEquals('showList', $result[0]->defaults['action']);
  582. $this->assertEquals('/articles/:id', $result[1]->template);
  583. $this->assertEquals('delete', $result[1]->defaults['action']);
  584. }
  585. /**
  586. * Test nesting resources
  587. *
  588. * @return void
  589. */
  590. public function testResourcesNested()
  591. {
  592. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  593. $routes->resources('Articles', function ($routes) {
  594. $this->assertEquals('/api/articles/', $routes->path());
  595. $this->assertEquals(['prefix' => 'api'], $routes->params());
  596. $routes->resources('Comments');
  597. $route = $this->collection->routes()[6];
  598. $this->assertEquals('/api/articles/:article_id/comments', $route->template);
  599. });
  600. }
  601. /**
  602. * Test connecting fallback routes.
  603. *
  604. * @return void
  605. */
  606. public function testFallbacks()
  607. {
  608. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  609. $routes->fallbacks();
  610. $all = $this->collection->routes();
  611. $this->assertEquals('/api/:controller', $all[0]->template);
  612. $this->assertEquals('/api/:controller/:action/*', $all[1]->template);
  613. $this->assertInstanceOf(Route::class, $all[0]);
  614. }
  615. /**
  616. * Test connecting fallback routes with specific route class
  617. *
  618. * @return void
  619. */
  620. public function testFallbacksWithClass()
  621. {
  622. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  623. $routes->fallbacks('InflectedRoute');
  624. $all = $this->collection->routes();
  625. $this->assertEquals('/api/:controller', $all[0]->template);
  626. $this->assertEquals('/api/:controller/:action/*', $all[1]->template);
  627. $this->assertInstanceOf(InflectedRoute::class, $all[0]);
  628. }
  629. /**
  630. * Test connecting fallback routes after setting default route class.
  631. *
  632. * @return void
  633. */
  634. public function testDefaultRouteClassFallbacks()
  635. {
  636. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  637. $routes->routeClass('TestApp\Routing\Route\DashedRoute');
  638. $routes->fallbacks();
  639. $all = $this->collection->routes();
  640. $this->assertInstanceOf('TestApp\Routing\Route\DashedRoute', $all[0]);
  641. }
  642. /**
  643. * Test adding a scope.
  644. *
  645. * @return void
  646. */
  647. public function testScope()
  648. {
  649. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  650. $routes->scope('/v1', ['version' => 1], function ($routes) {
  651. $this->assertEquals('/api/v1', $routes->path());
  652. $this->assertEquals(['prefix' => 'api', 'version' => 1], $routes->params());
  653. });
  654. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  655. $routes->scope('/v1', function ($routes) {
  656. $this->assertEquals('/api/v1', $routes->path());
  657. $this->assertEquals(['prefix' => 'api'], $routes->params());
  658. });
  659. }
  660. /**
  661. * Test using name prefixes.
  662. *
  663. * @return void
  664. */
  665. public function testNamePrefixes()
  666. {
  667. $routes = new RouteBuilder($this->collection, '/api', [], ['namePrefix' => 'api:']);
  668. $routes->scope('/v1', ['version' => 1, '_namePrefix' => 'v1:'], function ($routes) {
  669. $this->assertEquals('api:v1:', $routes->namePrefix());
  670. $routes->connect('/ping', ['controller' => 'Pings'], ['_name' => 'ping']);
  671. $routes->namePrefix('web:');
  672. $routes->connect('/pong', ['controller' => 'Pongs'], ['_name' => 'pong']);
  673. });
  674. $all = $this->collection->named();
  675. $this->assertArrayHasKey('api:v1:ping', $all);
  676. $this->assertArrayHasKey('web:pong', $all);
  677. }
  678. }