From fd30f4a21d6f44dda89a13f01f222d29aa270aed Mon Sep 17 00:00:00 2001 From: Titouan Galopin Date: Mon, 10 Sep 2018 21:53:03 +0200 Subject: [PATCH] Allow reuse of Session between requests --- .../HttpFoundation/Session/Session.php | 4 +++- .../Tests/Session/SessionTest.php | 21 +++++++++++++++++++ .../Storage/MockArraySessionStorageTest.php | 2 +- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index 725b97ea4e..8293cf2871 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -178,7 +178,9 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable */ public function setId($id) { - $this->storage->setId($id); + if ($this->storage->getId() !== $id) { + $this->storage->setId($id); + } } /** diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php index 8e3d55ecbd..acd5513896 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php @@ -70,6 +70,27 @@ class SessionTest extends TestCase $this->assertEquals('0123456789abcdef', $this->session->getId()); } + public function testSetIdAfterStart() + { + $this->session->start(); + $id = $this->session->getId(); + + $e = null; + try { + $this->session->setId($id); + } catch (\Exception $e) { + } + + $this->assertNull($e); + + try { + $this->session->setId('different'); + } catch (\Exception $e) { + } + + $this->assertInstanceOf('\LogicException', $e); + } + public function testSetName() { $this->assertEquals('MOCKSESSID', $this->session->getName()); diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockArraySessionStorageTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockArraySessionStorageTest.php index 0c182e0b0b..893e120ce1 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockArraySessionStorageTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockArraySessionStorageTest.php @@ -48,7 +48,7 @@ class MockArraySessionStorageTest extends TestCase $this->data = array( $this->attributes->getStorageKey() => array('foo' => 'bar'), $this->flashes->getStorageKey() => array('notice' => 'hello'), - ); + ); $this->storage = new MockArraySessionStorage(); $this->storage->registerBag($this->flashes);