MockArraySessionStorage.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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\Session\Storage;
  11. use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
  12. /**
  13. * MockArraySessionStorage mocks the session for unit tests.
  14. *
  15. * No PHP session is actually started since a session can be initialized
  16. * and shutdown only once per PHP execution cycle.
  17. *
  18. * When doing functional testing, you should use MockFileSessionStorage instead.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
  22. * @author Drak <drak@zikula.org>
  23. */
  24. class MockArraySessionStorage implements SessionStorageInterface
  25. {
  26. /**
  27. * @var string
  28. */
  29. protected $id = '';
  30. /**
  31. * @var string
  32. */
  33. protected $name;
  34. /**
  35. * @var bool
  36. */
  37. protected $started = false;
  38. /**
  39. * @var bool
  40. */
  41. protected $closed = false;
  42. /**
  43. * @var array
  44. */
  45. protected $data = array();
  46. /**
  47. * @var MetadataBag
  48. */
  49. protected $metadataBag;
  50. /**
  51. * @var array|SessionBagInterface[]
  52. */
  53. protected $bags = array();
  54. /**
  55. * Constructor.
  56. *
  57. * @param string $name Session name
  58. * @param MetadataBag $metaBag MetadataBag instance
  59. */
  60. public function __construct($name = 'MOCKSESSID', MetadataBag $metaBag = null)
  61. {
  62. $this->name = $name;
  63. $this->setMetadataBag($metaBag);
  64. }
  65. /**
  66. * Sets the session data.
  67. *
  68. * @param array $array
  69. */
  70. public function setSessionData(array $array)
  71. {
  72. $this->data = $array;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function start()
  78. {
  79. if ($this->started) {
  80. return true;
  81. }
  82. if (empty($this->id)) {
  83. $this->id = $this->generateId();
  84. }
  85. $this->loadSession();
  86. return true;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function regenerate($destroy = false, $lifetime = null)
  92. {
  93. if (!$this->started) {
  94. $this->start();
  95. }
  96. $this->metadataBag->stampNew($lifetime);
  97. $this->id = $this->generateId();
  98. return true;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function getId()
  104. {
  105. return $this->id;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function setId($id)
  111. {
  112. if ($this->started) {
  113. throw new \LogicException('Cannot set session ID after the session has started.');
  114. }
  115. $this->id = $id;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function getName()
  121. {
  122. return $this->name;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function setName($name)
  128. {
  129. $this->name = $name;
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function save()
  135. {
  136. if (!$this->started || $this->closed) {
  137. throw new \RuntimeException('Trying to save a session that was not started yet or was already closed');
  138. }
  139. // nothing to do since we don't persist the session data
  140. $this->closed = false;
  141. $this->started = false;
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function clear()
  147. {
  148. // clear out the bags
  149. foreach ($this->bags as $bag) {
  150. $bag->clear();
  151. }
  152. // clear out the session
  153. $this->data = array();
  154. // reconnect the bags to the session
  155. $this->loadSession();
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function registerBag(SessionBagInterface $bag)
  161. {
  162. $this->bags[$bag->getName()] = $bag;
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function getBag($name)
  168. {
  169. if (!isset($this->bags[$name])) {
  170. throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
  171. }
  172. if (!$this->started) {
  173. $this->start();
  174. }
  175. return $this->bags[$name];
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function isStarted()
  181. {
  182. return $this->started;
  183. }
  184. /**
  185. * Sets the MetadataBag.
  186. *
  187. * @param MetadataBag $bag
  188. */
  189. public function setMetadataBag(MetadataBag $bag = null)
  190. {
  191. if (null === $bag) {
  192. $bag = new MetadataBag();
  193. }
  194. $this->metadataBag = $bag;
  195. }
  196. /**
  197. * Gets the MetadataBag.
  198. *
  199. * @return MetadataBag
  200. */
  201. public function getMetadataBag()
  202. {
  203. return $this->metadataBag;
  204. }
  205. /**
  206. * Generates a session ID.
  207. *
  208. * This doesn't need to be particularly cryptographically secure since this is just
  209. * a mock.
  210. *
  211. * @return string
  212. */
  213. protected function generateId()
  214. {
  215. return hash('sha256', uniqid('ss_mock_', true));
  216. }
  217. protected function loadSession()
  218. {
  219. $bags = array_merge($this->bags, array($this->metadataBag));
  220. foreach ($bags as $bag) {
  221. $key = $bag->getStorageKey();
  222. $this->data[$key] = isset($this->data[$key]) ? $this->data[$key] : array();
  223. $bag->initialize($this->data[$key]);
  224. }
  225. $this->started = true;
  226. $this->closed = false;
  227. }
  228. }