diff --git a/packages/router/src/HttpApplication.php b/packages/router/src/HttpApplication.php index 78b6e6961..d4e1f1c2a 100644 --- a/packages/router/src/HttpApplication.php +++ b/packages/router/src/HttpApplication.php @@ -10,7 +10,7 @@ use Tempest\Core\Kernel; use Tempest\Core\Tempest; use Tempest\Http\RequestFactory; -use Tempest\Http\Session\SessionManager; +use Tempest\Http\Session\Session; use Tempest\Log\Channels\AppendLogChannel; use Tempest\Log\LogConfig; @@ -57,7 +57,7 @@ public function run(): void $router->dispatch($psrRequest), ); - $this->container->get(SessionManager::class)->cleanup(); + $this->container->get(Session::class)->cleanup(); $this->container->get(Kernel::class)->shutdown(); } diff --git a/tests/Fixtures/Controllers/ControllerWithSession.php b/tests/Fixtures/Controllers/ControllerWithSession.php new file mode 100644 index 000000000..d192bab6d --- /dev/null +++ b/tests/Fixtures/Controllers/ControllerWithSession.php @@ -0,0 +1,18 @@ +flash('test', 'hi'); + + return new Ok(); + } +} diff --git a/tests/Fixtures/Controllers/ControllerWithoutSession.php b/tests/Fixtures/Controllers/ControllerWithoutSession.php new file mode 100644 index 000000000..b04732bb5 --- /dev/null +++ b/tests/Fixtures/Controllers/ControllerWithoutSession.php @@ -0,0 +1,16 @@ +get('/') ->assertOk(); } + + #[Test] + public function session_is_not_set_even_when_it_was_cleaned_and_empty(): void + { + $this->appConfig->baseUri = 'http://127.0.0.1:8081'; + $process = new Process(['./tempest', 'serve', '127.0.0.1:8081'], getcwd()); + $process->start(); + usleep(200_000); + + $client = $this->get(HttpClient::class); + + $response = $client->get(uri(ControllerWithoutSession::class)); + $cookies = arr($response->getHeader('set-cookie')->values ?? []) + ->map(fn (string $cookie) => explode('=', $cookie)[0]); + + $this->assertFalse($cookies->contains('tempest_session_id')); + + $response = $client->get(uri(ControllerWithSession::class)); + $cookies = arr($response->getHeader('set-cookie')->values ?? []) + ->map(fn (string $cookie) => explode('=', $cookie)[0]); + + $this->assertTrue($cookies->contains('tempest_session_id')); + + $process->stop(); + } } diff --git a/tests/Integration/Http/RedisSessionTest.php b/tests/Integration/Http/RedisSessionTest.php index a38292736..98c79e1c0 100644 --- a/tests/Integration/Http/RedisSessionTest.php +++ b/tests/Integration/Http/RedisSessionTest.php @@ -29,6 +29,10 @@ protected function setUp(): void { parent::setUp(); + if (! extension_loaded('redis') || ! class_exists(\Redis::class)) { + $this->markTestSkipped('The `redis` extension is not loaded.'); + } + $this->container->config(new RedisSessionConfig(expiration: Duration::hours(2), prefix: 'test_session:')); $this->container->singleton( SessionManager::class,