菜谱项目

NativeSessionStorageTest.php 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  13. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  14. use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSessionHandler;
  15. use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
  16. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  17. use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
  18. /**
  19. * Test class for NativeSessionStorage.
  20. *
  21. * @author Drak <drak@zikula.org>
  22. *
  23. * These tests require separate processes.
  24. *
  25. * @runTestsInSeparateProcesses
  26. * @preserveGlobalState disabled
  27. */
  28. class NativeSessionStorageTest extends TestCase
  29. {
  30. private $savePath;
  31. protected function setUp()
  32. {
  33. $this->iniSet('session.save_handler', 'files');
  34. $this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test');
  35. if (!is_dir($this->savePath)) {
  36. mkdir($this->savePath);
  37. }
  38. }
  39. protected function tearDown()
  40. {
  41. session_write_close();
  42. array_map('unlink', glob($this->savePath.'/*'));
  43. if (is_dir($this->savePath)) {
  44. rmdir($this->savePath);
  45. }
  46. $this->savePath = null;
  47. }
  48. /**
  49. * @return NativeSessionStorage
  50. */
  51. protected function getStorage(array $options = array())
  52. {
  53. $storage = new NativeSessionStorage($options);
  54. $storage->registerBag(new AttributeBag());
  55. return $storage;
  56. }
  57. public function testBag()
  58. {
  59. $storage = $this->getStorage();
  60. $bag = new FlashBag();
  61. $storage->registerBag($bag);
  62. $this->assertSame($bag, $storage->getBag($bag->getName()));
  63. }
  64. /**
  65. * @expectedException \InvalidArgumentException
  66. */
  67. public function testRegisterBagException()
  68. {
  69. $storage = $this->getStorage();
  70. $storage->getBag('non_existing');
  71. }
  72. /**
  73. * @expectedException \LogicException
  74. */
  75. public function testRegisterBagForAStartedSessionThrowsException()
  76. {
  77. $storage = $this->getStorage();
  78. $storage->start();
  79. $storage->registerBag(new AttributeBag());
  80. }
  81. public function testGetId()
  82. {
  83. $storage = $this->getStorage();
  84. $this->assertSame('', $storage->getId(), 'Empty ID before starting session');
  85. $storage->start();
  86. $id = $storage->getId();
  87. $this->assertInternalType('string', $id);
  88. $this->assertNotSame('', $id);
  89. $storage->save();
  90. $this->assertSame($id, $storage->getId(), 'ID stays after saving session');
  91. }
  92. public function testRegenerate()
  93. {
  94. $storage = $this->getStorage();
  95. $storage->start();
  96. $id = $storage->getId();
  97. $storage->getBag('attributes')->set('lucky', 7);
  98. $storage->regenerate();
  99. $this->assertNotEquals($id, $storage->getId());
  100. $this->assertEquals(7, $storage->getBag('attributes')->get('lucky'));
  101. }
  102. public function testRegenerateDestroy()
  103. {
  104. $storage = $this->getStorage();
  105. $storage->start();
  106. $id = $storage->getId();
  107. $storage->getBag('attributes')->set('legs', 11);
  108. $storage->regenerate(true);
  109. $this->assertNotEquals($id, $storage->getId());
  110. $this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
  111. }
  112. public function testSessionGlobalIsUpToDateAfterIdRegeneration()
  113. {
  114. $storage = $this->getStorage();
  115. $storage->start();
  116. $storage->getBag('attributes')->set('lucky', 7);
  117. $storage->regenerate();
  118. $storage->getBag('attributes')->set('lucky', 42);
  119. $this->assertEquals(42, $_SESSION['_sf2_attributes']['lucky']);
  120. }
  121. public function testRegenerationFailureDoesNotFlagStorageAsStarted()
  122. {
  123. $storage = $this->getStorage();
  124. $this->assertFalse($storage->regenerate());
  125. $this->assertFalse($storage->isStarted());
  126. }
  127. public function testDefaultSessionCacheLimiter()
  128. {
  129. $this->iniSet('session.cache_limiter', 'nocache');
  130. $storage = new NativeSessionStorage();
  131. $this->assertEquals('', ini_get('session.cache_limiter'));
  132. }
  133. public function testExplicitSessionCacheLimiter()
  134. {
  135. $this->iniSet('session.cache_limiter', 'nocache');
  136. $storage = new NativeSessionStorage(array('cache_limiter' => 'public'));
  137. $this->assertEquals('public', ini_get('session.cache_limiter'));
  138. }
  139. public function testCookieOptions()
  140. {
  141. $options = array(
  142. 'cookie_lifetime' => 123456,
  143. 'cookie_path' => '/my/cookie/path',
  144. 'cookie_domain' => 'symfony.example.com',
  145. 'cookie_secure' => true,
  146. 'cookie_httponly' => false,
  147. );
  148. $this->getStorage($options);
  149. $temp = session_get_cookie_params();
  150. $gco = array();
  151. foreach ($temp as $key => $value) {
  152. $gco['cookie_'.$key] = $value;
  153. }
  154. $this->assertEquals($options, $gco);
  155. }
  156. /**
  157. * @expectedException \InvalidArgumentException
  158. */
  159. public function testSetSaveHandlerException()
  160. {
  161. $storage = $this->getStorage();
  162. $storage->setSaveHandler(new \stdClass());
  163. }
  164. public function testSetSaveHandler()
  165. {
  166. $this->iniSet('session.save_handler', 'files');
  167. $storage = $this->getStorage();
  168. $storage->setSaveHandler();
  169. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  170. $storage->setSaveHandler(null);
  171. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  172. $storage->setSaveHandler(new SessionHandlerProxy(new NativeSessionHandler()));
  173. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  174. $storage->setSaveHandler(new NativeSessionHandler());
  175. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  176. $storage->setSaveHandler(new SessionHandlerProxy(new NullSessionHandler()));
  177. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  178. $storage->setSaveHandler(new NullSessionHandler());
  179. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  180. }
  181. /**
  182. * @expectedException \RuntimeException
  183. */
  184. public function testStarted()
  185. {
  186. $storage = $this->getStorage();
  187. $this->assertFalse($storage->getSaveHandler()->isActive());
  188. $this->assertFalse($storage->isStarted());
  189. session_start();
  190. $this->assertTrue(isset($_SESSION));
  191. $this->assertTrue($storage->getSaveHandler()->isActive());
  192. // PHP session might have started, but the storage driver has not, so false is correct here
  193. $this->assertFalse($storage->isStarted());
  194. $key = $storage->getMetadataBag()->getStorageKey();
  195. $this->assertFalse(isset($_SESSION[$key]));
  196. $storage->start();
  197. }
  198. public function testRestart()
  199. {
  200. $storage = $this->getStorage();
  201. $storage->start();
  202. $id = $storage->getId();
  203. $storage->getBag('attributes')->set('lucky', 7);
  204. $storage->save();
  205. $storage->start();
  206. $this->assertSame($id, $storage->getId(), 'Same session ID after restarting');
  207. $this->assertSame(7, $storage->getBag('attributes')->get('lucky'), 'Data still available');
  208. }
  209. public function testCanCreateNativeSessionStorageWhenSessionAlreadyStarted()
  210. {
  211. session_start();
  212. $this->getStorage();
  213. // Assert no exception has been thrown by `getStorage()`
  214. $this->addToAssertionCount(1);
  215. }
  216. public function testSetSessionOptionsOnceSessionStartedIsIgnored()
  217. {
  218. session_start();
  219. $this->getStorage(array(
  220. 'name' => 'something-else',
  221. ));
  222. // Assert no exception has been thrown by `getStorage()`
  223. $this->addToAssertionCount(1);
  224. }
  225. }