< Summary

Information
Class: Elsa.Testing.Shared.Multitenancy.TestTenantAccessor
Assembly: Elsa.Testing.Shared
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Testing.Shared/Multitenancy/TestTenantAccessor.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 15
Coverable lines: 15
Total lines: 44
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
get_Default()100%210%
get_TenantId()100%210%
get_Tenant()100%210%
PushContext(...)0%2040%
.ctor(...)100%210%
Dispose()100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Testing.Shared/Multitenancy/TestTenantAccessor.cs

#LineLine coverage
 1using Elsa.Common.Multitenancy;
 2
 3namespace Elsa.Testing.Shared.Multitenancy;
 4
 5/// <summary>
 6/// A tenant accessor for tests that construct tenant-aware services directly.
 7/// </summary>
 8/// <remarks>
 9/// Defaults to the default tenant, which is what an untenanted test expects. The stores it feeds also
 10/// admit tenant-agnostic and unassigned records, so a test that never sets a tenant on its fixtures keeps
 11/// seeing them.
 12/// </remarks>
 013public sealed class TestTenantAccessor(string tenantId = Tenant.DefaultTenantId) : ITenantAccessor
 14{
 15    /// <summary>A shared instance scoped to the default tenant.</summary>
 016    public static ITenantAccessor Default { get; } = new TestTenantAccessor();
 17
 18    /// <inheritdoc />
 019    public string TenantId { get; private set; } = tenantId;
 20
 21    /// <inheritdoc />
 022    public Tenant? Tenant { get; private set; }
 23
 24    /// <inheritdoc />
 25    public IDisposable PushContext(Tenant? tenant)
 26    {
 027        var previousTenant = Tenant;
 028        var previousTenantId = TenantId;
 29
 030        Tenant = tenant;
 031        TenantId = tenant?.Id ?? Tenant.DefaultTenantId;
 32
 033        return new Restore(() =>
 034        {
 035            Tenant = previousTenant;
 036            TenantId = previousTenantId;
 037        });
 38    }
 39
 040    private sealed class Restore(Action onDispose) : IDisposable
 41    {
 042        public void Dispose() => onDispose();
 43    }
 44}