diff --git a/CHANGELOG.md b/CHANGELOG.md index f04b5bf..bec6c63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## 3.0.2 under development -- Chg #81: Add explicit use imports for classes and constants (@vjik) +- Enh #81: Add explicit use imports for classes and constants (@vjik) +- Bug #85: Open session in `Session::regenerateId()` if it is not active yet (@vjik) ## 3.0.1 December 17, 2025 diff --git a/src/Session.php b/src/Session.php index a83fede..24150da 100644 --- a/src/Session.php +++ b/src/Session.php @@ -117,17 +117,17 @@ public function getId(): ?string public function regenerateId(): void { - if ($this->isActive()) { - try { - if (session_regenerate_id(true)) { - /** - * @var string Without `id` parameter `session_id()` always returns string. - */ - $this->sessionId = session_id(); - } - } catch (Throwable $e) { - throw new SessionException('Failed to regenerate ID.', (int) $e->getCode(), $e); + $this->open(); + + try { + if (session_regenerate_id(true)) { + /** + * @var string Without `id` parameter `session_id()` always returns string. + */ + $this->sessionId = session_id(); } + } catch (Throwable $e) { + throw new SessionException('Failed to regenerate ID.', (int) $e->getCode(), $e); } } diff --git a/src/SessionInterface.php b/src/SessionInterface.php index 5e44dd2..563285c 100644 --- a/src/SessionInterface.php +++ b/src/SessionInterface.php @@ -54,6 +54,7 @@ public function setId(string $sessionId): void; /** * Regenerate session ID keeping data. + * Opens the session first if it is not active yet. */ public function regenerateId(): void; diff --git a/tests/SessionTest.php b/tests/SessionTest.php index 3d38dbd..97efff2 100644 --- a/tests/SessionTest.php +++ b/tests/SessionTest.php @@ -82,6 +82,20 @@ public function testRegenerateID(): void self::assertNotEquals($id, $session->getId()); } + public function testRegenerateIdOpensInactiveSession(): void + { + $session = $this->getSession(); + $session->open(); + $id = $session->getId(); + $session->close(); + self::assertFalse($session->isActive()); + + $session->regenerateId(); + + self::assertTrue($session->isActive()); + self::assertNotEquals($id, $session->getId()); + } + public function testDiscard(): void { $session = $this->getSession();