cookie.test.php 3.07 KB
Newer Older
1 2
<?php namespace Laravel;

3 4
use Symfony\Component\HttpFoundation\LaravelRequest as RequestFoundation;

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/**
 * Stub the global setcookie method into the Laravel namespace.
 */
function setcookie($name, $value, $time, $path, $domain, $secure)
{
	$_SERVER['cookie.stub'][$name] = compact('name', 'value', 'time', 'path', 'domain', 'secure');
}

function headers_sent()
{
	return $_SERVER['function.headers_sent'];
}

class CookieTest extends \PHPUnit_Framework_TestCase {

	/**
	 * Setup the test environment.
	 */
	public function setUp()
	{
		Cookie::$jar = array();
	}

	/**
	 * Tear down the test environment.
	 */
	public function tearDown()
	{
		Cookie::$jar = array();
	}

	/**
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	 * Set one of the $_SERVER variables.
	 *
	 * @param string  $key
	 * @param string  $value
	 */
	protected function setServerVar($key, $value)
	{
		$_SERVER[$key] = $value;

		$this->restartRequest();
	}

	/**
	 * Reinitialize the global request.
	 * 
	 * @return void
	 */
	protected function restartRequest()
	{
		// FIXME: Ugly hack, but old contents from previous requests seem to
		// trip up the Foundation class.
		$_FILES = array();

		Request::$foundation = RequestFoundation::createFromGlobals();
	}

	/**
64 65 66 67 68 69
	 * Test Cookie::has method.
	 *
	 * @group laravel
	 */
	public function testHasMethodIndicatesIfCookieInSet()
	{
70
		Cookie::$jar['foo'] = array('value' => Cookie::hash('bar').'+bar');
71 72 73 74 75 76 77 78 79 80 81 82 83 84
		$this->assertTrue(Cookie::has('foo'));
		$this->assertFalse(Cookie::has('bar'));

		Cookie::put('baz', 'foo');
		$this->assertTrue(Cookie::has('baz'));
	}

	/**
	 * Test the Cookie::get method.
	 *
	 * @group laravel
	 */
	public function testGetMethodCanReturnValueOfCookies()
	{
85
		Cookie::$jar['foo'] = array('value' => Cookie::hash('bar').'+bar');
86 87 88 89 90 91 92 93 94 95 96 97 98 99
		$this->assertEquals('bar', Cookie::get('foo'));

		Cookie::put('bar', 'baz');
		$this->assertEquals('baz', Cookie::get('bar'));
	}

	/**
	 * Test Cookie::forever method.
	 *
	 * @group laravel
	 */
	public function testForeverShouldUseATonOfMinutes()
	{
		Cookie::forever('foo', 'bar');
100
		$this->assertEquals(Cookie::hash('bar').'+bar', Cookie::$jar['foo']['value']);
101 102 103 104 105 106

		// Shouldn't be able to test this cause while we indicate -2000 seconds 
		// cookie expiration store timestamp.
		// $this->assertEquals(525600, Cookie::$jar['foo']['expiration']);

		$this->setServerVar('HTTPS', 'on');
107 108 109 110 111

		Cookie::forever('bar', 'baz', 'path', 'domain', true);
		$this->assertEquals('path', Cookie::$jar['bar']['path']);
		$this->assertEquals('domain', Cookie::$jar['bar']['domain']);
		$this->assertTrue(Cookie::$jar['bar']['secure']);
112 113

		$this->setServerVar('HTTPS', 'off');
114 115 116 117 118 119 120 121 122
	}

	/**
	 * Test the Cookie::forget method.
	 *
	 * @group laravel
	 */
	public function testForgetSetsCookieWithExpiration()
	{
123
		Cookie::forget('bar', 'path', 'domain');
124 125 126 127 128

		// Shouldn't be able to test this cause while we indicate -2000 seconds 
		// cookie expiration store timestamp.
		//$this->assertEquals(-2000, Cookie::$jar['bar']['expiration']);

129 130
		$this->assertEquals('path', Cookie::$jar['bar']['path']);
		$this->assertEquals('domain', Cookie::$jar['bar']['domain']);
131
		$this->assertFalse(Cookie::$jar['bar']['secure']);
132 133 134
	}

}