redirect.test.php 3.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

use Laravel\Routing\Router;

class RedirectTest extends PHPUnit_Framework_TestCase {

	/**
	 * Setup the test environment.
	 */
	public function setUp()
	{
		Config::set('session.driver', 'foo');
		Router::$routes = array();
		Router::$names = array();
15
		URL::$base = 'http://localhost/';
16 17 18 19 20 21 22 23 24 25 26 27
		Config::set('application.index', '');
	}

	/**
	 * Destroy the test environment.
	 */
	public function tearDown()
	{
		// @todo clear httpfoundation request data
		Config::set('session.driver', '');
		Router::$routes = array();
		Router::$names = array();
28
		URL::$base = '';
29 30 31 32 33 34 35 36 37 38 39 40 41
		Config::set('application.index', 'index.php');
		Session::$instance = null;
	}

	/**
	 * Test the Redirect::to method.
	 *
	 * @group laravel
	 */
	public function testSimpleRedirectSetsCorrectHeaders()
	{
		$redirect = Redirect::to('user/profile');

42 43
		$this->assertEquals(302, $redirect->status());
		$this->assertEquals('http://localhost/user/profile', $redirect->headers()->get('location'));
44 45 46

		$redirect = Redirect::to('user/profile', 301, true);

47 48
		$this->assertEquals(301, $redirect->status());
		$this->assertEquals('https://localhost/user/profile', $redirect->headers()->get('location'));
49 50 51

		$redirect = Redirect::to_secure('user/profile', 301);

52 53
		$this->assertEquals(301, $redirect->status());
		$this->assertEquals('https://localhost/user/profile', $redirect->headers()->get('location'));
54 55 56 57 58 59 60 61 62 63 64 65 66
	}

	/**
	 * Test the Redirect::to_route method.
	 *
	 * @group laravel
	 */
	public function testRedirectsCanBeGeneratedForNamedRoutes()
	{
		Route::get('redirect', array('as' => 'redirect'));
		Route::get('redirect/(:any)/(:any)', array('as' => 'redirect-2'));
		Route::get('secure/redirect', array('https' => true, 'as' => 'redirect-3'));

67 68 69 70
		$this->assertEquals(301, Redirect::to_route('redirect', array(), 301, true)->status());
		$this->assertEquals('http://localhost/redirect', Redirect::to_route('redirect')->headers()->get('location'));
		$this->assertEquals('https://localhost/secure/redirect', Redirect::to_route('redirect-3', array(), 302)->headers()->get('location'));
		$this->assertEquals('http://localhost/redirect/1/2', Redirect::to_route('redirect-2', array('1', '2'))->headers()->get('location'));
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	}

	/**
	 * Test the Redirect::with method.
	 *
	 * @group laravel
	 */
	public function testWithMethodFlashesItemToSession()
	{
		$this->setSession();

		$redirect = Redirect::to('')->with('name', 'Taylor');

		$this->assertEquals('Taylor', Session::$instance->session['data'][':new:']['name']);
	}

	/**
	 * Test the Redirect::with_input function.
	 *
	 * @group laravel
	 */
	public function testWithInputMethodFlashesInputToTheSession()
	{
		$this->setSession();

		$input = array('name' => 'Taylor', 'age' => 25);
		Request::foundation()->request->add($input);

		$redirect = Redirect::to('')->with_input();

		$this->assertEquals($input, Session::$instance->session['data'][':new:']['laravel_old_input']);

		$redirect = Redirect::to('')->with_input('only', array('name'));

		$this->assertEquals(array('name' => 'Taylor'), Session::$instance->session['data'][':new:']['laravel_old_input']);

		$redirect = Redirect::to('')->with_input('except', array('name'));

		$this->assertEquals(array('age' => 25), Session::$instance->session['data'][':new:']['laravel_old_input']);
	}

	/**
	 * Test the Redirect::with_errors method.
	 *
	 * @group laravel
	 */
	public function testWithErrorsFlashesErrorsToTheSession()
	{
		$this->setSession();

		Redirect::to('')->with_errors(array('name' => 'Taylor'));

		$this->assertEquals(array('name' => 'Taylor'), Session::$instance->session['data'][':new:']['errors']);

		$validator = Validator::make(array(), array());
		$validator->errors = array('name' => 'Taylor');

		Redirect::to('')->with_errors($validator);

		$this->assertEquals(array('name' => 'Taylor'), Session::$instance->session['data'][':new:']['errors']);
	}

	/**
	 * Set the session payload instance.
	 */
	protected function setSession()
	{
		$driver = $this->getMock('Laravel\\Session\\Drivers\\Driver');

		Session::$instance = new Laravel\Session\Payload($driver);
	}

}