ExecutionContext::CONTRACT, 'composition' => 'b4093c6f6707', 'operation' => 'purchasing_conflicts.authorise', 'scope' => ['tenant_id' => 1, 'company_id' => 2, 'unit_id' => 2], 'actor' => ['host_id' => 15, 'native_id' => 1], 'generation' => str_repeat('a', 64), 'schema_sha256' => str_repeat('b', 64), 'workflow_sha256' => str_repeat('c', 64), 'features' => ['conflict-release' => []]]; } test('capability exists only within the checked connection and execution', function () { $context = new ExecutionContext; $db = new stdClass; $checks = 0; expect($context->allows('conflict-release', $db))->toBeFalse(); $result = $context->within(compositionDeclaration(), $db, function () use (&$checks) { $checks++; }, function () use ($context, $db) { expect($context->allows('conflict-release', $db))->toBeTrue() ->and($context->allows('conflict-release', new stdClass))->toBeFalse() ->and($context->allows('order-cancellation-cycles', $db))->toBeFalse(); return 'complete'; }); expect($checks)->toBe(3)->and($result)->toBe('complete')->and($context->allows('conflict-release', $db))->toBeFalse(); }); test('revocation is rechecked and an exception clears the execution', function () { $context = new ExecutionContext; $db = new stdClass; $authorised = true; try { $context->within(compositionDeclaration(), $db, function () use (&$authorised) { if (! $authorised) { throw new RuntimeException('revoked'); } }, function () use ($context, $db, &$authorised) { $authorised = false; $context->allows('conflict-release', $db); }); } catch (RuntimeException $error) { expect($error->getMessage())->toBe('revoked'); } expect($context->allows('conflict-release', $db))->toBeFalse(); }); test('invalid declarations never enter the business closure', function (string $case) { $declaration = compositionDeclaration(); match ($case) { 'contract' => $declaration['contract'] = 'unknown', 'extra' => $declaration['admin'] = true, 'hash' => $declaration['workflow_sha256'] = 'unknown', 'scope' => $declaration['scope']['unit_id'] = null, 'identity' => $declaration['actor']['native_id'] = 0, 'feature' => $declaration['features']['all'] = [], 'component' => $declaration['features']['order-cancellation-cycles'] = ['root' => '/unknown'], }; (new ExecutionContext)->within($declaration, new stdClass, fn () => null, fn () => throw new RuntimeException('business executed')); })->with(['contract', 'extra', 'hash', 'scope', 'identity', 'feature', 'component'])->throws(LogicException::class); test('nested execution cannot replace the original authority', function () { $context = new ExecutionContext; $db = new stdClass; $context->within(compositionDeclaration(), $db, fn () => null, function () use ($context, $db) { expect(fn () => $context->within(compositionDeclaration(), $db, fn () => null, fn () => null))->toThrow(LogicException::class); expect($context->allows('conflict-release', $db))->toBeTrue(); }); expect($context->allows('conflict-release', $db))->toBeFalse(); }); test('read authority cannot execute a write even when the feature is available', function () { $context = new ExecutionContext; $db = new stdClass; $declaration = compositionDeclaration(); $declaration['operation'] = 'purchasing_conflicts.view'; $context->within($declaration, $db, fn () => null, function () use ($context, $db) { expect($context->allows('conflict-release', $db))->toBeTrue() ->and($context->permits('purchasing_conflicts.view', $db))->toBeTrue() ->and($context->permits('purchasing_conflicts.authorise', $db))->toBeFalse(); }); });