4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Session\Storage;
14 use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
17 * MockArraySessionStorage mocks the session for unit tests.
19 * No PHP session is actually started since a session can be initialized
20 * and shutdown only once per PHP execution cycle.
22 * When doing functional testing, you should use MockFileSessionStorage instead.
24 * @author Fabien Potencier <fabien@symfony.com>
25 * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
26 * @author Drak <drak@zikula.org>
28 class MockArraySessionStorage implements SessionStorageInterface
43 protected $started = false;
48 protected $closed = false;
53 protected $data = array();
58 protected $metadataBag;
61 * @var array|SessionBagInterface[]
63 protected $bags = array();
68 * @param string $name Session name
69 * @param MetadataBag $metaBag MetadataBag instance
71 public function __construct($name = 'MOCKSESSID', MetadataBag $metaBag = null)
74 $this->setMetadataBag($metaBag);
78 * Sets the session data.
82 public function setSessionData(array $array)
90 public function start()
96 if (empty($this->id)) {
97 $this->id = $this->generateId();
100 $this->loadSession();
108 public function regenerate($destroy = false, $lifetime = null)
110 if (!$this->started) {
114 $this->metadataBag->stampNew($lifetime);
115 $this->id = $this->generateId();
123 public function getId()
131 public function setId($id)
133 if ($this->started) {
134 throw new \LogicException('Cannot set session ID after the session has started.');
143 public function getName()
151 public function setName($name)
159 public function save()
161 if (!$this->started || $this->closed) {
162 throw new \RuntimeException('Trying to save a session that was not started yet or was already closed');
164 // nothing to do since we don't persist the session data
165 $this->closed = false;
166 $this->started = false;
172 public function clear()
174 // clear out the bags
175 foreach ($this->bags as $bag) {
179 // clear out the session
180 $this->data = array();
182 // reconnect the bags to the session
183 $this->loadSession();
189 public function registerBag(SessionBagInterface $bag)
191 $this->bags[$bag->getName()] = $bag;
197 public function getBag($name)
199 if (!isset($this->bags[$name])) {
200 throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
203 if (!$this->started) {
207 return $this->bags[$name];
213 public function isStarted()
215 return $this->started;
219 * Sets the MetadataBag.
221 * @param MetadataBag $bag
223 public function setMetadataBag(MetadataBag $bag = null)
226 $bag = new MetadataBag();
229 $this->metadataBag = $bag;
233 * Gets the MetadataBag.
235 * @return MetadataBag
237 public function getMetadataBag()
239 return $this->metadataBag;
243 * Generates a session ID.
245 * This doesn't need to be particularly cryptographically secure since this is just
250 protected function generateId()
252 return hash('sha256', uniqid('ss_mock_', true));
255 protected function loadSession()
257 $bags = array_merge($this->bags, array($this->metadataBag));
259 foreach ($bags as $bag) {
260 $key = $bag->getStorageKey();
261 $this->data[$key] = isset($this->data[$key]) ? $this->data[$key] : array();
262 $bag->initialize($this->data[$key]);
265 $this->started = true;
266 $this->closed = false;