fix(session): initialize data lazily in save() to prevent uninitialized property access (#252)
This commit is contained in:
parent
a98e01a4be
commit
61c20f1450
2 changed files with 10 additions and 1 deletions
|
|
@ -51,7 +51,7 @@ class Session implements SessionInterface
|
||||||
|
|
||||||
public function save(): bool
|
public function save(): bool
|
||||||
{
|
{
|
||||||
return $this->store->write($this->id, json_encode($this->data, \JSON_THROW_ON_ERROR));
|
return $this->store->write($this->id, json_encode($this->readData(), \JSON_THROW_ON_ERROR));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(string $key, mixed $default = null): mixed
|
public function get(string $key, mixed $default = null): mixed
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,15 @@ class SessionTest extends TestCase
|
||||||
$this->assertEquals(['foo' => 'bar'], $result);
|
$this->assertEquals(['foo' => 'bar'], $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSaveBeforeReadInitializesData()
|
||||||
|
{
|
||||||
|
$store = new InMemorySessionStore();
|
||||||
|
$session = new Session($store);
|
||||||
|
|
||||||
|
// save() before any get()/set() should not crash
|
||||||
|
$this->assertTrue($session->save());
|
||||||
|
}
|
||||||
|
|
||||||
public function testAllReturnsEmptyArrayForNullPayload()
|
public function testAllReturnsEmptyArrayForNullPayload()
|
||||||
{
|
{
|
||||||
$store = $this->getMockBuilder(InMemorySessionStore::class)
|
$store = $this->getMockBuilder(InMemorySessionStore::class)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue