PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php

https://bitbucket.org/gruenwaldt/loquitur-web
PHP | 279 lines | 200 code | 43 blank | 36 comment | 6 complexity | c78e1cf1be8cf2db585ebc8506533fb3 MD5 | raw file
Possible License(s): BSD-3-Clause
  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 Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSessionHandler;
  12. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  13. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  14. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  15. use Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy;
  16. use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
  17. /**
  18. * Test class for NativeSessionStorage.
  19. *
  20. * @author Drak <drak@zikula.org>
  21. *
  22. * These tests require separate processes.
  23. *
  24. * @runTestsInSeparateProcesses
  25. */
  26. class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase
  27. {
  28. private $savePath;
  29. protected function setUp()
  30. {
  31. ini_set('session.save_handler', 'files');
  32. ini_set('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test');
  33. if (!is_dir($this->savePath)) {
  34. mkdir($this->savePath);
  35. }
  36. }
  37. protected function tearDown()
  38. {
  39. session_write_close();
  40. array_map('unlink', glob($this->savePath.'/*'));
  41. if (is_dir($this->savePath)) {
  42. rmdir($this->savePath);
  43. }
  44. $this->savePath = null;
  45. }
  46. /**
  47. * @param array $options
  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. public function testGetId()
  73. {
  74. $storage = $this->getStorage();
  75. $this->assertEquals('', $storage->getId());
  76. $storage->start();
  77. $this->assertNotEquals('', $storage->getId());
  78. }
  79. public function testRegenerate()
  80. {
  81. $storage = $this->getStorage();
  82. $storage->start();
  83. $id = $storage->getId();
  84. $storage->getBag('attributes')->set('lucky', 7);
  85. $storage->regenerate();
  86. $this->assertNotEquals($id, $storage->getId());
  87. $this->assertEquals(7, $storage->getBag('attributes')->get('lucky'));
  88. }
  89. public function testRegenerateDestroy()
  90. {
  91. $storage = $this->getStorage();
  92. $storage->start();
  93. $id = $storage->getId();
  94. $storage->getBag('attributes')->set('legs', 11);
  95. $storage->regenerate(true);
  96. $this->assertNotEquals($id, $storage->getId());
  97. $this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
  98. }
  99. public function testDefaultSessionCacheLimiter()
  100. {
  101. ini_set('session.cache_limiter', 'nocache');
  102. $storage = new NativeSessionStorage();
  103. $this->assertEquals('', ini_get('session.cache_limiter'));
  104. }
  105. public function testExplicitSessionCacheLimiter()
  106. {
  107. ini_set('session.cache_limiter', 'nocache');
  108. $storage = new NativeSessionStorage(array('cache_limiter' => 'public'));
  109. $this->assertEquals('public', ini_get('session.cache_limiter'));
  110. }
  111. public function testCookieOptions()
  112. {
  113. $options = array(
  114. 'cookie_lifetime' => 123456,
  115. 'cookie_path' => '/my/cookie/path',
  116. 'cookie_domain' => 'symfony2.example.com',
  117. 'cookie_secure' => true,
  118. 'cookie_httponly' => false,
  119. );
  120. $this->getStorage($options);
  121. $temp = session_get_cookie_params();
  122. $gco = array();
  123. foreach ($temp as $key => $value) {
  124. $gco['cookie_'.$key] = $value;
  125. }
  126. $this->assertEquals($options, $gco);
  127. }
  128. /**
  129. * @expectedException \InvalidArgumentException
  130. */
  131. public function testSetSaveHandlerException()
  132. {
  133. $storage = $this->getStorage();
  134. $storage->setSaveHandler(new \StdClass);
  135. }
  136. public function testSetSaveHandler53()
  137. {
  138. if (version_compare(phpversion(), '5.4.0', '>=')) {
  139. $this->markTestSkipped('Test skipped, for PHP 5.3 only.');
  140. }
  141. ini_set('session.save_handler', 'files');
  142. $storage = $this->getStorage();
  143. $storage->setSaveHandler();
  144. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
  145. $storage->setSaveHandler(null);
  146. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
  147. $storage->setSaveHandler(new NativeSessionHandler());
  148. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
  149. $storage->setSaveHandler(new SessionHandlerProxy(new SessionHandler()));
  150. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  151. $storage->setSaveHandler(new SessionHandler());
  152. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  153. $storage->setSaveHandler(new NativeProxy());
  154. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
  155. }
  156. public function testSetSaveHandler54()
  157. {
  158. if (version_compare(phpversion(), '5.4.0', '<')) {
  159. $this->markTestSkipped('Test skipped, for PHP 5.4 only.');
  160. }
  161. ini_set('session.save_handler', 'files');
  162. $storage = $this->getStorage();
  163. $storage->setSaveHandler();
  164. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  165. $storage->setSaveHandler(null);
  166. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  167. $storage->setSaveHandler(new SessionHandlerProxy(new NativeSessionHandler()));
  168. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  169. $storage->setSaveHandler(new NativeSessionHandler());
  170. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  171. $storage->setSaveHandler(new SessionHandlerProxy(new SessionHandler()));
  172. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  173. $storage->setSaveHandler(new SessionHandler());
  174. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  175. }
  176. /**
  177. * @expectedException \RuntimeException
  178. */
  179. public function testStartedOutside53()
  180. {
  181. if (version_compare(phpversion(), '5.4.0', '>=')) {
  182. $this->markTestSkipped('Test skipped, for PHP 5.3 only.');
  183. }
  184. $storage = $this->getStorage();
  185. $this->assertFalse(isset($_SESSION));
  186. session_start();
  187. $this->assertTrue(isset($_SESSION));
  188. // PHP session might have started, but the storage driver has not, so false is correct here
  189. $this->assertFalse($storage->isStarted());
  190. $key = $storage->getMetadataBag()->getStorageKey();
  191. $this->assertFalse(isset($_SESSION[$key]));
  192. $storage->start();
  193. }
  194. /**
  195. * @expectedException \RuntimeException
  196. */
  197. public function testCanStartOutside54()
  198. {
  199. if (version_compare(phpversion(), '5.4.0', '<')) {
  200. $this->markTestSkipped('Test skipped, for PHP 5.4 only.');
  201. }
  202. $storage = $this->getStorage();
  203. $this->assertFalse(isset($_SESSION));
  204. $this->assertFalse($storage->getSaveHandler()->isActive());
  205. $this->assertFalse($storage->isStarted());
  206. session_start();
  207. $this->assertTrue(isset($_SESSION));
  208. $this->assertTrue($storage->getSaveHandler()->isActive());
  209. // PHP session might have started, but the storage driver has not, so false is correct here
  210. $this->assertFalse($storage->isStarted());
  211. $key = $storage->getMetadataBag()->getStorageKey();
  212. $this->assertFalse(isset($_SESSION[$key]));
  213. $storage->start();
  214. }
  215. }
  216. class SessionHandler implements \SessionHandlerInterface
  217. {
  218. public function open($savePath, $sessionName)
  219. {
  220. }
  221. public function close()
  222. {
  223. }
  224. public function read($id)
  225. {
  226. }
  227. public function write($id, $data)
  228. {
  229. }
  230. public function destroy($id)
  231. {
  232. }
  233. public function gc($maxlifetime)
  234. {
  235. }
  236. }