Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

fix unsent cookie issue #13

Merged
merged 1 commit into from
Apr 26, 2018
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
8 changes: 4 additions & 4 deletions src/PhpSessionPersistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public function persistSession(SessionInterface $session, ResponseInterface $res
$_SESSION = $session->toArray();
session_write_close();

if (empty($this->cookie)) {
if ($this->cookie) {
$sessionCookie = SetCookie::create(session_name())
->withValue(session_id())
->withValue($this->cookie)
->withPath(ini_get('session.cookie_path'));

return FigResponseCookies::set($response, $sessionCookie);
Expand Down Expand Up @@ -92,8 +92,8 @@ private function startSession(string $id, array $options = []) : void
private function regenerateSession() : void
{
session_write_close();
$this->cookie = null;
$this->startSession($this->generateSessionId(), [
$this->cookie = $this->generateSessionId();
$this->startSession($this->cookie, [
'use_strict_mode' => false,
]);
}
Expand Down
18 changes: 9 additions & 9 deletions test/PhpSessionPersistenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function testPersistSessionGeneratesCookieWithNewSessionIdIfSessionWasReg
}

/**
* If Session COOKIE is present, persistSession() method must return the original Response
* If Session COOKIE is present, persistSession() method must return Response with Set-Cookie header
*/
public function testPersistSessionReturnsOriginalResposneIfSessionCookiePresent()
{
Expand All @@ -131,11 +131,16 @@ public function testPersistSessionReturnsOriginalResposneIfSessionCookiePresent(
$session = $this->persistence->initializeSessionFromRequest($request);
$response = new Response();
$returnedResponse = $this->persistence->persistSession($session, $response);
$this->assertSame($response, $returnedResponse);
$this->assertNotSame($response, $returnedResponse);

$setCookie = FigResponseCookies::get($returnedResponse, session_name());
$this->assertInstanceOf(SetCookie::class, $setCookie);
$this->assertSame(session_id(), $setCookie->getValue());
$this->assertSame(ini_get('session.cookie_path'), $setCookie->getPath());
}

/**
* If Session COOKIE is not present, persistSession() method must return Response with Set-Cookie header
* If Session COOKIE is not present, persistSession() method must return the original Response
*/
public function testPersistSessionReturnsResponseWithSetCookieHeaderIfNoSessionCookiePresent()
{
Expand All @@ -144,12 +149,7 @@ public function testPersistSessionReturnsResponseWithSetCookieHeaderIfNoSessionC
$response = new Response();

$returnedResponse = $this->persistence->persistSession($session, $response);
$this->assertNotSame($response, $returnedResponse);

$setCookie = FigResponseCookies::get($returnedResponse, session_name());
$this->assertInstanceOf(SetCookie::class, $setCookie);
$this->assertSame(session_id(), $setCookie->getValue());
$this->assertSame(ini_get('session.cookie_path'), $setCookie->getPath());
$this->assertSame($response, $returnedResponse);
}

public function testPersistSessionIfSessionHasContents()
Expand Down