test(architectures): 拆分 RegistryInitializationHookBaseTests 辅助类型

- 拆分 RegistryInitializationHookBaseTests 末尾的测试辅助类型到同目录独立文件以消除 MA0048

- 更新 TestRegistry 的公开集合暴露方式为只读接口以规避 MA0016

- 补充新测试辅助类型的 XML 文档并保持原有测试行为不变
This commit is contained in:
gewuyou 2026-04-28 07:50:36 +08:00
parent a7be41367a
commit 54530d31d9
6 changed files with 486 additions and 295 deletions

View File

@ -1,11 +1,5 @@
using System.Reflection;
using GFramework.Core.Abstractions.Architectures;
using System;
using GFramework.Core.Abstractions.Enums;
using GFramework.Core.Abstractions.Lifecycle;
using GFramework.Core.Abstractions.Model;
using GFramework.Core.Abstractions.Systems;
using GFramework.Core.Abstractions.Utility;
using GFramework.Core.Architectures;
namespace GFramework.Core.Tests.Architectures;
@ -116,291 +110,3 @@ public class RegistryInitializationHookBaseTests
Assert.That(registry.RegisteredConfigs.Count, Is.EqualTo(2));
}
}
/// <summary>
/// 测试用的注册表初始化钩子实现
/// </summary>
public class TestRegistryInitializationHook : RegistryInitializationHookBase<TestRegistry, string>
{
public TestRegistryInitializationHook(
IEnumerable<string> configs,
ArchitecturePhase targetPhase = ArchitecturePhase.AfterSystemInit)
: base(configs, targetPhase)
{
}
protected override void RegisterConfig(TestRegistry registry, string config)
{
registry.Register(config);
}
}
/// <summary>
/// 测试用的注册表类
/// </summary>
public class TestRegistry : IUtility
{
public List<string> RegisteredConfigs { get; } = new();
public void Register(string config)
{
RegisteredConfigs.Add(config);
}
}
/// <summary>
/// 测试用的架构类(包含注册表)
/// </summary>
public class TestArchitectureWithRegistry : IArchitecture
{
private readonly TestRegistry _registry;
public TestArchitectureWithRegistry(TestRegistry registry)
{
_registry = registry;
Context = new TestArchitectureContextWithRegistry(registry);
}
public Action<IServiceCollection>? Configurator { get; }
public IArchitectureContext Context { get; }
Action<IServiceCollection>? IArchitecture.Configurator => Configurator;
T IArchitecture.RegisterSystem<T>(T system)
{
throw new NotSupportedException();
}
T IArchitecture.RegisterModel<T>(T model)
{
throw new NotSupportedException();
}
T IArchitecture.RegisterUtility<T>(T utility)
{
throw new NotSupportedException();
}
public void RegisterCqrsPipelineBehavior<TBehavior>() where TBehavior : class
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现显式程序集 CQRS 处理器接入入口。
/// </summary>
/// <param name="assembly">包含 CQRS 处理器或生成注册器的程序集。</param>
/// <exception cref="NotSupportedException">该测试替身不参与 CQRS 程序集接入路径验证。</exception>
public void RegisterCqrsHandlersFromAssembly(Assembly assembly)
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现显式程序集 CQRS 处理器接入入口。
/// </summary>
/// <param name="assemblies">要接入的程序集集合。</param>
/// <exception cref="NotSupportedException">该测试替身不参与 CQRS 程序集接入路径验证。</exception>
public void RegisterCqrsHandlersFromAssemblies(IEnumerable<Assembly> assemblies)
{
throw new NotSupportedException();
}
public IArchitectureModule InstallModule(IArchitectureModule module)
{
throw new NotSupportedException();
}
IArchitectureLifecycleHook IArchitecture.RegisterLifecycleHook(IArchitectureLifecycleHook hook)
{
throw new NotSupportedException();
}
Task IArchitecture.WaitUntilReadyAsync()
{
return WaitUntilReadyAsync();
}
public void RegisterUtility<T>(Action<T>? onCreated = default(Action<T>?)) where T : class, IUtility
{
throw new NotSupportedException();
}
public void RegisterModel<T>(Action<T>? onCreated = default(Action<T>?)) where T : class, IModel
{
throw new NotSupportedException();
}
public void RegisterSystem<T>(Action<T>? onCreated = default(Action<T>?)) where T : class, ISystem
{
throw new NotSupportedException();
}
public void Initialize()
{
}
public void Destroy()
{
throw new NotSupportedException();
}
Task IAsyncInitializable.InitializeAsync()
{
return InitializeAsync();
}
ValueTask IAsyncDestroyable.DestroyAsync()
{
return DestroyAsync();
}
public Task WaitUntilReadyAsync()
{
throw new NotSupportedException();
}
public void RegisterLifecycleHook(IArchitectureLifecycleHook hook)
{
}
public Task InitializeAsync()
{
throw new NotSupportedException();
}
public ValueTask DestroyAsync()
{
throw new NotSupportedException();
}
}
/// <summary>
/// 测试用的架构上下文类(包含注册表)
/// </summary>
public class TestArchitectureContextWithRegistry : TestArchitectureContext
{
private readonly TestRegistry _registry;
public TestArchitectureContextWithRegistry(TestRegistry registry)
{
_registry = registry;
}
public override TUtility? GetUtility<TUtility>() where TUtility : class
{
if (typeof(TUtility) == typeof(TestRegistry))
{
return _registry as TUtility;
}
return base.GetUtility<TUtility>();
}
}
/// <summary>
/// 测试用的架构类(不包含注册表)
/// </summary>
public class TestArchitectureWithoutRegistry : IArchitecture
{
public TestArchitectureWithoutRegistry()
{
Context = new TestArchitectureContext();
}
public IArchitectureContext Context { get; }
public Action<IServiceCollection>? Configurator { get; }
T IArchitecture.RegisterSystem<T>(T system)
{
throw new NotSupportedException();
}
T IArchitecture.RegisterModel<T>(T model)
{
throw new NotSupportedException();
}
T IArchitecture.RegisterUtility<T>(T utility)
{
throw new NotSupportedException();
}
public void RegisterCqrsPipelineBehavior<TBehavior>() where TBehavior : class
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现显式程序集 CQRS 处理器接入入口。
/// </summary>
/// <param name="assembly">包含 CQRS 处理器或生成注册器的程序集。</param>
/// <exception cref="NotSupportedException">该测试替身不参与 CQRS 程序集接入路径验证。</exception>
public void RegisterCqrsHandlersFromAssembly(Assembly assembly)
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现显式程序集 CQRS 处理器接入入口。
/// </summary>
/// <param name="assemblies">要接入的程序集集合。</param>
/// <exception cref="NotSupportedException">该测试替身不参与 CQRS 程序集接入路径验证。</exception>
public void RegisterCqrsHandlersFromAssemblies(IEnumerable<Assembly> assemblies)
{
throw new NotSupportedException();
}
public IArchitectureModule InstallModule(IArchitectureModule module)
{
throw new NotSupportedException();
}
IArchitectureLifecycleHook IArchitecture.RegisterLifecycleHook(IArchitectureLifecycleHook hook)
{
throw new NotSupportedException();
}
public Task WaitUntilReadyAsync()
{
throw new NotSupportedException();
}
public void RegisterUtility<T>(Action<T>? onCreated = default(Action<T>?)) where T : class, IUtility
{
throw new NotSupportedException();
}
public void RegisterModel<T>(Action<T>? onCreated = default(Action<T>?)) where T : class, IModel
{
throw new NotSupportedException();
}
public void RegisterSystem<T>(Action<T>? onCreated = default(Action<T>?)) where T : class, ISystem
{
throw new NotSupportedException();
}
public void Initialize()
{
}
public Task InitializeAsync()
{
throw new NotSupportedException();
}
public ValueTask DestroyAsync()
{
throw new NotSupportedException();
}
public void Destroy()
{
throw new NotSupportedException();
}
public void RegisterLifecycleHook(IArchitectureLifecycleHook hook)
{
}
}

View File

@ -0,0 +1,35 @@
using GFramework.Core.Architectures;
namespace GFramework.Core.Tests.Architectures;
/// <summary>
/// 为 <see cref="RegistryInitializationHookBaseTests" /> 在架构上下文中暴露 <see cref="TestRegistry" /> 的测试替身。
/// </summary>
public class TestArchitectureContextWithRegistry : TestArchitectureContext
{
private readonly TestRegistry _registry;
/// <summary>
/// 使用给定测试注册表创建上下文测试替身。
/// </summary>
/// <param name="registry">需要通过 <see cref="GetUtility{TUtility}" /> 返回的测试注册表。</param>
public TestArchitectureContextWithRegistry(TestRegistry registry)
{
_registry = registry;
}
/// <summary>
/// 在请求 <see cref="TestRegistry" /> 时返回测试注册表,其余类型回退到基类实现。
/// </summary>
/// <typeparam name="TUtility">请求的工具类型。</typeparam>
/// <returns>匹配时返回测试注册表,否则返回基类结果。</returns>
public override TUtility GetUtility<TUtility>()
{
if (typeof(TUtility) == typeof(TestRegistry))
{
return (TUtility)(object)_registry;
}
return base.GetUtility<TUtility>();
}
}

View File

@ -0,0 +1,204 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using GFramework.Core.Abstractions.Architectures;
using GFramework.Core.Abstractions.Lifecycle;
using GFramework.Core.Abstractions.Model;
using GFramework.Core.Abstractions.Systems;
using GFramework.Core.Abstractions.Utility;
namespace GFramework.Core.Tests.Architectures;
/// <summary>
/// 为 <see cref="RegistryInitializationHookBaseTests" /> 提供已挂接 <see cref="TestRegistry" /> 的架构测试替身。
/// </summary>
public class TestArchitectureWithRegistry : IArchitecture
{
/// <summary>
/// 使用给定测试注册表创建架构测试替身。
/// </summary>
/// <param name="registry">要通过架构上下文暴露给钩子的测试注册表。</param>
public TestArchitectureWithRegistry(TestRegistry registry)
{
Context = new TestArchitectureContextWithRegistry(registry);
}
/// <summary>
/// 获取测试替身公开的服务配置入口。
/// 当前切片不验证服务配置流程,因此始终保持为空。
/// </summary>
public Action<IServiceCollection>? Configurator { get; }
/// <summary>
/// 获取当前测试替身使用的架构上下文。
/// </summary>
public IArchitectureContext Context { get; }
Action<IServiceCollection>? IArchitecture.Configurator => Configurator;
T IArchitecture.RegisterSystem<T>(T system)
{
throw new NotSupportedException();
}
T IArchitecture.RegisterModel<T>(T model)
{
throw new NotSupportedException();
}
T IArchitecture.RegisterUtility<T>(T utility)
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现 CQRS 管道行为注册。
/// </summary>
/// <typeparam name="TBehavior">行为类型。</typeparam>
/// <exception cref="NotSupportedException">该测试替身不参与 CQRS 管道配置验证。</exception>
public void RegisterCqrsPipelineBehavior<TBehavior>() where TBehavior : class
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现显式程序集 CQRS 处理器接入入口。
/// </summary>
/// <param name="assembly">包含 CQRS 处理器或生成注册器的程序集。</param>
/// <exception cref="NotSupportedException">该测试替身不参与 CQRS 程序集接入路径验证。</exception>
public void RegisterCqrsHandlersFromAssembly(Assembly assembly)
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现显式程序集 CQRS 处理器接入入口。
/// </summary>
/// <param name="assemblies">要接入的程序集集合。</param>
/// <exception cref="NotSupportedException">该测试替身不参与 CQRS 程序集接入路径验证。</exception>
public void RegisterCqrsHandlersFromAssemblies(IEnumerable<Assembly> assemblies)
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现模块安装流程。
/// </summary>
/// <param name="module">要安装的模块。</param>
/// <returns>此方法始终抛出异常,不返回模块实例。</returns>
/// <exception cref="NotSupportedException">该测试替身不参与模块安装路径验证。</exception>
public IArchitectureModule InstallModule(IArchitectureModule module)
{
throw new NotSupportedException();
}
IArchitectureLifecycleHook IArchitecture.RegisterLifecycleHook(IArchitectureLifecycleHook hook)
{
throw new NotSupportedException();
}
Task IArchitecture.WaitUntilReadyAsync()
{
return WaitUntilReadyAsync();
}
/// <summary>
/// 测试替身未实现工具延迟注册入口。
/// </summary>
/// <typeparam name="T">工具类型。</typeparam>
/// <param name="onCreated">工具创建后的回调。</param>
/// <exception cref="NotSupportedException">该测试替身不参与工具注册路径验证。</exception>
public void RegisterUtility<T>(Action<T>? onCreated = default) where T : class, IUtility
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现 Model 延迟注册入口。
/// </summary>
/// <typeparam name="T">Model 类型。</typeparam>
/// <param name="onCreated">Model 创建后的回调。</param>
/// <exception cref="NotSupportedException">该测试替身不参与 Model 注册路径验证。</exception>
public void RegisterModel<T>(Action<T>? onCreated = default) where T : class, IModel
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现 System 延迟注册入口。
/// </summary>
/// <typeparam name="T">System 类型。</typeparam>
/// <param name="onCreated">System 创建后的回调。</param>
/// <exception cref="NotSupportedException">该测试替身不参与 System 注册路径验证。</exception>
public void RegisterSystem<T>(Action<T>? onCreated = default) where T : class, ISystem
{
throw new NotSupportedException();
}
/// <summary>
/// 初始化测试替身。
/// 该切片只需要上下文可用,因此初始化过程保持为空实现。
/// </summary>
public void Initialize()
{
}
/// <summary>
/// 测试替身未实现销毁路径。
/// </summary>
/// <exception cref="NotSupportedException">该测试替身不参与销毁路径验证。</exception>
public void Destroy()
{
throw new NotSupportedException();
}
Task IAsyncInitializable.InitializeAsync()
{
return InitializeAsync();
}
ValueTask IAsyncDestroyable.DestroyAsync()
{
return DestroyAsync();
}
/// <summary>
/// 测试替身未实现就绪等待流程。
/// </summary>
/// <returns>此方法始终抛出异常,不返回等待任务。</returns>
/// <exception cref="NotSupportedException">该测试替身不参与就绪等待路径验证。</exception>
public Task WaitUntilReadyAsync()
{
throw new NotSupportedException();
}
/// <summary>
/// 注册架构生命周期钩子。
/// 当前切片不依赖生命周期钩子执行,因此保持空实现。
/// </summary>
/// <param name="hook">要忽略的生命周期钩子。</param>
public void RegisterLifecycleHook(IArchitectureLifecycleHook hook)
{
}
/// <summary>
/// 测试替身未实现异步初始化路径。
/// </summary>
/// <returns>此方法始终抛出异常,不返回初始化任务。</returns>
/// <exception cref="NotSupportedException">该测试替身不参与异步初始化验证。</exception>
public Task InitializeAsync()
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现异步销毁路径。
/// </summary>
/// <returns>此方法始终抛出异常,不返回销毁任务。</returns>
/// <exception cref="NotSupportedException">该测试替身不参与异步销毁验证。</exception>
public ValueTask DestroyAsync()
{
throw new NotSupportedException();
}
}

View File

@ -0,0 +1,187 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using GFramework.Core.Abstractions.Architectures;
using GFramework.Core.Abstractions.Lifecycle;
using GFramework.Core.Abstractions.Model;
using GFramework.Core.Abstractions.Systems;
using GFramework.Core.Abstractions.Utility;
using GFramework.Core.Architectures;
namespace GFramework.Core.Tests.Architectures;
/// <summary>
/// 为 <see cref="RegistryInitializationHookBaseTests" /> 提供不包含 <see cref="TestRegistry" /> 的架构测试替身。
/// </summary>
public class TestArchitectureWithoutRegistry : IArchitecture
{
/// <summary>
/// 创建不包含测试注册表的架构替身。
/// </summary>
public TestArchitectureWithoutRegistry()
{
Context = new TestArchitectureContext();
}
/// <summary>
/// 获取测试替身公开的服务配置入口。
/// 当前切片不验证服务配置流程,因此始终保持为空。
/// </summary>
public Action<IServiceCollection>? Configurator { get; }
/// <summary>
/// 获取当前测试替身使用的架构上下文。
/// </summary>
public IArchitectureContext Context { get; }
T IArchitecture.RegisterSystem<T>(T system)
{
throw new NotSupportedException();
}
T IArchitecture.RegisterModel<T>(T model)
{
throw new NotSupportedException();
}
T IArchitecture.RegisterUtility<T>(T utility)
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现 CQRS 管道行为注册。
/// </summary>
/// <typeparam name="TBehavior">行为类型。</typeparam>
/// <exception cref="NotSupportedException">该测试替身不参与 CQRS 管道配置验证。</exception>
public void RegisterCqrsPipelineBehavior<TBehavior>() where TBehavior : class
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现显式程序集 CQRS 处理器接入入口。
/// </summary>
/// <param name="assembly">包含 CQRS 处理器或生成注册器的程序集。</param>
/// <exception cref="NotSupportedException">该测试替身不参与 CQRS 程序集接入路径验证。</exception>
public void RegisterCqrsHandlersFromAssembly(Assembly assembly)
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现显式程序集 CQRS 处理器接入入口。
/// </summary>
/// <param name="assemblies">要接入的程序集集合。</param>
/// <exception cref="NotSupportedException">该测试替身不参与 CQRS 程序集接入路径验证。</exception>
public void RegisterCqrsHandlersFromAssemblies(IEnumerable<Assembly> assemblies)
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现模块安装流程。
/// </summary>
/// <param name="module">要安装的模块。</param>
/// <returns>此方法始终抛出异常,不返回模块实例。</returns>
/// <exception cref="NotSupportedException">该测试替身不参与模块安装路径验证。</exception>
public IArchitectureModule InstallModule(IArchitectureModule module)
{
throw new NotSupportedException();
}
IArchitectureLifecycleHook IArchitecture.RegisterLifecycleHook(IArchitectureLifecycleHook hook)
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现就绪等待流程。
/// </summary>
/// <returns>此方法始终抛出异常,不返回等待任务。</returns>
/// <exception cref="NotSupportedException">该测试替身不参与就绪等待路径验证。</exception>
public Task WaitUntilReadyAsync()
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现工具延迟注册入口。
/// </summary>
/// <typeparam name="T">工具类型。</typeparam>
/// <param name="onCreated">工具创建后的回调。</param>
/// <exception cref="NotSupportedException">该测试替身不参与工具注册路径验证。</exception>
public void RegisterUtility<T>(Action<T>? onCreated = default) where T : class, IUtility
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现 Model 延迟注册入口。
/// </summary>
/// <typeparam name="T">Model 类型。</typeparam>
/// <param name="onCreated">Model 创建后的回调。</param>
/// <exception cref="NotSupportedException">该测试替身不参与 Model 注册路径验证。</exception>
public void RegisterModel<T>(Action<T>? onCreated = default) where T : class, IModel
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现 System 延迟注册入口。
/// </summary>
/// <typeparam name="T">System 类型。</typeparam>
/// <param name="onCreated">System 创建后的回调。</param>
/// <exception cref="NotSupportedException">该测试替身不参与 System 注册路径验证。</exception>
public void RegisterSystem<T>(Action<T>? onCreated = default) where T : class, ISystem
{
throw new NotSupportedException();
}
/// <summary>
/// 初始化测试替身。
/// 该切片只需要一个不含注册表的上下文,因此初始化过程保持为空实现。
/// </summary>
public void Initialize()
{
}
/// <summary>
/// 测试替身未实现异步初始化路径。
/// </summary>
/// <returns>此方法始终抛出异常,不返回初始化任务。</returns>
/// <exception cref="NotSupportedException">该测试替身不参与异步初始化验证。</exception>
public Task InitializeAsync()
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现异步销毁路径。
/// </summary>
/// <returns>此方法始终抛出异常,不返回销毁任务。</returns>
/// <exception cref="NotSupportedException">该测试替身不参与异步销毁验证。</exception>
public ValueTask DestroyAsync()
{
throw new NotSupportedException();
}
/// <summary>
/// 测试替身未实现销毁路径。
/// </summary>
/// <exception cref="NotSupportedException">该测试替身不参与销毁路径验证。</exception>
public void Destroy()
{
throw new NotSupportedException();
}
/// <summary>
/// 注册架构生命周期钩子。
/// 当前切片不依赖生命周期钩子执行,因此保持空实现。
/// </summary>
/// <param name="hook">要忽略的生命周期钩子。</param>
public void RegisterLifecycleHook(IArchitectureLifecycleHook hook)
{
}
}

View File

@ -0,0 +1,26 @@
using System.Collections.Generic;
using GFramework.Core.Abstractions.Utility;
namespace GFramework.Core.Tests.Architectures;
/// <summary>
/// 为 <see cref="RegistryInitializationHookBaseTests" /> 记录注册结果的测试注册表。
/// </summary>
public class TestRegistry : IUtility
{
private readonly List<string> _registeredConfigs = [];
/// <summary>
/// 获取已注册配置值的只读视图,避免将测试内部使用的列表实现暴露给调用方。
/// </summary>
public IReadOnlyList<string> RegisteredConfigs => _registeredConfigs;
/// <summary>
/// 记录一次配置注册。
/// </summary>
/// <param name="config">要追加到测试结果中的配置值。</param>
public void Register(string config)
{
_registeredConfigs.Add(config);
}
}

View File

@ -0,0 +1,33 @@
using System.Collections.Generic;
using GFramework.Core.Abstractions.Enums;
using GFramework.Core.Architectures;
namespace GFramework.Core.Tests.Architectures;
/// <summary>
/// 为 <see cref="RegistryInitializationHookBaseTests" /> 提供的注册表初始化钩子测试替身。
/// </summary>
public class TestRegistryInitializationHook : RegistryInitializationHookBase<TestRegistry, string>
{
/// <summary>
/// 使用给定配置集合和目标阶段创建测试钩子。
/// </summary>
/// <param name="configs">测试期间要注册到目标注册表的配置值。</param>
/// <param name="targetPhase">触发注册行为的架构阶段。</param>
public TestRegistryInitializationHook(
IEnumerable<string> configs,
ArchitecturePhase targetPhase = ArchitecturePhase.AfterSystemInit)
: base(configs, targetPhase)
{
}
/// <summary>
/// 将当前配置值写入测试注册表。
/// </summary>
/// <param name="registry">要接收配置值的测试注册表。</param>
/// <param name="config">当前遍历到的配置值。</param>
protected override void RegisterConfig(TestRegistry registry, string config)
{
registry.Register(config);
}
}