RouteBuilderTest.php 20 KB

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