RouteBuilderTest.php 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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 middleware group
  747. *
  748. * @return void
  749. */
  750. public function testMiddlewareGroup()
  751. {
  752. $func = function () {
  753. };
  754. $routes = new RouteBuilder($this->collection, '/api');
  755. $routes->registerMiddleware('test', $func);
  756. $routes->registerMiddleware('test_two', $func);
  757. $result = $routes->middlewareGroup('group', ['test', 'test_two']);
  758. $this->assertSame($result, $routes);
  759. $this->assertTrue($this->collection->hasMiddlewareGroup('group'));
  760. }
  761. /**
  762. * Test overlap between middleware name and group name
  763. *
  764. * @expectedException \RuntimeException
  765. * @expectedExceptionMessage Cannot add middle ware group 'test' . A middleware by this name has already been registered.
  766. * @return void
  767. */
  768. public function testMiddlewareGroupOverlap()
  769. {
  770. $func = function () {
  771. };
  772. $routes = new RouteBuilder($this->collection, '/api');
  773. $routes->registerMiddleware('test', $func);
  774. $result = $routes->middlewareGroup('test', ['test']);
  775. }
  776. /**
  777. * Test applying middleware to a scope when it doesn't exist
  778. *
  779. * @expectedException \RuntimeException
  780. * @expectedExceptionMessage Cannot apply 'bad' middleware or middleware group to path '/api'. It has not been registered.
  781. * @return void
  782. */
  783. public function testApplyMiddlewareInvalidName()
  784. {
  785. $routes = new RouteBuilder($this->collection, '/api');
  786. $routes->applyMiddleware('bad');
  787. }
  788. /**
  789. * Test applying middleware to a scope
  790. *
  791. * @return void
  792. */
  793. public function testApplyMiddleware()
  794. {
  795. $func = function () {
  796. };
  797. $routes = new RouteBuilder($this->collection, '/api');
  798. $routes->registerMiddleware('test', $func)
  799. ->registerMiddleware('test2', $func);
  800. $result = $routes->applyMiddleware('test', 'test2');
  801. $this->assertSame($result, $routes);
  802. $this->assertEquals(
  803. [$func, $func],
  804. $this->collection->getMatchingMiddleware('/api/v1/ping')
  805. );
  806. }
  807. /**
  808. * @return array
  809. */
  810. public static function httpMethodProvider()
  811. {
  812. return [
  813. ['GET'],
  814. ['POST'],
  815. ['PUT'],
  816. ['PATCH'],
  817. ['DELETE'],
  818. ['OPTIONS'],
  819. ['HEAD'],
  820. ];
  821. }
  822. /**
  823. * Test that the HTTP method helpers create the right kind of routes.
  824. *
  825. * @dataProvider httpMethodProvider
  826. * @return void
  827. */
  828. public function testHttpMethods($method)
  829. {
  830. $routes = new RouteBuilder($this->collection, '/', [], ['namePrefix' => 'app:']);
  831. $route = $routes->{strtolower($method)}(
  832. '/bookmarks/:id',
  833. ['controller' => 'Bookmarks', 'action' => 'view'],
  834. 'route-name'
  835. );
  836. $this->assertInstanceOf(Route::class, $route, 'Should return a route');
  837. $this->assertSame($method, $route->defaults['_method']);
  838. $this->assertSame('app:route-name', $route->options['_name']);
  839. $this->assertSame('/bookmarks/:id', $route->template);
  840. $this->assertEquals(
  841. ['plugin' => null, 'controller' => 'Bookmarks', 'action' => 'view', '_method' => $method],
  842. $route->defaults
  843. );
  844. }
  845. /**
  846. * Integration test for http method helpers and route fluent method
  847. *
  848. * @return void
  849. */
  850. public function testHttpMethodIntegration()
  851. {
  852. $routes = new RouteBuilder($this->collection, '/');
  853. $routes->scope('/', function ($routes) {
  854. $routes->get('/faq/:page', ['controller' => 'Pages', 'action' => 'faq'], 'faq')
  855. ->setPatterns(['page' => '[a-z0-9_]+'])
  856. ->setHost('docs.example.com');
  857. $routes->post('/articles/:id', ['controller' => 'Articles', 'action' => 'update'], 'article:update')
  858. ->setPatterns(['id' => '[0-9]+'])
  859. ->setPass(['id']);
  860. });
  861. $this->assertCount(2, $this->collection->routes());
  862. $this->assertEquals(['faq', 'article:update'], array_keys($this->collection->named()));
  863. $this->assertNotEmpty($this->collection->parse('/faq/things_you_know', 'GET'));
  864. $result = $this->collection->parse('/articles/123', 'POST');
  865. $this->assertEquals(['123'], $result['pass']);
  866. }
  867. /**
  868. * Test loading routes from a missing plugin
  869. *
  870. * @expectedException Cake\Core\Exception\MissingPluginException
  871. * @return void
  872. */
  873. public function testLoadPluginBadPlugin()
  874. {
  875. $routes = new RouteBuilder($this->collection, '/');
  876. $routes->loadPlugin('Nope');
  877. }
  878. /**
  879. * Test loading routes from a missing file
  880. *
  881. * @expectedException InvalidArgumentException
  882. * @expectedExceptionMessage Cannot load routes for the plugin named TestPlugin.
  883. * @return void
  884. */
  885. public function testLoadPluginBadFile()
  886. {
  887. Plugin::load('TestPlugin');
  888. $routes = new RouteBuilder($this->collection, '/');
  889. $routes->loadPlugin('TestPlugin', 'nope.php');
  890. }
  891. /**
  892. * Test loading routes with success
  893. *
  894. * @return void
  895. */
  896. public function testLoadPlugin()
  897. {
  898. Plugin::load('TestPlugin');
  899. $routes = new RouteBuilder($this->collection, '/');
  900. $routes->loadPlugin('TestPlugin');
  901. $this->assertCount(1, $this->collection->routes());
  902. $this->assertNotEmpty($this->collection->parse('/test_plugin', 'GET'));
  903. }
  904. }