2026-11-16 16:53:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Tests\Integration\WithApp\Infra\Adapters\Services;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
|
use Infrastructure\Adapters\Services\AppSessionService;
|
|
|
|
|
use Shared\Helpers\UuidHelper;
|
|
|
|
|
|
|
|
|
|
describe('AppSessionService', function (): void {
|
|
|
|
|
it('generates a valid UUIDv7 SessionId and persists its string value when session key is absent', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$sessionKey = 'test_session_id';
|
|
|
|
|
Session::forget($sessionKey);
|
|
|
|
|
$service = new AppSessionService($sessionKey);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$sessionId = $service->get();
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect(UuidHelper::isV7($sessionId->value))->toBeTrue()
|
|
|
|
|
->and(Session::has($sessionKey))->toBeTrue()
|
|
|
|
|
->and(Session::get($sessionKey))->toBe($sessionId->value->toString());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns the exact same SessionId on subsequent get calls without overwriting the session value', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$sessionKey = 'test_session_id';
|
|
|
|
|
Session::forget($sessionKey);
|
|
|
|
|
$service = new AppSessionService($sessionKey);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$first = $service->get();
|
|
|
|
|
$second = $service->get();
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($second->value->toString())->toBe($first->value->toString())
|
|
|
|
|
->and(Session::get($sessionKey))->toBe($first->value->toString());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('hydrates an existing UUID string already stored in Session under the configured key', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$sessionKey = 'test_session_id';
|
2026-11-17 12:15:39 +01:00
|
|
|
$existingUuid = UuidHelper::v7()->toString();
|
2026-11-16 16:53:21 +01:00
|
|
|
Session::put($sessionKey, $existingUuid);
|
|
|
|
|
$service = new AppSessionService($sessionKey);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$sessionId = $service->get();
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($sessionId->value->toString())->toBe($existingUuid)
|
|
|
|
|
->and(Session::get($sessionKey))->toBe($existingUuid);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('keeps separate AppSessionService instances with distinct session keys isolated', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$firstKey = 'first_session_id';
|
|
|
|
|
$secondKey = 'second_session_id';
|
|
|
|
|
Session::forget([$firstKey, $secondKey]);
|
|
|
|
|
$firstService = new AppSessionService($firstKey);
|
|
|
|
|
$secondService = new AppSessionService($secondKey);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$firstId = $firstService->get();
|
|
|
|
|
$secondId = $secondService->get();
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($firstId->value->toString())->not->toBe($secondId->value->toString())
|
|
|
|
|
->and(Session::get($firstKey))->toBe($firstId->value->toString())
|
|
|
|
|
->and(Session::get($secondKey))->toBe($secondId->value->toString());
|
|
|
|
|
});
|
|
|
|
|
});
|