RouteCollectionTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Routing;
  16. use Cake\Routing\RouteBuilder;
  17. use Cake\Routing\RouteCollection;
  18. use Cake\Routing\Router;
  19. use Cake\Routing\Route\Route;
  20. use Cake\TestSuite\TestCase;
  21. class RouteCollectionTest extends TestCase
  22. {
  23. /**
  24. * Setup method
  25. *
  26. * @return void
  27. */
  28. public function setUp()
  29. {
  30. parent::setUp();
  31. $this->collection = new RouteCollection();
  32. }
  33. /**
  34. * Test parse() throws an error on unknown routes.
  35. *
  36. * @expectedException \Cake\Routing\Exception\MissingRouteException
  37. * @expectedExceptionMessage A route matching "/" could not be found
  38. */
  39. public function testParseMissingRoute()
  40. {
  41. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  42. $routes->connect('/', ['controller' => 'Articles']);
  43. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  44. $result = $this->collection->parse('/');
  45. $this->assertEquals([], $result, 'Should not match, missing /b');
  46. }
  47. /**
  48. * Test parsing routes.
  49. *
  50. * @return void
  51. */
  52. public function testParse()
  53. {
  54. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  55. $routes->connect('/', ['controller' => 'Articles']);
  56. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  57. $routes->connect('/media/search/*', ['controller' => 'Media', 'action' => 'search']);
  58. $result = $this->collection->parse('/b/');
  59. $expected = [
  60. 'controller' => 'Articles',
  61. 'action' => 'index',
  62. 'pass' => [],
  63. 'plugin' => null,
  64. 'key' => 'value',
  65. ];
  66. $this->assertEquals($expected, $result);
  67. $result = $this->collection->parse('/b/the-thing?one=two');
  68. $expected = [
  69. 'controller' => 'Articles',
  70. 'action' => 'view',
  71. 'id' => 'the-thing',
  72. 'pass' => [],
  73. 'plugin' => null,
  74. 'key' => 'value',
  75. '?' => ['one' => 'two'],
  76. ];
  77. $this->assertEquals($expected, $result);
  78. $result = $this->collection->parse('/b/media/search');
  79. $expected = [
  80. 'key' => 'value',
  81. 'pass' => [],
  82. 'plugin' => null,
  83. 'controller' => 'Media',
  84. 'action' => 'search'
  85. ];
  86. $this->assertEquals($expected, $result);
  87. $result = $this->collection->parse('/b/media/search/thing');
  88. $expected = [
  89. 'key' => 'value',
  90. 'pass' => ['thing'],
  91. 'plugin' => null,
  92. 'controller' => 'Media',
  93. 'action' => 'search'
  94. ];
  95. $this->assertEquals($expected, $result);
  96. }
  97. /**
  98. * Test that parsing checks all the related path scopes.
  99. *
  100. * @return void
  101. */
  102. public function testParseFallback()
  103. {
  104. $routes = new RouteBuilder($this->collection, '/', []);
  105. $routes->resources('Articles');
  106. $routes->connect('/:controller', ['action' => 'index'], [], ['routeClass' => 'InflectedRoute']);
  107. $routes->connect('/:controller/:action', [], ['routeClass' => 'InflectedRoute']);
  108. $result = $this->collection->parse('/articles/add');
  109. $expected = [
  110. 'controller' => 'Articles',
  111. 'action' => 'add',
  112. 'plugin' => null,
  113. 'pass' => []
  114. ];
  115. $this->assertEquals($expected, $result);
  116. }
  117. /**
  118. * Test match() throws an error on unknown routes.
  119. *
  120. * @expectedException \Cake\Routing\Exception\MissingRouteException
  121. * @expectedExceptionMessage A route matching "array (
  122. */
  123. public function testMatchError()
  124. {
  125. $context = [
  126. '_base' => '/',
  127. '_scheme' => 'http',
  128. '_host' => 'example.org',
  129. ];
  130. $routes = new RouteBuilder($this->collection, '/b');
  131. $routes->connect('/', ['controller' => 'Articles']);
  132. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'add'], $context);
  133. $this->assertFalse($result, 'No matches');
  134. }
  135. /**
  136. * Test matching routes.
  137. *
  138. * @return void
  139. */
  140. public function testMatch()
  141. {
  142. $context = [
  143. '_base' => '/',
  144. '_scheme' => 'http',
  145. '_host' => 'example.org',
  146. ];
  147. $routes = new RouteBuilder($this->collection, '/b');
  148. $routes->connect('/', ['controller' => 'Articles']);
  149. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  150. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'index'], $context);
  151. $this->assertEquals('b', $result);
  152. $result = $this->collection->match(
  153. ['id' => 'thing', 'plugin' => null, 'controller' => 'Articles', 'action' => 'view'],
  154. $context
  155. );
  156. $this->assertEquals('b/thing', $result);
  157. }
  158. /**
  159. * Test matching routes with names
  160. *
  161. * @return void
  162. */
  163. public function testMatchNamed()
  164. {
  165. $context = [
  166. '_base' => '/',
  167. '_scheme' => 'http',
  168. '_host' => 'example.org',
  169. ];
  170. $routes = new RouteBuilder($this->collection, '/b');
  171. $routes->connect('/', ['controller' => 'Articles']);
  172. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  173. $result = $this->collection->match(['_name' => 'article:view', 'id' => '2'], $context);
  174. $this->assertEquals('/b/2', $result);
  175. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'view', 'id' => '2'], $context);
  176. $this->assertEquals('b/2', $result);
  177. }
  178. /**
  179. * Test matching routes with names and failing
  180. *
  181. * @expectedException \Cake\Routing\Exception\MissingRouteException
  182. * @return void
  183. */
  184. public function testMatchNamedError()
  185. {
  186. $context = [
  187. '_base' => '/',
  188. '_scheme' => 'http',
  189. '_host' => 'example.org',
  190. ];
  191. $routes = new RouteBuilder($this->collection, '/b');
  192. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  193. $this->collection->match(['_name' => 'derp'], $context);
  194. }
  195. /**
  196. * Test matching plugin routes.
  197. *
  198. * @return void
  199. */
  200. public function testMatchPlugin()
  201. {
  202. $context = [
  203. '_base' => '/',
  204. '_scheme' => 'http',
  205. '_host' => 'example.org',
  206. ];
  207. $routes = new RouteBuilder($this->collection, '/contacts', ['plugin' => 'Contacts']);
  208. $routes->connect('/', ['controller' => 'Contacts']);
  209. $result = $this->collection->match(['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'], $context);
  210. $this->assertEquals('contacts', $result);
  211. }
  212. /**
  213. * Test that prefixes increase the specificity of a route match.
  214. *
  215. * Connect the admin route after the non prefixed version, this means
  216. * the non-prefix route would have a more specific name (users:index)
  217. *
  218. * @return void
  219. */
  220. public function testMatchPrefixSpecificity()
  221. {
  222. $context = [
  223. '_base' => '/',
  224. '_scheme' => 'http',
  225. '_host' => 'example.org',
  226. ];
  227. $routes = new RouteBuilder($this->collection, '/');
  228. $routes->connect('/:action/*', ['controller' => 'Users']);
  229. $routes->connect('/admin/:controller', ['prefix' => 'admin', 'action' => 'index']);
  230. $url = [
  231. 'plugin' => null,
  232. 'prefix' => 'admin',
  233. 'controller' => 'Users',
  234. 'action' => 'index'
  235. ];
  236. $result = $this->collection->match($url, $context);
  237. $this->assertEquals('admin/Users', $result);
  238. $url = [
  239. 'plugin' => null,
  240. 'controller' => 'Users',
  241. 'action' => 'index'
  242. ];
  243. $result = $this->collection->match($url, $context);
  244. $this->assertEquals('index', $result);
  245. }
  246. /**
  247. * Test getting named routes.
  248. *
  249. * @return void
  250. */
  251. public function testNamed()
  252. {
  253. $routes = new RouteBuilder($this->collection, '/l');
  254. $routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
  255. $routes->connect('/:controller/:action/*');
  256. $all = $this->collection->named();
  257. $this->assertCount(1, $all);
  258. $this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
  259. $this->assertEquals('/l/:controller', $all['cntrl']->template);
  260. }
  261. /**
  262. * Test the add() and routes() method.
  263. *
  264. * @return void
  265. */
  266. public function testAddingRoutes()
  267. {
  268. $one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  269. $two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
  270. $this->collection->add($one);
  271. $this->collection->add($two);
  272. $routes = $this->collection->routes();
  273. $this->assertCount(2, $routes);
  274. $this->assertSame($one, $routes[0]);
  275. $this->assertSame($two, $routes[1]);
  276. }
  277. /**
  278. * Test basic get/set of extensions.
  279. *
  280. * @return void
  281. */
  282. public function testExtensions()
  283. {
  284. $this->assertEquals([], $this->collection->extensions());
  285. $this->collection->extensions('json');
  286. $this->assertEquals(['json'], $this->collection->extensions());
  287. $this->collection->extensions(['rss', 'xml']);
  288. $this->assertEquals(['json', 'rss', 'xml'], $this->collection->extensions());
  289. $this->collection->extensions(['csv'], false);
  290. $this->assertEquals(['csv'], $this->collection->extensions());
  291. }
  292. }