[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.
This commit is contained in:
Baldur Rensch 2012-12-14 11:40:30 -08:00
parent 6b9ee87b62
commit 098b593591
6 changed files with 28 additions and 0 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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();

View File

@ -151,6 +151,7 @@ class SessionTest extends \PHPUnit_Framework_TestCase
public function testSave()
{
$this->session->start();
$this->session->save();
}

View File

@ -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();
}
}

View File

@ -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);