'tenant-env', 'name' => 'Preferred Tenant', 'is_current' => false, ]); Tenant::create([ 'tenant_id' => 'other-tenant', 'name' => 'Other Tenant', 'is_current' => true, ]); $resolved = Tenant::current(); expect($resolved->id)->toBe($preferred->id); restoreIntuneTenantId($originalEnv); }); it('throws when env tenant is missing or inactive', function () { $originalEnv = getenv('INTUNE_TENANT_ID'); putenv('INTUNE_TENANT_ID=missing-tenant'); expect(fn () => Tenant::current())->toThrow( \RuntimeException::class, 'Configured INTUNE_TENANT_ID tenant is missing or inactive.' ); restoreIntuneTenantId($originalEnv); }); it('returns tenant marked as current when no env override', function () { $originalEnv = getenv('INTUNE_TENANT_ID'); putenv('INTUNE_TENANT_ID='); $current = Tenant::create([ 'tenant_id' => 'tenant-current', 'name' => 'Current Tenant', 'is_current' => true, ]); Tenant::create([ 'tenant_id' => 'tenant-inactive', 'name' => 'Inactive Current Flag', 'status' => 'archived', ]); $resolved = Tenant::current(); expect($resolved->id)->toBe($current->id); restoreIntuneTenantId($originalEnv); }); it('throws when no current tenant is selected', function () { $originalEnv = getenv('INTUNE_TENANT_ID'); putenv('INTUNE_TENANT_ID='); Tenant::create([ 'tenant_id' => 'tenant-one', 'name' => 'Tenant One', 'is_current' => false, ]); expect(fn () => Tenant::current())->toThrow(\RuntimeException::class, 'No current tenant selected.'); restoreIntuneTenantId($originalEnv); }); it('makeCurrent toggles flags within a transaction', function () { $originalEnv = getenv('INTUNE_TENANT_ID'); putenv('INTUNE_TENANT_ID='); $first = Tenant::create([ 'tenant_id' => 'tenant-first', 'name' => 'First Tenant', 'is_current' => true, ]); $second = Tenant::create([ 'tenant_id' => 'tenant-second', 'name' => 'Second Tenant', ]); $second->makeCurrent(); expect($second->fresh()->is_current)->toBeTrue(); expect($first->fresh()->is_current)->toBeFalse(); restoreIntuneTenantId($originalEnv); });