SessionManagerTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. use Laravel\IoC;
  3. use Laravel\Config;
  4. use Laravel\Session\Manager;
  5. class SessionManagerTest extends PHPUnit_Framework_TestCase {
  6. public function setUp()
  7. {
  8. Manager::$session = array();
  9. }
  10. /**
  11. * @dataProvider mockProvider
  12. */
  13. public function test_session_manager_calls_transporter_get($driver, $transporter)
  14. {
  15. $transporter->expects($this->once())->method('get');
  16. Manager::start($driver, $transporter);
  17. }
  18. /**
  19. * @dataProvider mockProvider
  20. */
  21. public function test_session_manager_calls_driver_load_with_session_id($driver, $transporter)
  22. {
  23. $transporter->expects($this->any())
  24. ->method('get')
  25. ->will($this->returnValue('something'));
  26. $driver->expects($this->once())
  27. ->method('load')
  28. ->with($this->equalTo('something'));
  29. Manager::start($driver, $transporter);
  30. }
  31. /**
  32. * @dataProvider mockProvider
  33. */
  34. public function test_session_manager_returns_payload_when_found($driver, $transporter)
  35. {
  36. $this->setDriverExpectation($driver, 'load', $this->getDummySession());
  37. Manager::start($driver, $transporter);
  38. $this->assertEquals(Manager::$session, $this->getDummySession());
  39. }
  40. /**
  41. * @dataProvider mockProvider
  42. */
  43. public function test_session_manager_creates_new_session_when_session_is_null($driver, $transporter)
  44. {
  45. $this->setDriverExpectation($driver, 'load', null);
  46. Manager::start($driver, $transporter);
  47. $this->assertTrue(is_array(Manager::$session['data']));
  48. $this->assertEquals(strlen(Manager::$session['id']), 40);
  49. }
  50. /**
  51. * @dataProvider mockProvider
  52. */
  53. public function test_session_manager_creates_new_session_when_session_is_expired($driver, $transporter)
  54. {
  55. $dateTime = new DateTime('1970-01-01');
  56. $this->setDriverExpectation($driver, 'load', array('last_activity' => $dateTime->getTimestamp()));
  57. Manager::start($driver, $transporter);
  58. $this->assertEquals(strlen(Manager::$session['id']), 40);
  59. }
  60. /**
  61. * @dataProvider mockProvider
  62. */
  63. public function test_session_manager_sets_csrf_token_if_one_is_not_present($driver, $transporter)
  64. {
  65. $session = $this->getDummySession();
  66. unset($session['data']['csrf_token']);
  67. $this->setDriverExpectation($driver, 'load', $session);
  68. Manager::start($driver, $transporter);
  69. $this->assertTrue(isset(Manager::$session['data']['csrf_token']));
  70. $this->assertEquals(strlen(Manager::$session['data']['csrf_token']), 16);
  71. }
  72. /**
  73. * @dataProvider mockProvider
  74. */
  75. public function test_close_method_calls_driver_and_transporter($driver, $transporter)
  76. {
  77. $driver->expects($this->any())
  78. ->method('load')
  79. ->will($this->returnValue($this->getDummySession()));
  80. Manager::start($driver, $transporter);
  81. $driver->expects($this->once())
  82. ->method('save');
  83. $transporter->expects($this->once())
  84. ->method('put');
  85. Manager::close($driver, $transporter);
  86. }
  87. /**
  88. * @dataProvider mockProvider
  89. */
  90. public function test_close_method_calls_sweep_when_driver_is_sweeper($driver, $transporter)
  91. {
  92. $driver = $this->getMock('SweeperStub', array('sweep'));
  93. $driver->expects($this->once())->method('sweep');
  94. Manager::start($driver, $transporter);
  95. Config::$items['session']['sweepage'] = array(100, 100);
  96. Manager::close($driver, $transporter);
  97. }
  98. /**
  99. * @dataProvider mockProvider
  100. */
  101. public function test_close_method_doesnt_call_sweep_when_driver_isnt_sweeper($driver, $transporter)
  102. {
  103. $driver = $this->getMock('Laravel\\Session\\Drivers\\Driver', array('sweep', 'load', 'save', 'delete'));
  104. $driver->expects($this->never())->method('sweep');
  105. Manager::start($driver, $transporter);
  106. Config::$items['session']['sweepage'] = array(100, 100);
  107. Manager::close($driver, $transporter);
  108. }
  109. public function test_has_method_indicates_if_item_exists_in_payload()
  110. {
  111. Manager::$session = $this->getDummyData();
  112. $this->assertTrue(Manager::has('name'));
  113. $this->assertTrue(Manager::has('age'));
  114. $this->assertTrue(Manager::has('gender'));
  115. $this->assertFalse(Manager::has('something'));
  116. $this->assertFalse(Manager::has('id'));
  117. $this->assertFalse(Manager::has('last_activity'));
  118. }
  119. public function test_get_method_returns_item_from_payload()
  120. {
  121. Manager::$session = $this->getDummyData();
  122. $this->assertEquals(Manager::get('name'), 'Taylor');
  123. $this->assertEquals(Manager::get('age'), 25);
  124. $this->assertEquals(Manager::get('gender'), 'male');
  125. }
  126. public function test_get_method_returns_default_when_item_doesnt_exist()
  127. {
  128. Manager::$session = $this->getDummyData();
  129. $this->assertNull(Manager::get('something'));
  130. $this->assertEquals('Taylor', Manager::get('something', 'Taylor'));
  131. $this->assertEquals('Taylor', Manager::get('something', function() {return 'Taylor';}));
  132. }
  133. public function test_put_method_adds_to_payload()
  134. {
  135. Manager::$session = $this->getDummyData();
  136. Manager::put('name', 'Weldon');
  137. Manager::put('workmate', 'Joe');
  138. $this->assertEquals(Manager::$session['data']['name'], 'Weldon');
  139. $this->assertEquals(Manager::$session['data']['workmate'], 'Joe');
  140. }
  141. public function test_flash_method_puts_item_in_flash_data()
  142. {
  143. Manager::$session = array();
  144. Manager::flash('name', 'Taylor');
  145. $this->assertEquals(Manager::$session['data'][':new:name'], 'Taylor');
  146. }
  147. public function test_reflash_keeps_all_session_data()
  148. {
  149. Manager::$session = array('data' => array(':old:name' => 'Taylor', ':old:age' => 25));
  150. Manager::reflash();
  151. $this->assertTrue(isset(Manager::$session['data'][':new:name']));
  152. $this->assertTrue(isset(Manager::$session['data'][':new:age']));
  153. $this->assertFalse(isset(Manager::$session['data'][':old:name']));
  154. $this->assertFalse(isset(Manager::$session['data'][':old:age']));
  155. }
  156. public function test_keep_method_keeps_specified_session_data()
  157. {
  158. Manager::$session = array('data' => array(':old:name' => 'Taylor', ':old:age' => 25));
  159. Manager::keep('name');
  160. $this->assertTrue(isset(Manager::$session['data'][':new:name']));
  161. $this->assertFalse(isset(Manager::$session['data'][':old:name']));
  162. Manager::$session = array('data' => array(':old:name' => 'Taylor', ':old:age' => 25));
  163. Manager::keep(array('name', 'age'));
  164. $this->assertTrue(isset(Manager::$session['data'][':new:name']));
  165. $this->assertTrue(isset(Manager::$session['data'][':new:age']));
  166. $this->assertFalse(isset(Manager::$session['data'][':old:name']));
  167. $this->assertFalse(isset(Manager::$session['data'][':old:age']));
  168. }
  169. public function test_flush_method_clears_payload_data()
  170. {
  171. Manager::$session = array('data' => array('name' => 'Taylor'));
  172. Manager::flush();
  173. $this->assertEquals(count(Manager::$session['data']), 0);
  174. }
  175. public function test_regenerate_session_sets_new_session_id()
  176. {
  177. Manager::$session = array('id' => 'something');
  178. Manager::regenerate();
  179. $this->assertTrue(Manager::$regenerated);
  180. $this->assertEquals(strlen(Manager::$session['id']), 40);
  181. }
  182. public function test_age_method_sets_last_activity_time()
  183. {
  184. $data = $this->getDummyData();
  185. unset($data['last_activity']);
  186. Manager::$session = $data;
  187. Manager::age();
  188. $this->assertTrue(isset(Manager::$session['last_activity']));
  189. }
  190. public function test_age_method_ages_all_flash_data()
  191. {
  192. Manager::$session = $this->getDummyData();
  193. Manager::age();
  194. $this->assertTrue(isset(Manager::$session['data'][':old:age']));
  195. $this->assertFalse(isset(Manager::$session['data'][':old:gender']));
  196. }
  197. public function test_age_method_returns_session_array()
  198. {
  199. Manager::$session = $this->getDummyData();
  200. $age = Manager::age();
  201. $this->assertEquals($age['id'], 'something');
  202. }
  203. // ---------------------------------------------------------------------
  204. // Support Functions
  205. // ---------------------------------------------------------------------
  206. public function getDummyData()
  207. {
  208. return array('id' => 'something', 'last_activity' => time(), 'data' => array(
  209. 'name' => 'Taylor',
  210. ':new:age' => 25,
  211. ':old:gender' => 'male',
  212. 'state' => 'Oregon',
  213. ));
  214. }
  215. // ---------------------------------------------------------------------
  216. // Providers
  217. // ---------------------------------------------------------------------
  218. public function mockProvider()
  219. {
  220. return array(array($this->getMockDriver(), $this->getMockTransporter()));
  221. }
  222. // ---------------------------------------------------------------------
  223. // Support Functions
  224. // ---------------------------------------------------------------------
  225. private function setDriverExpectation($mock, $method, $session)
  226. {
  227. $mock->expects($this->any())
  228. ->method($method)
  229. ->will($this->returnValue($session));
  230. }
  231. private function getMockDriver()
  232. {
  233. return $this->getMock('Laravel\\Session\\Drivers\\Driver');
  234. }
  235. private function getMockTransporter()
  236. {
  237. return $this->getMock('Laravel\\Session\\Transporters\\Transporter', array('get', 'put'));
  238. }
  239. private function getDummySession()
  240. {
  241. return array(
  242. 'id' => 'something',
  243. 'last_activity' => time(),
  244. 'data' => array(
  245. 'name' => 'Taylor',
  246. 'csrf_token' => 'token'
  247. ));
  248. }
  249. private function getConfig()
  250. {
  251. return Config::$items['session'];
  252. }
  253. }
  254. // ---------------------------------------------------------------------
  255. // Stubs
  256. // ---------------------------------------------------------------------
  257. class SweeperStub implements Laravel\Session\Drivers\Driver, Laravel\Session\Drivers\Sweeper {
  258. public function load($id) {}
  259. public function save($session, $config, $exists) {}
  260. public function delete($id) {}
  261. public function sweep($expiration) {}
  262. }