Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement InitializePersistenceIdInterface #52

Merged
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
9 changes: 9 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<file src="src/CacheSessionPersistence.php">
<MixedArgumentTypeCoercion>
<code>$sessionData</code>
<code><![CDATA[$session->toArray()]]></code>
</MixedArgumentTypeCoercion>
<MixedInferredReturnType>
<code>array</code>
Expand All @@ -14,6 +15,9 @@
<UnusedProperty>
<code>$persistent</code>
</UnusedProperty>
<UndefinedInterfaceMethod>
<code>getId</code>
</UndefinedInterfaceMethod>
</file>
<file src="src/CacheSessionPersistenceFactory.php">
<MixedArgument>
Expand Down Expand Up @@ -70,5 +74,10 @@
<code>cacheHeaders</code>
<code>validCacheLimiters</code>
</PossiblyUnusedMethod>
<UndefinedInterfaceMethod>
<code>getId</code>
<code>getId</code>
<code>getId</code>
</UndefinedInterfaceMethod>
</file>
</files>
12 changes: 11 additions & 1 deletion src/CacheSessionPersistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Mezzio\Session\Cache;

use Mezzio\Session\InitializePersistenceIdInterface;
use Mezzio\Session\Persistence\CacheHeadersGeneratorTrait;
use Mezzio\Session\Persistence\Http;
use Mezzio\Session\Persistence\SessionCookieAwareTrait;
Expand All @@ -26,7 +27,7 @@
* During persistence, if the session regeneration flag is true, a new session
* identifier is created, and the session re-started.
*/
class CacheSessionPersistence implements SessionPersistenceInterface
class CacheSessionPersistence implements InitializePersistenceIdInterface, SessionPersistenceInterface
{
use CacheHeadersGeneratorTrait;
use SessionCookieAwareTrait;
Expand Down Expand Up @@ -197,4 +198,13 @@ private function persistSessionDataToCache(string $id, array $data): void
$item->expiresAfter($this->cacheExpire);
$this->cache->save($item);
}

public function initializeId(SessionInterface $session): SessionInterface
{
if ($session->getId() === '' || $session->isRegenerated()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we would assert the $session instance implements SessionIdentifierAwareInterface, but, we should probably get mezzio-session v2 out the door and make that method part of SessionInterface. See mezzio/mezzio-session#59

$session = new Session($session->toArray(), $this->generateSessionId());
}

return $session;
}
}
41 changes: 41 additions & 0 deletions test/CacheSessionPersistenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -956,4 +956,45 @@ public function testPersistenceDurationInSessionDataWithValueOfZeroIgnoresGlobal
$this->assertNotSame($response, $result);
$this->assertCookieHasNoExpiryDirective($result);
}

public function testInitializeIdReturnsSessionWithId(): void
{
$persistence = new CacheSessionPersistence(
$this->cachePool,
'test',
);
$session = new Session(['foo' => 'bar']);
$actual = $persistence->initializeId($session);

$this->assertNotSame($session, $actual);
$this->assertNotEmpty($actual->getId());
$this->assertSame(['foo' => 'bar'], $actual->toArray());
}

public function testInitializeIdRegeneratesSessionId(): void
{
$persistence = new CacheSessionPersistence(
$this->cachePool,
'test',
);
$session = new Session(['foo' => 'bar'], 'original-id');
$session = $session->regenerate();
$actual = $persistence->initializeId($session);

$this->assertNotEmpty($actual->getId());
$this->assertNotSame('original-id', $actual->getId());
$this->assertFalse($actual->isRegenerated());
}

public function testInitializeIdReturnsSessionUnaltered(): void
{
$persistence = new CacheSessionPersistence(
$this->cachePool,
'test',
);
$session = new Session(['foo' => 'bar'], 'original-id');
$actual = $persistence->initializeId($session);

$this->assertSame($session, $actual);
}
}