RouteBuilderTest.php 29 KB

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