refactor(modules): configure integrations through runtime contexts

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-08-31 22:40:25 -04:00
parent 9fcfe32ea7
commit 8cedd8a18a
16 changed files with 378 additions and 71 deletions
+48 -6
View File
@@ -8,6 +8,10 @@ use KTXC\Console\Firewall\FirewallMaintenanceCommand;
use KTXC\Console\Firewall\FirewallSetupCommand;
use KTXC\Console\Event\EventsDebugCommand;
use KTXC\Module\Module;
use KTXC\Module\Configuration\BrowserModuleContext;
use KTXC\Module\Configuration\ConsoleModuleContext;
use KTXC\Preview\PreviewManager;
use KTXC\Resource\ProviderManager;
use KTXC\Service\FirewallService;
use KTXC\Service\SystemFirewallRuleService;
use KTXC\Service\SystemFirewallLogService;
@@ -15,6 +19,8 @@ use KTXC\Service\TenantFirewallLogService;
use KTXC\Service\TenantFirewallStatusService;
use KTXC\Service\SystemFirewallStatusService;
use KTXC\Service\TenantFirewallRuleService;
use KTXC\Service\TenantService;
use KTXC\SystemStore\SystemStoreManager;
use KTXC\Event\EventListenerRegistry;
use KTXC\Security\Event\AccessDeniedEvent;
use KTXC\Security\Event\AuthenticationFailedEvent;
@@ -32,6 +38,8 @@ use KTXF\Event\DeliveryMode;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
final class CoreModuleTest extends TestCase
{
@@ -40,7 +48,7 @@ final class CoreModuleTest extends TestCase
public function registersListeners(): void
{
$registry = new EventListenerRegistry();
$module = new Module($registry);
$module = $this->module($registry);
$module->boot();
$definitions = $registry->definitions();
@@ -83,18 +91,21 @@ final class CoreModuleTest extends TestCase
#[TestDox('Core exposes the event registry debug command')]
public function exposesDebugCommand(): void
{
$module = new Module(new EventListenerRegistry());
$module = $this->module();
$context = new ConsoleModuleContext();
$module->configure($context);
$commands = $context->commands();
self::assertContains(EventsDebugCommand::class, $module->registerCI());
self::assertContains(FirewallSetupCommand::class, $module->registerCI());
self::assertContains(FirewallMaintenanceCommand::class, $module->registerCI());
self::assertContains(EventsDebugCommand::class, $commands);
self::assertContains(FirewallSetupCommand::class, $commands);
self::assertContains(FirewallMaintenanceCommand::class, $commands);
}
#[Test]
#[TestDox('Core registers dedicated system firewall permissions')]
public function registersSystemFirewallPermissions(): void
{
$permissions = (new Module(new EventListenerRegistry()))->permissions();
$permissions = $this->module()->permissions();
self::assertArrayHasKey(SystemFirewallRuleService::PERMISSION_READ, $permissions);
self::assertArrayHasKey(SystemFirewallRuleService::PERMISSION_MANAGE, $permissions);
@@ -107,4 +118,35 @@ final class CoreModuleTest extends TestCase
self::assertArrayHasKey(SystemFirewallStatusService::PERMISSION_MAINTENANCE_READ, $permissions);
self::assertArrayHasKey(SystemFirewallStatusService::PERMISSION_SETTINGS_MANAGE, $permissions);
}
#[Test]
#[TestDox('Core resolves preview browser configuration')]
public function resolvesPreviewBrowserConfiguration(): void
{
$module = $this->module();
$context = new BrowserModuleContext('tenant-id', 'identity-id');
$module->configure($context);
self::assertSame([
'preview' => [
'enabled' => false,
'storage' => false,
'generation' => false,
],
], $context->configuration());
}
private function module(?EventListenerRegistry $registry = null): Module
{
$tenants = $this->createStub(TenantService::class);
$providers = new ProviderManager($this->createStub(ContainerInterface::class));
$previews = new PreviewManager(
$tenants,
$providers,
new SystemStoreManager($tenants, $providers),
$this->createStub(LoggerInterface::class),
);
return new Module($registry ?? new EventListenerRegistry(), $previews);
}
}
@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace KTXT\Unit\Module;
use KTXC\Module\Configuration\BrowserModuleContext;
use KTXC\Module\Configuration\ConsoleModuleContext;
use KTXF\Module\Configuration\BrowserModuleContextInterface;
use KTXF\Module\Configuration\ConsoleModuleContextInterface;
use KTXF\Module\Configuration\ModuleContextInterface;
use KTXF\Module\Configuration\ModuleContextType;
use KTXF\Module\ModuleInstanceAbstract;
use LogicException;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class ModuleContextTest extends TestCase
{
#[Test]
public function moduleRegistersItselfWithTheActiveContext(): void
{
$module = new TestContextModule();
$browser = new BrowserModuleContext('tenant-id', 'identity-id');
$console = new ConsoleModuleContext();
$module->configure($browser);
$module->configure($console);
self::assertSame(
[
'test_context' => [
'handle' => 'test_context',
'namespace' => 'TestContext',
'version' => '0.0.1',
'label' => 'Test Context',
'author' => 'Ktrix',
'description' => 'Tests module contexts',
'boot' => 'static/module.mjs',
],
],
$browser->modules(),
);
self::assertSame(
['test' => ['tenant' => 'tenant-id', 'identity' => 'identity-id']],
$browser->configuration(),
);
self::assertSame(['ExampleCommand'], $console->commands());
self::assertSame(ModuleContextType::Browser, $browser->type());
self::assertSame(ModuleContextType::Console, $console->type());
}
#[Test]
public function rejectsDuplicateBrowserConfigurationKeys(): void
{
$context = new BrowserModuleContext('tenant-id', 'identity-id');
$context->set('test', true);
$this->expectException(LogicException::class);
$context->set('test', false);
}
}
final class TestContextModule extends ModuleInstanceAbstract
{
public function handle(): string { return 'test_context'; }
public function version(): string { return '0.0.1'; }
public function label(): string { return 'Test Context'; }
public function description(): string { return 'Tests module contexts'; }
public function author(): string { return 'Ktrix'; }
public function configure(ModuleContextInterface $context): void
{
if ($context instanceof BrowserModuleContextInterface) {
$context->registerModule($this, 'TestContext', 'static/module.mjs');
$context->set('test', [
'tenant' => $context->tenantIdentifier(),
'identity' => $context->identityIdentifier(),
]);
}
if ($context instanceof ConsoleModuleContextInterface) {
$context->registerCommand('ExampleCommand');
$context->registerCommand('ExampleCommand');
}
}
}