RouteBuilderTest.php 24 KB

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