diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php index b18d071d95..a1fcf539f8 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php @@ -159,6 +159,9 @@ class MockArraySessionStorage implements SessionStorageInterface */ public function save() { + if (!$this->started || $this->closed) { + throw new \RuntimeException("Trying to save a session that was not started yet or was already closed"); + } // nothing to do since we don't persist the session data $this->closed = false; } diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php index 80aa44b06b..280630914a 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php @@ -97,6 +97,10 @@ class MockFileSessionStorage extends MockArraySessionStorage */ public function save() { + if (!$this->started) { + throw new \RuntimeException("Trying to save a session that was not started yet or was already closed"); + } + file_put_contents($this->getFilePath(), serialize($this->data)); // this is needed for Silex, where the session object is re-used across requests diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php index 26a741829b..711eaa29a3 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php @@ -110,6 +110,9 @@ interface SessionStorageInterface * used for a storage object design for unit or functional testing where * a real PHP session would interfere with testing, in which case it * it should actually persist the session data if required. + * + * @throws \RuntimeException If the session is saved without being started, or if the session + * is already closed. */ public function save(); diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php index 7bdc127fdb..68c416ca0f 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php @@ -151,6 +151,7 @@ class SessionTest extends \PHPUnit_Framework_TestCase public function testSave() { + $this->session->start(); $this->session->save(); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockArraySessionStorageTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockArraySessionStorageTest.php index c9b2a7480c..2a6f6bb7e8 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockArraySessionStorageTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockArraySessionStorageTest.php @@ -95,4 +95,12 @@ class MockArraySessionStorageTest extends \PHPUnit_Framework_TestCase $this->storage->start(); $this->assertNotEquals('', $this->storage->getId()); } + + /** + * @expectedException RuntimeException + */ + public function testUnstartedSave() + { + $this->storage->save(); + } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockFileSessionStorageTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockFileSessionStorageTest.php index f18089d92a..d42c62a25c 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockFileSessionStorageTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockFileSessionStorageTest.php @@ -106,6 +106,15 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase $this->assertEquals('bar', $storage2->getBag('attributes')->get('foo'), 'values persist between instances'); } + /** + * @expectedException RuntimeException + */ + public function testSaveWithoutStart() + { + $storage1 = $this->getStorage(); + $storage1->save(); + } + private function getStorage() { $storage = new MockFileSessionStorage($this->sessionDir);