123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php namespace Laravel;
- 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 {
-
- public function setUp()
- {
- Cookie::$jar = array();
- }
-
- public function tearDown()
- {
- Cookie::$jar = array();
- }
-
- public function testHasMethodIndicatesIfCookieInSet()
- {
- Cookie::$jar['foo'] = array('value' => 'bar');
- $this->assertTrue(Cookie::has('foo'));
- $this->assertFalse(Cookie::has('bar'));
- Cookie::put('baz', 'foo');
- $this->assertTrue(Cookie::has('baz'));
- }
-
- public function testGetMethodCanReturnValueOfCookies()
- {
- Cookie::$jar['foo'] = array('value' => 'bar');
- $this->assertEquals('bar', Cookie::get('foo'));
- Cookie::put('bar', 'baz');
- $this->assertEquals('baz', Cookie::get('bar'));
- }
-
- public function testTamperedCookiesAreReturnedAsNull()
- {
- $_COOKIE['foo'] = Cookie::sign('foo', 'bar');
- $this->assertEquals('bar', Cookie::get('foo'));
- $_COOKIE['foo'] .= '-baz';
- $this->assertNull(Cookie::get('foo'));
- $_COOKIE['foo'] = Cookie::sign('foo', 'bar');
- $_COOKIE['foo'] = 'aslk'.$_COOKIE['foo'];
- $this->assertNull(Cookie::get('foo'));
- }
-
- public function testForeverShouldUseATonOfMinutes()
- {
- Cookie::forever('foo', 'bar');
- $this->assertEquals('bar', Cookie::$jar['foo']['value']);
- $this->assertEquals(525600, Cookie::$jar['foo']['minutes']);
- 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']);
- }
-
- public function testForgetSetsCookieWithExpiration()
- {
- Cookie::forget('bar', 'path', 'domain', true);
- $this->assertEquals(-2000, Cookie::$jar['bar']['minutes']);
- $this->assertEquals('path', Cookie::$jar['bar']['path']);
- $this->assertEquals('domain', Cookie::$jar['bar']['domain']);
- $this->assertTrue(Cookie::$jar['bar']['secure']);
- }
-
- public function testSendMethodSetsProperValuesOnCookie()
- {
- $_SERVER['cookie.stub'] = array();
- $_SERVER['function.headers_sent'] = false;
- Cookie::send();
- $this->assertTrue(count($_SERVER['cookie.stub']) == 0);
- Cookie::put('foo', 'bar', 20, 'path', 'domain', true);
- Cookie::send();
- $this->assertTrue(count($_SERVER['cookie.stub']) == 1);
- $this->assertEquals('foo', $_SERVER['cookie.stub']['foo']['name']);
- $this->assertEquals(Cookie::sign('foo', 'bar'), $_SERVER['cookie.stub']['foo']['value']);
- $this->assertEquals('path', $_SERVER['cookie.stub']['foo']['path']);
- $this->assertEquals('domain', $_SERVER['cookie.stub']['foo']['domain']);
- $this->assertEquals((time() + (20 * 60)), $_SERVER['cookie.stub']['foo']['time']);
- $this->assertTrue($_SERVER['cookie.stub']['foo']['secure']);
- Cookie::put('bar', 'baz', 0);
- Cookie::send();
- $this->assertEquals(0, $_SERVER['cookie.stub']['bar']['time']);
- }
- }
|