Files
server/tests/php/Unit/Module/ModuleContextTest.php
T
2026-08-31 22:55:11 -04:00

86 lines
3.0 KiB
PHP

<?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');
}
}
}