SessionManagerTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. use Laravel\IoC;
  3. use Laravel\Session\Manager;
  4. class SessionManagerTest extends PHPUnit_Framework_TestCase {
  5. /**
  6. * @dataProvider mockProvider
  7. */
  8. public function test_session_manager_calls_transporter_get($driver, $transporter)
  9. {
  10. $transporter->expects($this->once())->method('get');
  11. $manager = new Manager($driver, $transporter);
  12. $manager->payload($this->getConfig());
  13. }
  14. /**
  15. * @dataProvider mockProvider
  16. */
  17. public function test_session_manager_calls_driver_load_with_session_id($driver, $transporter)
  18. {
  19. $transporter->expects($this->any())
  20. ->method('get')
  21. ->will($this->returnValue('something'));
  22. $driver->expects($this->once())
  23. ->method('load')
  24. ->with($this->equalTo('something'));
  25. $manager = new Manager($driver, $transporter);
  26. $manager->payload($this->getConfig());
  27. }
  28. /**
  29. * @dataProvider mockProvider
  30. */
  31. public function test_session_manager_returns_payload_when_found($driver, $transporter)
  32. {
  33. $this->setDriverExpectation($driver, 'load', $this->getDummySession());
  34. $manager = new Manager($driver, $transporter);
  35. $payload = $manager->payload($this->getConfig());
  36. $this->assertInstanceOf('Laravel\\Session\\Payload', $payload);
  37. $this->assertEquals($payload->session, $this->getDummySession());
  38. }
  39. /**
  40. * @dataProvider mockProvider
  41. */
  42. public function test_session_manager_creates_new_session_when_session_is_null($driver, $transporter)
  43. {
  44. $this->setDriverExpectation($driver, 'load', null);
  45. $manager = new Manager($driver, $transporter);
  46. $payload = $manager->payload($this->getConfig());
  47. $this->assertInstanceOf('Laravel\\Session\\Payload', $payload);
  48. $this->assertEquals(strlen($payload->session['id']), 40);
  49. $this->assertTrue(is_array($payload->session['data']));
  50. }
  51. /**
  52. * @dataProvider mockProvider
  53. */
  54. public function test_session_manager_creates_new_session_when_session_is_expired($driver, $transporter)
  55. {
  56. $dateTime = new DateTime('1970-01-01');
  57. $this->setDriverExpectation($driver, 'load', array('last_activity' => $dateTime->getTimestamp()));
  58. $manager = new Manager($driver, $transporter);
  59. $payload = $manager->payload($this->getConfig());
  60. $this->assertInstanceOf('Laravel\\Session\\Payload', $payload);
  61. $this->assertEquals(strlen($payload->session['id']), 40);
  62. }
  63. /**
  64. * @dataProvider mockProvider
  65. */
  66. public function test_session_manager_sets_csrf_token_if_one_is_not_present($driver, $transporter)
  67. {
  68. $session = $this->getDummySession();
  69. unset($session['data']['csrf_token']);
  70. $this->setDriverExpectation($driver, 'load', $session);
  71. $manager = new Manager($driver, $transporter);
  72. $payload = $manager->payload($this->getConfig());
  73. $this->assertTrue(isset($payload->session['data']['csrf_token']));
  74. $this->assertEquals(strlen($payload->session['data']['csrf_token']), 16);
  75. }
  76. /**
  77. * @dataProvider mockProvider
  78. */
  79. public function test_close_method_calls_driver_and_transporter($driver, $transporter)
  80. {
  81. $driver->expects($this->any())
  82. ->method('load')
  83. ->will($this->returnValue($this->getDummySession()));
  84. $manager = new Manager($driver, $transporter);
  85. $payload = $this->getMock(
  86. 'Laravel\\Session\\Payload',
  87. array('age'),
  88. array(array('id' => 'something'))
  89. );
  90. $payload->expects($this->any())
  91. ->method('age')
  92. ->will($this->returnValue('something'));
  93. $driver->expects($this->once())
  94. ->method('save')
  95. ->with('something', $this->getConfig());
  96. $transporter->expects($this->once())
  97. ->method('put')
  98. ->with('something', $this->getConfig());
  99. $manager->close($payload, $this->getConfig());
  100. }
  101. /**
  102. * @dataProvider mockProvider
  103. */
  104. public function test_close_method_calls_sweep_when_driver_is_sweeper($driver, $transporter)
  105. {
  106. $driver = $this->getMock('SweeperStub', array('sweep'));
  107. $driver->expects($this->once())->method('sweep');
  108. $manager = new Manager($driver, $transporter);
  109. $config = $this->getConfig();
  110. $config['sweepage'] = array(100, 100);
  111. $manager->close(new Laravel\Session\Payload($this->getDummySession()), $config);
  112. }
  113. /**
  114. * @dataProvider mockProvider
  115. */
  116. public function test_close_method_doesnt_call_sweep_when_driver_isnt_sweeper($driver, $transporter)
  117. {
  118. $driver = $this->getMock('Laravel\\Session\\Drivers\\Driver', array('sweep', 'load', 'save', 'delete'));
  119. $driver->expects($this->never())->method('sweep');
  120. $manager = new Manager($driver, $transporter);
  121. $config = $this->getConfig();
  122. $config['sweepage'] = array(100, 100);
  123. $manager->close(new Laravel\Session\Payload($this->getDummySession()), $config);
  124. }
  125. // ---------------------------------------------------------------------
  126. // Providers
  127. // ---------------------------------------------------------------------
  128. public function mockProvider()
  129. {
  130. return array(array($this->getMockDriver(), $this->getMockTransporter()));
  131. }
  132. // ---------------------------------------------------------------------
  133. // Support Functions
  134. // ---------------------------------------------------------------------
  135. private function setDriverExpectation($mock, $method, $session)
  136. {
  137. $mock->expects($this->any())
  138. ->method($method)
  139. ->will($this->returnValue($session));
  140. }
  141. private function getMockDriver()
  142. {
  143. return $this->getMock('Laravel\\Session\\Drivers\\Driver');
  144. }
  145. private function getMockTransporter()
  146. {
  147. return $this->getMock('Laravel\\Session\\Transporters\\Transporter', array('get', 'put'));
  148. }
  149. private function getDummySession()
  150. {
  151. return array(
  152. 'id' => 'something',
  153. 'last_activity' => time(),
  154. 'data' => array(
  155. 'name' => 'Taylor',
  156. 'csrf_token' => 'token'
  157. ));
  158. }
  159. private function getConfig()
  160. {
  161. return Laravel\Config::get('session');
  162. }
  163. }
  164. // ---------------------------------------------------------------------
  165. // Stubs
  166. // ---------------------------------------------------------------------
  167. class SweeperStub implements Laravel\Session\Drivers\Driver, Laravel\Session\Drivers\Sweeper {
  168. public function load($id) {}
  169. public function save($session, $config, $exists) {}
  170. public function delete($id) {}
  171. public function sweep($expiration) {}
  172. }