Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@
* @author Ross Motley <[email protected]>
* @author Oliver Radwell <[email protected]>
*/
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;
}
Expand Down Expand Up @@ -67,10 +74,10 @@ public function gc($maxlifetime)
/**
* {@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;
}
Expand All @@ -94,4 +101,24 @@ public function write($sessionId, $sessionData)

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ protected function setUp()
$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')
Expand All @@ -43,7 +49,7 @@ public function testCloses()
$this->assertTrue($result);
}

public function testDestroys()
public function testDestroy()
{
$sessionId = 'xyz';

Expand Down Expand Up @@ -80,27 +86,27 @@ public function testGc()
$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';
Expand All @@ -116,10 +122,10 @@ public function testReads()

$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';
Expand All @@ -138,4 +144,43 @@ public function testWrites()

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