RouteCollectionTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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\Route\Route;
  17. use Cake\Routing\Router;
  18. use Cake\Routing\RouteBuilder;
  19. use Cake\Routing\RouteCollection;
  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. $result = $this->collection->parse('/b/');
  54. $expected = [
  55. 'controller' => 'Articles',
  56. 'action' => 'index',
  57. 'pass' => [],
  58. 'plugin' => null,
  59. 'key' => 'value',
  60. ];
  61. $this->assertEquals($expected, $result);
  62. $result = $this->collection->parse('/b/the-thing?one=two');
  63. $expected = [
  64. 'controller' => 'Articles',
  65. 'action' => 'view',
  66. 'id' => 'the-thing',
  67. 'pass' => [],
  68. 'plugin' => null,
  69. 'key' => 'value',
  70. '?' => ['one' => 'two'],
  71. ];
  72. $this->assertEquals($expected, $result);
  73. }
  74. /**
  75. * Test match() throws an error on unknown routes.
  76. *
  77. * @expectedException Cake\Routing\Error\MissingRouteException
  78. * @expectedExceptionMessage A route matching "array (
  79. */
  80. public function testMatchError() {
  81. $context = [
  82. '_base' => '/',
  83. '_scheme' => 'http',
  84. '_host' => 'example.org',
  85. ];
  86. $routes = new RouteBuilder($this->collection, '/b');
  87. $routes->connect('/', ['controller' => 'Articles']);
  88. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'add'], $context);
  89. $this->assertFalse($result, 'No matches');
  90. }
  91. /**
  92. * Test matching routes.
  93. *
  94. * @return void
  95. */
  96. public function testMatch() {
  97. $context = [
  98. '_base' => '/',
  99. '_scheme' => 'http',
  100. '_host' => 'example.org',
  101. ];
  102. $routes = new RouteBuilder($this->collection, '/b');
  103. $routes->connect('/', ['controller' => 'Articles']);
  104. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  105. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'index'], $context);
  106. $this->assertEquals('b', $result);
  107. $result = $this->collection->match(
  108. ['id' => 'thing', 'plugin' => null, 'controller' => 'Articles', 'action' => 'view'],
  109. $context);
  110. $this->assertEquals('b/thing', $result);
  111. }
  112. /**
  113. * Test matching routes with names
  114. *
  115. * @return void
  116. */
  117. public function testMatchNamed() {
  118. $context = [
  119. '_base' => '/',
  120. '_scheme' => 'http',
  121. '_host' => 'example.org',
  122. ];
  123. $routes = new RouteBuilder($this->collection, '/b');
  124. $routes->connect('/', ['controller' => 'Articles']);
  125. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  126. $result = $this->collection->match(['_name' => 'article:view', 'id' => '2'], $context);
  127. $this->assertEquals('/b/2', $result);
  128. }
  129. /**
  130. * Test matching routes with names and failing
  131. *
  132. * @expectedException Cake\Routing\Error\MissingRouteException
  133. * @return void
  134. */
  135. public function testMatchNamedError() {
  136. $context = [
  137. '_base' => '/',
  138. '_scheme' => 'http',
  139. '_host' => 'example.org',
  140. ];
  141. $routes = new RouteBuilder($this->collection, '/b');
  142. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  143. $this->collection->match(['_name' => 'derp'], $context);
  144. }
  145. /**
  146. * Test matching plugin routes.
  147. *
  148. * @return void
  149. */
  150. public function testMatchPlugin() {
  151. $context = [
  152. '_base' => '/',
  153. '_scheme' => 'http',
  154. '_host' => 'example.org',
  155. ];
  156. $routes = new RouteBuilder($this->collection, '/contacts', ['plugin' => 'Contacts']);
  157. $routes->connect('/', ['controller' => 'Contacts']);
  158. $result = $this->collection->match(['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'], $context);
  159. $this->assertEquals('contacts', $result);
  160. }
  161. /**
  162. * Test that prefixes increase the specificity of a route match.
  163. *
  164. * Connect the admin route after the non prefixed version, this means
  165. * the non-prefix route would have a more specific name (users:index)
  166. *
  167. * @return void
  168. */
  169. public function testMatchPrefixSpecificity() {
  170. $context = [
  171. '_base' => '/',
  172. '_scheme' => 'http',
  173. '_host' => 'example.org',
  174. ];
  175. $routes = new RouteBuilder($this->collection, '/');
  176. $routes->connect('/:action/*', ['controller' => 'Users']);
  177. $routes->connect('/admin/:controller', ['prefix' => 'admin', 'action' => 'index']);
  178. $url = [
  179. 'plugin' => null,
  180. 'prefix' => 'admin',
  181. 'controller' => 'Users',
  182. 'action' => 'index'
  183. ];
  184. $result = $this->collection->match($url, $context);
  185. $this->assertEquals('admin/Users', $result);
  186. $url = [
  187. 'plugin' => null,
  188. 'controller' => 'Users',
  189. 'action' => 'index'
  190. ];
  191. $result = $this->collection->match($url, $context);
  192. $this->assertEquals('index', $result);
  193. }
  194. /**
  195. * Test getting named routes.
  196. *
  197. * @return void
  198. */
  199. public function testNamed() {
  200. $routes = new RouteBuilder($this->collection, '/l');
  201. $routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
  202. $routes->connect('/:controller/:action/*');
  203. $all = $this->collection->named();
  204. $this->assertCount(1, $all);
  205. $this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
  206. $this->assertEquals('/l/:controller', $all['cntrl']->template);
  207. }
  208. /**
  209. * Test the add() and routes() method.
  210. *
  211. * @return void
  212. */
  213. public function testAddingRoutes() {
  214. $one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  215. $two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
  216. $this->collection->add($one);
  217. $this->collection->add($two);
  218. $routes = $this->collection->routes();
  219. $this->assertCount(2, $routes);
  220. $this->assertSame($one, $routes[0]);
  221. $this->assertSame($two, $routes[1]);
  222. }
  223. }