Skip to content

Commit 098b593

Browse files
author
Baldur Rensch
committed
[Session] Added exception to save method
A RuntimeException is thrown if there is an attempt to save the session without it being started, or if it has already been closed.
1 parent 6b9ee87 commit 098b593

File tree

6 files changed

+28
-0
lines changed

6 files changed

+28
-0
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php

+3
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ public function setName($name)
159159
*/
160160
public function save()
161161
{
162+
if (!$this->started || $this->closed) {
163+
throw new \RuntimeException("Trying to save a session that was not started yet or was already closed");
164+
}
162165
// nothing to do since we don't persist the session data
163166
$this->closed = false;
164167
}

src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public function regenerate($destroy = false, $lifetime = null)
9797
*/
9898
public function save()
9999
{
100+
if (!$this->started) {
101+
throw new \RuntimeException("Trying to save a session that was not started yet or was already closed");
102+
}
103+
100104
file_put_contents($this->getFilePath(), serialize($this->data));
101105

102106
// this is needed for Silex, where the session object is re-used across requests

src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public function regenerate($destroy = false, $lifetime = null);
110110
* used for a storage object design for unit or functional testing where
111111
* a real PHP session would interfere with testing, in which case it
112112
* it should actually persist the session data if required.
113+
*
114+
* @throws \RuntimeException If the session is saved without being started, or if the session
115+
* is already closed.
113116
*/
114117
public function save();
115118

src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public function testMigrateDestroy()
151151

152152
public function testSave()
153153
{
154+
$this->session->start();
154155
$this->session->save();
155156
}
156157

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockArraySessionStorageTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,12 @@ public function testGetId()
9595
$this->storage->start();
9696
$this->assertNotEquals('', $this->storage->getId());
9797
}
98+
99+
/**
100+
* @expectedException RuntimeException
101+
*/
102+
public function testUnstartedSave()
103+
{
104+
$this->storage->save();
105+
}
98106
}

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockFileSessionStorageTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ public function testMultipleInstances()
106106
$this->assertEquals('bar', $storage2->getBag('attributes')->get('foo'), 'values persist between instances');
107107
}
108108

109+
/**
110+
* @expectedException RuntimeException
111+
*/
112+
public function testSaveWithoutStart()
113+
{
114+
$storage1 = $this->getStorage();
115+
$storage1->save();
116+
}
117+
109118
private function getStorage()
110119
{
111120
$storage = new MockFileSessionStorage($this->sessionDir);

0 commit comments

Comments
 (0)