RouteCollectionTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 getting named routes.
  163. *
  164. * @return void
  165. */
  166. public function testNamed() {
  167. $routes = new RouteBuilder($this->collection, '/l');
  168. $routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
  169. $routes->connect('/:controller/:action/*');
  170. $all = $this->collection->named();
  171. $this->assertCount(1, $all);
  172. $this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
  173. $this->assertEquals('/l/:controller', $all['cntrl']->template);
  174. }
  175. /**
  176. * Test adding with an error
  177. */
  178. public function testAdd() {
  179. }
  180. /**
  181. * Test the routes() method.
  182. *
  183. * @return void
  184. */
  185. public function testRoutes() {
  186. }
  187. }