route.test.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. use Laravel\Routing\Route;
  3. class RouteTest extends PHPUnit_Framework_TestCase {
  4. /**
  5. * Tear down the testing environment.
  6. */
  7. public static function tearDownAfterClass()
  8. {
  9. unset($_SERVER['REQUEST_METHOD']);
  10. unset(Filter::$filters['test-after']);
  11. unset(Filter::$filters['test-before']);
  12. unset(Filter::$filters['test-params']);
  13. unset(Filter::$filters['test-multi-1']);
  14. unset(Filter::$filters['test-multi-2']);
  15. }
  16. /**
  17. * Destroy the testing environment.
  18. */
  19. public function tearDown()
  20. {
  21. Request::$route = null;
  22. }
  23. /**
  24. * Tests the Route::is method.
  25. *
  26. * @group laravel
  27. */
  28. public function testIsMethodIndicatesIfTheRouteHasAGivenName()
  29. {
  30. $route = new Route('GET', '/', array('as' => 'profile'));
  31. $this->assertTrue($route->is('profile'));
  32. $this->assertFalse($route->is('something'));
  33. }
  34. /**
  35. * Test the basic execution of a route.
  36. *
  37. * @group laravel
  38. */
  39. public function testBasicRoutesCanBeExecutedProperly()
  40. {
  41. $route = new Route('GET', '', array(function() { return 'Route!'; }));
  42. $this->assertEquals('Route!', $route->call()->content);
  43. $this->assertInstanceOf('Laravel\\Response', $route->call());
  44. }
  45. /**
  46. * Test that route parameters are passed into the handlers.
  47. *
  48. * @group laravel
  49. */
  50. public function testRouteParametersArePassedIntoTheHandler()
  51. {
  52. $route = new Route('GET', '', array(function($var) { return $var; }), array('Taylor'));
  53. $this->assertEquals('Taylor', $route->call()->content);
  54. $this->assertInstanceOf('Laravel\\Response', $route->call());
  55. }
  56. /**
  57. * Test that calling a route calls the global before and after filters.
  58. *
  59. * @group laravel
  60. */
  61. public function testCallingARouteCallsTheBeforeAndAfterFilters()
  62. {
  63. $route = new Route('GET', '', array(function() { return 'Hi!'; }));
  64. $_SERVER['before'] = false;
  65. $_SERVER['after'] = false;
  66. $route->call();
  67. $this->assertTrue($_SERVER['before']);
  68. $this->assertTrue($_SERVER['after']);
  69. }
  70. /**
  71. * Test that before filters override the route response.
  72. *
  73. * @group laravel
  74. */
  75. public function testBeforeFiltersOverrideTheRouteResponse()
  76. {
  77. Filter::register('test-before', function()
  78. {
  79. return 'Filtered!';
  80. });
  81. $route = new Route('GET', '', array('before' => 'test-before', function() {
  82. return 'Route!';
  83. }));
  84. $this->assertEquals('Filtered!', $route->call()->content);
  85. }
  86. /**
  87. * Test that after filters do not affect the route response.
  88. *
  89. * @group laravel
  90. */
  91. public function testAfterFilterDoesNotAffectTheResponse()
  92. {
  93. $_SERVER['test-after'] = false;
  94. Filter::register('test-after', function()
  95. {
  96. $_SERVER['test-after'] = true;
  97. return 'Filtered!';
  98. });
  99. $route = new Route('GET', '', array('after' => 'test-after', function()
  100. {
  101. return 'Route!';
  102. }));
  103. $this->assertEquals('Route!', $route->call()->content);
  104. $this->assertTrue($_SERVER['test-after']);
  105. }
  106. /**
  107. * Test that the route calls the appropriate controller method when delegating.
  108. *
  109. * @group laravel
  110. */
  111. public function testControllerActionCalledWhenDelegating()
  112. {
  113. $_SERVER['REQUEST_METHOD'] = 'GET';
  114. $route = new Route('GET', '', array('uses' => 'auth@index'));
  115. $this->assertEquals('action_index', $route->call()->content);
  116. }
  117. /**
  118. * Test that filter parameters are passed to the filter.
  119. *
  120. * @group laravel
  121. */
  122. public function testFilterParametersArePassedToFilter()
  123. {
  124. Filter::register('test-params', function($var1, $var2)
  125. {
  126. return $var1.$var2;
  127. });
  128. $route = new Route('GET', '', array('before' => 'test-params:1,2'));
  129. $this->assertEquals('12', $route->call()->content);
  130. }
  131. /**
  132. * Test that multiple filters can be assigned to a route.
  133. *
  134. * @group laravel
  135. */
  136. public function testMultipleFiltersCanBeAssignedToARoute()
  137. {
  138. $_SERVER['test-multi-1'] = false;
  139. $_SERVER['test-multi-2'] = false;
  140. Filter::register('test-multi-1', function() { $_SERVER['test-multi-1'] = true; });
  141. Filter::register('test-multi-2', function() { $_SERVER['test-multi-2'] = true; });
  142. $route = new Route('GET', '', array('before' => 'test-multi-1|test-multi-2'));
  143. $route->call();
  144. $this->assertTrue($_SERVER['test-multi-1']);
  145. $this->assertTrue($_SERVER['test-multi-2']);
  146. }
  147. }