MockArraySessionStorage.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. protected string $id = '';
  27. protected string $name;
  28. protected bool $started = false;
  29. protected bool $closed = false;
  30. protected array $data = [];
  31. protected MetadataBag $metadataBag;
  32. /**
  33. * @var SessionBagInterface[]
  34. */
  35. protected array $bags = [];
  36. public function __construct(string $name = 'MOCKSESSID', ?MetadataBag $metaBag = null)
  37. {
  38. $this->name = $name;
  39. $this->setMetadataBag($metaBag);
  40. }
  41. public function setSessionData(array $array): void
  42. {
  43. $this->data = $array;
  44. }
  45. public function start(): bool
  46. {
  47. if ($this->started) {
  48. return true;
  49. }
  50. if (!$this->id) {
  51. $this->id = $this->generateId();
  52. }
  53. $this->loadSession();
  54. return true;
  55. }
  56. public function regenerate(bool $destroy = false, ?int $lifetime = null): bool
  57. {
  58. if (!$this->started) {
  59. $this->start();
  60. }
  61. $this->metadataBag->stampNew($lifetime);
  62. $this->id = $this->generateId();
  63. return true;
  64. }
  65. public function getId(): string
  66. {
  67. return $this->id;
  68. }
  69. public function setId(string $id): void
  70. {
  71. if ($this->started) {
  72. throw new \LogicException('Cannot set session ID after the session has started.');
  73. }
  74. $this->id = $id;
  75. }
  76. public function getName(): string
  77. {
  78. return $this->name;
  79. }
  80. public function setName(string $name): void
  81. {
  82. $this->name = $name;
  83. }
  84. public function save(): void
  85. {
  86. if (!$this->started || $this->closed) {
  87. throw new \RuntimeException('Trying to save a session that was not started yet or was already closed.');
  88. }
  89. // nothing to do since we don't persist the session data
  90. $this->closed = false;
  91. $this->started = false;
  92. }
  93. public function clear(): void
  94. {
  95. // clear out the bags
  96. foreach ($this->bags as $bag) {
  97. $bag->clear();
  98. }
  99. // clear out the session
  100. $this->data = [];
  101. // reconnect the bags to the session
  102. $this->loadSession();
  103. }
  104. public function registerBag(SessionBagInterface $bag): void
  105. {
  106. $this->bags[$bag->getName()] = $bag;
  107. }
  108. public function getBag(string $name): SessionBagInterface
  109. {
  110. if (!isset($this->bags[$name])) {
  111. throw new \InvalidArgumentException(sprintf('The SessionBagInterface "%s" is not registered.', $name));
  112. }
  113. if (!$this->started) {
  114. $this->start();
  115. }
  116. return $this->bags[$name];
  117. }
  118. public function isStarted(): bool
  119. {
  120. return $this->started;
  121. }
  122. public function setMetadataBag(?MetadataBag $bag): void
  123. {
  124. $this->metadataBag = $bag ?? new MetadataBag();
  125. }
  126. /**
  127. * Gets the MetadataBag.
  128. */
  129. public function getMetadataBag(): MetadataBag
  130. {
  131. return $this->metadataBag;
  132. }
  133. /**
  134. * Generates a session ID.
  135. *
  136. * This doesn't need to be particularly cryptographically secure since this is just
  137. * a mock.
  138. */
  139. protected function generateId(): string
  140. {
  141. return hash('xxh128', uniqid('ss_mock_', true));
  142. }
  143. protected function loadSession(): void
  144. {
  145. $bags = array_merge($this->bags, [$this->metadataBag]);
  146. foreach ($bags as $bag) {
  147. $key = $bag->getStorageKey();
  148. $this->data[$key] ??= [];
  149. $bag->initialize($this->data[$key]);
  150. }
  151. $this->started = true;
  152. $this->closed = false;
  153. }
  154. }