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();
66 * @param string $name Session name
67 * @param MetadataBag $metaBag MetadataBag instance
69 public function __construct($name = 'MOCKSESSID', MetadataBag $metaBag = null)
72 $this->setMetadataBag($metaBag);
75 public function setSessionData(array $array)
83 public function start()
89 if (empty($this->id)) {
90 $this->id = $this->generateId();
101 public function regenerate($destroy = false, $lifetime = null)
103 if (!$this->started) {
107 $this->metadataBag->stampNew($lifetime);
108 $this->id = $this->generateId();
116 public function getId()
124 public function setId($id)
126 if ($this->started) {
127 throw new \LogicException('Cannot set session ID after the session has started.');
136 public function getName()
144 public function setName($name)
152 public function save()
154 if (!$this->started || $this->closed) {
155 throw new \RuntimeException('Trying to save a session that was not started yet or was already closed');
157 // nothing to do since we don't persist the session data
158 $this->closed = false;
159 $this->started = false;
165 public function clear()
167 // clear out the bags
168 foreach ($this->bags as $bag) {
172 // clear out the session
173 $this->data = array();
175 // reconnect the bags to the session
176 $this->loadSession();
182 public function registerBag(SessionBagInterface $bag)
184 $this->bags[$bag->getName()] = $bag;
190 public function getBag($name)
192 if (!isset($this->bags[$name])) {
193 throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
196 if (!$this->started) {
200 return $this->bags[$name];
206 public function isStarted()
208 return $this->started;
211 public function setMetadataBag(MetadataBag $bag = null)
214 $bag = new MetadataBag();
217 $this->metadataBag = $bag;
221 * Gets the MetadataBag.
223 * @return MetadataBag
225 public function getMetadataBag()
227 return $this->metadataBag;
231 * Generates a session ID.
233 * This doesn't need to be particularly cryptographically secure since this is just
238 protected function generateId()
240 return hash('sha256', uniqid('ss_mock_', true));
243 protected function loadSession()
245 $bags = array_merge($this->bags, array($this->metadataBag));
247 foreach ($bags as $bag) {
248 $key = $bag->getStorageKey();
249 $this->data[$key] = isset($this->data[$key]) ? $this->data[$key] : array();
250 $bag->initialize($this->data[$key]);
253 $this->started = true;
254 $this->closed = false;