RouteBuilderTest.php 30 KB

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