RouteBuilderTest.php 23 KB

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