minor #26828 [HttpFoundation] Have MigratingSessionHandler implement SessionUpdateTimestampHandlerInterface (nicolas-grekas)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[HttpFoundation] Have MigratingSessionHandler implement SessionUpdateTimestampHandlerInterface

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Forgotten in #26096

Commits
-------

5d7117bf0a [HttpFoundation] Have MigratingSessionHandler implement SessionUpdateTimestampHandlerInterface
This commit is contained in:
Tobias Schultze 2018-04-07 21:59:24 +02:00
commit 629e82dca6
2 changed files with 86 additions and 14 deletions

View File

@ -20,13 +20,20 @@ namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
* @author Ross Motley <ross.motley@amara.com>
* @author Oliver Radwell <oliver.radwell@amara.com>
*/
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;
}
}

View File

@ -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);
}
}