Skip to content

Commit

Permalink
Merge pull request #52 from SmartFrame-Technologies/initialize-persis…
Browse files Browse the repository at this point in the history
…tence-id

Implement InitializePersistenceIdInterface
  • Loading branch information
gsteel authored Jun 29, 2024
2 parents 0118d65 + c601591 commit b99e723
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
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()) {
$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);
}
}

0 comments on commit b99e723

Please sign in to comment.