MockArraySessionStorage.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 = [];
  46. /**
  47. * @var MetadataBag
  48. */
  49. protected $metadataBag;
  50. /**
  51. * @var array|SessionBagInterface[]
  52. */
  53. protected $bags = [];
  54. public function __construct(string $name = 'MOCKSESSID', MetadataBag $metaBag = null)
  55. {
  56. $this->name = $name;
  57. $this->setMetadataBag($metaBag);
  58. }
  59. public function setSessionData(array $array)
  60. {
  61. $this->data = $array;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function start()
  67. {
  68. if ($this->started) {
  69. return true;
  70. }
  71. if (empty($this->id)) {
  72. $this->id = $this->generateId();
  73. }
  74. $this->loadSession();
  75. return true;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function regenerate($destroy = false, $lifetime = null)
  81. {
  82. if (!$this->started) {
  83. $this->start();
  84. }
  85. $this->metadataBag->stampNew($lifetime);
  86. $this->id = $this->generateId();
  87. return true;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getId()
  93. {
  94. return $this->id;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function setId($id)
  100. {
  101. if ($this->started) {
  102. throw new \LogicException('Cannot set session ID after the session has started.');
  103. }
  104. $this->id = $id;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getName()
  110. {
  111. return $this->name;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function setName($name)
  117. {
  118. $this->name = $name;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function save()
  124. {
  125. if (!$this->started || $this->closed) {
  126. throw new \RuntimeException('Trying to save a session that was not started yet or was already closed');
  127. }
  128. // nothing to do since we don't persist the session data
  129. $this->closed = false;
  130. $this->started = false;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function clear()
  136. {
  137. // clear out the bags
  138. foreach ($this->bags as $bag) {
  139. $bag->clear();
  140. }
  141. // clear out the session
  142. $this->data = [];
  143. // reconnect the bags to the session
  144. $this->loadSession();
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function registerBag(SessionBagInterface $bag)
  150. {
  151. $this->bags[$bag->getName()] = $bag;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function getBag($name)
  157. {
  158. if (!isset($this->bags[$name])) {
  159. throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
  160. }
  161. if (!$this->started) {
  162. $this->start();
  163. }
  164. return $this->bags[$name];
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function isStarted()
  170. {
  171. return $this->started;
  172. }
  173. public function setMetadataBag(MetadataBag $bag = null)
  174. {
  175. if (null === $bag) {
  176. $bag = new MetadataBag();
  177. }
  178. $this->metadataBag = $bag;
  179. }
  180. /**
  181. * Gets the MetadataBag.
  182. *
  183. * @return MetadataBag
  184. */
  185. public function getMetadataBag()
  186. {
  187. return $this->metadataBag;
  188. }
  189. /**
  190. * Generates a session ID.
  191. *
  192. * This doesn't need to be particularly cryptographically secure since this is just
  193. * a mock.
  194. *
  195. * @return string
  196. */
  197. protected function generateId()
  198. {
  199. return hash('sha256', uniqid('ss_mock_', true));
  200. }
  201. protected function loadSession()
  202. {
  203. $bags = array_merge($this->bags, [$this->metadataBag]);
  204. foreach ($bags as $bag) {
  205. $key = $bag->getStorageKey();
  206. $this->data[$key] = isset($this->data[$key]) ? $this->data[$key] : [];
  207. $bag->initialize($this->data[$key]);
  208. }
  209. $this->started = true;
  210. $this->closed = false;
  211. }
  212. }