diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MigratingSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MigratingSessionHandler.php index abe1f36189..5293d2448a 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MigratingSessionHandler.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MigratingSessionHandler.php @@ -20,13 +20,20 @@ namespace Symfony\Component\HttpFoundation\Session\Storage\Handler; * @author Ross Motley * @author Oliver Radwell */ -class MigratingSessionHandler implements \SessionHandlerInterface +class MigratingSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface { private $currentHandler; private $writeOnlyHandler; public function __construct(\SessionHandlerInterface $currentHandler, \SessionHandlerInterface $writeOnlyHandler) { + if (!$currentHandler instanceof \SessionUpdateTimestampHandlerInterface) { + $currentHandler = new StrictSessionHandler($currentHandler); + } + if (!$writeOnlyHandler instanceof \SessionUpdateTimestampHandlerInterface) { + $writeOnlyHandler = new StrictSessionHandler($writeOnlyHandler); + } + $this->currentHandler = $currentHandler; $this->writeOnlyHandler = $writeOnlyHandler; } @@ -67,10 +74,10 @@ class MigratingSessionHandler implements \SessionHandlerInterface /** * {@inheritdoc} */ - public function open($savePath, $sessionId) + public function open($savePath, $sessionName) { - $result = $this->currentHandler->open($savePath, $sessionId); - $this->writeOnlyHandler->open($savePath, $sessionId); + $result = $this->currentHandler->open($savePath, $sessionName); + $this->writeOnlyHandler->open($savePath, $sessionName); return $result; } @@ -94,4 +101,24 @@ class MigratingSessionHandler implements \SessionHandlerInterface return $result; } + + /** + * {@inheritdoc} + */ + public function validateId($sessionId) + { + // No reading from new handler until switch-over + return $this->currentHandler->validateId($sessionId); + } + + /** + * {@inheritdoc} + */ + public function updateTimestamp($sessionId, $sessionData) + { + $result = $this->currentHandler->updateTimestamp($sessionId, $sessionData); + $this->writeOnlyHandler->updateTimestamp($sessionId, $sessionData); + + return $result; + } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MigratingSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MigratingSessionHandlerTest.php index 05fd7b7ab9..1c0f3ca663 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MigratingSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MigratingSessionHandlerTest.php @@ -28,7 +28,13 @@ class MigratingSessionHandlerTest extends TestCase $this->dualHandler = new MigratingSessionHandler($this->currentHandler, $this->writeOnlyHandler); } - public function testCloses() + public function testInstanceOf() + { + $this->assertInstanceOf(\SessionHandlerInterface::class, $this->dualHandler); + $this->assertInstanceOf(\SessionUpdateTimestampHandlerInterface::class, $this->dualHandler); + } + + public function testClose() { $this->currentHandler->expects($this->once()) ->method('close') @@ -43,7 +49,7 @@ class MigratingSessionHandlerTest extends TestCase $this->assertTrue($result); } - public function testDestroys() + public function testDestroy() { $sessionId = 'xyz'; @@ -80,27 +86,27 @@ class MigratingSessionHandlerTest extends TestCase $this->assertTrue($result); } - public function testOpens() + public function testOpen() { $savePath = '/path/to/save/location'; - $sessionId = 'xyz'; + $sessionName = 'xyz'; $this->currentHandler->expects($this->once()) ->method('open') - ->with($savePath, $sessionId) + ->with($savePath, $sessionName) ->will($this->returnValue(true)); $this->writeOnlyHandler->expects($this->once()) ->method('open') - ->with($savePath, $sessionId) + ->with($savePath, $sessionName) ->will($this->returnValue(false)); - $result = $this->dualHandler->open($savePath, $sessionId); + $result = $this->dualHandler->open($savePath, $sessionName); $this->assertTrue($result); } - public function testReads() + public function testRead() { $sessionId = 'xyz'; $readValue = 'something'; @@ -116,10 +122,10 @@ class MigratingSessionHandlerTest extends TestCase $result = $this->dualHandler->read($sessionId); - $this->assertEquals($readValue, $result); + $this->assertSame($readValue, $result); } - public function testWrites() + public function testWrite() { $sessionId = 'xyz'; $data = 'my-serialized-data'; @@ -138,4 +144,43 @@ class MigratingSessionHandlerTest extends TestCase $this->assertTrue($result); } + + public function testValidateId() + { + $sessionId = 'xyz'; + $readValue = 'something'; + + $this->currentHandler->expects($this->once()) + ->method('read') + ->with($sessionId) + ->will($this->returnValue($readValue)); + + $this->writeOnlyHandler->expects($this->never()) + ->method('read') + ->with($this->any()); + + $result = $this->dualHandler->validateId($sessionId); + + $this->assertTrue($result); + } + + public function testUpdateTimestamp() + { + $sessionId = 'xyz'; + $data = 'my-serialized-data'; + + $this->currentHandler->expects($this->once()) + ->method('write') + ->with($sessionId, $data) + ->will($this->returnValue(true)); + + $this->writeOnlyHandler->expects($this->once()) + ->method('write') + ->with($sessionId, $data) + ->will($this->returnValue(false)); + + $result = $this->dualHandler->updateTimestamp($sessionId, $data); + + $this->assertTrue($result); + } }