Skip to content
Open
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
4 changes: 2 additions & 2 deletions packages/router/src/HttpApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Fixtures/Controllers/ControllerWithSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Tests\Tempest\Fixtures\Controllers;

use Tempest\Http\Responses\Ok;
use Tempest\Http\Session\Session;
use Tempest\Router\Get;

final class ControllerWithSession
{
#[Get('/controller-with-session')]
public function __invoke(Session $session): Ok
{
$session->flash('test', 'hi');

return new Ok();
}
}
16 changes: 16 additions & 0 deletions tests/Fixtures/Controllers/ControllerWithoutSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Tests\Tempest\Fixtures\Controllers;

use Tempest\Http\Responses\Ok;
use Tempest\Router\Get;
use Tempest\Router\Stateless;

final class ControllerWithoutSession
{
#[Stateless, Get('/controller-without-session')]
public function __invoke(): Ok
{
return new Ok();
}
}
33 changes: 33 additions & 0 deletions tests/Integration/Application/HttpApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

namespace Tests\Tempest\Integration\Application;

use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\Process\Process;
use Tempest\HttpClient\HttpClient;
use Tests\Tempest\Fixtures\Controllers\ControllerWithoutSession;
use Tests\Tempest\Fixtures\Controllers\ControllerWithSession;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;

use function Tempest\Router\uri;
use function Tempest\Support\arr;

/**
* @internal
*/
Expand All @@ -17,4 +25,29 @@ public function test_http_application_run(): void
->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();
}
}
4 changes: 4 additions & 0 deletions tests/Integration/Http/RedisSessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading