RequestTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. class RequestTest extends PHPUnit_Framework_TestCase {
  3. public function setUp()
  4. {
  5. unset($_SERVER['PATH_INFO'], $_SERVER['REQUEST_METHOD']);
  6. $route = new System\Route(null, null);
  7. $route->callback = array('name' => 'test', 'do' => function() {});
  8. System\Request::$route = $route;
  9. }
  10. public function tearDown()
  11. {
  12. System\Request::$route = null;
  13. }
  14. /**
  15. * @expectedException Exception
  16. */
  17. public function testUriMethodThrowsExceptionWhenCantDetermineUri()
  18. {
  19. System\Request::uri();
  20. }
  21. public function testUriMethodReturnsPathInfoWhenSet()
  22. {
  23. $_SERVER['PATH_INFO'] = 'test';
  24. $_SERVER['REQUEST_METHOD'] = 'blah';
  25. $this->assertEquals(System\Request::uri(), 'test');
  26. }
  27. /**
  28. * @dataProvider rootUriProvider1
  29. */
  30. public function testUriMethodReturnsSingleSlashOnRequestForRoot($uri)
  31. {
  32. Config::set('application.url', 'http://example.com');
  33. Config::set('appliation.index', '');
  34. $_SERVER['REQUEST_URI'] = $uri;
  35. $this->assertEquals(System\Request::uri(), '/');
  36. }
  37. public function rootUriProvider1()
  38. {
  39. return array(
  40. array(''),
  41. array('/'),
  42. array('/index.php'),
  43. array('/index.php/'),
  44. array('/index.php///'),
  45. array('http://example.com'),
  46. array('http://example.com/'),
  47. );
  48. }
  49. /**
  50. * @dataProvider rootUriProvider2
  51. */
  52. public function testUriMethodReturnsSingleSlashOnRequestForFolderNestedRoot($uri)
  53. {
  54. Config::set('application.url', 'http://example.com/laravel/public');
  55. Config::set('appliation.index', 'index.php');
  56. $_SERVER['REQUEST_URI'] = $uri;
  57. $this->assertEquals(System\Request::uri(), '/');
  58. }
  59. public function rootUriProvider2()
  60. {
  61. return array(
  62. array('http://example.com/laravel/public'),
  63. array('http://example.com/laravel/public/index.php'),
  64. array('http://example.com/laravel/public/index.php/'),
  65. array('http://example.com/laravel/public/index.php///'),
  66. array(''),
  67. array('/'),
  68. array('/index.php'),
  69. array('/index.php/'),
  70. array('/index.php///'),
  71. array('http://example.com'),
  72. array('http://example.com/'),
  73. );
  74. }
  75. /**
  76. * @dataProvider segmentedUriProvider1
  77. */
  78. public function testUriMethodReturnsSegmentForSingleSegmentUri($uri)
  79. {
  80. Config::set('application.url', 'http://example.com');
  81. Config::set('appliation.index', '');
  82. $_SERVER['REQUEST_URI'] = $uri;
  83. $this->assertEquals(System\Request::uri(), 'user');
  84. }
  85. public function segmentedUriProvider1()
  86. {
  87. return array(
  88. array('http://example.com/user'),
  89. array('http://example.com/user/'),
  90. array('http://example.com/user//'),
  91. );
  92. }
  93. /**
  94. * @dataProvider segmentedUriProvider2
  95. */
  96. public function testUriMethodReturnsSegmentsForMultiSegmentUri($uri)
  97. {
  98. Config::set('application.url', 'http://example.com');
  99. Config::set('appliation.index', '');
  100. $_SERVER['REQUEST_URI'] = $uri;
  101. $this->assertEquals(System\Request::uri(), 'user/something');
  102. }
  103. public function segmentedUriProvider2()
  104. {
  105. return array(
  106. array('http://example.com/user/something'),
  107. array('http://example.com/user/something/'),
  108. array('http://example.com/user/something//'),
  109. );
  110. }
  111. public function testMethodForNonSpoofedRequests()
  112. {
  113. $_SERVER['REQUEST_METHOD'] = 'GET';
  114. $this->assertEquals(System\Request::method(), 'GET');
  115. }
  116. public function testMethodForSpoofedRequests()
  117. {
  118. $_SERVER['REQUEST_METHOD'] = 'GET';
  119. $_POST['REQUEST_METHOD'] = 'PUT';
  120. $this->assertEquals(System\Request::method(), 'PUT');
  121. $_POST['REQUEST_METHOD'] = 'DELETE';
  122. $this->assertEquals(System\Request::method(), 'DELETE');
  123. }
  124. public function testRouteIsReturnsFalseWhenNoSuchNamedRouteExists()
  125. {
  126. $route = new System\Route(null, null);
  127. $route->callback = function() {};
  128. System\Request::$route = $route;
  129. $this->assertFalse(System\Request::route_is('test'));
  130. $this->assertFalse(System\Request::route_is_test());
  131. }
  132. public function testRouteIsReturnsFalseWhenWrongRouteNameIsGiven()
  133. {
  134. $this->assertFalse(System\Request::route_is('something'));
  135. $this->assertFalse(System\Request::route_is_something());
  136. }
  137. public function testRouteIsReturnsTrueWhenNamedRouteExists()
  138. {
  139. $this->assertTrue(System\Request::route_is('test'));
  140. $this->assertTrue(System\Request::route_is_test());
  141. }
  142. }