RouteCollectionTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace Illuminate\Tests\Routing;
  3. use ArrayIterator;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Routing\Route;
  6. use Illuminate\Routing\RouteCollection;
  7. use LogicException;
  8. use PHPUnit\Framework\TestCase;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. class RouteCollectionTest extends TestCase
  11. {
  12. /**
  13. * @var \Illuminate\Routing\RouteCollection
  14. */
  15. protected $routeCollection;
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->routeCollection = new RouteCollection;
  20. }
  21. public function testRouteCollectionCanAddRoute()
  22. {
  23. $this->routeCollection->add(new Route('GET', 'foo', [
  24. 'uses' => 'FooController@index',
  25. 'as' => 'foo_index',
  26. ]));
  27. $this->assertCount(1, $this->routeCollection);
  28. }
  29. public function testRouteCollectionAddReturnsTheRoute()
  30. {
  31. $outputRoute = $this->routeCollection->add($inputRoute = new Route('GET', 'foo', [
  32. 'uses' => 'FooController@index',
  33. 'as' => 'foo_index',
  34. ]));
  35. $this->assertInstanceOf(Route::class, $outputRoute);
  36. $this->assertEquals($inputRoute, $outputRoute);
  37. }
  38. public function testRouteCollectionCanRetrieveByName()
  39. {
  40. $this->routeCollection->add($routeIndex = new Route('GET', 'foo/index', [
  41. 'uses' => 'FooController@index',
  42. 'as' => 'route_name',
  43. ]));
  44. $this->assertSame('route_name', $routeIndex->getName());
  45. $this->assertSame('route_name', $this->routeCollection->getByName('route_name')->getName());
  46. $this->assertEquals($routeIndex, $this->routeCollection->getByName('route_name'));
  47. }
  48. public function testRouteCollectionCanRetrieveByAction()
  49. {
  50. $this->routeCollection->add($routeIndex = new Route('GET', 'foo/index', $action = [
  51. 'uses' => 'FooController@index',
  52. 'as' => 'route_name',
  53. ]));
  54. $this->assertSame($action, $routeIndex->getAction());
  55. }
  56. public function testRouteCollectionCanGetIterator()
  57. {
  58. $this->routeCollection->add(new Route('GET', 'foo/index', [
  59. 'uses' => 'FooController@index',
  60. 'as' => 'foo_index',
  61. ]));
  62. $this->assertInstanceOf(ArrayIterator::class, $this->routeCollection->getIterator());
  63. }
  64. public function testRouteCollectionCanGetIteratorWhenEmpty()
  65. {
  66. $this->assertCount(0, $this->routeCollection);
  67. $this->assertInstanceOf(ArrayIterator::class, $this->routeCollection->getIterator());
  68. }
  69. public function testRouteCollectionCanGetIteratorWhenRouteAreAdded()
  70. {
  71. $this->routeCollection->add($routeIndex = new Route('GET', 'foo/index', [
  72. 'uses' => 'FooController@index',
  73. 'as' => 'foo_index',
  74. ]));
  75. $this->assertCount(1, $this->routeCollection);
  76. $this->routeCollection->add($routeShow = new Route('GET', 'bar/show', [
  77. 'uses' => 'BarController@show',
  78. 'as' => 'bar_show',
  79. ]));
  80. $this->assertCount(2, $this->routeCollection);
  81. $this->assertInstanceOf(ArrayIterator::class, $this->routeCollection->getIterator());
  82. }
  83. public function testRouteCollectionCanHandleSameRoute()
  84. {
  85. $routeIndex = new Route('GET', 'foo/index', [
  86. 'uses' => 'FooController@index',
  87. 'as' => 'foo_index',
  88. ]);
  89. $this->routeCollection->add($routeIndex);
  90. $this->assertCount(1, $this->routeCollection);
  91. // Add exactly the same route
  92. $this->routeCollection->add($routeIndex);
  93. $this->assertCount(1, $this->routeCollection);
  94. // Add a non-existing route
  95. $this->routeCollection->add(new Route('GET', 'bar/show', [
  96. 'uses' => 'BarController@show',
  97. 'as' => 'bar_show',
  98. ]));
  99. $this->assertCount(2, $this->routeCollection);
  100. }
  101. public function testRouteCollectionCanRefreshNameLookups()
  102. {
  103. $routeIndex = new Route('GET', 'foo/index', [
  104. 'uses' => 'FooController@index',
  105. ]);
  106. // The name of the route is not yet set. It will be while adding if to the RouteCollection.
  107. $this->assertNull($routeIndex->getName());
  108. // The route name is set by calling \Illuminate\Routing\Route::name()
  109. $this->routeCollection->add($routeIndex)->name('route_name');
  110. // No route is found. This is normal, as no refresh as been done.
  111. $this->assertNull($this->routeCollection->getByName('route_name'));
  112. // After the refresh, the name will be properly set to the route.
  113. $this->routeCollection->refreshNameLookups();
  114. $this->assertEquals($routeIndex, $this->routeCollection->getByName('route_name'));
  115. }
  116. public function testRouteCollectionCanGetAllRoutes()
  117. {
  118. $this->routeCollection->add($routeIndex = new Route('GET', 'foo/index', [
  119. 'uses' => 'FooController@index',
  120. 'as' => 'foo_index',
  121. ]));
  122. $this->routeCollection->add($routeShow = new Route('GET', 'foo/show', [
  123. 'uses' => 'FooController@show',
  124. 'as' => 'foo_show',
  125. ]));
  126. $this->routeCollection->add($routeNew = new Route('POST', 'bar', [
  127. 'uses' => 'BarController@create',
  128. 'as' => 'bar_create',
  129. ]));
  130. $allRoutes = [
  131. $routeIndex,
  132. $routeShow,
  133. $routeNew,
  134. ];
  135. $this->assertEquals($allRoutes, $this->routeCollection->getRoutes());
  136. }
  137. public function testRouteCollectionCanGetRoutesByName()
  138. {
  139. $routesByName = [
  140. 'foo_index' => new Route('GET', 'foo/index', [
  141. 'uses' => 'FooController@index',
  142. 'as' => 'foo_index',
  143. ]),
  144. 'foo_show' => new Route('GET', 'foo/show', [
  145. 'uses' => 'FooController@show',
  146. 'as' => 'foo_show',
  147. ]),
  148. 'bar_create' => new Route('POST', 'bar', [
  149. 'uses' => 'BarController@create',
  150. 'as' => 'bar_create',
  151. ]),
  152. ];
  153. $this->routeCollection->add($routesByName['foo_index']);
  154. $this->routeCollection->add($routesByName['foo_show']);
  155. $this->routeCollection->add($routesByName['bar_create']);
  156. $this->assertSame($routesByName, $this->routeCollection->getRoutesByName());
  157. }
  158. public function testRouteCollectionCanGetRoutesByMethod()
  159. {
  160. $routes = [
  161. 'foo_index' => new Route('GET', 'foo/index', [
  162. 'uses' => 'FooController@index',
  163. 'as' => 'foo_index',
  164. ]),
  165. 'foo_show' => new Route('GET', 'foo/show', [
  166. 'uses' => 'FooController@show',
  167. 'as' => 'foo_show',
  168. ]),
  169. 'bar_create' => new Route('POST', 'bar', [
  170. 'uses' => 'BarController@create',
  171. 'as' => 'bar_create',
  172. ]),
  173. ];
  174. $this->routeCollection->add($routes['foo_index']);
  175. $this->routeCollection->add($routes['foo_show']);
  176. $this->routeCollection->add($routes['bar_create']);
  177. $this->assertSame([
  178. 'GET' => [
  179. 'foo/index' => $routes['foo_index'],
  180. 'foo/show' => $routes['foo_show'],
  181. ],
  182. 'HEAD' => [
  183. 'foo/index' => $routes['foo_index'],
  184. 'foo/show' => $routes['foo_show'],
  185. ],
  186. 'POST' => [
  187. 'bar' => $routes['bar_create'],
  188. ],
  189. ], $this->routeCollection->getRoutesByMethod());
  190. }
  191. public function testRouteCollectionCleansUpOverwrittenRoutes()
  192. {
  193. // Create two routes with the same path and method.
  194. $routeA = new Route('GET', 'product', ['controller' => 'View@view', 'as' => 'routeA']);
  195. $routeB = new Route('GET', 'product', ['controller' => 'OverwrittenView@view', 'as' => 'overwrittenRouteA']);
  196. $this->routeCollection->add($routeA);
  197. $this->routeCollection->add($routeB);
  198. // Check if the lookups of $routeA and $routeB are there.
  199. $this->assertEquals($routeA, $this->routeCollection->getByName('routeA'));
  200. $this->assertEquals($routeA, $this->routeCollection->getByAction('View@view'));
  201. $this->assertEquals($routeB, $this->routeCollection->getByName('overwrittenRouteA'));
  202. $this->assertEquals($routeB, $this->routeCollection->getByAction('OverwrittenView@view'));
  203. // Rebuild the lookup arrays.
  204. $this->routeCollection->refreshNameLookups();
  205. $this->routeCollection->refreshActionLookups();
  206. // The lookups of $routeA should not be there anymore, because they are no longer valid.
  207. $this->assertNull($this->routeCollection->getByName('routeA'));
  208. $this->assertNull($this->routeCollection->getByAction('View@view'));
  209. // The lookups of $routeB are still there.
  210. $this->assertEquals($routeB, $this->routeCollection->getByName('overwrittenRouteA'));
  211. $this->assertEquals($routeB, $this->routeCollection->getByAction('OverwrittenView@view'));
  212. }
  213. public function testCannotCacheDuplicateRouteNames()
  214. {
  215. $this->routeCollection->add(
  216. new Route('GET', 'users', ['uses' => 'UsersController@index', 'as' => 'users'])
  217. );
  218. $this->routeCollection->add(
  219. new Route('GET', 'users/{user}', ['uses' => 'UsersController@show', 'as' => 'users'])
  220. );
  221. $this->expectException(LogicException::class);
  222. $this->routeCollection->compile();
  223. }
  224. public function testRouteCollectionDontMatchNonMatchingDoubleSlashes()
  225. {
  226. $this->expectException(NotFoundHttpException::class);
  227. $this->routeCollection->add(new Route('GET', 'foo', [
  228. 'uses' => 'FooController@index',
  229. 'as' => 'foo_index',
  230. ]));
  231. $request = Request::create('', 'GET');
  232. // We have to set uri in REQUEST_URI otherwise Request uses parse_url() which trim the slashes
  233. $request->server->set(
  234. 'REQUEST_URI', '//foo'
  235. );
  236. $this->routeCollection->match($request);
  237. }
  238. }