Fixed detection of an active session

This commit is contained in:
Florin Patan 2012-06-09 22:45:38 +03:00
parent c07e9163a6
commit f72ba0a27d
7 changed files with 41 additions and 1 deletions

View File

@ -518,7 +518,7 @@ class Request
*/ */
public function hasSession() public function hasSession()
{ {
return null !== $this->session; return null !== $this->session && $this->session->isStarted();
} }
/** /**

View File

@ -130,6 +130,14 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
$this->storage->getBag($this->attributeName)->clear(); $this->storage->getBag($this->attributeName)->clear();
} }
/**
* {@inheritdoc}
*/
public function isStarted()
{
return $this->storage->isStarted();
}
/** /**
* Returns an iterator for attributes. * Returns an iterator for attributes.
* *

View File

@ -174,6 +174,15 @@ interface SessionInterface
*/ */
function clear(); function clear();
/**
* Check if a session was started
*
* @api
*
* @return boolean
*/
public function isStarted();
/** /**
* Registers a SessionBagInterface with the session. * Registers a SessionBagInterface with the session.
* *

View File

@ -199,6 +199,16 @@ class MockArraySessionStorage implements SessionStorageInterface
return $this->bags[$name]; return $this->bags[$name];
} }
/**
* Check if the session was started or not
*
* @return boolean
*/
public function isStarted()
{
return $this->started;
}
/** /**
* Sets the MetadataBag. * Sets the MetadataBag.
* *

View File

@ -289,6 +289,16 @@ class NativeSessionStorage implements SessionStorageInterface
return $this->metadataBag; return $this->metadataBag;
} }
/**
* Check if the session was started or not
*
* @return boolean
*/
public function isStarted()
{
return $this->started;
}
/** /**
* Sets session.* ini variables. * Sets session.* ini variables.
* *

View File

@ -884,6 +884,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($request->hasSession()); $this->assertFalse($request->hasSession());
$request->setSession(new Session(new MockArraySessionStorage())); $request->setSession(new Session(new MockArraySessionStorage()));
$request->getSession()->start();
$this->assertTrue($request->hasSession()); $this->assertTrue($request->hasSession());
} }
@ -895,6 +896,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$request->cookies->set('MOCKSESSID', 'foo'); $request->cookies->set('MOCKSESSID', 'foo');
$this->assertFalse($request->hasPreviousSession()); $this->assertFalse($request->hasPreviousSession());
$request->setSession(new Session(new MockArraySessionStorage())); $request->setSession(new Session(new MockArraySessionStorage()));
$request->getSession()->start();
$this->assertTrue($request->hasPreviousSession()); $this->assertTrue($request->hasPreviousSession());
} }

View File

@ -85,6 +85,7 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase
protected function runSessionOnKernelResponse($newToken, $original = null) protected function runSessionOnKernelResponse($newToken, $original = null)
{ {
$session = new Session(new MockArraySessionStorage()); $session = new Session(new MockArraySessionStorage());
$session->start();
if ($original !== null) { if ($original !== null) {
$session->set('_security_session', $original); $session->set('_security_session', $original);