diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php index 918735601d..2e5c0fedc7 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php @@ -89,7 +89,7 @@ class MockArraySessionStorage implements SessionStorageInterface */ public function start() { - if ($this->started && !$this->closed) { + if ($this->started) { return true; } diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php index b1766503f1..7a0c7a85e4 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php @@ -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(); } diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/PhpBridgeSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/PhpBridgeSessionStorage.php index 6b5ad65056..6554578c6d 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/PhpBridgeSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/PhpBridgeSessionStorage.php @@ -38,7 +38,7 @@ class PhpBridgeSessionStorage extends NativeSessionStorage */ public function start() { - if ($this->started && !$this->closed) { + if ($this->started) { return true; } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php index 1e79812118..e74f52cb3b 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php @@ -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'); + } }