refactor: migrate to scoped execution contexts

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-07-27 00:50:43 -04:00
parent 4416920a32
commit 3556be7c85
2 changed files with 24 additions and 27 deletions
+15 -18
View File
@@ -6,7 +6,7 @@ namespace KTXT\AuthenticationProviderPassword\Tests\Integration;
use KTXC\Http\Request\Request;
use KTXC\Http\Response\Response;
use KTXC\Server;
use KTXC\Application;
use KTXC\Stores\UserRolesStore;
use PHPUnit\Framework\TestCase;
@@ -19,11 +19,11 @@ use PHPUnit\Framework\TestCase;
* through the real HTTP kernel (routing, tenant resolution, auth middleware,
* controller), not by calling controllers directly.
*
* A fresh Server (and DI container) is built for every request() call,
* A fresh Application (and DI container) is built for every request() call,
* mirroring the one-process-per-request model this app is actually
* deployed under. Session identity is a container-lifetime singleton that
* AuthenticationMiddleware only ever sets, never clears, so reusing one
* Server across requests would leak an authenticated identity from one
* Application across requests would leak an authenticated identity from one
* "request" into the next — a problem that can't occur in production,
* where each request gets its own process, but very much can here.
*
@@ -37,7 +37,7 @@ abstract class IntegrationTestCase extends TestCase
public static function setUpBeforeClass(): void
{
if (static::buildServer()->environment() === 'prod') {
if (static::buildApplication()->environment() === 'prod') {
self::fail('Refusing to run integration tests against a server configured for the "prod" environment.');
}
@@ -58,9 +58,9 @@ abstract class IntegrationTestCase extends TestCase
}
}
protected static function buildServer(): Server
protected static function buildApplication(): Application
{
return new Server(SERVER_ROOT);
return Application::create(SERVER_ROOT);
}
// =========================================================================
@@ -103,17 +103,16 @@ abstract class IntegrationTestCase extends TestCase
*/
protected static function createRoleWithPermissions(array $permissions, string $label = 'Test Role'): string
{
$server = static::buildServer();
$application = static::buildApplication();
try {
$server->kernel()->boot();
$store = $server->container()->get(UserRolesStore::class);
$application->kernel()->boot();
$store = $application->container()->get(UserRolesStore::class);
$role = $store->createRole(static::$tenantIdentifier, [
'label' => $label,
'permissions' => $permissions,
]);
} finally {
restore_error_handler();
restore_exception_handler();
$application->shutdown();
}
return $role['rid'];
@@ -174,15 +173,13 @@ abstract class IntegrationTestCase extends TestCase
$uri = 'http://' . static::$tenantDomain . $path;
$request = Request::create($uri, $method, [], [], [], $server, $content);
// Kernel::boot() registers a global error/exception handler per
// instance and never removes it (fine under one-process-per-request
// in production; here it would otherwise stack a handler per test).
// Pop back to whatever was in place before this request.
// A fresh application mirrors production while shutdown restores its
// process-level error handler after the in-process request.
$application = static::buildApplication();
try {
$response = static::buildServer()->handle($request);
$response = $application->handleHttp($request);
} finally {
restore_error_handler();
restore_exception_handler();
$application->shutdown();
}
$body = $response->getContent();