MockArraySessionStorage.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 boolean
  36. */
  37. protected $started = false;
  38. /**
  39. * @var boolean
  40. */
  41. protected $closed = false;
  42. /**
  43. * @var array
  44. */
  45. protected $data = array();
  46. /**
  47. * Constructor.
  48. *
  49. * @param string $name Session name
  50. */
  51. public function __construct($name = 'MOCKSESSID')
  52. {
  53. $this->name = $name;
  54. }
  55. /**
  56. * Sets the session data.
  57. *
  58. * @param array $array
  59. */
  60. public function setSessionData(array $array)
  61. {
  62. $this->data = $array;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function start()
  68. {
  69. if ($this->started && !$this->closed) {
  70. return true;
  71. }
  72. if (empty($this->id)) {
  73. $this->id = $this->generateId();
  74. }
  75. $this->loadSession();
  76. return true;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function regenerate($destroy = false)
  82. {
  83. if (!$this->started) {
  84. $this->start();
  85. }
  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. // nothing to do since we don't persist the session data
  126. $this->closed = false;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function clear()
  132. {
  133. // clear out the bags
  134. foreach ($this->bags as $bag) {
  135. $bag->clear();
  136. }
  137. // clear out the session
  138. $this->data = array();
  139. // reconnect the bags to the session
  140. $this->loadSession();
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function registerBag(SessionBagInterface $bag)
  146. {
  147. $this->bags[$bag->getName()] = $bag;
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. public function getBag($name)
  153. {
  154. if (!isset($this->bags[$name])) {
  155. throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
  156. }
  157. if (!$this->started) {
  158. $this->start();
  159. }
  160. return $this->bags[$name];
  161. }
  162. /**
  163. * Generates a session ID.
  164. *
  165. * This doesn't need to be particularly cryptographically secure since this is just
  166. * a mock.
  167. *
  168. * @return string
  169. */
  170. protected function generateId()
  171. {
  172. return sha1(uniqid(mt_rand()));
  173. }
  174. protected function loadSession()
  175. {
  176. foreach ($this->bags as $bag) {
  177. $key = $bag->getStorageKey();
  178. $this->data[$key] = isset($this->data[$key]) ? $this->data[$key] : array();
  179. $bag->initialize($this->data[$key]);
  180. }
  181. $this->started = true;
  182. $this->closed = false;
  183. }
  184. }