Files
server/tests/php/Unit/Execution/ExecutionScopeTest.php
T
2026-07-27 00:48:11 -04:00

94 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXT\Unit\Execution;
use KTXC\Application\Execution\ExecutionContext;
use KTXC\Application\Execution\ExecutionDescriptor;
use KTXC\Application\Execution\ExecutionScope;
use KTXC\Context\IdentityContext;
use KTXC\Context\TenantContext;
use KTXC\Models\Identity\User;
use KTXC\Models\Tenant\TenantObject;
use KTXC\Service\TenantService;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
final class ExecutionScopeTest extends TestCase
{
#[Test]
#[TestDox('Disposal clears tenant and identity state')]
public function clears(): void
{
$tenantObject = (new TenantObject())
->setIdentifier('tenant-a')
->setEnabled(true);
$service = $this->createStub(TenantService::class);
$service->method('fetchById')->willReturn($tenantObject);
$tenant = new TenantContext($service);
$identity = new IdentityContext();
$descriptor = ExecutionDescriptor::cli();
$scope = new ExecutionScope(
$descriptor,
ExecutionContext::fromDescriptor($descriptor),
$tenant,
$identity,
);
$tenant->resolveIdentifier('tenant-a');
$user = new User();
$user->setId('user-a');
$identity->initialize($user);
$scope->dispose();
self::assertFalse($tenant->present());
self::assertFalse($identity->present());
}
#[Test]
#[TestDox('New scopes cannot see state from an earlier execution')]
public function isolates(): void
{
$tenantObject = (new TenantObject())
->setIdentifier('tenant-a')
->setEnabled(true);
$service = $this->createStub(TenantService::class);
$service->method('fetchById')->willReturn($tenantObject);
$tenant = new TenantContext($service);
$identity = new IdentityContext();
$tenant->resolveIdentifier('tenant-a');
$identity->initialize(new User());
$descriptor = ExecutionDescriptor::cli();
new ExecutionScope(
$descriptor,
ExecutionContext::fromDescriptor($descriptor),
$tenant,
$identity,
);
self::assertFalse($tenant->present());
self::assertFalse($identity->present());
}
#[Test]
#[TestDox('Execution scopes can only be marked terminated once')]
public function terminatesOnce(): void
{
$descriptor = ExecutionDescriptor::cli();
$scope = new ExecutionScope(
$descriptor,
ExecutionContext::fromDescriptor($descriptor),
new TenantContext($this->createStub(TenantService::class)),
new IdentityContext(),
);
$scope->markTerminated();
$this->expectException(\LogicException::class);
$scope->markTerminated();
}
}