RouteBuilderTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 extensions being connected to routes.
  151. *
  152. * @return void
  153. */
  154. public function testConnectExtensions()
  155. {
  156. $routes = new RouteBuilder(
  157. $this->collection,
  158. '/l',
  159. [],
  160. ['extensions' => ['json']]
  161. );
  162. $this->assertEquals(['json'], $routes->extensions());
  163. $routes->connect('/:controller');
  164. $route = $this->collection->routes()[0];
  165. $this->assertEquals(['json'], $route->options['_ext']);
  166. $routes->extensions(['xml', 'json']);
  167. $routes->connect('/:controller/:action');
  168. $new = $this->collection->routes()[1];
  169. $this->assertEquals(['json'], $route->options['_ext']);
  170. $this->assertEquals(['xml', 'json'], $new->options['_ext']);
  171. }
  172. /**
  173. * Test adding additional extensions will be merged with current.
  174. *
  175. * @return void
  176. */
  177. public function testConnectExtensionsAdd()
  178. {
  179. $routes = new RouteBuilder(
  180. $this->collection,
  181. '/l',
  182. [],
  183. ['extensions' => ['json']]
  184. );
  185. $this->assertEquals(['json'], $routes->extensions());
  186. $routes->addExtensions(['xml']);
  187. $this->assertEquals(['json', 'xml'], $routes->extensions());
  188. $routes->addExtensions('csv');
  189. $this->assertEquals(['json', 'xml', 'csv'], $routes->extensions());
  190. }
  191. /**
  192. * test that extensions() accepts a string.
  193. *
  194. * @return void
  195. */
  196. public function testExtensionsString()
  197. {
  198. $routes = new RouteBuilder($this->collection, '/l');
  199. $routes->extensions('json');
  200. $this->assertEquals(['json'], $routes->extensions());
  201. }
  202. /**
  203. * Test error on invalid route class
  204. *
  205. * @expectedException \InvalidArgumentException
  206. * @expectedExceptionMessage Route class not found, or route class is not a subclass of
  207. * @return void
  208. */
  209. public function testConnectErrorInvalidRouteClass()
  210. {
  211. $routes = new RouteBuilder(
  212. $this->collection,
  213. '/l',
  214. [],
  215. ['extensions' => ['json']]
  216. );
  217. $routes->connect('/:controller', [], ['routeClass' => '\StdClass']);
  218. }
  219. /**
  220. * Test conflicting parameters raises an exception.
  221. *
  222. * @expectedException \BadMethodCallException
  223. * @expectedExceptionMessage You cannot define routes that conflict with the scope.
  224. * @return void
  225. */
  226. public function testConnectConflictingParameters()
  227. {
  228. $routes = new RouteBuilder($this->collection, '/admin', ['plugin' => 'TestPlugin']);
  229. $routes->connect('/', ['plugin' => 'TestPlugin2', 'controller' => 'Dashboard', 'action' => 'view']);
  230. }
  231. /**
  232. * Test connecting redirect routes.
  233. *
  234. * @return void
  235. */
  236. public function testRedirect()
  237. {
  238. $routes = new RouteBuilder($this->collection, '/');
  239. $routes->redirect('/p/:id', ['controller' => 'posts', 'action' => 'view'], ['status' => 301]);
  240. $route = $this->collection->routes()[0];
  241. $this->assertInstanceOf('Cake\Routing\Route\RedirectRoute', $route);
  242. $routes->redirect('/old', '/forums', ['status' => 301]);
  243. $route = $this->collection->routes()[1];
  244. $this->assertInstanceOf('Cake\Routing\Route\RedirectRoute', $route);
  245. $this->assertEquals('/forums', $route->redirect[0]);
  246. }
  247. /**
  248. * Test creating sub-scopes with prefix()
  249. *
  250. * @return void
  251. */
  252. public function testPrefix()
  253. {
  254. $routes = new RouteBuilder($this->collection, '/path', ['key' => 'value']);
  255. $res = $routes->prefix('admin', function ($r) {
  256. $this->assertInstanceOf('Cake\Routing\RouteBuilder', $r);
  257. $this->assertCount(0, $this->collection->routes());
  258. $this->assertEquals('/path/admin', $r->path());
  259. $this->assertEquals(['prefix' => 'admin', 'key' => 'value'], $r->params());
  260. });
  261. $this->assertNull($res);
  262. }
  263. /**
  264. * Test creating sub-scopes with prefix()
  265. *
  266. * @return void
  267. */
  268. public function testNestedPrefix()
  269. {
  270. $routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'admin']);
  271. $res = $routes->prefix('api', function ($r) {
  272. $this->assertEquals('/admin/api', $r->path());
  273. $this->assertEquals(['prefix' => 'admin/api'], $r->params());
  274. });
  275. $this->assertNull($res);
  276. }
  277. /**
  278. * Test creating sub-scopes with plugin()
  279. *
  280. * @return void
  281. */
  282. public function testNestedPlugin()
  283. {
  284. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  285. $res = $routes->plugin('Contacts', function ($r) {
  286. $this->assertEquals('/b/contacts', $r->path());
  287. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  288. $r->connect('/:controller');
  289. $route = $this->collection->routes()[0];
  290. $this->assertEquals(
  291. ['key' => 'value', 'plugin' => 'Contacts', 'action' => 'index'],
  292. $route->defaults
  293. );
  294. });
  295. $this->assertNull($res);
  296. }
  297. /**
  298. * Test creating sub-scopes with plugin() + path option
  299. *
  300. * @return void
  301. */
  302. public function testNestedPluginPathOption()
  303. {
  304. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  305. $routes->plugin('Contacts', ['path' => '/people'], function ($r) {
  306. $this->assertEquals('/b/people', $r->path());
  307. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  308. });
  309. }
  310. /**
  311. * Test connecting resources.
  312. *
  313. * @return void
  314. */
  315. public function testResources()
  316. {
  317. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  318. $routes->resources('Articles', ['_ext' => 'json']);
  319. $all = $this->collection->routes();
  320. $this->assertCount(5, $all);
  321. $this->assertEquals('/api/articles', $all[0]->template);
  322. $this->assertEquals(
  323. ['controller', 'action', '_method', 'prefix', 'plugin'],
  324. array_keys($all[0]->defaults)
  325. );
  326. $this->assertEquals('json', $all[0]->options['_ext']);
  327. $this->assertEquals('Articles', $all[0]->defaults['controller']);
  328. }
  329. /**
  330. * Test connecting resources with a prefix
  331. *
  332. * @return void
  333. */
  334. public function testResourcesPrefix()
  335. {
  336. $routes = new RouteBuilder($this->collection, '/api');
  337. $routes->resources('Articles', ['prefix' => 'rest']);
  338. $all = $this->collection->routes();
  339. $this->assertEquals('rest', $all[0]->defaults['prefix']);
  340. }
  341. /**
  342. * Test that resource prefixes work within a prefixed scope.
  343. *
  344. * @return void
  345. */
  346. public function testResourcesNestedPrefix()
  347. {
  348. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  349. $routes->resources('Articles', ['prefix' => 'rest']);
  350. $all = $this->collection->routes();
  351. $this->assertCount(5, $all);
  352. $this->assertEquals('/api/articles', $all[0]->template);
  353. foreach ($all as $route) {
  354. $this->assertEquals('api/rest', $route->defaults['prefix']);
  355. $this->assertEquals('Articles', $route->defaults['controller']);
  356. }
  357. }
  358. /**
  359. * Test connecting resources with the inflection option
  360. *
  361. * @return void
  362. */
  363. public function testResourcesInflection()
  364. {
  365. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  366. $routes->resources('BlogPosts', ['_ext' => 'json', 'inflect' => 'dasherize']);
  367. $all = $this->collection->routes();
  368. $this->assertCount(5, $all);
  369. $this->assertEquals('/api/blog-posts', $all[0]->template);
  370. $this->assertEquals(
  371. ['controller', 'action', '_method', 'prefix', 'plugin'],
  372. array_keys($all[0]->defaults)
  373. );
  374. $this->assertEquals('BlogPosts', $all[0]->defaults['controller']);
  375. }
  376. /**
  377. * Test connecting nested resources with the inflection option
  378. *
  379. * @return void
  380. */
  381. public function testResourcesNestedInflection()
  382. {
  383. $routes = new RouteBuilder($this->collection, '/api');
  384. $routes->resources(
  385. 'NetworkObjects',
  386. ['inflect' => 'dasherize'],
  387. function ($routes) {
  388. $routes->resources('Attributes');
  389. }
  390. );
  391. $all = $this->collection->routes();
  392. $this->assertCount(10, $all);
  393. $this->assertEquals('/api/network-objects', $all[0]->template);
  394. $this->assertEquals('/api/network-objects/:id', $all[2]->template);
  395. $this->assertEquals('/api/network-objects/:network_object_id/attributes', $all[5]->template);
  396. }
  397. /**
  398. * Test connecting resources with additional mappings
  399. *
  400. * @return void
  401. */
  402. public function testResourcesMappings()
  403. {
  404. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  405. $routes->resources('Articles', [
  406. '_ext' => 'json',
  407. 'map' => [
  408. 'delete_all' => ['action' => 'deleteAll', 'method' => 'DELETE'],
  409. 'update_many' => ['action' => 'updateAll', 'method' => 'DELETE', 'path' => '/updateAll'],
  410. ]
  411. ]);
  412. $all = $this->collection->routes();
  413. $this->assertCount(7, $all);
  414. $this->assertEquals('/api/articles/delete_all', $all[5]->template, 'Path defaults to key name.');
  415. $this->assertEquals(
  416. ['controller', 'action', '_method', 'prefix', 'plugin'],
  417. array_keys($all[5]->defaults)
  418. );
  419. $this->assertEquals('Articles', $all[5]->defaults['controller']);
  420. $this->assertEquals('deleteAll', $all[5]->defaults['action']);
  421. $this->assertEquals('/api/articles/updateAll', $all[6]->template, 'Explicit path option');
  422. $this->assertEquals(
  423. ['controller', 'action', '_method', 'prefix', 'plugin'],
  424. array_keys($all[6]->defaults)
  425. );
  426. $this->assertEquals('Articles', $all[6]->defaults['controller']);
  427. $this->assertEquals('updateAll', $all[6]->defaults['action']);
  428. }
  429. /**
  430. * Test connecting resources.
  431. *
  432. * @return void
  433. */
  434. public function testResourcesInScope()
  435. {
  436. Router::scope('/api', ['prefix' => 'api'], function ($routes) {
  437. $routes->extensions(['json']);
  438. $routes->resources('Articles');
  439. });
  440. $url = Router::url([
  441. 'prefix' => 'api',
  442. 'controller' => 'Articles',
  443. 'action' => 'edit',
  444. '_method' => 'PUT',
  445. 'id' => 99
  446. ]);
  447. $this->assertEquals('/api/articles/99', $url);
  448. $url = Router::url([
  449. 'prefix' => 'api',
  450. 'controller' => 'Articles',
  451. 'action' => 'edit',
  452. '_method' => 'PUT',
  453. '_ext' => 'json',
  454. 'id' => 99
  455. ]);
  456. $this->assertEquals('/api/articles/99.json', $url);
  457. }
  458. /**
  459. * Test resource parsing.
  460. *
  461. * @return void
  462. */
  463. public function testResourcesParsing()
  464. {
  465. $routes = new RouteBuilder($this->collection, '/');
  466. $routes->resources('Articles');
  467. $_SERVER['REQUEST_METHOD'] = 'GET';
  468. $result = $this->collection->parse('/articles');
  469. $this->assertEquals('Articles', $result['controller']);
  470. $this->assertEquals('index', $result['action']);
  471. $this->assertEquals([], $result['pass']);
  472. $result = $this->collection->parse('/articles/1');
  473. $this->assertEquals('Articles', $result['controller']);
  474. $this->assertEquals('view', $result['action']);
  475. $this->assertEquals([1], $result['pass']);
  476. $_SERVER['REQUEST_METHOD'] = 'POST';
  477. $result = $this->collection->parse('/articles');
  478. $this->assertEquals('Articles', $result['controller']);
  479. $this->assertEquals('add', $result['action']);
  480. $this->assertEquals([], $result['pass']);
  481. $_SERVER['REQUEST_METHOD'] = 'PUT';
  482. $result = $this->collection->parse('/articles/1');
  483. $this->assertEquals('Articles', $result['controller']);
  484. $this->assertEquals('edit', $result['action']);
  485. $this->assertEquals([1], $result['pass']);
  486. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  487. $result = $this->collection->parse('/articles/1');
  488. $this->assertEquals('Articles', $result['controller']);
  489. $this->assertEquals('delete', $result['action']);
  490. $this->assertEquals([1], $result['pass']);
  491. }
  492. /**
  493. * Test the only option of RouteBuilder.
  494. *
  495. * @return void
  496. */
  497. public function testResourcesOnlyString()
  498. {
  499. $routes = new RouteBuilder($this->collection, '/');
  500. $routes->resources('Articles', ['only' => 'index']);
  501. $result = $this->collection->routes();
  502. $this->assertCount(1, $result);
  503. $this->assertEquals('/articles', $result[0]->template);
  504. }
  505. /**
  506. * Test the only option of RouteBuilder.
  507. *
  508. * @return void
  509. */
  510. public function testResourcesOnlyArray()
  511. {
  512. $routes = new RouteBuilder($this->collection, '/');
  513. $routes->resources('Articles', ['only' => ['index', 'delete']]);
  514. $result = $this->collection->routes();
  515. $this->assertCount(2, $result);
  516. $this->assertEquals('/articles', $result[0]->template);
  517. $this->assertEquals('index', $result[0]->defaults['action']);
  518. $this->assertEquals('GET', $result[0]->defaults['_method']);
  519. $this->assertEquals('/articles/:id', $result[1]->template);
  520. $this->assertEquals('delete', $result[1]->defaults['action']);
  521. $this->assertEquals('DELETE', $result[1]->defaults['_method']);
  522. }
  523. /**
  524. * Test the actions option of RouteBuilder.
  525. *
  526. * @return void
  527. */
  528. public function testResourcesActions()
  529. {
  530. $routes = new RouteBuilder($this->collection, '/');
  531. $routes->resources('Articles', [
  532. 'only' => ['index', 'delete'],
  533. 'actions' => ['index' => 'showList']
  534. ]);
  535. $result = $this->collection->routes();
  536. $this->assertCount(2, $result);
  537. $this->assertEquals('/articles', $result[0]->template);
  538. $this->assertEquals('showList', $result[0]->defaults['action']);
  539. $this->assertEquals('/articles/:id', $result[1]->template);
  540. $this->assertEquals('delete', $result[1]->defaults['action']);
  541. }
  542. /**
  543. * Test nesting resources
  544. *
  545. * @return void
  546. */
  547. public function testResourcesNested()
  548. {
  549. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  550. $routes->resources('Articles', function ($routes) {
  551. $this->assertEquals('/api/articles/', $routes->path());
  552. $this->assertEquals(['prefix' => 'api'], $routes->params());
  553. $routes->resources('Comments');
  554. $route = $this->collection->routes()[6];
  555. $this->assertEquals('/api/articles/:article_id/comments', $route->template);
  556. });
  557. }
  558. /**
  559. * Test connecting fallback routes.
  560. *
  561. * @return void
  562. */
  563. public function testFallbacks()
  564. {
  565. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  566. $routes->fallbacks();
  567. $all = $this->collection->routes();
  568. $this->assertEquals('/api/:controller', $all[0]->template);
  569. $this->assertEquals('/api/:controller/:action/*', $all[1]->template);
  570. $this->assertInstanceOf('Cake\Routing\Route\Route', $all[0]);
  571. }
  572. /**
  573. * Test connecting fallback routes with specific route class
  574. *
  575. * @return void
  576. */
  577. public function testFallbacksWithClass()
  578. {
  579. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  580. $routes->fallbacks('InflectedRoute');
  581. $all = $this->collection->routes();
  582. $this->assertEquals('/api/:controller', $all[0]->template);
  583. $this->assertEquals('/api/:controller/:action/*', $all[1]->template);
  584. $this->assertInstanceOf('Cake\Routing\Route\InflectedRoute', $all[0]);
  585. }
  586. /**
  587. * Test connecting fallback routes after setting default route class.
  588. *
  589. * @return void
  590. */
  591. public function testDefaultRouteClassFallbacks()
  592. {
  593. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  594. $routes->routeClass('TestApp\Routing\Route\DashedRoute');
  595. $routes->fallbacks();
  596. $all = $this->collection->routes();
  597. $this->assertInstanceOf('TestApp\Routing\Route\DashedRoute', $all[0]);
  598. }
  599. /**
  600. * Test adding a scope.
  601. *
  602. * @return void
  603. */
  604. public function testScope()
  605. {
  606. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  607. $routes->scope('/v1', ['version' => 1], function ($routes) {
  608. $this->assertEquals('/api/v1', $routes->path());
  609. $this->assertEquals(['prefix' => 'api', 'version' => 1], $routes->params());
  610. });
  611. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  612. $routes->scope('/v1', function ($routes) {
  613. $this->assertEquals('/api/v1', $routes->path());
  614. $this->assertEquals(['prefix' => 'api'], $routes->params());
  615. });
  616. }
  617. /**
  618. * Test using name prefixes.
  619. *
  620. * @return void
  621. */
  622. public function testNamePrefixes()
  623. {
  624. $routes = new RouteBuilder($this->collection, '/api', [], ['namePrefix' => 'api:']);
  625. $routes->scope('/v1', ['version' => 1, '_namePrefix' => 'v1:'], function ($routes) {
  626. $this->assertEquals('api:v1:', $routes->namePrefix());
  627. $routes->connect('/ping', ['controller' => 'Pings'], ['_name' => 'ping']);
  628. $routes->namePrefix('web:');
  629. $routes->connect('/pong', ['controller' => 'Pongs'], ['_name' => 'pong']);
  630. });
  631. $all = $this->collection->named();
  632. $this->assertArrayHasKey('api:v1:ping', $all);
  633. $this->assertArrayHasKey('web:pong', $all);
  634. }
  635. }