RouteBuilderTest.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Routing;
  17. use BadMethodCallException;
  18. use Cake\Core\Exception\MissingPluginException;
  19. use Cake\Core\Plugin;
  20. use Cake\Http\ServerRequest;
  21. use Cake\Routing\Route\InflectedRoute;
  22. use Cake\Routing\Route\RedirectRoute;
  23. use Cake\Routing\Route\Route;
  24. use Cake\Routing\RouteBuilder;
  25. use Cake\Routing\RouteCollection;
  26. use Cake\Routing\Router;
  27. use Cake\TestSuite\TestCase;
  28. use InvalidArgumentException;
  29. use PHPUnit\Framework\Attributes\DataProvider;
  30. use TestApp\Routing\Route\DashedRoute;
  31. /**
  32. * RouteBuilder test case
  33. */
  34. class RouteBuilderTest extends TestCase
  35. {
  36. /**
  37. * @var \Cake\Routing\RouteCollection
  38. */
  39. protected $collection;
  40. /**
  41. * Setup method
  42. */
  43. public function setUp(): void
  44. {
  45. parent::setUp();
  46. $this->collection = new RouteCollection();
  47. }
  48. /**
  49. * Teardown method
  50. */
  51. public function tearDown(): void
  52. {
  53. parent::tearDown();
  54. $this->clearPlugins();
  55. }
  56. /**
  57. * Test path()
  58. */
  59. public function testPath(): void
  60. {
  61. $routes = new RouteBuilder($this->collection, '/some/path');
  62. $this->assertSame('/some/path', $routes->path());
  63. $routes = new RouteBuilder($this->collection, '/{book_id}');
  64. $this->assertSame('/', $routes->path());
  65. $routes = new RouteBuilder($this->collection, '/path/{book_id}');
  66. $this->assertSame('/path/', $routes->path());
  67. $routes = new RouteBuilder($this->collection, '/path/book{book_id}');
  68. $this->assertSame('/path/book', $routes->path());
  69. }
  70. /**
  71. * Test params()
  72. */
  73. public function testParams(): void
  74. {
  75. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'Api']);
  76. $this->assertEquals(['prefix' => 'Api'], $routes->params());
  77. }
  78. /**
  79. * Test getting connected routes.
  80. */
  81. public function testRoutes(): void
  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. public function testRouteClass(): void
  95. {
  96. $routes = new RouteBuilder(
  97. $this->collection,
  98. '/l',
  99. [],
  100. ['routeClass' => 'InflectedRoute']
  101. );
  102. $routes->connect('/{controller}', ['action' => 'index']);
  103. $routes->connect('/{controller}/{action}/*');
  104. $all = $this->collection->routes();
  105. $this->assertInstanceOf(InflectedRoute::class, $all[0]);
  106. $this->assertInstanceOf(InflectedRoute::class, $all[1]);
  107. $this->collection = new RouteCollection();
  108. $routes = new RouteBuilder($this->collection, '/l');
  109. $this->assertSame($routes, $routes->setRouteClass(DashedRoute::class));
  110. $this->assertSame(DashedRoute::class, $routes->getRouteClass());
  111. $routes->connect('/{controller}', ['action' => 'index']);
  112. $all = $this->collection->routes();
  113. $this->assertInstanceOf(DashedRoute::class, $all[0]);
  114. }
  115. /**
  116. * Test connecting an instance routes.
  117. */
  118. public function testConnectInstance(): void
  119. {
  120. $routes = new RouteBuilder($this->collection, '/l', ['prefix' => 'Api']);
  121. $route = new Route('/{controller}');
  122. $this->assertSame($route, $routes->connect($route));
  123. $result = $this->collection->routes()[0];
  124. $this->assertSame($route, $result);
  125. }
  126. /**
  127. * Test connecting basic routes.
  128. */
  129. public function testConnectBasic(): void
  130. {
  131. $routes = new RouteBuilder($this->collection, '/l', ['prefix' => 'Api']);
  132. $route = $routes->connect('/{controller}');
  133. $this->assertInstanceOf(Route::class, $route);
  134. $this->assertSame($route, $this->collection->routes()[0]);
  135. $this->assertSame('/l/{controller}', $route->template);
  136. $expected = ['prefix' => 'Api', 'action' => 'index', 'plugin' => null];
  137. $this->assertEquals($expected, $route->defaults);
  138. }
  139. /**
  140. * Test that compiling a route results in an trailing / optional pattern.
  141. */
  142. public function testConnectTrimTrailingSlash(): void
  143. {
  144. $routes = new RouteBuilder($this->collection, '/articles', ['controller' => 'Articles']);
  145. $routes->connect('/', ['action' => 'index']);
  146. $expected = [
  147. 'plugin' => null,
  148. 'controller' => 'Articles',
  149. 'action' => 'index',
  150. 'pass' => [],
  151. '_matchedRoute' => '/articles',
  152. ];
  153. $result = $this->collection->parseRequest(new ServerRequest(['url' => '/articles']));
  154. unset($result['_route']);
  155. $this->assertEquals($expected, $result);
  156. $result = $this->collection->parseRequest(new ServerRequest(['url' => '/articles/']));
  157. unset($result['_route']);
  158. $this->assertEquals($expected, $result);
  159. }
  160. /**
  161. * Test connect() with short string syntax
  162. */
  163. public function testConnectShortStringInvalid(): void
  164. {
  165. $this->expectException(InvalidArgumentException::class);
  166. $routes = new RouteBuilder($this->collection, '/');
  167. $routes->connect('/my-articles/view', 'Articles:no');
  168. }
  169. /**
  170. * Test connect() with short string syntax
  171. */
  172. public function testConnectShortString(): void
  173. {
  174. $routes = new RouteBuilder($this->collection, '/');
  175. $routes->connect('/my-articles/view', 'Articles::view');
  176. $expected = [
  177. 'pass' => [],
  178. 'controller' => 'Articles',
  179. 'action' => 'view',
  180. 'plugin' => null,
  181. '_matchedRoute' => '/my-articles/view',
  182. ];
  183. $result = $this->collection->parseRequest(new ServerRequest(['url' => '/my-articles/view']));
  184. unset($result['_route']);
  185. $this->assertEquals($expected, $result);
  186. $url = $expected['_matchedRoute'];
  187. unset($expected['_matchedRoute']);
  188. $this->assertSame($url, '/' . $this->collection->match($expected, []));
  189. }
  190. /**
  191. * Test connect() with short string syntax
  192. */
  193. public function testConnectShortStringPrefix(): void
  194. {
  195. $routes = new RouteBuilder($this->collection, '/');
  196. $routes->connect('/admin/bookmarks', 'Admin/Bookmarks::index');
  197. $expected = [
  198. 'pass' => [],
  199. 'plugin' => null,
  200. 'prefix' => 'Admin',
  201. 'controller' => 'Bookmarks',
  202. 'action' => 'index',
  203. '_matchedRoute' => '/admin/bookmarks',
  204. ];
  205. $result = $this->collection->parseRequest(new ServerRequest(['url' => '/admin/bookmarks']));
  206. unset($result['_route']);
  207. $this->assertEquals($expected, $result);
  208. $url = $expected['_matchedRoute'];
  209. unset($expected['_matchedRoute']);
  210. $this->assertSame($url, '/' . $this->collection->match($expected, []));
  211. }
  212. /**
  213. * Test connect() with short string syntax
  214. */
  215. public function testConnectShortStringPlugin(): void
  216. {
  217. $routes = new RouteBuilder($this->collection, '/');
  218. $routes->connect('/blog/articles/view', 'Blog.Articles::view');
  219. $expected = [
  220. 'pass' => [],
  221. 'plugin' => 'Blog',
  222. 'controller' => 'Articles',
  223. 'action' => 'view',
  224. '_matchedRoute' => '/blog/articles/view',
  225. ];
  226. $result = $this->collection->parseRequest(new ServerRequest(['url' => '/blog/articles/view']));
  227. unset($result['_route']);
  228. $this->assertEquals($expected, $result);
  229. $url = $expected['_matchedRoute'];
  230. unset($expected['_matchedRoute']);
  231. $this->assertSame($url, '/' . $this->collection->match($expected, []));
  232. }
  233. /**
  234. * Test connect() with short string syntax
  235. */
  236. public function testConnectShortStringPluginPrefix(): void
  237. {
  238. $routes = new RouteBuilder($this->collection, '/');
  239. $routes->connect('/admin/blog/articles/view', 'Vendor/Blog.Management/Admin/Articles::view');
  240. $expected = [
  241. 'pass' => [],
  242. 'plugin' => 'Vendor/Blog',
  243. 'prefix' => 'Management/Admin',
  244. 'controller' => 'Articles',
  245. 'action' => 'view',
  246. '_matchedRoute' => '/admin/blog/articles/view',
  247. ];
  248. $result = $this->collection->parseRequest(new ServerRequest(['url' => '/admin/blog/articles/view']));
  249. unset($result['_route']);
  250. $this->assertEquals($expected, $result);
  251. $url = $expected['_matchedRoute'];
  252. unset($expected['_matchedRoute']);
  253. $this->assertSame($url, '/' . $this->collection->match($expected, []));
  254. }
  255. /**
  256. * Test if a route name already exist
  257. */
  258. public function testNameExists(): void
  259. {
  260. $routes = new RouteBuilder($this->collection, '/l', ['prefix' => 'Api']);
  261. $this->assertFalse($routes->nameExists('myRouteName'));
  262. $routes->connect('myRouteUrl', ['action' => 'index'], ['_name' => 'myRouteName']);
  263. $this->assertTrue($routes->nameExists('myRouteName'));
  264. }
  265. /**
  266. * Test setExtensions() and getExtensions().
  267. */
  268. public function testExtensions(): void
  269. {
  270. $routes = new RouteBuilder($this->collection, '/l');
  271. $this->assertSame($routes, $routes->setExtensions(['html']));
  272. $this->assertSame(['html'], $routes->getExtensions());
  273. }
  274. /**
  275. * Test extensions being connected to routes.
  276. */
  277. public function testConnectExtensions(): void
  278. {
  279. $routes = new RouteBuilder(
  280. $this->collection,
  281. '/l',
  282. [],
  283. ['extensions' => ['json']]
  284. );
  285. $this->assertEquals(['json'], $routes->getExtensions());
  286. $routes->connect('/{controller}');
  287. $route = $this->collection->routes()[0];
  288. $this->assertEquals(['json'], $route->options['_ext']);
  289. $routes->setExtensions(['xml', 'json']);
  290. $routes->connect('/{controller}/{action}');
  291. $new = $this->collection->routes()[1];
  292. $this->assertEquals(['json'], $route->options['_ext']);
  293. $this->assertEquals(['xml', 'json'], $new->options['_ext']);
  294. }
  295. /**
  296. * Test adding additional extensions will be merged with current.
  297. */
  298. public function testConnectExtensionsAdd(): void
  299. {
  300. $routes = new RouteBuilder(
  301. $this->collection,
  302. '/l',
  303. [],
  304. ['extensions' => ['json']]
  305. );
  306. $this->assertEquals(['json'], $routes->getExtensions());
  307. $routes->addExtensions(['xml']);
  308. $this->assertEquals(['json', 'xml'], $routes->getExtensions());
  309. $routes->addExtensions('csv');
  310. $this->assertEquals(['json', 'xml', 'csv'], $routes->getExtensions());
  311. }
  312. /**
  313. * test that setExtensions() accepts a string.
  314. */
  315. public function testExtensionsString(): void
  316. {
  317. $routes = new RouteBuilder($this->collection, '/l');
  318. $routes->setExtensions('json');
  319. $this->assertEquals(['json'], $routes->getExtensions());
  320. }
  321. /**
  322. * Test conflicting parameters raises an exception.
  323. */
  324. public function testConnectConflictingParameters(): void
  325. {
  326. $this->expectException(BadMethodCallException::class);
  327. $this->expectExceptionMessage('You cannot define routes that conflict with the scope.');
  328. $routes = new RouteBuilder($this->collection, '/admin', ['plugin' => 'TestPlugin']);
  329. $routes->connect('/', ['plugin' => 'TestPlugin2', 'controller' => 'Dashboard', 'action' => 'view']);
  330. }
  331. /**
  332. * Test connecting redirect routes.
  333. */
  334. public function testRedirect(): void
  335. {
  336. $routes = new RouteBuilder($this->collection, '/');
  337. $routes->redirect('/p/{id}', ['controller' => 'Posts', 'action' => 'view'], ['status' => 301]);
  338. $route = $this->collection->routes()[0];
  339. $this->assertInstanceOf(RedirectRoute::class, $route);
  340. $routes->redirect('/old', '/forums', ['status' => 301]);
  341. $route = $this->collection->routes()[1];
  342. $this->assertInstanceOf(RedirectRoute::class, $route);
  343. $this->assertSame('/forums', $route->redirect[0]);
  344. $route = $routes->redirect('/old', '/forums');
  345. $this->assertInstanceOf(RedirectRoute::class, $route);
  346. $this->assertSame($route, $this->collection->routes()[2]);
  347. }
  348. /**
  349. * Test using a custom route class for redirect routes.
  350. */
  351. public function testRedirectWithCustomRouteClass(): void
  352. {
  353. $routes = new RouteBuilder($this->collection, '/');
  354. $routes->redirect('/old', '/forums', ['status' => 301, 'routeClass' => 'InflectedRoute']);
  355. $route = $this->collection->routes()[0];
  356. $this->assertInstanceOf(InflectedRoute::class, $route);
  357. }
  358. /**
  359. * Test creating sub-scopes with prefix()
  360. */
  361. public function testPrefix(): void
  362. {
  363. $routes = new RouteBuilder($this->collection, '/path', ['key' => 'value']);
  364. $res = $routes->prefix('admin', ['param' => 'value'], function (RouteBuilder $r): void {
  365. $this->assertInstanceOf(RouteBuilder::class, $r);
  366. $this->assertCount(0, $this->collection->routes());
  367. $this->assertSame('/path/admin', $r->path());
  368. $this->assertEquals(['prefix' => 'Admin', 'key' => 'value', 'param' => 'value'], $r->params());
  369. });
  370. $this->assertSame($routes, $res);
  371. }
  372. /**
  373. * Test creating sub-scopes with prefix()
  374. */
  375. public function testPrefixWithNoParams(): void
  376. {
  377. $routes = new RouteBuilder($this->collection, '/path', ['key' => 'value']);
  378. $res = $routes->prefix('admin', function (RouteBuilder $r): void {
  379. $this->assertInstanceOf(RouteBuilder::class, $r);
  380. $this->assertCount(0, $this->collection->routes());
  381. $this->assertSame('/path/admin', $r->path());
  382. $this->assertEquals(['prefix' => 'Admin', 'key' => 'value'], $r->params());
  383. });
  384. $this->assertSame($routes, $res);
  385. }
  386. /**
  387. * Test creating sub-scopes with prefix()
  388. */
  389. public function testNestedPrefix(): void
  390. {
  391. $routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'Admin']);
  392. $res = $routes->prefix('api', ['_namePrefix' => 'api:'], function (RouteBuilder $r): void {
  393. $this->assertSame('/admin/api', $r->path());
  394. $this->assertEquals(['prefix' => 'Admin/Api'], $r->params());
  395. $this->assertSame('api:', $r->namePrefix());
  396. });
  397. $this->assertSame($routes, $res);
  398. }
  399. /**
  400. * Test creating sub-scopes with prefix()
  401. */
  402. public function testPathWithDotInPrefix(): void
  403. {
  404. $routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'Admin']);
  405. $res = $routes->prefix('Api', function (RouteBuilder $r): void {
  406. $r->prefix('v10', ['path' => '/v1.0'], function (RouteBuilder $r2): void {
  407. $this->assertSame('/admin/api/v1.0', $r2->path());
  408. $this->assertEquals(['prefix' => 'Admin/Api/V10'], $r2->params());
  409. $r2->prefix('b1', ['path' => '/beta.1'], function (RouteBuilder $r3): void {
  410. $this->assertSame('/admin/api/v1.0/beta.1', $r3->path());
  411. $this->assertEquals(['prefix' => 'Admin/Api/V10/B1'], $r3->params());
  412. });
  413. });
  414. });
  415. $this->assertSame($routes, $res);
  416. }
  417. /**
  418. * Test creating sub-scopes with plugin()
  419. */
  420. public function testPlugin(): void
  421. {
  422. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  423. $res = $routes->plugin('Contacts', function (RouteBuilder $r): void {
  424. $this->assertSame('/b/contacts', $r->path());
  425. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  426. $r->connect('/{controller}');
  427. $route = $this->collection->routes()[0];
  428. $this->assertEquals(
  429. ['key' => 'value', 'plugin' => 'Contacts', 'action' => 'index'],
  430. $route->defaults
  431. );
  432. });
  433. $this->assertSame($routes, $res);
  434. }
  435. /**
  436. * Test creating sub-scopes with plugin() + path option
  437. */
  438. public function testPluginPathOption(): void
  439. {
  440. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  441. $routes->plugin('Contacts', ['path' => '/people'], function (RouteBuilder $r): void {
  442. $this->assertSame('/b/people', $r->path());
  443. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  444. });
  445. }
  446. /**
  447. * Test creating sub-scopes with plugin() + namePrefix option
  448. */
  449. public function testPluginNamePrefix(): void
  450. {
  451. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  452. $routes->plugin('Contacts', ['_namePrefix' => 'contacts.'], function (RouteBuilder $r): void {
  453. $this->assertEquals('contacts.', $r->namePrefix());
  454. });
  455. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  456. $routes->namePrefix('default.');
  457. $routes->plugin('Blog', ['_namePrefix' => 'blog.'], function (RouteBuilder $r): void {
  458. $this->assertEquals('default.blog.', $r->namePrefix(), 'Should combine nameprefix');
  459. });
  460. }
  461. /**
  462. * Test connecting resources.
  463. */
  464. public function testResources(): void
  465. {
  466. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'Api']);
  467. $routes->resources('Articles', ['_ext' => 'json']);
  468. $all = $this->collection->routes();
  469. $this->assertCount(5, $all);
  470. $this->assertSame('/api/articles', $all[4]->template);
  471. $this->assertEquals(
  472. ['controller', 'action', '_method', 'prefix', 'plugin'],
  473. array_keys($all[0]->defaults)
  474. );
  475. $this->assertSame('json', $all[0]->options['_ext']);
  476. $this->assertSame('Articles', $all[0]->defaults['controller']);
  477. }
  478. /**
  479. * Test connecting resources with a path
  480. */
  481. public function testResourcesPathOption(): void
  482. {
  483. $routes = new RouteBuilder($this->collection, '/api');
  484. $routes->resources('Articles', ['path' => 'posts'], function (RouteBuilder $routes): void {
  485. $routes->resources('Comments');
  486. });
  487. $all = $this->collection->routes();
  488. $this->assertSame('Articles', $all[8]->defaults['controller']);
  489. $this->assertSame('/api/posts', $all[8]->template);
  490. $this->assertSame('/api/posts/{id}', $all[1]->template);
  491. $this->assertSame(
  492. '/api/posts/{article_id}/comments',
  493. $all[4]->template,
  494. 'parameter name should reflect resource name'
  495. );
  496. }
  497. /**
  498. * Test connecting resources with a prefix
  499. */
  500. public function testResourcesPrefix(): void
  501. {
  502. $routes = new RouteBuilder($this->collection, '/api');
  503. $routes->resources('Articles', ['prefix' => 'Rest']);
  504. $all = $this->collection->routes();
  505. $this->assertSame('Rest', $all[0]->defaults['prefix']);
  506. }
  507. /**
  508. * Test that resource prefixes work within a prefixed scope.
  509. */
  510. public function testResourcesNestedPrefix(): void
  511. {
  512. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'Api']);
  513. $routes->resources('Articles', ['prefix' => 'Rest']);
  514. $all = $this->collection->routes();
  515. $this->assertCount(5, $all);
  516. $this->assertSame('/api/articles', $all[4]->template);
  517. foreach ($all as $route) {
  518. $this->assertSame('Api/Rest', $route->defaults['prefix']);
  519. $this->assertSame('Articles', $route->defaults['controller']);
  520. }
  521. }
  522. /**
  523. * Test connecting resources with the inflection option
  524. */
  525. public function testResourcesInflection(): void
  526. {
  527. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'Api']);
  528. $routes->resources('BlogPosts', ['_ext' => 'json', 'inflect' => 'dasherize']);
  529. $all = $this->collection->routes();
  530. $this->assertCount(5, $all);
  531. $this->assertSame('/api/blog-posts', $all[4]->template);
  532. $this->assertEquals(
  533. ['controller', 'action', '_method', 'prefix', 'plugin'],
  534. array_keys($all[0]->defaults)
  535. );
  536. $this->assertSame('BlogPosts', $all[0]->defaults['controller']);
  537. }
  538. /**
  539. * Test connecting nested resources with the inflection option
  540. */
  541. public function testResourcesNestedInflection(): void
  542. {
  543. $routes = new RouteBuilder($this->collection, '/api');
  544. $routes->resources(
  545. 'NetworkObjects',
  546. ['inflect' => 'dasherize'],
  547. function (RouteBuilder $routes): void {
  548. $routes->resources('Attributes');
  549. }
  550. );
  551. $all = $this->collection->routes();
  552. $this->assertCount(10, $all);
  553. $this->assertSame('/api/network-objects', $all[8]->template);
  554. $this->assertSame('/api/network-objects/{id}', $all[2]->template);
  555. $this->assertSame('/api/network-objects/{network_object_id}/attributes', $all[4]->template);
  556. }
  557. /**
  558. * Test connecting resources with additional mappings
  559. */
  560. public function testResourcesMappings(): void
  561. {
  562. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'Api']);
  563. $routes->resources('Articles', [
  564. '_ext' => 'json',
  565. 'map' => [
  566. 'delete_all' => ['action' => 'deleteAll', 'method' => 'DELETE'],
  567. 'update_many' => ['action' => 'updateAll', 'method' => 'DELETE', 'path' => '/updateAll'],
  568. ],
  569. ]);
  570. $all = $this->collection->routes();
  571. $this->assertCount(7, $all);
  572. $this->assertSame('/api/articles/delete_all', $all[1]->template, 'Path defaults to key name.');
  573. $this->assertEquals(
  574. ['controller', 'action', '_method', 'prefix', 'plugin'],
  575. array_keys($all[5]->defaults)
  576. );
  577. $this->assertSame('Articles', $all[5]->defaults['controller']);
  578. $this->assertSame('deleteAll', $all[1]->defaults['action']);
  579. $this->assertSame('/api/articles/updateAll', $all[0]->template, 'Explicit path option');
  580. $this->assertEquals(
  581. ['controller', 'action', '_method', 'prefix', 'plugin'],
  582. array_keys($all[6]->defaults)
  583. );
  584. $this->assertSame('Articles', $all[6]->defaults['controller']);
  585. $this->assertSame('updateAll', $all[0]->defaults['action']);
  586. }
  587. /**
  588. * Test connecting resources with restricted mappings.
  589. */
  590. public function testResourcesWithMapOnly(): void
  591. {
  592. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'Api']);
  593. $routes->resources('Articles', [
  594. 'map' => [
  595. 'conditions' => ['action' => 'conditions', 'method' => 'DeLeTe'],
  596. ],
  597. 'only' => ['conditions'],
  598. ]);
  599. $all = $this->collection->routes();
  600. $this->assertCount(1, $all);
  601. $this->assertSame('DELETE', $all[0]->defaults['_method'], 'method should be normalized.');
  602. $this->assertSame('Articles', $all[0]->defaults['controller']);
  603. $this->assertSame('conditions', $all[0]->defaults['action']);
  604. $result = $this->collection->parseRequest(new ServerRequest([
  605. 'url' => '/api/articles/conditions',
  606. 'environment' => [
  607. 'REQUEST_METHOD' => 'DELETE',
  608. ],
  609. ]));
  610. $this->assertNotNull($result);
  611. }
  612. /**
  613. * Test connecting resources.
  614. */
  615. public function testResourcesInScope(): void
  616. {
  617. $builder = Router::createRouteBuilder('/');
  618. $builder->scope('/api', ['prefix' => 'Api'], function (RouteBuilder $routes): void {
  619. $routes->setExtensions(['json']);
  620. $routes->resources('Articles');
  621. });
  622. $url = Router::url([
  623. 'prefix' => 'Api',
  624. 'controller' => 'Articles',
  625. 'action' => 'edit',
  626. '_method' => 'PUT',
  627. 'id' => '99',
  628. ]);
  629. $this->assertSame('/api/articles/99', $url);
  630. $url = Router::url([
  631. 'prefix' => 'Api',
  632. 'controller' => 'Articles',
  633. 'action' => 'edit',
  634. '_method' => 'PUT',
  635. '_ext' => 'json',
  636. 'id' => '99',
  637. ]);
  638. $this->assertSame('/api/articles/99.json', $url);
  639. }
  640. /**
  641. * Test resource parsing.
  642. */
  643. public function testResourcesParsing(): void
  644. {
  645. $routes = new RouteBuilder($this->collection, '/');
  646. $routes->resources('Articles');
  647. $result = $this->collection->parseRequest(new ServerRequest([
  648. 'url' => '/articles',
  649. 'environment' => [
  650. 'REQUEST_METHOD' => 'GET',
  651. ],
  652. ]));
  653. $this->assertSame('Articles', $result['controller']);
  654. $this->assertSame('index', $result['action']);
  655. $this->assertEquals([], $result['pass']);
  656. $result = $this->collection->parseRequest(new ServerRequest([
  657. 'url' => '/articles/1',
  658. 'environment' => [
  659. 'REQUEST_METHOD' => 'GET',
  660. ],
  661. ]));
  662. $this->assertSame('Articles', $result['controller']);
  663. $this->assertSame('view', $result['action']);
  664. $this->assertEquals([1], $result['pass']);
  665. $result = $this->collection->parseRequest(new ServerRequest([
  666. 'url' => '/articles',
  667. 'environment' => [
  668. 'REQUEST_METHOD' => 'POST',
  669. ],
  670. ]));
  671. $this->assertSame('Articles', $result['controller']);
  672. $this->assertSame('add', $result['action']);
  673. $this->assertEquals([], $result['pass']);
  674. $result = $this->collection->parseRequest(new ServerRequest([
  675. 'url' => '/articles/1',
  676. 'environment' => [
  677. 'REQUEST_METHOD' => 'PUT',
  678. ],
  679. ]));
  680. $this->assertSame('Articles', $result['controller']);
  681. $this->assertSame('edit', $result['action']);
  682. $this->assertEquals([1], $result['pass']);
  683. $result = $this->collection->parseRequest(new ServerRequest([
  684. 'url' => '/articles/1',
  685. 'environment' => [
  686. 'REQUEST_METHOD' => 'DELETE',
  687. ],
  688. ]));
  689. $this->assertSame('Articles', $result['controller']);
  690. $this->assertSame('delete', $result['action']);
  691. $this->assertEquals([1], $result['pass']);
  692. }
  693. /**
  694. * Test the only option of RouteBuilder.
  695. */
  696. public function testResourcesOnlyString(): void
  697. {
  698. $routes = new RouteBuilder($this->collection, '/');
  699. $routes->resources('Articles', ['only' => 'index']);
  700. $result = $this->collection->routes();
  701. $this->assertCount(1, $result);
  702. $this->assertSame('/articles', $result[0]->template);
  703. }
  704. /**
  705. * Test the only option of RouteBuilder.
  706. */
  707. public function testResourcesOnlyArray(): void
  708. {
  709. $routes = new RouteBuilder($this->collection, '/');
  710. $routes->resources('Articles', ['only' => ['index', 'delete']]);
  711. $result = $this->collection->routes();
  712. $this->assertCount(2, $result);
  713. $this->assertSame('/articles', $result[1]->template);
  714. $this->assertSame('index', $result[1]->defaults['action']);
  715. $this->assertSame('GET', $result[1]->defaults['_method']);
  716. $this->assertSame('/articles/{id}', $result[0]->template);
  717. $this->assertSame('delete', $result[0]->defaults['action']);
  718. $this->assertSame('DELETE', $result[0]->defaults['_method']);
  719. }
  720. /**
  721. * Test the actions option of RouteBuilder.
  722. */
  723. public function testResourcesActions(): void
  724. {
  725. $routes = new RouteBuilder($this->collection, '/');
  726. $routes->resources('Articles', [
  727. 'only' => ['index', 'delete'],
  728. 'actions' => ['index' => 'showList'],
  729. ]);
  730. $result = $this->collection->routes();
  731. $this->assertCount(2, $result);
  732. $this->assertSame('/articles', $result[1]->template);
  733. $this->assertSame('showList', $result[1]->defaults['action']);
  734. $this->assertSame('/articles/{id}', $result[0]->template);
  735. $this->assertSame('delete', $result[0]->defaults['action']);
  736. }
  737. /**
  738. * Test nesting resources
  739. */
  740. public function testResourcesNested(): void
  741. {
  742. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'Api']);
  743. $routes->resources('Articles', function (RouteBuilder $routes): void {
  744. $this->assertSame('/api/articles/', $routes->path());
  745. $this->assertEquals(['prefix' => 'Api'], $routes->params());
  746. $routes->resources('Comments');
  747. $route = $this->collection->routes()[3];
  748. $this->assertSame('/api/articles/{article_id}/comments', $route->template);
  749. });
  750. }
  751. /**
  752. * Test connecting fallback routes.
  753. */
  754. public function testFallbacks(): void
  755. {
  756. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'Api']);
  757. $routes->fallbacks();
  758. $all = $this->collection->routes();
  759. $this->assertSame('/api/{controller}', $all[0]->template);
  760. $this->assertSame('/api/{controller}/{action}/*', $all[1]->template);
  761. $this->assertInstanceOf(Route::class, $all[0]);
  762. }
  763. /**
  764. * Test connecting fallback routes with specific route class
  765. */
  766. public function testFallbacksWithClass(): void
  767. {
  768. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'Api']);
  769. $routes->fallbacks('InflectedRoute');
  770. $all = $this->collection->routes();
  771. $this->assertSame('/api/{controller}', $all[0]->template);
  772. $this->assertSame('/api/{controller}/{action}/*', $all[1]->template);
  773. $this->assertInstanceOf(InflectedRoute::class, $all[0]);
  774. }
  775. /**
  776. * Test connecting fallback routes after setting default route class.
  777. */
  778. public function testDefaultRouteClassFallbacks(): void
  779. {
  780. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'Api']);
  781. $routes->setRouteClass(DashedRoute::class);
  782. $routes->fallbacks();
  783. $all = $this->collection->routes();
  784. $this->assertInstanceOf(DashedRoute::class, $all[0]);
  785. }
  786. /**
  787. * Test adding a scope.
  788. */
  789. public function testScope(): void
  790. {
  791. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'Api']);
  792. $routes->scope('/v1', ['version' => 1], function (RouteBuilder $routes): void {
  793. $this->assertSame('/api/v1', $routes->path());
  794. $this->assertEquals(['prefix' => 'Api', 'version' => 1], $routes->params());
  795. });
  796. }
  797. /**
  798. * Test adding a scope with action in the scope
  799. */
  800. public function testScopeWithAction(): void
  801. {
  802. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'Api']);
  803. $routes->scope('/prices', ['controller' => 'Prices', 'action' => 'view'], function (RouteBuilder $routes): void {
  804. $routes->connect('/shared', ['shared' => true]);
  805. $routes->get('/exclusive', ['exclusive' => true]);
  806. });
  807. $all = $this->collection->routes();
  808. $this->assertCount(2, $all);
  809. $this->assertSame('view', $all[0]->defaults['action']);
  810. $this->assertArrayHasKey('shared', $all[0]->defaults);
  811. $this->assertSame('view', $all[1]->defaults['action']);
  812. $this->assertArrayHasKey('exclusive', $all[1]->defaults);
  813. }
  814. /**
  815. * Test that exception is thrown if callback is not a valid callable.
  816. */
  817. public function testScopeException(): void
  818. {
  819. $this->expectException(InvalidArgumentException::class);
  820. $this->expectExceptionMessage('Need a valid Closure to connect routes.');
  821. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'Api']);
  822. $routes->scope('/v1', ['fail'], null);
  823. }
  824. /**
  825. * Test that nested scopes inherit middleware.
  826. */
  827. public function testScopeInheritMiddleware(): void
  828. {
  829. $routes = new RouteBuilder(
  830. $this->collection,
  831. '/api',
  832. ['prefix' => 'Api'],
  833. ['middleware' => ['auth']]
  834. );
  835. $routes->scope('/v1', function (RouteBuilder $routes): void {
  836. $this->assertSame(['auth'], $routes->getMiddleware(), 'Should inherit middleware');
  837. $this->assertSame('/api/v1', $routes->path());
  838. $this->assertEquals(['prefix' => 'Api'], $routes->params());
  839. });
  840. }
  841. /**
  842. * Test using name prefixes.
  843. */
  844. public function testNamePrefixes(): void
  845. {
  846. $routes = new RouteBuilder($this->collection, '/api', [], ['namePrefix' => 'api:']);
  847. $routes->scope('/v1', ['version' => 1, '_namePrefix' => 'v1:'], function (RouteBuilder $routes): void {
  848. $this->assertSame('api:v1:', $routes->namePrefix());
  849. $routes->connect('/ping', ['controller' => 'Pings'], ['_name' => 'ping']);
  850. $routes->namePrefix('web:');
  851. $routes->connect('/pong', ['controller' => 'Pongs'], ['_name' => 'pong']);
  852. });
  853. $all = $this->collection->named();
  854. $this->assertArrayHasKey('api:v1:ping', $all);
  855. $this->assertArrayHasKey('web:pong', $all);
  856. }
  857. /**
  858. * Test adding middleware to the collection.
  859. */
  860. public function testRegisterMiddleware(): void
  861. {
  862. $func = function (): void {
  863. };
  864. $routes = new RouteBuilder($this->collection, '/api');
  865. $result = $routes->registerMiddleware('test', $func);
  866. $this->assertSame($result, $routes);
  867. $this->assertTrue($this->collection->hasMiddleware('test'));
  868. $this->assertTrue($this->collection->middlewareExists('test'));
  869. }
  870. /**
  871. * Test middleware group
  872. */
  873. public function testMiddlewareGroup(): void
  874. {
  875. $func = function (): void {
  876. };
  877. $routes = new RouteBuilder($this->collection, '/api');
  878. $routes->registerMiddleware('test', $func);
  879. $routes->registerMiddleware('test_two', $func);
  880. $result = $routes->middlewareGroup('group', ['test', 'test_two']);
  881. $this->assertSame($result, $routes);
  882. $this->assertTrue($this->collection->hasMiddlewareGroup('group'));
  883. $this->assertTrue($this->collection->middlewareExists('group'));
  884. }
  885. /**
  886. * Test overlap between middleware name and group name
  887. */
  888. public function testMiddlewareGroupOverlap(): void
  889. {
  890. $this->expectException(InvalidArgumentException::class);
  891. $this->expectExceptionMessage("Cannot add middleware group 'test'. A middleware by this name has already been registered.");
  892. $func = function (): void {
  893. };
  894. $routes = new RouteBuilder($this->collection, '/api');
  895. $routes->registerMiddleware('test', $func);
  896. $routes->middlewareGroup('test', ['test']);
  897. }
  898. /**
  899. * Test applying middleware to a scope when it doesn't exist
  900. */
  901. public function testApplyMiddlewareInvalidName(): void
  902. {
  903. $this->expectException(InvalidArgumentException::class);
  904. $this->expectExceptionMessage('Cannot apply `bad` middleware or middleware group. Use `registerMiddleware()` to register middleware');
  905. $routes = new RouteBuilder($this->collection, '/api');
  906. $routes->applyMiddleware('bad');
  907. }
  908. /**
  909. * Test applying middleware to a scope
  910. */
  911. public function testApplyMiddleware(): void
  912. {
  913. $func = function (): void {
  914. };
  915. $routes = new RouteBuilder($this->collection, '/api');
  916. $routes->registerMiddleware('test', $func)
  917. ->registerMiddleware('test2', $func);
  918. $result = $routes->applyMiddleware('test', 'test2');
  919. $this->assertSame($result, $routes);
  920. }
  921. /**
  922. * Test that applyMiddleware() merges with previous data.
  923. */
  924. public function testApplyMiddlewareMerges(): void
  925. {
  926. $func = function (): void {
  927. };
  928. $routes = new RouteBuilder($this->collection, '/api');
  929. $routes->registerMiddleware('test', $func)
  930. ->registerMiddleware('test2', $func);
  931. $routes->applyMiddleware('test');
  932. $routes->applyMiddleware('test2');
  933. $this->assertSame(['test', 'test2'], $routes->getMiddleware());
  934. }
  935. /**
  936. * Test that applyMiddleware() uses unique middleware set
  937. */
  938. public function testApplyMiddlewareUnique(): void
  939. {
  940. $func = function (): void {
  941. };
  942. $routes = new RouteBuilder($this->collection, '/api');
  943. $routes->registerMiddleware('test', $func)
  944. ->registerMiddleware('test2', $func);
  945. $routes->applyMiddleware('test', 'test2');
  946. $routes->applyMiddleware('test2', 'test');
  947. $this->assertEquals(['test', 'test2'], $routes->getMiddleware());
  948. }
  949. /**
  950. * Test applying middleware results in middleware attached to the route.
  951. */
  952. public function testApplyMiddlewareAttachToRoutes(): void
  953. {
  954. $func = function (): void {
  955. };
  956. $routes = new RouteBuilder($this->collection, '/api');
  957. $routes->registerMiddleware('test', $func)
  958. ->registerMiddleware('test2', $func);
  959. $routes->applyMiddleware('test', 'test2');
  960. $route = $routes->get('/docs', ['controller' => 'Docs']);
  961. $this->assertSame(['test', 'test2'], $route->getMiddleware());
  962. }
  963. /**
  964. * @return array
  965. */
  966. public static function httpMethodProvider(): array
  967. {
  968. return [
  969. ['GET'],
  970. ['POST'],
  971. ['PUT'],
  972. ['PATCH'],
  973. ['DELETE'],
  974. ['OPTIONS'],
  975. ['HEAD'],
  976. ];
  977. }
  978. /**
  979. * Test that the HTTP method helpers create the right kind of routes.
  980. */
  981. #[DataProvider('httpMethodProvider')]
  982. public function testHttpMethods(string $method): void
  983. {
  984. $routes = new RouteBuilder($this->collection, '/', [], ['namePrefix' => 'app:']);
  985. $route = $routes->{strtolower($method)}(
  986. '/bookmarks/{id}',
  987. ['controller' => 'Bookmarks', 'action' => 'view'],
  988. 'route-name'
  989. );
  990. $this->assertInstanceOf(Route::class, $route, 'Should return a route');
  991. $this->assertSame($method, $route->defaults['_method']);
  992. $this->assertSame('app:route-name', $route->options['_name']);
  993. $this->assertSame('/bookmarks/{id}', $route->template);
  994. $this->assertEquals(
  995. ['plugin' => null, 'controller' => 'Bookmarks', 'action' => 'view', '_method' => $method],
  996. $route->defaults
  997. );
  998. }
  999. /**
  1000. * Test that the HTTP method helpers create the right kind of routes.
  1001. */
  1002. #[DataProvider('httpMethodProvider')]
  1003. public function testHttpMethodsStringTarget(string $method): void
  1004. {
  1005. $routes = new RouteBuilder($this->collection, '/', [], ['namePrefix' => 'app:']);
  1006. $route = $routes->{strtolower($method)}(
  1007. '/bookmarks/{id}',
  1008. 'Bookmarks::view',
  1009. 'route-name'
  1010. );
  1011. $this->assertInstanceOf(Route::class, $route, 'Should return a route');
  1012. $this->assertSame($method, $route->defaults['_method']);
  1013. $this->assertSame('app:route-name', $route->options['_name']);
  1014. $this->assertSame('/bookmarks/{id}', $route->template);
  1015. $this->assertEquals(
  1016. ['plugin' => null, 'controller' => 'Bookmarks', 'action' => 'view', '_method' => $method],
  1017. $route->defaults
  1018. );
  1019. }
  1020. /**
  1021. * Integration test for http method helpers and route fluent method
  1022. */
  1023. public function testHttpMethodIntegration(): void
  1024. {
  1025. $routes = new RouteBuilder($this->collection, '/');
  1026. $routes->scope('/', function (RouteBuilder $routes): void {
  1027. $routes->get('/faq/{page}', ['controller' => 'Pages', 'action' => 'faq'], 'faq')
  1028. ->setPatterns(['page' => '[a-z0-9_]+'])
  1029. ->setHost('docs.example.com');
  1030. $routes->post('/articles/{id}', ['controller' => 'Articles', 'action' => 'update'], 'article:update')
  1031. ->setPatterns(['id' => '[0-9]+'])
  1032. ->setPass(['id']);
  1033. });
  1034. $this->assertCount(2, $this->collection->routes());
  1035. $this->assertEquals(['faq', 'article:update'], array_keys($this->collection->named()));
  1036. $this->assertNotEmpty($this->collection->parseRequest(new ServerRequest([
  1037. 'url' => '/faq/things_you_know',
  1038. 'environment' => [
  1039. 'REQUEST_METHOD' => 'GET',
  1040. 'HTTP_HOST' => 'docs.example.com',
  1041. ],
  1042. ])));
  1043. $result = $this->collection->parseRequest(new ServerRequest([
  1044. 'url' => '/articles/123',
  1045. 'environment' => [
  1046. 'REQUEST_METHOD' => 'POST',
  1047. ],
  1048. ]));
  1049. $this->assertEquals(['123'], $result['pass']);
  1050. }
  1051. /**
  1052. * Test loading routes from a missing plugin
  1053. */
  1054. public function testLoadPluginBadPlugin(): void
  1055. {
  1056. $this->expectException(MissingPluginException::class);
  1057. $routes = new RouteBuilder($this->collection, '/');
  1058. $routes->loadPlugin('Nope');
  1059. }
  1060. /**
  1061. * Test loading routes with success
  1062. */
  1063. public function testLoadPlugin(): void
  1064. {
  1065. $this->loadPlugins(['TestPlugin']);
  1066. $routes = new RouteBuilder($this->collection, '/');
  1067. $routes->loadPlugin('TestPlugin');
  1068. $this->assertCount(1, $this->collection->routes());
  1069. $this->assertNotEmpty($this->collection->parseRequest(new ServerRequest(['url' => '/test_plugin'])));
  1070. $plugin = Plugin::getCollection()->get('TestPlugin');
  1071. $this->assertFalse($plugin->isEnabled('routes'), 'Hook should be disabled preventing duplicate routes');
  1072. }
  1073. }