RouteCollectionTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. * Setup method
  24. *
  25. * @return void
  26. */
  27. public function setUp() {
  28. parent::setUp();
  29. $this->collection = new RouteCollection();
  30. }
  31. /**
  32. * Test parse() throws an error on unknown routes.
  33. *
  34. * @expectedException Cake\Routing\Error\MissingRouteException
  35. * @expectedExceptionMessage A route matching "/" could not be found
  36. */
  37. public function testParseMissingRoute() {
  38. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  39. $routes->connect('/', ['controller' => 'Articles']);
  40. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  41. $result = $this->collection->parse('/');
  42. $this->assertEquals([], $result, 'Should not match, missing /b');
  43. }
  44. /**
  45. * Test parsing routes.
  46. *
  47. * @return void
  48. */
  49. public function testParse() {
  50. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  51. $routes->connect('/', ['controller' => 'Articles']);
  52. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  53. $routes->connect('/media/search/*', ['controller' => 'Media', 'action' => 'search']);
  54. $result = $this->collection->parse('/b/');
  55. $expected = [
  56. 'controller' => 'Articles',
  57. 'action' => 'index',
  58. 'pass' => [],
  59. 'plugin' => null,
  60. 'key' => 'value',
  61. ];
  62. $this->assertEquals($expected, $result);
  63. $result = $this->collection->parse('/b/the-thing?one=two');
  64. $expected = [
  65. 'controller' => 'Articles',
  66. 'action' => 'view',
  67. 'id' => 'the-thing',
  68. 'pass' => [],
  69. 'plugin' => null,
  70. 'key' => 'value',
  71. '?' => ['one' => 'two'],
  72. ];
  73. $this->assertEquals($expected, $result);
  74. $result = $this->collection->parse('/b/media/search');
  75. $expected = [
  76. 'key' => 'value',
  77. 'pass' => [],
  78. 'plugin' => null,
  79. 'controller' => 'Media',
  80. 'action' => 'search'
  81. ];
  82. $this->assertEquals($expected, $result);
  83. $result = $this->collection->parse('/b/media/search/thing');
  84. $expected = [
  85. 'key' => 'value',
  86. 'pass' => ['thing'],
  87. 'plugin' => null,
  88. 'controller' => 'Media',
  89. 'action' => 'search'
  90. ];
  91. $this->assertEquals($expected, $result);
  92. }
  93. /**
  94. * Test that parsing checks all the related path scopes.
  95. *
  96. * @return void
  97. */
  98. public function testParseFallback() {
  99. $routes = new RouteBuilder($this->collection, '/', []);
  100. $routes->resources('Articles');
  101. $routes->connect('/:controller', ['action' => 'index'], [], ['routeClass' => 'InflectedRoute']);
  102. $routes->connect('/:controller/:action', [], ['routeClass' => 'InflectedRoute']);
  103. $result = $this->collection->parse('/articles/add');
  104. $expected = [
  105. 'controller' => 'Articles',
  106. 'action' => 'add',
  107. 'plugin' => null,
  108. 'pass' => []
  109. ];
  110. $this->assertEquals($expected, $result);
  111. }
  112. /**
  113. * Test match() throws an error on unknown routes.
  114. *
  115. * @expectedException Cake\Routing\Error\MissingRouteException
  116. * @expectedExceptionMessage A route matching "array (
  117. */
  118. public function testMatchError() {
  119. $context = [
  120. '_base' => '/',
  121. '_scheme' => 'http',
  122. '_host' => 'example.org',
  123. ];
  124. $routes = new RouteBuilder($this->collection, '/b');
  125. $routes->connect('/', ['controller' => 'Articles']);
  126. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'add'], $context);
  127. $this->assertFalse($result, 'No matches');
  128. }
  129. /**
  130. * Test matching routes.
  131. *
  132. * @return void
  133. */
  134. public function testMatch() {
  135. $context = [
  136. '_base' => '/',
  137. '_scheme' => 'http',
  138. '_host' => 'example.org',
  139. ];
  140. $routes = new RouteBuilder($this->collection, '/b');
  141. $routes->connect('/', ['controller' => 'Articles']);
  142. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  143. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'index'], $context);
  144. $this->assertEquals('b', $result);
  145. $result = $this->collection->match(
  146. ['id' => 'thing', 'plugin' => null, 'controller' => 'Articles', 'action' => 'view'],
  147. $context);
  148. $this->assertEquals('b/thing', $result);
  149. }
  150. /**
  151. * Test matching routes with names
  152. *
  153. * @return void
  154. */
  155. public function testMatchNamed() {
  156. $context = [
  157. '_base' => '/',
  158. '_scheme' => 'http',
  159. '_host' => 'example.org',
  160. ];
  161. $routes = new RouteBuilder($this->collection, '/b');
  162. $routes->connect('/', ['controller' => 'Articles']);
  163. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  164. $result = $this->collection->match(['_name' => 'article:view', 'id' => '2'], $context);
  165. $this->assertEquals('/b/2', $result);
  166. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'view', 'id' => '2'], $context);
  167. $this->assertEquals('b/2', $result);
  168. }
  169. /**
  170. * Test matching routes with names and failing
  171. *
  172. * @expectedException Cake\Routing\Error\MissingRouteException
  173. * @return void
  174. */
  175. public function testMatchNamedError() {
  176. $context = [
  177. '_base' => '/',
  178. '_scheme' => 'http',
  179. '_host' => 'example.org',
  180. ];
  181. $routes = new RouteBuilder($this->collection, '/b');
  182. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  183. $this->collection->match(['_name' => 'derp'], $context);
  184. }
  185. /**
  186. * Test matching plugin routes.
  187. *
  188. * @return void
  189. */
  190. public function testMatchPlugin() {
  191. $context = [
  192. '_base' => '/',
  193. '_scheme' => 'http',
  194. '_host' => 'example.org',
  195. ];
  196. $routes = new RouteBuilder($this->collection, '/contacts', ['plugin' => 'Contacts']);
  197. $routes->connect('/', ['controller' => 'Contacts']);
  198. $result = $this->collection->match(['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'], $context);
  199. $this->assertEquals('contacts', $result);
  200. }
  201. /**
  202. * Test that prefixes increase the specificity of a route match.
  203. *
  204. * Connect the admin route after the non prefixed version, this means
  205. * the non-prefix route would have a more specific name (users:index)
  206. *
  207. * @return void
  208. */
  209. public function testMatchPrefixSpecificity() {
  210. $context = [
  211. '_base' => '/',
  212. '_scheme' => 'http',
  213. '_host' => 'example.org',
  214. ];
  215. $routes = new RouteBuilder($this->collection, '/');
  216. $routes->connect('/:action/*', ['controller' => 'Users']);
  217. $routes->connect('/admin/:controller', ['prefix' => 'admin', 'action' => 'index']);
  218. $url = [
  219. 'plugin' => null,
  220. 'prefix' => 'admin',
  221. 'controller' => 'Users',
  222. 'action' => 'index'
  223. ];
  224. $result = $this->collection->match($url, $context);
  225. $this->assertEquals('admin/Users', $result);
  226. $url = [
  227. 'plugin' => null,
  228. 'controller' => 'Users',
  229. 'action' => 'index'
  230. ];
  231. $result = $this->collection->match($url, $context);
  232. $this->assertEquals('index', $result);
  233. }
  234. /**
  235. * Test getting named routes.
  236. *
  237. * @return void
  238. */
  239. public function testNamed() {
  240. $routes = new RouteBuilder($this->collection, '/l');
  241. $routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
  242. $routes->connect('/:controller/:action/*');
  243. $all = $this->collection->named();
  244. $this->assertCount(1, $all);
  245. $this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
  246. $this->assertEquals('/l/:controller', $all['cntrl']->template);
  247. }
  248. /**
  249. * Test the add() and routes() method.
  250. *
  251. * @return void
  252. */
  253. public function testAddingRoutes() {
  254. $one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  255. $two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
  256. $this->collection->add($one);
  257. $this->collection->add($two);
  258. $routes = $this->collection->routes();
  259. $this->assertCount(2, $routes);
  260. $this->assertSame($one, $routes[0]);
  261. $this->assertSame($two, $routes[1]);
  262. }
  263. }