RouteBuilderTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://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->assertSame($route, $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. $route = $routes->connect('/:controller');
  138. $this->assertInstanceOf(Route::class, $route);
  139. $this->assertSame($route, $this->collection->routes()[0]);
  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 prefix()
  322. *
  323. * @return void
  324. */
  325. public function testPathWithDotInPrefix()
  326. {
  327. $routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'admin']);
  328. $res = $routes->prefix('api', function ($r) {
  329. $r->prefix('v10', ['path' => '/v1.0'], function ($r2) {
  330. $this->assertEquals('/admin/api/v1.0', $r2->path());
  331. $this->assertEquals(['prefix' => 'admin/api/v10'], $r2->params());
  332. $r2->prefix('b1', ['path' => '/beta.1'], function ($r3) {
  333. $this->assertEquals('/admin/api/v1.0/beta.1', $r3->path());
  334. $this->assertEquals(['prefix' => 'admin/api/v10/b1'], $r3->params());
  335. });
  336. });
  337. });
  338. $this->assertNull($res);
  339. }
  340. /**
  341. * Test creating sub-scopes with plugin()
  342. *
  343. * @return void
  344. */
  345. public function testNestedPlugin()
  346. {
  347. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  348. $res = $routes->plugin('Contacts', function ($r) {
  349. $this->assertEquals('/b/contacts', $r->path());
  350. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  351. $r->connect('/:controller');
  352. $route = $this->collection->routes()[0];
  353. $this->assertEquals(
  354. ['key' => 'value', 'plugin' => 'Contacts', 'action' => 'index'],
  355. $route->defaults
  356. );
  357. });
  358. $this->assertNull($res);
  359. }
  360. /**
  361. * Test creating sub-scopes with plugin() + path option
  362. *
  363. * @return void
  364. */
  365. public function testNestedPluginPathOption()
  366. {
  367. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  368. $routes->plugin('Contacts', ['path' => '/people'], function ($r) {
  369. $this->assertEquals('/b/people', $r->path());
  370. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  371. });
  372. }
  373. /**
  374. * Test connecting resources.
  375. *
  376. * @return void
  377. */
  378. public function testResources()
  379. {
  380. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  381. $routes->resources('Articles', ['_ext' => 'json']);
  382. $all = $this->collection->routes();
  383. $this->assertCount(5, $all);
  384. $this->assertEquals('/api/articles', $all[0]->template);
  385. $this->assertEquals(
  386. ['controller', 'action', '_method', 'prefix', 'plugin'],
  387. array_keys($all[0]->defaults)
  388. );
  389. $this->assertEquals('json', $all[0]->options['_ext']);
  390. $this->assertEquals('Articles', $all[0]->defaults['controller']);
  391. }
  392. /**
  393. * Test connecting resources with a path
  394. *
  395. * @return void
  396. */
  397. public function testResourcesPathOption()
  398. {
  399. $routes = new RouteBuilder($this->collection, '/api');
  400. $routes->resources('Articles', ['path' => 'posts'], function ($routes) {
  401. $routes->resources('Comments');
  402. });
  403. $all = $this->collection->routes();
  404. $this->assertEquals('Articles', $all[0]->defaults['controller']);
  405. $this->assertEquals('/api/posts', $all[0]->template);
  406. $this->assertEquals('/api/posts/:id', $all[2]->template);
  407. $this->assertEquals(
  408. '/api/posts/:article_id/comments',
  409. $all[6]->template,
  410. 'parameter name should reflect resource name'
  411. );
  412. }
  413. /**
  414. * Test connecting resources with a prefix
  415. *
  416. * @return void
  417. */
  418. public function testResourcesPrefix()
  419. {
  420. $routes = new RouteBuilder($this->collection, '/api');
  421. $routes->resources('Articles', ['prefix' => 'rest']);
  422. $all = $this->collection->routes();
  423. $this->assertEquals('rest', $all[0]->defaults['prefix']);
  424. }
  425. /**
  426. * Test that resource prefixes work within a prefixed scope.
  427. *
  428. * @return void
  429. */
  430. public function testResourcesNestedPrefix()
  431. {
  432. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  433. $routes->resources('Articles', ['prefix' => 'rest']);
  434. $all = $this->collection->routes();
  435. $this->assertCount(5, $all);
  436. $this->assertEquals('/api/articles', $all[0]->template);
  437. foreach ($all as $route) {
  438. $this->assertEquals('api/rest', $route->defaults['prefix']);
  439. $this->assertEquals('Articles', $route->defaults['controller']);
  440. }
  441. }
  442. /**
  443. * Test connecting resources with the inflection option
  444. *
  445. * @return void
  446. */
  447. public function testResourcesInflection()
  448. {
  449. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  450. $routes->resources('BlogPosts', ['_ext' => 'json', 'inflect' => 'dasherize']);
  451. $all = $this->collection->routes();
  452. $this->assertCount(5, $all);
  453. $this->assertEquals('/api/blog-posts', $all[0]->template);
  454. $this->assertEquals(
  455. ['controller', 'action', '_method', 'prefix', 'plugin'],
  456. array_keys($all[0]->defaults)
  457. );
  458. $this->assertEquals('BlogPosts', $all[0]->defaults['controller']);
  459. }
  460. /**
  461. * Test connecting nested resources with the inflection option
  462. *
  463. * @return void
  464. */
  465. public function testResourcesNestedInflection()
  466. {
  467. $routes = new RouteBuilder($this->collection, '/api');
  468. $routes->resources(
  469. 'NetworkObjects',
  470. ['inflect' => 'dasherize'],
  471. function ($routes) {
  472. $routes->resources('Attributes');
  473. }
  474. );
  475. $all = $this->collection->routes();
  476. $this->assertCount(10, $all);
  477. $this->assertEquals('/api/network-objects', $all[0]->template);
  478. $this->assertEquals('/api/network-objects/:id', $all[2]->template);
  479. $this->assertEquals('/api/network-objects/:network_object_id/attributes', $all[5]->template);
  480. }
  481. /**
  482. * Test connecting resources with additional mappings
  483. *
  484. * @return void
  485. */
  486. public function testResourcesMappings()
  487. {
  488. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  489. $routes->resources('Articles', [
  490. '_ext' => 'json',
  491. 'map' => [
  492. 'delete_all' => ['action' => 'deleteAll', 'method' => 'DELETE'],
  493. 'update_many' => ['action' => 'updateAll', 'method' => 'DELETE', 'path' => '/updateAll'],
  494. ]
  495. ]);
  496. $all = $this->collection->routes();
  497. $this->assertCount(7, $all);
  498. $this->assertEquals('/api/articles/delete_all', $all[5]->template, 'Path defaults to key name.');
  499. $this->assertEquals(
  500. ['controller', 'action', '_method', 'prefix', 'plugin'],
  501. array_keys($all[5]->defaults)
  502. );
  503. $this->assertEquals('Articles', $all[5]->defaults['controller']);
  504. $this->assertEquals('deleteAll', $all[5]->defaults['action']);
  505. $this->assertEquals('/api/articles/updateAll', $all[6]->template, 'Explicit path option');
  506. $this->assertEquals(
  507. ['controller', 'action', '_method', 'prefix', 'plugin'],
  508. array_keys($all[6]->defaults)
  509. );
  510. $this->assertEquals('Articles', $all[6]->defaults['controller']);
  511. $this->assertEquals('updateAll', $all[6]->defaults['action']);
  512. }
  513. /**
  514. * Test connecting resources.
  515. *
  516. * @return void
  517. */
  518. public function testResourcesInScope()
  519. {
  520. Router::scope('/api', ['prefix' => 'api'], function ($routes) {
  521. $routes->extensions(['json']);
  522. $routes->resources('Articles');
  523. });
  524. $url = Router::url([
  525. 'prefix' => 'api',
  526. 'controller' => 'Articles',
  527. 'action' => 'edit',
  528. '_method' => 'PUT',
  529. 'id' => 99
  530. ]);
  531. $this->assertEquals('/api/articles/99', $url);
  532. $url = Router::url([
  533. 'prefix' => 'api',
  534. 'controller' => 'Articles',
  535. 'action' => 'edit',
  536. '_method' => 'PUT',
  537. '_ext' => 'json',
  538. 'id' => 99
  539. ]);
  540. $this->assertEquals('/api/articles/99.json', $url);
  541. }
  542. /**
  543. * Test resource parsing.
  544. *
  545. * @return void
  546. */
  547. public function testResourcesParsing()
  548. {
  549. $routes = new RouteBuilder($this->collection, '/');
  550. $routes->resources('Articles');
  551. $_SERVER['REQUEST_METHOD'] = 'GET';
  552. $result = $this->collection->parse('/articles');
  553. $this->assertEquals('Articles', $result['controller']);
  554. $this->assertEquals('index', $result['action']);
  555. $this->assertEquals([], $result['pass']);
  556. $result = $this->collection->parse('/articles/1');
  557. $this->assertEquals('Articles', $result['controller']);
  558. $this->assertEquals('view', $result['action']);
  559. $this->assertEquals([1], $result['pass']);
  560. $_SERVER['REQUEST_METHOD'] = 'POST';
  561. $result = $this->collection->parse('/articles');
  562. $this->assertEquals('Articles', $result['controller']);
  563. $this->assertEquals('add', $result['action']);
  564. $this->assertEquals([], $result['pass']);
  565. $_SERVER['REQUEST_METHOD'] = 'PUT';
  566. $result = $this->collection->parse('/articles/1');
  567. $this->assertEquals('Articles', $result['controller']);
  568. $this->assertEquals('edit', $result['action']);
  569. $this->assertEquals([1], $result['pass']);
  570. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  571. $result = $this->collection->parse('/articles/1');
  572. $this->assertEquals('Articles', $result['controller']);
  573. $this->assertEquals('delete', $result['action']);
  574. $this->assertEquals([1], $result['pass']);
  575. }
  576. /**
  577. * Test the only option of RouteBuilder.
  578. *
  579. * @return void
  580. */
  581. public function testResourcesOnlyString()
  582. {
  583. $routes = new RouteBuilder($this->collection, '/');
  584. $routes->resources('Articles', ['only' => 'index']);
  585. $result = $this->collection->routes();
  586. $this->assertCount(1, $result);
  587. $this->assertEquals('/articles', $result[0]->template);
  588. }
  589. /**
  590. * Test the only option of RouteBuilder.
  591. *
  592. * @return void
  593. */
  594. public function testResourcesOnlyArray()
  595. {
  596. $routes = new RouteBuilder($this->collection, '/');
  597. $routes->resources('Articles', ['only' => ['index', 'delete']]);
  598. $result = $this->collection->routes();
  599. $this->assertCount(2, $result);
  600. $this->assertEquals('/articles', $result[0]->template);
  601. $this->assertEquals('index', $result[0]->defaults['action']);
  602. $this->assertEquals('GET', $result[0]->defaults['_method']);
  603. $this->assertEquals('/articles/:id', $result[1]->template);
  604. $this->assertEquals('delete', $result[1]->defaults['action']);
  605. $this->assertEquals('DELETE', $result[1]->defaults['_method']);
  606. }
  607. /**
  608. * Test the actions option of RouteBuilder.
  609. *
  610. * @return void
  611. */
  612. public function testResourcesActions()
  613. {
  614. $routes = new RouteBuilder($this->collection, '/');
  615. $routes->resources('Articles', [
  616. 'only' => ['index', 'delete'],
  617. 'actions' => ['index' => 'showList']
  618. ]);
  619. $result = $this->collection->routes();
  620. $this->assertCount(2, $result);
  621. $this->assertEquals('/articles', $result[0]->template);
  622. $this->assertEquals('showList', $result[0]->defaults['action']);
  623. $this->assertEquals('/articles/:id', $result[1]->template);
  624. $this->assertEquals('delete', $result[1]->defaults['action']);
  625. }
  626. /**
  627. * Test nesting resources
  628. *
  629. * @return void
  630. */
  631. public function testResourcesNested()
  632. {
  633. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  634. $routes->resources('Articles', function ($routes) {
  635. $this->assertEquals('/api/articles/', $routes->path());
  636. $this->assertEquals(['prefix' => 'api'], $routes->params());
  637. $routes->resources('Comments');
  638. $route = $this->collection->routes()[6];
  639. $this->assertEquals('/api/articles/:article_id/comments', $route->template);
  640. });
  641. }
  642. /**
  643. * Test connecting fallback routes.
  644. *
  645. * @return void
  646. */
  647. public function testFallbacks()
  648. {
  649. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  650. $routes->fallbacks();
  651. $all = $this->collection->routes();
  652. $this->assertEquals('/api/:controller', $all[0]->template);
  653. $this->assertEquals('/api/:controller/:action/*', $all[1]->template);
  654. $this->assertInstanceOf(Route::class, $all[0]);
  655. }
  656. /**
  657. * Test connecting fallback routes with specific route class
  658. *
  659. * @return void
  660. */
  661. public function testFallbacksWithClass()
  662. {
  663. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  664. $routes->fallbacks('InflectedRoute');
  665. $all = $this->collection->routes();
  666. $this->assertEquals('/api/:controller', $all[0]->template);
  667. $this->assertEquals('/api/:controller/:action/*', $all[1]->template);
  668. $this->assertInstanceOf(InflectedRoute::class, $all[0]);
  669. }
  670. /**
  671. * Test connecting fallback routes after setting default route class.
  672. *
  673. * @return void
  674. */
  675. public function testDefaultRouteClassFallbacks()
  676. {
  677. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  678. $routes->routeClass('TestApp\Routing\Route\DashedRoute');
  679. $routes->fallbacks();
  680. $all = $this->collection->routes();
  681. $this->assertInstanceOf('TestApp\Routing\Route\DashedRoute', $all[0]);
  682. }
  683. /**
  684. * Test adding a scope.
  685. *
  686. * @return void
  687. */
  688. public function testScope()
  689. {
  690. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  691. $routes->scope('/v1', ['version' => 1], function ($routes) {
  692. $this->assertEquals('/api/v1', $routes->path());
  693. $this->assertEquals(['prefix' => 'api', 'version' => 1], $routes->params());
  694. });
  695. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  696. $routes->scope('/v1', function ($routes) {
  697. $this->assertEquals('/api/v1', $routes->path());
  698. $this->assertEquals(['prefix' => 'api'], $routes->params());
  699. });
  700. }
  701. /**
  702. * Test using name prefixes.
  703. *
  704. * @return void
  705. */
  706. public function testNamePrefixes()
  707. {
  708. $routes = new RouteBuilder($this->collection, '/api', [], ['namePrefix' => 'api:']);
  709. $routes->scope('/v1', ['version' => 1, '_namePrefix' => 'v1:'], function ($routes) {
  710. $this->assertEquals('api:v1:', $routes->namePrefix());
  711. $routes->connect('/ping', ['controller' => 'Pings'], ['_name' => 'ping']);
  712. $routes->namePrefix('web:');
  713. $routes->connect('/pong', ['controller' => 'Pongs'], ['_name' => 'pong']);
  714. });
  715. $all = $this->collection->named();
  716. $this->assertArrayHasKey('api:v1:ping', $all);
  717. $this->assertArrayHasKey('web:pong', $all);
  718. }
  719. /**
  720. * Test adding middleware to the collection.
  721. *
  722. * @return void
  723. */
  724. public function testRegisterMiddleware()
  725. {
  726. $func = function () {
  727. };
  728. $routes = new RouteBuilder($this->collection, '/api');
  729. $result = $routes->registerMiddleware('test', $func);
  730. $this->assertSame($result, $routes);
  731. $this->assertTrue($this->collection->hasMiddleware('test'));
  732. }
  733. /**
  734. * Test registering invalid middleware
  735. *
  736. * @expectedException \RuntimeException
  737. * @expectedExceptionMessage The 'bad' middleware is not a callable object.
  738. * @return void
  739. */
  740. public function testRegisterMiddlewareString()
  741. {
  742. $routes = new RouteBuilder($this->collection, '/api');
  743. $routes->registerMiddleware('bad', 'strlen');
  744. }
  745. /**
  746. * Test applying middleware to a scope when it doesn't exist
  747. *
  748. * @expectedException \RuntimeException
  749. * @expectedExceptionMessage Cannot apply 'bad' middleware to path '/api'. It has not been registered.
  750. * @return void
  751. */
  752. public function testApplyMiddlewareInvalidName()
  753. {
  754. $routes = new RouteBuilder($this->collection, '/api');
  755. $routes->applyMiddleware('bad');
  756. }
  757. /**
  758. * Test applying middleware to a scope
  759. *
  760. * @return void
  761. */
  762. public function testApplyMiddleware()
  763. {
  764. $func = function () {
  765. };
  766. $routes = new RouteBuilder($this->collection, '/api');
  767. $routes->registerMiddleware('test', $func)
  768. ->registerMiddleware('test2', $func);
  769. $result = $routes->applyMiddleware('test', 'test2');
  770. $this->assertSame($result, $routes);
  771. $this->assertEquals(
  772. [$func, $func],
  773. $this->collection->getMatchingMiddleware('/api/v1/ping')
  774. );
  775. }
  776. /**
  777. * @return array
  778. */
  779. public static function httpMethodProvider()
  780. {
  781. return [
  782. ['GET'],
  783. ['POST'],
  784. ['PUT'],
  785. ['PATCH'],
  786. ['DELETE'],
  787. ['OPTIONS'],
  788. ['HEAD'],
  789. ];
  790. }
  791. /**
  792. * Test that the HTTP method helpers create the right kind of routes.
  793. *
  794. * @dataProvider httpMethodProvider
  795. * @return void
  796. */
  797. public function testHttpMethods($method)
  798. {
  799. $routes = new RouteBuilder($this->collection, '/', [], ['namePrefix' => 'app:']);
  800. $route = $routes->{strtolower($method)}(
  801. '/bookmarks/:id',
  802. ['controller' => 'Bookmarks', 'action' => 'view'],
  803. 'route-name'
  804. );
  805. $this->assertInstanceOf(Route::class, $route, 'Should return a route');
  806. $this->assertSame($method, $route->defaults['_method']);
  807. $this->assertSame('app:route-name', $route->options['_name']);
  808. $this->assertSame('/bookmarks/:id', $route->template);
  809. $this->assertEquals(
  810. ['plugin' => null, 'controller' => 'Bookmarks', 'action' => 'view', '_method' => $method],
  811. $route->defaults
  812. );
  813. }
  814. /**
  815. * Integration test for http method helpers and route fluent method
  816. *
  817. * @return void
  818. */
  819. public function testHttpMethodIntegration()
  820. {
  821. $routes = new RouteBuilder($this->collection, '/');
  822. $routes->scope('/', function ($routes) {
  823. $routes->get('/faq/:page', ['controller' => 'Pages', 'action' => 'faq'], 'faq')
  824. ->setPatterns(['page' => '[a-z0-9_]+'])
  825. ->setHost('docs.example.com');
  826. $routes->post('/articles/:id', ['controller' => 'Articles', 'action' => 'update'], 'article:update')
  827. ->setPatterns(['id' => '[0-9]+'])
  828. ->setPass(['id']);
  829. });
  830. $this->assertCount(2, $this->collection->routes());
  831. $this->assertEquals(['faq', 'article:update'], array_keys($this->collection->named()));
  832. $this->assertNotEmpty($this->collection->parse('/faq/things_you_know'));
  833. $result = $this->collection->parse('/articles/123');
  834. $this->assertEquals(['123'], $result['pass']);
  835. }
  836. /**
  837. * Test loading routes from a missing plugin
  838. *
  839. * @expectedException Cake\Core\Exception\MissingPluginException
  840. * @return void
  841. */
  842. public function testLoadPluginBadPlugin()
  843. {
  844. $routes = new RouteBuilder($this->collection, '/');
  845. $routes->loadPlugin('Nope');
  846. }
  847. /**
  848. * Test loading routes from a missing file
  849. *
  850. * @expectedException InvalidArgumentException
  851. * @expectedExceptionMessage Cannot load routes for the plugin named TestPlugin.
  852. * @return void
  853. */
  854. public function testLoadPluginBadFile()
  855. {
  856. Plugin::load('TestPlugin');
  857. $routes = new RouteBuilder($this->collection, '/');
  858. $routes->loadPlugin('TestPlugin', 'nope.php');
  859. }
  860. /**
  861. * Test loading routes with success
  862. *
  863. * @return void
  864. */
  865. public function testLoadPlugin()
  866. {
  867. Plugin::load('TestPlugin');
  868. $routes = new RouteBuilder($this->collection, '/');
  869. $routes->loadPlugin('TestPlugin');
  870. $this->assertCount(1, $this->collection->routes());
  871. $this->assertNotEmpty($this->collection->parse('/test_plugin'));
  872. }
  873. }