PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/gencer/symfony
PHP | 257 lines | 182 code | 39 blank | 36 comment | 6 complexity | 6c298b02409522cb996465b7169bb502 MD5 | raw file
  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\Attribute\AttributeBag;
  12. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  13. use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSessionHandler;
  14. use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
  15. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  16. use Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy;
  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. */
  27. class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase
  28. {
  29. private $savePath;
  30. protected function setUp()
  31. {
  32. ini_set('session.save_handler', 'files');
  33. ini_set('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test');
  34. if (!is_dir($this->savePath)) {
  35. mkdir($this->savePath);
  36. }
  37. }
  38. protected function tearDown()
  39. {
  40. session_write_close();
  41. array_map('unlink', glob($this->savePath.'/*'));
  42. if (is_dir($this->savePath)) {
  43. rmdir($this->savePath);
  44. }
  45. $this->savePath = null;
  46. }
  47. /**
  48. * @param array $options
  49. *
  50. * @return NativeSessionStorage
  51. */
  52. protected function getStorage(array $options = array())
  53. {
  54. $storage = new NativeSessionStorage($options);
  55. $storage->registerBag(new AttributeBag());
  56. return $storage;
  57. }
  58. public function testBag()
  59. {
  60. $storage = $this->getStorage();
  61. $bag = new FlashBag();
  62. $storage->registerBag($bag);
  63. $this->assertSame($bag, $storage->getBag($bag->getName()));
  64. }
  65. /**
  66. * @expectedException \InvalidArgumentException
  67. */
  68. public function testRegisterBagException()
  69. {
  70. $storage = $this->getStorage();
  71. $storage->getBag('non_existing');
  72. }
  73. public function testGetId()
  74. {
  75. $storage = $this->getStorage();
  76. $this->assertEquals('', $storage->getId());
  77. $storage->start();
  78. $this->assertNotEquals('', $storage->getId());
  79. $storage->save();
  80. $this->assertNotEquals('', $storage->getId());
  81. }
  82. public function testRegenerate()
  83. {
  84. $storage = $this->getStorage();
  85. $storage->start();
  86. $id = $storage->getId();
  87. $storage->getBag('attributes')->set('lucky', 7);
  88. $storage->regenerate();
  89. $this->assertNotEquals($id, $storage->getId());
  90. $this->assertEquals(7, $storage->getBag('attributes')->get('lucky'));
  91. }
  92. public function testRegenerateDestroy()
  93. {
  94. $storage = $this->getStorage();
  95. $storage->start();
  96. $id = $storage->getId();
  97. $storage->getBag('attributes')->set('legs', 11);
  98. $storage->regenerate(true);
  99. $this->assertNotEquals($id, $storage->getId());
  100. $this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
  101. }
  102. public function testDefaultSessionCacheLimiter()
  103. {
  104. ini_set('session.cache_limiter', 'nocache');
  105. $storage = new NativeSessionStorage();
  106. $this->assertEquals('', ini_get('session.cache_limiter'));
  107. }
  108. public function testExplicitSessionCacheLimiter()
  109. {
  110. ini_set('session.cache_limiter', 'nocache');
  111. $storage = new NativeSessionStorage(array('cache_limiter' => 'public'));
  112. $this->assertEquals('public', ini_get('session.cache_limiter'));
  113. }
  114. public function testCookieOptions()
  115. {
  116. $options = array(
  117. 'cookie_lifetime' => 123456,
  118. 'cookie_path' => '/my/cookie/path',
  119. 'cookie_domain' => 'symfony2.example.com',
  120. 'cookie_secure' => true,
  121. 'cookie_httponly' => false,
  122. );
  123. $this->getStorage($options);
  124. $temp = session_get_cookie_params();
  125. $gco = array();
  126. foreach ($temp as $key => $value) {
  127. $gco['cookie_'.$key] = $value;
  128. }
  129. $this->assertEquals($options, $gco);
  130. }
  131. /**
  132. * @expectedException \InvalidArgumentException
  133. */
  134. public function testSetSaveHandlerException()
  135. {
  136. $storage = $this->getStorage();
  137. $storage->setSaveHandler(new \stdClass());
  138. }
  139. public function testSetSaveHandler53()
  140. {
  141. if (version_compare(phpversion(), '5.4.0', '>=')) {
  142. $this->markTestSkipped('Test skipped, for PHP 5.3 only.');
  143. }
  144. ini_set('session.save_handler', 'files');
  145. $storage = $this->getStorage();
  146. $storage->setSaveHandler();
  147. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
  148. $storage->setSaveHandler(null);
  149. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
  150. $storage->setSaveHandler(new NativeSessionHandler());
  151. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
  152. $storage->setSaveHandler(new SessionHandlerProxy(new NullSessionHandler()));
  153. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  154. $storage->setSaveHandler(new NullSessionHandler());
  155. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  156. $storage->setSaveHandler(new NativeProxy());
  157. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
  158. }
  159. public function testSetSaveHandler54()
  160. {
  161. if (version_compare(phpversion(), '5.4.0', '<')) {
  162. $this->markTestSkipped('Test skipped, for PHP 5.4 only.');
  163. }
  164. ini_set('session.save_handler', 'files');
  165. $storage = $this->getStorage();
  166. $storage->setSaveHandler();
  167. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  168. $storage->setSaveHandler(null);
  169. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  170. $storage->setSaveHandler(new SessionHandlerProxy(new NativeSessionHandler()));
  171. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  172. $storage->setSaveHandler(new NativeSessionHandler());
  173. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  174. $storage->setSaveHandler(new SessionHandlerProxy(new NullSessionHandler()));
  175. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  176. $storage->setSaveHandler(new NullSessionHandler());
  177. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  178. }
  179. /**
  180. * @expectedException \RuntimeException
  181. */
  182. public function testStartedOutside53()
  183. {
  184. if (version_compare(phpversion(), '5.4.0', '>=')) {
  185. $this->markTestSkipped('Test skipped, for PHP 5.3 only.');
  186. }
  187. $storage = $this->getStorage();
  188. $this->assertFalse(isset($_SESSION));
  189. session_start();
  190. $this->assertTrue(isset($_SESSION));
  191. // PHP session might have started, but the storage driver has not, so false is correct here
  192. $this->assertFalse($storage->isStarted());
  193. $key = $storage->getMetadataBag()->getStorageKey();
  194. $this->assertFalse(isset($_SESSION[$key]));
  195. $storage->start();
  196. }
  197. /**
  198. * @expectedException \RuntimeException
  199. */
  200. public function testCanStartOutside54()
  201. {
  202. if (version_compare(phpversion(), '5.4.0', '<')) {
  203. $this->markTestSkipped('Test skipped, for PHP 5.4 only.');
  204. }
  205. $storage = $this->getStorage();
  206. $this->assertFalse(isset($_SESSION));
  207. $this->assertFalse($storage->getSaveHandler()->isActive());
  208. $this->assertFalse($storage->isStarted());
  209. session_start();
  210. $this->assertTrue(isset($_SESSION));
  211. $this->assertTrue($storage->getSaveHandler()->isActive());
  212. // PHP session might have started, but the storage driver has not, so false is correct here
  213. $this->assertFalse($storage->isStarted());
  214. $key = $storage->getMetadataBag()->getStorageKey();
  215. $this->assertFalse(isset($_SESSION[$key]));
  216. $storage->start();
  217. }
  218. }