| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 | <?phpuse Laravel\IoC;class SessionDriverTest extends PHPUnit_Framework_TestCase {	public function testStartMethodStartsNewSessionWhenNullIDGiven()	{		$driver = IoC::resolve('laravel.session.file');				$driver->start(IoC::resolve('laravel.config'), null);				$this->assertTrue(is_string($driver->session['id']));		$this->assertEquals(strlen($driver->session['id']), 40);		$this->assertTrue(is_array($driver->session['data']));		$this->assertEquals(strlen($driver->session['data']['csrf_token']), 16);	}	public function testStartMethodCallsLoadWhenIDIsGiven()	{		$mock = $this->getFileDriverMock();		$mock->expects($this->once())							->method('load')							->with($this->equalTo('something'));		$mock->start(IoC::resolve('laravel.config'), 'something');	}	public function testSessionIsLoadedWhenIDIsValid()	{		$mock = $this->getFileDriverMock();		$time = time();		$session = array('id' => 'something', 'last_activity' => $time, 'data' => array('name' => 'Taylor', 'csrf_token' => 'token'));		$this->setMockLoadExpectations($mock, $session);		$mock->start(IoC::resolve('laravel.config'), 'something');		$this->assertEquals($mock->session['id'], 'something');		$this->assertEquals($mock->session['last_activity'], $time);		$this->assertEquals($mock->session['data'], array('name' => 'Taylor', 'csrf_token' => 'token'));	}	public function testSessionIsRestartedWhenLoadedSessionIsExpired()	{		$mock = $this->getFileDriverMock();				$time = new DateTime('2009-01-01');		$time = $time->getTimestamp();		$session = array('id' => 'something', 'last_activity' => $time, 'data' => array('name' => 'Taylor'));		$this->setMockLoadExpectations($mock, $session);		$mock->start(IoC::resolve('laravel.config'), 'something');		$this->assertEquals(strlen($mock->session['id']), 40);		$this->assertFalse(isset($mock->session['data']['name']));		$this->assertTrue(isset($mock->session['data']['csrf_token']));	}	public function testHasMethodIndicatesIfItemExistsInSession()	{		$mock = $this->getSessionDriverWithData();		$this->assertTrue($mock->has('name'));		$this->assertFalse($mock->has('test'));	}	public function testGetMethodGetsItemsFromTheSession()	{		$mock = $this->getSessionDriverWithData();		$this->assertNull($mock->get('test'));		$this->assertEquals($mock->get('name'), 'Taylor');		$this->assertEquals($mock->name, 'Taylor');		$this->assertEquals($mock->get('test', 'Taylor'), 'Taylor');		$this->assertEquals($mock->get('test', function() {return 'Taylor';}), 'Taylor');		$mock->session['data'][':old:test1'] = 'test1';		$mock->session['data'][':new:test2'] = 'test2';		$this->assertEquals($mock->get('test1'), 'test1');		$this->assertEquals($mock->get('test2'), 'test2');	}	public function testPutMethodPutsItemsInTheSession()	{		$mock = $this->getSessionDriverWithData();		$mock->put('name', 'Tony');		$mock->age = 30;				$this->assertEquals($mock->session['data']['name'], 'Tony');		$this->assertEquals($mock->session['data']['age'], 30);	}	public function testFlashMethodPutsItemsInFlashData()	{		$mock = $this->getSessionDriverWithData();				$mock->flash('name', 'James');				$this->assertEquals($mock->session['data'][':new:name'], 'James');		}	public function testKeepMethodRejuvenatesFlashData()	{		$mock = $this->getSessionDriverWithData();				$mock->session['data'][':old:test'] = 'test';		$mock->keep('test');				$this->assertFalse(isset($mock->session['data'][':old:test']));		$this->assertEquals($mock->session['data'][':new:test'], 'test');	}	public function testKeepMethodRejuvenatesAllFlashDataInArray()	{		$mock = $this->getSessionDriverWithData();		$mock->session['data'][':old:test1'] = 'test1';		$mock->session['data'][':old:test2'] = 'test2';		$mock->keep(array('test1', 'test2'));		$this->assertFalse(isset($mock->session['data'][':old:test1']));		$this->assertFalse(isset($mock->session['data'][':old:test2']));		$this->assertEquals($mock->session['data'][':new:test1'], 'test1');		$this->assertEquals($mock->session['data'][':new:test2'], 'test2');	}	public function testReflashMethodRejuvenatesAllFlashData()	{		$mock = $this->getSessionDriverWithData();		$mock->session['data'][':old:test1'] = 'test1';		$mock->session['data'][':old:test2'] = 'test2';		$mock->reflash();		$this->assertFalse(isset($mock->session['data'][':old:test1']));		$this->assertFalse(isset($mock->session['data'][':old:test2']));		$this->assertEquals($mock->session['data'][':new:test1'], 'test1');		$this->assertEquals($mock->session['data'][':new:test2'], 'test2');	}	public function testForgetMethodRemovesDataFromSession()	{		$mock = $this->getSessionDriverWithData();				$mock->forget('name');		$this->assertFalse(isset($mock->session['data']['name']));	}	public function testFlushMethodsClearsEntireSessionData()	{		$mock = $this->getSessionDriverWithData();		$mock->flush();		$this->assertEquals(count($mock->session['data']), 0);	}	public function testRegenerateMethodDeletesSessionAndResetsID()	{		$mock = $this->getMock('Laravel\\Session\\Drivers\\File', array('load', 'delete'), $this->getFileDriverConstructor());		$this->setMockLoadExpectations($mock, $this->getDummySession());		$mock->expects($this->once())							->method('delete')							->with($this->equalTo('something'));		$mock->start(IoC::resolve('laravel.config'), 'something');		$mock->regenerate();		$this->assertEquals(strlen($mock->session['id']), 40);	}	public function testCloseMethodFlashesOldInputData()	{		$mock = $this->getMock('Laravel\\Session\\Drivers\\File', array('save'), $this->getFileDriverConstructor());		$this->setMockLoadExpectations($mock, $this->getDummySession());		$mock->start(IoC::resolve('laravel.config'), 'something');		$mock->close(new InputStub, time());		$this->assertEquals($mock->session['data'][':old:laravel_old_input'], array('name' => 'Taylor'));	}	public function testCloseMethodAgesFlashData()	{		$mock = $this->getSessionDriverWithData();		$mock->session['data'][':old:old'] = 'old';		$mock->flash('flash', 'flash');		$mock->close(new InputStub, time());		$this->assertFalse(isset($mock->session['data'][':old:old']));		$this->assertFalse(isset($mock->session['data'][':new:flash']));		$this->assertEquals($mock->session['data'][':old:flash'], 'flash');	}	public function testCloseMethodSavesSession()	{		$mock = $this->getMock('Laravel\\Session\\Drivers\\File', array('load', 'save', 'sweep'), $this->getFileDriverConstructor());		$session = $this->getDummySession();		$session['data']['csrf_token'] = 'token';		$this->setMockLoadExpectations($mock, $session);		$expect = $session;		Laravel\Arr::set($expect, 'data.:old:laravel_old_input', array('name' => 'Taylor'));		$mock->expects($this->once())							->method('save')							->with($this->equalTo($expect));		$mock->start(IoC::resolve('laravel.config'), 'something');		$mock->close(new InputStub, $mock->session['last_activity']);	}	/**	 * @dataProvider cookieMethodProvider	 */	public function testCookieMethodWritesCookie($expire_on_close, $minutes)	{		$mock = $this->getSessionDriverWithData();		$config = IoC::resolve('laravel.config');		$config->set('session.expire_on_close', $expire_on_close);		$mock->start($config, 'something');		$cookieMock = $this->getMock('Laravel\\Cookie', array('put'), array(array()));		$cookieMock->expects($this->once())						->method('put')						->with('laravel_session', 'something', $minutes, $config->get('session.path'), $config->get('session.domain'));		$mock->cookie($cookieMock);	}	// -----------------------------------------------------------------------------------	// Utility Methods & Providers	// -----------------------------------------------------------------------------------	public function getSessionDriverWithData()	{		$mock = $this->getFileDriverMock();		$this->setMockLoadExpectations($mock, $this->getDummySession());		$mock->start(IoC::resolve('laravel.config'), 'something');		return $mock;	}	private function getFileDriverMock()	{		return $this->getMock('Laravel\\Session\\Drivers\\File', array('load', 'save'), $this->getFileDriverConstructor());	}	private function getFileDriverConstructor()	{		return array(IoC::resolve('laravel.file'), null);	}	private function setMockLoadExpectations($mock, $session)	{		$mock->expects($this->any())							->method('load')							->will($this->returnValue($session));	}	private function getDummySession()	{		return array('id' => 'something', 'last_activity' => time(), 'data' => array('name' => 'Taylor'));	}	public function cookieMethodProvider()	{		return array(			array(false, 60),			array(true, 0),		);	}}// -----------------------------------------------------------------------------------// Stub Classes// -----------------------------------------------------------------------------------class InputStub extends Laravel\Input {	public function __construct() {}	public function get($key = null, $default = null)	{		return array('name' => 'Taylor');	}}class CookieStub extends Laravel\Cookie {	public function put() {}}
 |