RoutesTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. define('TEST_BASE_PATH', dirname(realpath(__FILE__)) . '/');
  3. class RoutesTest extends PHPUnit_Framework_TestCase {
  4. public static function setUpBeforeClass() {
  5. // changing paths
  6. IoC::container()->register('laravel.routing.caller', function($c)
  7. {
  8. return new \Laravel\Routing\Caller($c, require TEST_BASE_PATH.'filters'.EXT, CONTROLLER_PATH);
  9. });
  10. IoC::container()->register('laravel.routing.loader', function($c)
  11. {
  12. return new \Laravel\Routing\Loader(TEST_BASE_PATH,TEST_BASE_PATH . 'routes/');
  13. }, true);
  14. }
  15. protected function setUp()
  16. {
  17. $_POST = array();
  18. unset($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);
  19. }
  20. /**
  21. * tests
  22. * GET /test/wildcard/(:num)/(:any)
  23. * GET /test/optwildcard/(:any+)
  24. */
  25. public function testWildCards()
  26. {
  27. $response = $this->processRoute('/test/wildcard/123456/joe');
  28. $this->assertEquals($response->content, '123456/joe');
  29. $response = $this->processRoute('/test/wildcard/123456');
  30. $this->assertEquals($response->content->view, 'error/404');
  31. $response = $this->processRoute('/test/wildcard/abc123');
  32. $this->assertEquals($response->content, 'abc123');
  33. $response = $this->processRoute('/test/optwildcard/foo');
  34. $this->assertEquals($response->content, 'foo');
  35. $response = $this->processRoute('/test/optwildcard');
  36. $this->assertEquals($response->content, '');
  37. }
  38. /**
  39. * tests GET /test/direct
  40. */
  41. public function testDirect()
  42. {
  43. $response = $this->processRoute('/test/direct');
  44. $this->assertEquals($response->content, 'direct');
  45. $response = $this->processRoute('/test/doesnt/exist');
  46. $this->assertEquals($response->content->view, 'error/404');
  47. }
  48. /**
  49. * tests GET /test/multi and GET /test/altmulti
  50. * both routes are the same
  51. */
  52. public function testMultiRoutes()
  53. {
  54. $response = $this->processRoute('/test/multi');
  55. $this->assertEquals($response->content, 'multi test');
  56. $response = $this->processRoute('/test/altmulti');
  57. $this->assertEquals($response->content, 'multi test');
  58. }
  59. /**
  60. * tests post
  61. */
  62. public function testPost()
  63. {
  64. $response = $this->processRoute('/test/postrequest', 'POST');
  65. $this->assertEquals($response->content, 'POST request');
  66. }
  67. /**
  68. * tests route spoofing
  69. */
  70. public function testSpoofing()
  71. {
  72. $_POST['__spoofer'] = 'PUT';
  73. $response = $this->processRoute('/test/putrequest');
  74. $this->assertEquals($response->content, 'PUT request');
  75. }
  76. /**
  77. * tests filters
  78. */
  79. public function testFilters()
  80. {
  81. $response = $this->processRoute('/test/filter/before');
  82. $this->assertEquals($response->content, 'filtered before');
  83. $response = $this->processRoute('/test/filter/after');
  84. $this->assertEquals($response->content, 'filtered after');
  85. $response = $this->processRoute('/test/filter/multi');
  86. $this->assertEquals($response->content, 'filtered after filtered after2');
  87. }
  88. private function processRoute($uri, $method = 'GET')
  89. {
  90. $_SERVER['REQUEST_URI'] = $uri;
  91. $_SERVER['REQUEST_METHOD'] = $method;
  92. // not using container resolve because it is a singleton and that makes it so we can't change $_SERVER
  93. $request = new \Laravel\Request(new \Laravel\URI($_SERVER), $_SERVER, $_POST);
  94. $router = IoC::container()->resolve('laravel.routing.router');
  95. list($method, $uri) = array($request->method(), $request->uri());
  96. $route = $router->route($request, $method, $uri);
  97. if ( ! is_null($route))
  98. {
  99. $response = IoC::container()->resolve('laravel.routing.caller')->call($route);
  100. }
  101. else
  102. {
  103. $response = Response::error('404');
  104. }
  105. return $response;
  106. }
  107. }