RouteCollectionTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 match() throws an error on unknown routes.
  95. *
  96. * @expectedException Cake\Routing\Error\MissingRouteException
  97. * @expectedExceptionMessage A route matching "array (
  98. */
  99. public function testMatchError() {
  100. $context = [
  101. '_base' => '/',
  102. '_scheme' => 'http',
  103. '_host' => 'example.org',
  104. ];
  105. $routes = new RouteBuilder($this->collection, '/b');
  106. $routes->connect('/', ['controller' => 'Articles']);
  107. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'add'], $context);
  108. $this->assertFalse($result, 'No matches');
  109. }
  110. /**
  111. * Test matching routes.
  112. *
  113. * @return void
  114. */
  115. public function testMatch() {
  116. $context = [
  117. '_base' => '/',
  118. '_scheme' => 'http',
  119. '_host' => 'example.org',
  120. ];
  121. $routes = new RouteBuilder($this->collection, '/b');
  122. $routes->connect('/', ['controller' => 'Articles']);
  123. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  124. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'index'], $context);
  125. $this->assertEquals('b', $result);
  126. $result = $this->collection->match(
  127. ['id' => 'thing', 'plugin' => null, 'controller' => 'Articles', 'action' => 'view'],
  128. $context);
  129. $this->assertEquals('b/thing', $result);
  130. }
  131. /**
  132. * Test matching routes with names
  133. *
  134. * @return void
  135. */
  136. public function testMatchNamed() {
  137. $context = [
  138. '_base' => '/',
  139. '_scheme' => 'http',
  140. '_host' => 'example.org',
  141. ];
  142. $routes = new RouteBuilder($this->collection, '/b');
  143. $routes->connect('/', ['controller' => 'Articles']);
  144. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  145. $result = $this->collection->match(['_name' => 'article:view', 'id' => '2'], $context);
  146. $this->assertEquals('/b/2', $result);
  147. }
  148. /**
  149. * Test matching routes with names and failing
  150. *
  151. * @expectedException Cake\Routing\Error\MissingRouteException
  152. * @return void
  153. */
  154. public function testMatchNamedError() {
  155. $context = [
  156. '_base' => '/',
  157. '_scheme' => 'http',
  158. '_host' => 'example.org',
  159. ];
  160. $routes = new RouteBuilder($this->collection, '/b');
  161. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  162. $this->collection->match(['_name' => 'derp'], $context);
  163. }
  164. /**
  165. * Test matching plugin routes.
  166. *
  167. * @return void
  168. */
  169. public function testMatchPlugin() {
  170. $context = [
  171. '_base' => '/',
  172. '_scheme' => 'http',
  173. '_host' => 'example.org',
  174. ];
  175. $routes = new RouteBuilder($this->collection, '/contacts', ['plugin' => 'Contacts']);
  176. $routes->connect('/', ['controller' => 'Contacts']);
  177. $result = $this->collection->match(['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'], $context);
  178. $this->assertEquals('contacts', $result);
  179. }
  180. /**
  181. * Test that prefixes increase the specificity of a route match.
  182. *
  183. * Connect the admin route after the non prefixed version, this means
  184. * the non-prefix route would have a more specific name (users:index)
  185. *
  186. * @return void
  187. */
  188. public function testMatchPrefixSpecificity() {
  189. $context = [
  190. '_base' => '/',
  191. '_scheme' => 'http',
  192. '_host' => 'example.org',
  193. ];
  194. $routes = new RouteBuilder($this->collection, '/');
  195. $routes->connect('/:action/*', ['controller' => 'Users']);
  196. $routes->connect('/admin/:controller', ['prefix' => 'admin', 'action' => 'index']);
  197. $url = [
  198. 'plugin' => null,
  199. 'prefix' => 'admin',
  200. 'controller' => 'Users',
  201. 'action' => 'index'
  202. ];
  203. $result = $this->collection->match($url, $context);
  204. $this->assertEquals('admin/Users', $result);
  205. $url = [
  206. 'plugin' => null,
  207. 'controller' => 'Users',
  208. 'action' => 'index'
  209. ];
  210. $result = $this->collection->match($url, $context);
  211. $this->assertEquals('index', $result);
  212. }
  213. /**
  214. * Test getting named routes.
  215. *
  216. * @return void
  217. */
  218. public function testNamed() {
  219. $routes = new RouteBuilder($this->collection, '/l');
  220. $routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
  221. $routes->connect('/:controller/:action/*');
  222. $all = $this->collection->named();
  223. $this->assertCount(1, $all);
  224. $this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
  225. $this->assertEquals('/l/:controller', $all['cntrl']->template);
  226. }
  227. /**
  228. * Test the add() and routes() method.
  229. *
  230. * @return void
  231. */
  232. public function testAddingRoutes() {
  233. $one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  234. $two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
  235. $this->collection->add($one);
  236. $this->collection->add($two);
  237. $routes = $this->collection->routes();
  238. $this->assertCount(2, $routes);
  239. $this->assertSame($one, $routes[0]);
  240. $this->assertSame($two, $routes[1]);
  241. }
  242. }