[Server] Stop Session::forget() from creating missing segments (#484)
This commit is contained in:
parent
418034f2b0
commit
4630d7424e
2 changed files with 17 additions and 5 deletions
|
|
@ -110,15 +110,12 @@ class Session implements SessionInterface
|
||||||
while (\count($segments) > 1) {
|
while (\count($segments) > 1) {
|
||||||
$segment = array_shift($segments);
|
$segment = array_shift($segments);
|
||||||
if (!isset($data[$segment]) || !\is_array($data[$segment])) {
|
if (!isset($data[$segment]) || !\is_array($data[$segment])) {
|
||||||
$data[$segment] = [];
|
return;
|
||||||
}
|
}
|
||||||
$data = &$data[$segment];
|
$data = &$data[$segment];
|
||||||
}
|
}
|
||||||
|
|
||||||
$lastKey = array_shift($segments);
|
unset($data[array_shift($segments)]);
|
||||||
if (isset($data[$lastKey])) {
|
|
||||||
unset($data[$lastKey]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function clear(): void
|
public function clear(): void
|
||||||
|
|
|
||||||
|
|
@ -245,6 +245,21 @@ class SessionTest extends TestCase
|
||||||
$this->assertFalse($this->session->has('nonexistent'));
|
$this->assertFalse($this->session->has('nonexistent'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testForgetDoesNotCreateIntermediateSegments(): void
|
||||||
|
{
|
||||||
|
$this->session->forget('a.b');
|
||||||
|
|
||||||
|
$this->assertSame([], $this->session->all());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testForgetDoesNotOverwriteNonArrayIntermediateValue(): void
|
||||||
|
{
|
||||||
|
$this->session->set('key', 'string_value');
|
||||||
|
$this->session->forget('key.nested');
|
||||||
|
|
||||||
|
$this->assertSame('string_value', $this->session->get('key'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testSessionCanStoreVariousDataTypes(): void
|
public function testSessionCanStoreVariousDataTypes(): void
|
||||||
{
|
{
|
||||||
$this->session->set('string', 'value');
|
$this->session->set('string', 'value');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue