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

Skip to content

Commit 5d7117b

Browse files
[HttpFoundation] Have MigratingSessionHandler implement SessionUpdateTimestampHandlerInterface
1 parent dc29b27 commit 5d7117b

File tree

2 files changed

+86
-14
lines changed

2 files changed

+86
-14
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MigratingSessionHandler.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,20 @@
2020
* @author Ross Motley <[email protected]>
2121
* @author Oliver Radwell <[email protected]>
2222
*/
23-
class MigratingSessionHandler implements \SessionHandlerInterface
23+
class MigratingSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
2424
{
2525
private $currentHandler;
2626
private $writeOnlyHandler;
2727

2828
public function __construct(\SessionHandlerInterface $currentHandler, \SessionHandlerInterface $writeOnlyHandler)
2929
{
30+
if (!$currentHandler instanceof \SessionUpdateTimestampHandlerInterface) {
31+
$currentHandler = new StrictSessionHandler($currentHandler);
32+
}
33+
if (!$writeOnlyHandler instanceof \SessionUpdateTimestampHandlerInterface) {
34+
$writeOnlyHandler = new StrictSessionHandler($writeOnlyHandler);
35+
}
36+
3037
$this->currentHandler = $currentHandler;
3138
$this->writeOnlyHandler = $writeOnlyHandler;
3239
}
@@ -67,10 +74,10 @@ public function gc($maxlifetime)
6774
/**
6875
* {@inheritdoc}
6976
*/
70-
public function open($savePath, $sessionId)
77+
public function open($savePath, $sessionName)
7178
{
72-
$result = $this->currentHandler->open($savePath, $sessionId);
73-
$this->writeOnlyHandler->open($savePath, $sessionId);
79+
$result = $this->currentHandler->open($savePath, $sessionName);
80+
$this->writeOnlyHandler->open($savePath, $sessionName);
7481

7582
return $result;
7683
}
@@ -94,4 +101,24 @@ public function write($sessionId, $sessionData)
94101

95102
return $result;
96103
}
104+
105+
/**
106+
* {@inheritdoc}
107+
*/
108+
public function validateId($sessionId)
109+
{
110+
// No reading from new handler until switch-over
111+
return $this->currentHandler->validateId($sessionId);
112+
}
113+
114+
/**
115+
* {@inheritdoc}
116+
*/
117+
public function updateTimestamp($sessionId, $sessionData)
118+
{
119+
$result = $this->currentHandler->updateTimestamp($sessionId, $sessionData);
120+
$this->writeOnlyHandler->updateTimestamp($sessionId, $sessionData);
121+
122+
return $result;
123+
}
97124
}

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MigratingSessionHandlerTest.php

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ protected function setUp()
2828
$this->dualHandler = new MigratingSessionHandler($this->currentHandler, $this->writeOnlyHandler);
2929
}
3030

31-
public function testCloses()
31+
public function testInstanceOf()
32+
{
33+
$this->assertInstanceOf(\SessionHandlerInterface::class, $this->dualHandler);
34+
$this->assertInstanceOf(\SessionUpdateTimestampHandlerInterface::class, $this->dualHandler);
35+
}
36+
37+
public function testClose()
3238
{
3339
$this->currentHandler->expects($this->once())
3440
->method('close')
@@ -43,7 +49,7 @@ public function testCloses()
4349
$this->assertTrue($result);
4450
}
4551

46-
public function testDestroys()
52+
public function testDestroy()
4753
{
4854
$sessionId = 'xyz';
4955

@@ -80,27 +86,27 @@ public function testGc()
8086
$this->assertTrue($result);
8187
}
8288

83-
public function testOpens()
89+
public function testOpen()
8490
{
8591
$savePath = '/path/to/save/location';
86-
$sessionId = 'xyz';
92+
$sessionName = 'xyz';
8793

8894
$this->currentHandler->expects($this->once())
8995
->method('open')
90-
->with($savePath, $sessionId)
96+
->with($savePath, $sessionName)
9197
->will($this->returnValue(true));
9298

9399
$this->writeOnlyHandler->expects($this->once())
94100
->method('open')
95-
->with($savePath, $sessionId)
101+
->with($savePath, $sessionName)
96102
->will($this->returnValue(false));
97103

98-
$result = $this->dualHandler->open($savePath, $sessionId);
104+
$result = $this->dualHandler->open($savePath, $sessionName);
99105

100106
$this->assertTrue($result);
101107
}
102108

103-
public function testReads()
109+
public function testRead()
104110
{
105111
$sessionId = 'xyz';
106112
$readValue = 'something';
@@ -116,10 +122,10 @@ public function testReads()
116122

117123
$result = $this->dualHandler->read($sessionId);
118124

119-
$this->assertEquals($readValue, $result);
125+
$this->assertSame($readValue, $result);
120126
}
121127

122-
public function testWrites()
128+
public function testWrite()
123129
{
124130
$sessionId = 'xyz';
125131
$data = 'my-serialized-data';
@@ -138,4 +144,43 @@ public function testWrites()
138144

139145
$this->assertTrue($result);
140146
}
147+
148+
public function testValidateId()
149+
{
150+
$sessionId = 'xyz';
151+
$readValue = 'something';
152+
153+
$this->currentHandler->expects($this->once())
154+
->method('read')
155+
->with($sessionId)
156+
->will($this->returnValue($readValue));
157+
158+
$this->writeOnlyHandler->expects($this->never())
159+
->method('read')
160+
->with($this->any());
161+
162+
$result = $this->dualHandler->validateId($sessionId);
163+
164+
$this->assertTrue($result);
165+
}
166+
167+
public function testUpdateTimestamp()
168+
{
169+
$sessionId = 'xyz';
170+
$data = 'my-serialized-data';
171+
172+
$this->currentHandler->expects($this->once())
173+
->method('write')
174+
->with($sessionId, $data)
175+
->will($this->returnValue(true));
176+
177+
$this->writeOnlyHandler->expects($this->once())
178+
->method('write')
179+
->with($sessionId, $data)
180+
->will($this->returnValue(false));
181+
182+
$result = $this->dualHandler->updateTimestamp($sessionId, $data);
183+
184+
$this->assertTrue($result);
185+
}
141186
}

0 commit comments

Comments
 (0)