using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using GFramework.Core.Abstractions.Architectures;
using GFramework.Core.Abstractions.Command;
using GFramework.Core.Abstractions.Environment;
using GFramework.Core.Abstractions.Events;
using GFramework.Core.Abstractions.Ioc;
using GFramework.Core.Abstractions.Model;
using GFramework.Core.Abstractions.Query;
using GFramework.Core.Abstractions.Systems;
using GFramework.Core.Abstractions.Utility;
using GFramework.Core.Command;
using GFramework.Core.Environment;
using GFramework.Core.Events;
using GFramework.Core.Ioc;
using GFramework.Core.Query;
using GFramework.Cqrs.Abstractions.Cqrs;
using ICommand = GFramework.Core.Abstractions.Command.ICommand;
namespace GFramework.Core.Tests.Architectures;
///
/// 为架构相关测试替身提供共享的 基础实现。
///
///
/// 该基类统一维护容器解析、共享 语义,以及 legacy / CQRS 入口的显式失败契约,
/// 以避免多个测试上下文因为并行复制实现而在后续演进中发生语义漂移。
///
public abstract class TestArchitectureContextBase : IArchitectureContext
{
private readonly MicrosoftDiContainer _container = new();
private readonly DefaultEnvironment _environment = new();
private readonly EventBus _eventBus = new();
///
/// 获取用于解析测试服务的依赖注入容器。
///
public IIocContainer Container => _container;
///
/// 获取测试事件总线实例。
///
///
/// 返回同一个缓存事件总线,以便 、 与
/// 在同一份订阅状态上协作。
///
public IEventBus EventBus => _eventBus;
///
/// 获取测试命令执行器实例。
///
public ICommandExecutor CommandExecutor => new CommandExecutor();
///
/// 获取测试查询执行器实例。
///
public IQueryExecutor QueryExecutor => new QueryExecutor();
///
/// 获取默认测试环境对象。
///
public IEnvironment Environment => _environment;
///
/// 获取指定类型的服务实例。
///
/// 服务类型。
/// 已注册的服务实例。
/// 未注册服务时抛出。
public TService GetService() where TService : class
{
return _container.GetRequired();
}
///
/// 获取指定类型的所有服务实例。
///
/// 服务类型。
/// 服务实例列表。
public IReadOnlyList GetServices() where TService : class
{
return _container.GetAll();
}
///
/// 获取指定类型的模型实例。
///
/// 模型类型。
/// 已注册的模型实例。
/// 未注册模型时抛出。
public TModel GetModel() where TModel : class, IModel
{
return _container.GetRequired();
}
///
/// 获取指定类型的所有模型实例。
///
/// 模型类型。
/// 模型实例列表。
public IReadOnlyList GetModels() where TModel : class, IModel
{
return _container.GetAll();
}
///
/// 获取指定类型的系统实例。
///
/// 系统类型。
/// 已注册的系统实例。
/// 未注册系统时抛出。
public TSystem GetSystem() where TSystem : class, ISystem
{
return _container.GetRequired();
}
///
/// 获取指定类型的所有系统实例。
///
/// 系统类型。
/// 系统实例列表。
public IReadOnlyList GetSystems() where TSystem : class, ISystem
{
return _container.GetAll();
}
///
/// 获取指定类型的工具实例。
///
/// 工具类型。
/// 已注册的工具实例。
/// 未注册工具时抛出。
public virtual TUtility GetUtility() where TUtility : class, IUtility
{
return _container.GetRequired();
}
///
/// 获取指定类型的所有工具实例。
///
/// 工具类型。
/// 工具实例列表。
public IReadOnlyList GetUtilities() where TUtility : class, IUtility
{
return _container.GetAll();
}
///
/// 获取指定类型的所有服务实例,并按优先级排序。
///
/// 服务类型。
/// 按优先级排序后的服务实例列表。
public IReadOnlyList GetServicesByPriority() where TService : class
{
return _container.GetAllByPriority();
}
///
/// 获取指定类型的所有系统实例,并按优先级排序。
///
/// 系统类型。
/// 按优先级排序后的系统实例列表。
public IReadOnlyList GetSystemsByPriority() where TSystem : class, ISystem
{
return _container.GetAllByPriority();
}
///
/// 获取指定类型的所有模型实例,并按优先级排序。
///
/// 模型类型。
/// 按优先级排序后的模型实例列表。
public IReadOnlyList GetModelsByPriority() where TModel : class, IModel
{
return _container.GetAllByPriority();
}
///
/// 获取指定类型的所有工具实例,并按优先级排序。
///
/// 工具类型。
/// 按优先级排序后的工具实例列表。
public IReadOnlyList GetUtilitiesByPriority() where TUtility : class, IUtility
{
return _container.GetAllByPriority();
}
///
/// 发送无参数事件。
///
/// 事件类型。
public void SendEvent() where TEvent : new()
{
_eventBus.Send();
}
///
/// 发送带参数事件。
///
/// 事件类型。
/// 事件实例。
/// 为 。
public void SendEvent(TEvent e) where TEvent : class
{
ArgumentNullException.ThrowIfNull(e);
_eventBus.Send(e);
}
///
/// 注册事件处理器。
///
/// 事件类型。
/// 事件处理委托。
/// 用于测试的事件注销句柄。
/// 为 。
public IUnRegister RegisterEvent(Action handler)
{
ArgumentNullException.ThrowIfNull(handler);
return _eventBus.Register(handler);
}
///
/// 取消注册事件处理器。
///
/// 事件类型。
/// 事件处理委托。
/// 为 。
public void UnRegisterEvent(Action onEvent)
{
ArgumentNullException.ThrowIfNull(onEvent);
_eventBus.UnRegister(onEvent);
}
///
/// 测试桩:异步发送统一 CQRS 请求。
///
/// 响应类型。
/// 要发送的请求。
/// 取消令牌。
/// 请求响应任务。
/// 该测试桩不支持此成员。
public ValueTask SendRequestAsync(
IRequest request,
CancellationToken cancellationToken = default)
{
throw new NotSupportedException();
}
///
/// 测试桩:同步发送统一 CQRS 请求。
///
/// 响应类型。
/// 要发送的请求。
/// 请求响应。
/// 该测试桩不支持此成员。
public TResponse SendRequest(IRequest request)
{
throw new NotSupportedException();
}
///
/// 测试桩:异步发送 CQRS 命令并返回响应。
///
/// 命令响应类型。
/// 要发送的命令。
/// 取消令牌。
/// 命令响应任务。
/// 该测试桩不支持此成员。
public ValueTask SendCommandAsync(
GFramework.Cqrs.Abstractions.Cqrs.Command.ICommand command,
CancellationToken cancellationToken = default)
{
throw new NotSupportedException();
}
///
/// 测试桩:同步发送 CQRS 命令并返回响应。
///
/// 命令响应类型。
/// 要发送的命令。
/// 命令响应。
/// 该测试桩不支持此成员。
public TResponse SendCommand(GFramework.Cqrs.Abstractions.Cqrs.Command.ICommand command)
{
throw new NotSupportedException();
}
///
/// 测试桩:异步发送 CQRS 查询并返回结果。
///
/// 查询结果类型。
/// 要发送的查询。
/// 取消令牌。
/// 查询结果任务。
/// 该测试桩不支持此成员。
public ValueTask SendQueryAsync(
GFramework.Cqrs.Abstractions.Cqrs.Query.IQuery query,
CancellationToken cancellationToken = default)
{
throw new NotSupportedException();
}
///
/// 测试桩:同步发送 CQRS 查询并返回结果。
///
/// 查询结果类型。
/// 要发送的查询。
/// 查询结果。
/// 该测试桩不支持此成员。
public TResponse SendQuery(GFramework.Cqrs.Abstractions.Cqrs.Query.IQuery query)
{
throw new NotSupportedException();
}
///
/// 测试桩:异步发布 CQRS 通知。
///
/// 通知类型。
/// 要发布的通知。
/// 取消令牌。
/// 通知发布任务。
/// 该测试桩不支持此成员。
public ValueTask PublishAsync(
TNotification notification,
CancellationToken cancellationToken = default)
where TNotification : INotification
{
throw new NotSupportedException();
}
///
/// 测试桩:创建 CQRS 流式请求响应序列。
///
/// 流式响应元素类型。
/// 流式请求。
/// 取消令牌。
/// 异步响应流。
/// 该测试桩不支持此成员。
public IAsyncEnumerable CreateStream(
IStreamRequest request,
CancellationToken cancellationToken = default)
{
throw new NotSupportedException();
}
///
/// 测试桩:异步发送无返回值 CQRS 命令。
///
/// 命令类型。
/// 要发送的命令。
/// 取消令牌。
/// 命令发送任务。
/// 该测试桩不支持此成员。
public ValueTask SendAsync(TCommand command, CancellationToken cancellationToken = default)
where TCommand : IRequest
{
throw new NotSupportedException();
}
///
/// 测试桩:异步发送带返回值的 CQRS 请求。
///
/// 响应类型。
/// 要发送的请求。
/// 取消令牌。
/// 请求响应任务。
/// 该测试桩不支持此成员。
public ValueTask SendAsync(
IRequest command,
CancellationToken cancellationToken = default)
{
throw new NotSupportedException();
}
///
/// 发送旧版命令。
///
/// 命令对象。
/// 该测试桩不支持旧版命令执行入口。
public void SendCommand(ICommand command)
{
throw new NotSupportedException();
}
///
/// 发送旧版带返回值命令。
///
/// 返回值类型。
/// 命令对象。
/// 此方法始终抛出异常,不返回结果。
/// 该测试桩不支持旧版命令执行入口。
public TResult SendCommand(ICommand command)
{
throw new NotSupportedException();
}
///
/// 异步发送旧版命令。
///
/// 命令对象。
/// 已失败的任务。
public Task SendCommandAsync(IAsyncCommand command)
{
return Task.FromException(new NotSupportedException());
}
///
/// 异步发送旧版带返回值命令。
///
/// 返回值类型。
/// 命令对象。
/// 已失败的任务。
public Task SendCommandAsync(IAsyncCommand command)
{
return Task.FromException(new NotSupportedException());
}
///
/// 发送旧版查询请求。
///
/// 查询结果类型。
/// 查询对象。
/// 此方法始终抛出异常,不返回结果。
/// 该测试桩不支持旧版查询执行入口。
public TResult SendQuery(IQuery query)
{
throw new NotSupportedException();
}
///
/// 异步发送旧版查询请求。
///
/// 查询结果类型。
/// 异步查询对象。
/// 已失败的任务。
public Task SendQueryAsync(IAsyncQuery query)
{
return Task.FromException(new NotSupportedException());
}
///
/// 获取当前环境对象。
///
/// 默认测试环境对象。
public IEnvironment GetEnvironment()
{
return Environment;
}
}