Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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.
  • Loading branch information
Baldur Rensch committed Dec 14, 2012
1 parent 6b9ee87 commit 098b593
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 0 deletions.
Expand Up @@ -159,6 +159,9 @@ public function setName($name)
*/
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;
}
Expand Down
Expand Up @@ -97,6 +97,10 @@ public function regenerate($destroy = false, $lifetime = null)
*/
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
Expand Down
Expand Up @@ -110,6 +110,9 @@ public function regenerate($destroy = false, $lifetime = null);
* 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();

Expand Down
Expand Up @@ -151,6 +151,7 @@ public function testMigrateDestroy()

public function testSave()
{
$this->session->start();
$this->session->save();
}

Expand Down
Expand Up @@ -95,4 +95,12 @@ public function testGetId()
$this->storage->start();
$this->assertNotEquals('', $this->storage->getId());
}

/**
* @expectedException RuntimeException
*/
public function testUnstartedSave()
{
$this->storage->save();
}
}
Expand Up @@ -106,6 +106,15 @@ public function testMultipleInstances()
$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);
Expand Down

0 comments on commit 098b593

Please sign in to comment.