Skip to content

Commit

Permalink
Fix PSalm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk committed May 8, 2023
1 parent bbf9590 commit c8a7261
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 49 deletions.
23 changes: 12 additions & 11 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Lcobucci\Clock\SystemClock;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key\InMemory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use PSR7Sessions\Storageless\Http\Config;
use PSR7Sessions\Storageless\Http\SessionMiddleware;
use PSR7Sessions\Storageless\Session\SessionInterface;

Expand All @@ -42,16 +42,17 @@
// then point your browser at `http://localhost:8888/`

$sessionMiddleware = new SessionMiddleware(
Configuration::forSymmetricSigner(
new Sha256(),
InMemory::plainText('c9UA8QKLSmDEn4DhNeJIad/4JugZd/HvrjyKrS0jOes='), // // signature key (important: change this to your own)
),
SetCookie::create('an-example-cookie-name')
->withSecure(false) // false on purpose, unless you have https locally
->withHttpOnly(true)
->withPath('/'),
1200, // 20 minutes
new SystemClock(new DateTimeZone(date_default_timezone_get())),
(new Config(
Configuration::forSymmetricSigner(
new Sha256(),
InMemory::plainText('c9UA8QKLSmDEn4DhNeJIad/4JugZd/HvrjyKrS0jOes='), // // signature key (important: change this to your own)
),
))->withCookie(
SetCookie::create('an-example-cookie-name')
->withSecure(false) // false on purpose, unless you have https locally
->withHttpOnly(true)
->withPath('/'),
)->withIdleTimeout(1200), // 20 minutes
);

$myMiddleware = new class implements RequestHandlerInterface {
Expand Down
70 changes: 32 additions & 38 deletions test/StoragelessTest/Http/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Lcobucci\JWT\Signer\Key\InMemory;
use PHPUnit\Framework\TestCase;
use PSR7Sessions\Storageless\Http\Config;
use SplObjectStorage;

use function random_bytes;

Expand Down Expand Up @@ -81,42 +80,37 @@ public function testProvideNonEmptyDefaultsForScalarAttributes(): void

public function testImmutability(): void
{
$map = new SplObjectStorage();

$config = new Config($this->jwtConfig);
$map[$config] = true;
self::assertNotSame($this->jwtConfig, $config->getJwtConfiguration());

$jwtConfig = clone $this->jwtConfig;
$config = $config->withJwtConfiguration($jwtConfig);
$map[$config] = true;
self::assertNotSame($jwtConfig, $config->getJwtConfiguration());

$clock = FrozenClock::fromUTC();
$config = $config->withClock($clock);
$map[$config] = true;
self::assertNotSame($clock, $config->getClock());

$cookie = SetCookie::create('foo');
$config = $config->withCookie($cookie);
$map[$config] = true;
self::assertNotSame($cookie, $config->getCookie());

$idleTimeout = $config->getIdleTimeout() + 1;
$config = $config->withIdleTimeout($idleTimeout);
$map[$config] = true;
self::assertSame($idleTimeout, $config->getIdleTimeout());

$refreshTime = $config->getRefreshTime() + 1;
$config = $config->withRefreshTime($refreshTime);
$map[$config] = true;
self::assertSame($refreshTime, $config->getRefreshTime());

$sessionAttribute = $config->getSessionAttribute() . 'foo';
$config = $config->withSessionAttribute($sessionAttribute);
$map[$config] = true;
self::assertSame($sessionAttribute, $config->getSessionAttribute());

self::assertCount(7, $map);
$leftConfig = new Config($this->jwtConfig);
self::assertNotSame($this->jwtConfig, $leftConfig->getJwtConfiguration());

$jwtConfig = clone $this->jwtConfig;
$rightConfig = $leftConfig->withJwtConfiguration($jwtConfig);
self::assertNotSame($leftConfig, $rightConfig);
self::assertNotSame($jwtConfig, $rightConfig->getJwtConfiguration());

$clock = FrozenClock::fromUTC();
$leftConfig = $rightConfig->withClock($clock);
self::assertNotSame($leftConfig, $rightConfig);
self::assertNotSame($clock, $leftConfig->getClock());

$cookie = SetCookie::create('foo');
$rightConfig = $leftConfig->withCookie($cookie);
self::assertNotSame($leftConfig, $rightConfig);
self::assertNotSame($cookie, $rightConfig->getCookie());

$idleTimeout = $leftConfig->getIdleTimeout() + 1;
$leftConfig = $rightConfig->withIdleTimeout($idleTimeout);
self::assertNotSame($leftConfig, $rightConfig);
self::assertSame($idleTimeout, $leftConfig->getIdleTimeout());

$refreshTime = $leftConfig->getRefreshTime() + 1;
$rightConfig = $leftConfig->withRefreshTime($refreshTime);
self::assertNotSame($leftConfig, $rightConfig);
self::assertSame($refreshTime, $rightConfig->getRefreshTime());

$sessionAttribute = $leftConfig->getSessionAttribute() . 'foo';
$leftConfig = $rightConfig->withSessionAttribute($sessionAttribute);
self::assertNotSame($leftConfig, $rightConfig);
self::assertSame($sessionAttribute, $leftConfig->getSessionAttribute());
}
}

0 comments on commit c8a7261

Please sign in to comment.