redirect.test.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. use Laravel\Routing\Router;
  3. class RedirectTest extends PHPUnit_Framework_TestCase {
  4. /**
  5. * Setup the test environment.
  6. */
  7. public function setUp()
  8. {
  9. Config::set('session.driver', 'foo');
  10. Router::$routes = array();
  11. Router::$names = array();
  12. Config::set('application.url', 'http://localhost');
  13. Config::set('application.index', '');
  14. }
  15. /**
  16. * Destroy the test environment.
  17. */
  18. public function tearDown()
  19. {
  20. Input::$input = array();
  21. Config::set('session.driver', '');
  22. Router::$routes = array();
  23. Router::$names = array();
  24. Config::set('application.url', '');
  25. Config::set('application.index', 'index.php');
  26. Session::$instance = null;
  27. }
  28. /**
  29. * Test the Redirect::to method.
  30. *
  31. * @group laravel
  32. */
  33. public function testSimpleRedirectSetsCorrectHeaders()
  34. {
  35. $redirect = Redirect::to('user/profile');
  36. $this->assertEquals(302, $redirect->status);
  37. $this->assertEquals('http://localhost/user/profile', $redirect->headers['location']);
  38. $redirect = Redirect::to('user/profile', 301, true);
  39. $this->assertEquals(301, $redirect->status);
  40. $this->assertEquals('https://localhost/user/profile', $redirect->headers['location']);
  41. $redirect = Redirect::to_secure('user/profile', 301);
  42. $this->assertEquals(301, $redirect->status);
  43. $this->assertEquals('https://localhost/user/profile', $redirect->headers['location']);
  44. }
  45. /**
  46. * Test the Redirect::to_route method.
  47. *
  48. * @group laravel
  49. */
  50. public function testRedirectsCanBeGeneratedForNamedRoutes()
  51. {
  52. Route::get('redirect', array('as' => 'redirect'));
  53. Route::get('redirect/(:any)/(:any)', array('as' => 'redirect-2'));
  54. Route::get('secure/redirect', array('https' => true, 'as' => 'redirect-3'));
  55. $this->assertEquals(301, Redirect::to_route('redirect', array(), 301, true)->status);
  56. $this->assertEquals('http://localhost/redirect', Redirect::to_route('redirect')->headers['location']);
  57. $this->assertEquals('https://localhost/secure/redirect', Redirect::to_route('redirect-3', array(), 302)->headers['location']);
  58. $this->assertEquals('http://localhost/redirect/1/2', Redirect::to_route('redirect-2', array('1', '2'))->headers['location']);
  59. }
  60. /**
  61. * Test the Redirect::with method.
  62. *
  63. * @group laravel
  64. */
  65. public function testWithMethodFlashesItemToSession()
  66. {
  67. $this->setSession();
  68. $redirect = Redirect::to('')->with('name', 'Taylor');
  69. $this->assertEquals('Taylor', Session::$instance->session['data'][':new:']['name']);
  70. }
  71. /**
  72. * Test the Redirect::with_input function.
  73. *
  74. * @group laravel
  75. */
  76. public function testWithInputMethodFlashesInputToTheSession()
  77. {
  78. $this->setSession();
  79. Input::$input = $input = array('name' => 'Taylor', 'age' => 25);
  80. $redirect = Redirect::to('')->with_input();
  81. $this->assertEquals($input, Session::$instance->session['data'][':new:']['laravel_old_input']);
  82. $redirect = Redirect::to('')->with_input('only', array('name'));
  83. $this->assertEquals(array('name' => 'Taylor'), Session::$instance->session['data'][':new:']['laravel_old_input']);
  84. $redirect = Redirect::to('')->with_input('except', array('name'));
  85. $this->assertEquals(array('age' => 25), Session::$instance->session['data'][':new:']['laravel_old_input']);
  86. }
  87. /**
  88. * Test the Redirect::with_errors method.
  89. *
  90. * @group laravel
  91. */
  92. public function testWithErrorsFlashesErrorsToTheSession()
  93. {
  94. $this->setSession();
  95. Redirect::to('')->with_errors(array('name' => 'Taylor'));
  96. $this->assertEquals(array('name' => 'Taylor'), Session::$instance->session['data'][':new:']['errors']);
  97. $validator = Validator::make(array(), array());
  98. $validator->errors = array('name' => 'Taylor');
  99. Redirect::to('')->with_errors($validator);
  100. $this->assertEquals(array('name' => 'Taylor'), Session::$instance->session['data'][':new:']['errors']);
  101. }
  102. /**
  103. * Set the session payload instance.
  104. */
  105. protected function setSession()
  106. {
  107. $driver = $this->getMock('Laravel\\Session\\Drivers\\Driver');
  108. Session::$instance = new Laravel\Session\Payload($driver);
  109. }
  110. }