routing.test.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. use Laravel\Routing\Router;
  3. class RoutingTest extends PHPUnit_Framework_TestCase {
  4. /**
  5. * Destroy the testing environment.
  6. */
  7. public function setUp()
  8. {
  9. Bundle::$started = array();
  10. Bundle::$routed = array();
  11. Router::$names = array();
  12. Router::$routes = array();
  13. }
  14. /**
  15. * Destroy the testing environment.
  16. */
  17. public function tearDown()
  18. {
  19. Bundle::$started = array();
  20. Bundle::$routed = array();
  21. Router::$names = array();
  22. Router::$routes = array();
  23. }
  24. /**
  25. * Test the Router::find method.
  26. *
  27. * @group laravel
  28. */
  29. public function testNamedRoutesCanBeLocatedByTheRouter()
  30. {
  31. Route::get('/', array('as' => 'home'));
  32. Route::get('dashboard', array('as' => 'dashboard'));
  33. $home = Router::find('home');
  34. $dashboard = Router::find('dashboard');
  35. $this->assertTrue(isset($home['/']));
  36. $this->assertTrue(isset($dashboard['dashboard']));
  37. }
  38. /**
  39. * Test the basic routing mechanism.
  40. *
  41. * @group laravel
  42. */
  43. public function testBasicRouteCanBeRouted()
  44. {
  45. Route::get('/', function() {});
  46. Route::get('home, main', function() {});
  47. $this->assertEquals('/', Router::route('GET', '/')->uri);
  48. $this->assertEquals('home', Router::route('GET', 'home')->uri);
  49. $this->assertEquals('main', Router::route('GET', 'main')->uri);
  50. }
  51. /**
  52. * Test that the router can handle basic wildcards.
  53. *
  54. * @group laravel
  55. */
  56. public function testWildcardRoutesCanBeRouted()
  57. {
  58. Route::get('user/(:num)', function() {});
  59. Route::get('profile/(:any)/(:num)', function() {});
  60. $this->assertNull(Router::route('GET', 'user/1.5'));
  61. $this->assertNull(Router::route('GET', 'user/taylor'));
  62. $this->assertEquals(array(25), Router::route('GET', 'user/25')->parameters);
  63. $this->assertEquals('user/(:num)', Router::route('GET', 'user/1')->uri);
  64. $this->assertNull(Router::route('GET', 'profile/1/otwell'));
  65. $this->assertNull(Router::route('POST', 'profile/taylor/1'));
  66. $this->assertNull(Router::route('GET', 'profile/taylor/otwell'));
  67. $this->assertNull(Router::route('GET', 'profile/taylor/1/otwell'));
  68. $this->assertEquals(array('taylor', 25), Router::route('GET', 'profile/taylor/25')->parameters);
  69. $this->assertEquals('profile/(:any)/(:num)', Router::route('GET', 'profile/taylor/1')->uri);
  70. }
  71. /**
  72. * Test that optional wildcards can be routed.
  73. *
  74. * @group laravel
  75. */
  76. public function testOptionalWildcardsCanBeRouted()
  77. {
  78. Route::get('user/(:num?)', function() {});
  79. Route::get('profile/(:any)/(:any?)', function() {});
  80. $this->assertNull(Router::route('GET', 'user/taylor'));
  81. $this->assertEquals('user/(:num?)', Router::route('GET', 'user')->uri);
  82. $this->assertEquals(array(25), Router::route('GET', 'user/25')->parameters);
  83. $this->assertEquals('user/(:num?)', Router::route('GET', 'user/1')->uri);
  84. $this->assertNull(Router::route('GET', 'profile/taylor/otwell/test'));
  85. $this->assertEquals('profile/(:any)/(:any?)', Router::route('GET', 'profile/taylor')->uri);
  86. $this->assertEquals('profile/(:any)/(:any?)', Router::route('GET', 'profile/taylor/25')->uri);
  87. $this->assertEquals('profile/(:any)/(:any?)', Router::route('GET', 'profile/taylor/otwell')->uri);
  88. $this->assertEquals(array('taylor', 'otwell'), Router::route('GET', 'profile/taylor/otwell')->parameters);
  89. }
  90. /**
  91. * Test that basic controller routing is working.
  92. *
  93. * @group laravel
  94. */
  95. public function testBasicRouteToControllerIsRouted()
  96. {
  97. $this->assertEquals('auth@(:1)', Router::route('GET', 'auth')->action['uses']);
  98. $this->assertEquals('home@(:1)', Router::route('GET', 'home/index')->action['uses']);
  99. $this->assertEquals('home@(:1)', Router::route('GET', 'home/profile')->action['uses']);
  100. $this->assertEquals('admin.panel@(:1)', Router::route('GET', 'admin/panel')->action['uses']);
  101. $this->assertEquals('admin.panel@(:1)', Router::route('GET', 'admin/panel/show')->action['uses']);
  102. }
  103. /**
  104. * Test basic bundle route resolution.
  105. *
  106. * @group laravel
  107. */
  108. public function testRoutesToBundlesCanBeResolved()
  109. {
  110. $this->assertNull(Router::route('GET', 'dashboard/foo'));
  111. $this->assertEquals('dashboard', Router::route('GET', 'dashboard')->uri);
  112. }
  113. /**
  114. * Test bundle controller route resolution.
  115. *
  116. * @group laravel
  117. */
  118. public function testBundleControllersCanBeResolved()
  119. {
  120. $this->assertEquals('dashboard::panel@(:1)', Router::route('GET', 'dashboard/panel')->action['uses']);
  121. $this->assertEquals('dashboard::panel@(:1)', Router::route('GET', 'dashboard/panel/show')->action['uses']);
  122. }
  123. /**
  124. * Test foreign characters can be used in routes.
  125. *
  126. * @group laravel
  127. */
  128. public function testForeignCharsInRoutes()
  129. {
  130. Route::get(urlencode('مدرس_رياضيات').'/(:any)', function() {});
  131. Route::get(urlencode('مدرس_رياضيات'), function() {});
  132. Route::get(urlencode('ÇœŪ'), function() {});
  133. Route::get(urlencode('私は料理が大好き'), function() {});
  134. $this->assertEquals(array(urlencode('مدرس_رياضيات')), Router::route('GET', urlencode('مدرس_رياضيات').'/'.urlencode('مدرس_رياضيات'))->parameters);
  135. $this->assertEquals(urlencode('مدرس_رياضيات'), Router::route('GET', urlencode('مدرس_رياضيات'))->uri);
  136. $this->assertEquals(urlencode('ÇœŪ'), Router::route('GET', urlencode('ÇœŪ'))->uri);
  137. $this->assertEquals(urlencode('私は料理が大好き'), Router::route('GET', urlencode('私は料理が大好き'))->uri);
  138. }
  139. }