SessionDriverTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. use Laravel\IoC;
  3. class SessionDriverTest extends PHPUnit_Framework_TestCase {
  4. public function testStartMethodStartsNewSessionWhenNullIDGiven()
  5. {
  6. $driver = IoC::resolve('laravel.session.file');
  7. $driver->start(IoC::resolve('laravel.config'), null);
  8. $this->assertTrue(is_string($driver->session['id']));
  9. $this->assertEquals(strlen($driver->session['id']), 40);
  10. $this->assertTrue(is_array($driver->session['data']));
  11. $this->assertEquals(strlen($driver->session['data']['csrf_token']), 16);
  12. }
  13. public function testStartMethodCallsLoadWhenIDIsGiven()
  14. {
  15. $mock = $this->getFileDriverMock();
  16. $mock->expects($this->once())
  17. ->method('load')
  18. ->with($this->equalTo('something'));
  19. $mock->start(IoC::resolve('laravel.config'), 'something');
  20. }
  21. public function testSessionIsLoadedWhenIDIsValid()
  22. {
  23. $mock = $this->getFileDriverMock();
  24. $time = time();
  25. $session = array('id' => 'something', 'last_activity' => $time, 'data' => array('name' => 'Taylor', 'csrf_token' => 'token'));
  26. $this->setMockLoadExpectations($mock, $session);
  27. $mock->start(IoC::resolve('laravel.config'), 'something');
  28. $this->assertEquals($mock->session['id'], 'something');
  29. $this->assertEquals($mock->session['last_activity'], $time);
  30. $this->assertEquals($mock->session['data'], array('name' => 'Taylor', 'csrf_token' => 'token'));
  31. }
  32. public function testSessionIsRestartedWhenLoadedSessionIsExpired()
  33. {
  34. $mock = $this->getFileDriverMock();
  35. $time = new DateTime('2009-01-01');
  36. $time = $time->getTimestamp();
  37. $session = array('id' => 'something', 'last_activity' => $time, 'data' => array('name' => 'Taylor'));
  38. $this->setMockLoadExpectations($mock, $session);
  39. $mock->start(IoC::resolve('laravel.config'), 'something');
  40. $this->assertEquals(strlen($mock->session['id']), 40);
  41. $this->assertFalse(isset($mock->session['data']['name']));
  42. $this->assertTrue(isset($mock->session['data']['csrf_token']));
  43. }
  44. public function testHasMethodIndicatesIfItemExistsInSession()
  45. {
  46. $mock = $this->getSessionDriverWithData();
  47. $this->assertTrue($mock->has('name'));
  48. $this->assertFalse($mock->has('test'));
  49. }
  50. public function testGetMethodGetsItemsFromTheSession()
  51. {
  52. $mock = $this->getSessionDriverWithData();
  53. $this->assertNull($mock->get('test'));
  54. $this->assertEquals($mock->get('name'), 'Taylor');
  55. $this->assertEquals($mock->name, 'Taylor');
  56. $this->assertEquals($mock->get('test', 'Taylor'), 'Taylor');
  57. $this->assertEquals($mock->get('test', function() {return 'Taylor';}), 'Taylor');
  58. $mock->session['data'][':old:test1'] = 'test1';
  59. $mock->session['data'][':new:test2'] = 'test2';
  60. $this->assertEquals($mock->get('test1'), 'test1');
  61. $this->assertEquals($mock->get('test2'), 'test2');
  62. }
  63. public function testPutMethodPutsItemsInTheSession()
  64. {
  65. $mock = $this->getSessionDriverWithData();
  66. $mock->put('name', 'Tony');
  67. $mock->age = 30;
  68. $this->assertEquals($mock->session['data']['name'], 'Tony');
  69. $this->assertEquals($mock->session['data']['age'], 30);
  70. }
  71. public function testFlashMethodPutsItemsInFlashData()
  72. {
  73. $mock = $this->getSessionDriverWithData();
  74. $mock->flash('name', 'James');
  75. $this->assertEquals($mock->session['data'][':new:name'], 'James');
  76. }
  77. public function testKeepMethodRejuvenatesFlashData()
  78. {
  79. $mock = $this->getSessionDriverWithData();
  80. $mock->session['data'][':old:test'] = 'test';
  81. $mock->keep('test');
  82. $this->assertFalse(isset($mock->session['data'][':old:test']));
  83. $this->assertEquals($mock->session['data'][':new:test'], 'test');
  84. }
  85. public function testKeepMethodRejuvenatesAllFlashDataInArray()
  86. {
  87. $mock = $this->getSessionDriverWithData();
  88. $mock->session['data'][':old:test1'] = 'test1';
  89. $mock->session['data'][':old:test2'] = 'test2';
  90. $mock->keep(array('test1', 'test2'));
  91. $this->assertFalse(isset($mock->session['data'][':old:test1']));
  92. $this->assertFalse(isset($mock->session['data'][':old:test2']));
  93. $this->assertEquals($mock->session['data'][':new:test1'], 'test1');
  94. $this->assertEquals($mock->session['data'][':new:test2'], 'test2');
  95. }
  96. public function testReflashMethodRejuvenatesAllFlashData()
  97. {
  98. $mock = $this->getSessionDriverWithData();
  99. $mock->session['data'][':old:test1'] = 'test1';
  100. $mock->session['data'][':old:test2'] = 'test2';
  101. $mock->reflash();
  102. $this->assertFalse(isset($mock->session['data'][':old:test1']));
  103. $this->assertFalse(isset($mock->session['data'][':old:test2']));
  104. $this->assertEquals($mock->session['data'][':new:test1'], 'test1');
  105. $this->assertEquals($mock->session['data'][':new:test2'], 'test2');
  106. }
  107. public function testForgetMethodRemovesDataFromSession()
  108. {
  109. $mock = $this->getSessionDriverWithData();
  110. $mock->forget('name');
  111. $this->assertFalse(isset($mock->session['data']['name']));
  112. }
  113. public function testFlushMethodsClearsEntireSessionData()
  114. {
  115. $mock = $this->getSessionDriverWithData();
  116. $mock->flush();
  117. $this->assertEquals(count($mock->session['data']), 0);
  118. }
  119. public function testRegenerateMethodDeletesSessionAndResetsID()
  120. {
  121. $mock = $this->getMock('Laravel\\Session\\Drivers\\File', array('load', 'delete'), $this->getFileDriverConstructor());
  122. $this->setMockLoadExpectations($mock, $this->getDummySession());
  123. $mock->expects($this->once())
  124. ->method('delete')
  125. ->with($this->equalTo('something'));
  126. $mock->start(IoC::resolve('laravel.config'), 'something');
  127. $mock->regenerate();
  128. $this->assertEquals(strlen($mock->session['id']), 40);
  129. }
  130. public function testCloseMethodFlashesOldInputData()
  131. {
  132. $mock = $this->getMock('Laravel\\Session\\Drivers\\File', array('save'), $this->getFileDriverConstructor());
  133. $this->setMockLoadExpectations($mock, $this->getDummySession());
  134. $mock->start(IoC::resolve('laravel.config'), 'something');
  135. $mock->close(new InputStub, time());
  136. $this->assertEquals($mock->session['data'][':old:laravel_old_input'], array('name' => 'Taylor'));
  137. }
  138. public function testCloseMethodAgesFlashData()
  139. {
  140. $mock = $this->getSessionDriverWithData();
  141. $mock->session['data'][':old:old'] = 'old';
  142. $mock->flash('flash', 'flash');
  143. $mock->close(new InputStub, time());
  144. $this->assertFalse(isset($mock->session['data'][':old:old']));
  145. $this->assertFalse(isset($mock->session['data'][':new:flash']));
  146. $this->assertEquals($mock->session['data'][':old:flash'], 'flash');
  147. }
  148. public function testCloseMethodSavesSession()
  149. {
  150. $mock = $this->getMock('Laravel\\Session\\Drivers\\File', array('load', 'save', 'sweep'), $this->getFileDriverConstructor());
  151. $session = $this->getDummySession();
  152. $session['data']['csrf_token'] = 'token';
  153. $this->setMockLoadExpectations($mock, $session);
  154. $expect = $session;
  155. Laravel\Arr::set($expect, 'data.:old:laravel_old_input', array('name' => 'Taylor'));
  156. $mock->expects($this->once())
  157. ->method('save')
  158. ->with($this->equalTo($expect));
  159. $mock->start(IoC::resolve('laravel.config'), 'something');
  160. $mock->close(new InputStub, $mock->session['last_activity']);
  161. }
  162. /**
  163. * @dataProvider cookieMethodProvider
  164. */
  165. public function testCookieMethodWritesCookie($expire_on_close, $minutes)
  166. {
  167. $mock = $this->getSessionDriverWithData();
  168. $config = IoC::resolve('laravel.config');
  169. $config->set('session.expire_on_close', $expire_on_close);
  170. $mock->start($config, 'something');
  171. $cookieMock = $this->getMock('Laravel\\Cookie', array('put'), array(array()));
  172. $cookieMock->expects($this->once())
  173. ->method('put')
  174. ->with('laravel_session', 'something', $minutes, $config->get('session.path'), $config->get('session.domain'));
  175. $mock->cookie($cookieMock);
  176. }
  177. // -----------------------------------------------------------------------------------
  178. // Utility Methods & Providers
  179. // -----------------------------------------------------------------------------------
  180. public function getSessionDriverWithData()
  181. {
  182. $mock = $this->getFileDriverMock();
  183. $this->setMockLoadExpectations($mock, $this->getDummySession());
  184. $mock->start(IoC::resolve('laravel.config'), 'something');
  185. return $mock;
  186. }
  187. private function getFileDriverMock()
  188. {
  189. return $this->getMock('Laravel\\Session\\Drivers\\File', array('load', 'save'), $this->getFileDriverConstructor());
  190. }
  191. private function getFileDriverConstructor()
  192. {
  193. return array(IoC::resolve('laravel.file'), null);
  194. }
  195. private function setMockLoadExpectations($mock, $session)
  196. {
  197. $mock->expects($this->any())
  198. ->method('load')
  199. ->will($this->returnValue($session));
  200. }
  201. private function getDummySession()
  202. {
  203. return array('id' => 'something', 'last_activity' => time(), 'data' => array('name' => 'Taylor'));
  204. }
  205. public function cookieMethodProvider()
  206. {
  207. return array(
  208. array(false, 60),
  209. array(true, 0),
  210. );
  211. }
  212. }
  213. // -----------------------------------------------------------------------------------
  214. // Stub Classes
  215. // -----------------------------------------------------------------------------------
  216. class InputStub extends Laravel\Input {
  217. public function __construct() {}
  218. public function get($key = null, $default = null)
  219. {
  220. return array('name' => 'Taylor');
  221. }
  222. }
  223. class CookieStub extends Laravel\Cookie {
  224. public function put() {}
  225. }