fix session restart on PHP 5.3

this also removes some useless code
This commit is contained in:
Tobias Schultze 2014-12-12 18:30:27 +01:00
parent 39a3379842
commit b9d3c92ca9
4 changed files with 26 additions and 40 deletions

View File

@ -89,7 +89,7 @@ class MockArraySessionStorage implements SessionStorageInterface
*/
public function start()
{
if ($this->started && !$this->closed) {
if ($this->started) {
return true;
}

View File

@ -126,7 +126,7 @@ class NativeSessionStorage implements SessionStorageInterface
*/
public function start()
{
if ($this->started && !$this->closed) {
if ($this->started) {
return true;
}
@ -134,7 +134,7 @@ class NativeSessionStorage implements SessionStorageInterface
throw new \RuntimeException('Failed to start the session: already started by PHP.');
}
if (PHP_VERSION_ID < 50400 && isset($_SESSION) && session_id()) {
if (PHP_VERSION_ID < 50400 && !$this->closed && isset($_SESSION) && session_id()) {
// not 100% fool-proof, but is the most reliable way to determine if a session is active in PHP 5.3
throw new \RuntimeException('Failed to start the session: already started by PHP ($_SESSION is set).');
}
@ -162,10 +162,6 @@ class NativeSessionStorage implements SessionStorageInterface
*/
public function getId()
{
if (!$this->started && !$this->closed) {
return ''; // returning empty is consistent with session_id() behaviour
}
return $this->saveHandler->getId();
}

View File

@ -38,7 +38,7 @@ class PhpBridgeSessionStorage extends NativeSessionStorage
*/
public function start()
{
if ($this->started && !$this->closed) {
if ($this->started) {
return true;
}

View File

@ -85,13 +85,15 @@ class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase
public function testGetId()
{
$storage = $this->getStorage();
$this->assertEquals('', $storage->getId());
$this->assertSame('', $storage->getId(), 'Empty ID before starting session');
$storage->start();
$this->assertNotEquals('', $storage->getId());
$id = $storage->getId();
$this->assertInternalType('string', $id);
$this->assertNotSame('', $id);
$storage->save();
$this->assertNotEquals('', $storage->getId());
$this->assertSame($id, $storage->getId(), 'ID stays after saving session');
}
public function testRegenerate()
@ -209,35 +211,8 @@ class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase
/**
* @expectedException \RuntimeException
*/
public function testStartedOutside53()
public function testStartedOutside()
{
if (PHP_VERSION_ID >= 50400) {
$this->markTestSkipped('Test skipped, for PHP 5.3 only.');
}
$storage = $this->getStorage();
$this->assertFalse(isset($_SESSION));
session_start();
$this->assertTrue(isset($_SESSION));
// PHP session might have started, but the storage driver has not, so false is correct here
$this->assertFalse($storage->isStarted());
$key = $storage->getMetadataBag()->getStorageKey();
$this->assertFalse(isset($_SESSION[$key]));
$storage->start();
}
/**
* @expectedException \RuntimeException
*/
public function testCanStartOutside54()
{
if (PHP_VERSION_ID < 50400) {
$this->markTestSkipped('Test skipped, for PHP 5.4 only.');
}
$storage = $this->getStorage();
$this->assertFalse(isset($_SESSION));
@ -246,7 +221,10 @@ class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase
session_start();
$this->assertTrue(isset($_SESSION));
$this->assertTrue($storage->getSaveHandler()->isActive());
if (PHP_VERSION_ID >= 50400) {
// this only works in PHP >= 5.4 where session_status is available
$this->assertTrue($storage->getSaveHandler()->isActive());
}
// PHP session might have started, but the storage driver has not, so false is correct here
$this->assertFalse($storage->isStarted());
@ -254,4 +232,16 @@ class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase
$this->assertFalse(isset($_SESSION[$key]));
$storage->start();
}
public function testRestart()
{
$storage = $this->getStorage();
$storage->start();
$id = $storage->getId();
$storage->getBag('attributes')->set('lucky', 7);
$storage->save();
$storage->start();
$this->assertSame($id, $storage->getId(), 'Same session ID after restarting');
$this->assertSame(7, $storage->getBag('attributes')->get('lucky'), 'Data still available');
}
}