[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:
parent
6b9ee87b62
commit
098b593591
@ -159,6 +159,9 @@ class MockArraySessionStorage implements SessionStorageInterface
|
|||||||
*/
|
*/
|
||||||
public function save()
|
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
|
// nothing to do since we don't persist the session data
|
||||||
$this->closed = false;
|
$this->closed = false;
|
||||||
}
|
}
|
||||||
|
@ -97,6 +97,10 @@ class MockFileSessionStorage extends MockArraySessionStorage
|
|||||||
*/
|
*/
|
||||||
public function save()
|
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));
|
file_put_contents($this->getFilePath(), serialize($this->data));
|
||||||
|
|
||||||
// this is needed for Silex, where the session object is re-used across requests
|
// this is needed for Silex, where the session object is re-used across requests
|
||||||
|
@ -110,6 +110,9 @@ interface SessionStorageInterface
|
|||||||
* used for a storage object design for unit or functional testing where
|
* used for a storage object design for unit or functional testing where
|
||||||
* a real PHP session would interfere with testing, in which case it
|
* a real PHP session would interfere with testing, in which case it
|
||||||
* it should actually persist the session data if required.
|
* 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();
|
public function save();
|
||||||
|
|
||||||
|
@ -151,6 +151,7 @@ class SessionTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testSave()
|
public function testSave()
|
||||||
{
|
{
|
||||||
|
$this->session->start();
|
||||||
$this->session->save();
|
$this->session->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,4 +95,12 @@ class MockArraySessionStorageTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->storage->start();
|
$this->storage->start();
|
||||||
$this->assertNotEquals('', $this->storage->getId());
|
$this->assertNotEquals('', $this->storage->getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException RuntimeException
|
||||||
|
*/
|
||||||
|
public function testUnstartedSave()
|
||||||
|
{
|
||||||
|
$this->storage->save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,6 +106,15 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('bar', $storage2->getBag('attributes')->get('foo'), 'values persist between instances');
|
$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()
|
private function getStorage()
|
||||||
{
|
{
|
||||||
$storage = new MockFileSessionStorage($this->sessionDir);
|
$storage = new MockFileSessionStorage($this->sessionDir);
|
||||||
|
Reference in New Issue
Block a user