Skip to content

Commit b0a1869

Browse files
authored
Merge branch 'main' into dev
2 parents ad600f6 + 810e841 commit b0a1869

File tree

9 files changed

+251
-0
lines changed

9 files changed

+251
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using OpenMediator.Buses;
3+
using OpenMediator.Shared.Integration.Test;
4+
using OpenMediator.Shared.Integration.Test.Commands;
5+
6+
namespace OpenMediator.Integration.Test.Commands;
7+
8+
public sealed class CustomMediatorMiddlewareTest
9+
{
10+
private readonly IServiceProvider _serviceProvider;
11+
private readonly IMediatorBus _mediator;
12+
private TestDependency _testDependency;
13+
14+
public CustomMediatorMiddlewareTest()
15+
{
16+
_testDependency = new TestDependency();
17+
var services = new ServiceCollection();
18+
services.AddOpenMediator(config =>
19+
{
20+
config.RegisterCommandsFromAssembly(typeof(GetUserCommandHandler).Assembly);
21+
});
22+
services.AddSingleton(_testDependency);
23+
_serviceProvider = services.BuildServiceProvider();
24+
_mediator = _serviceProvider.GetService<IMediatorBus>()!;
25+
}
26+
27+
[Fact]
28+
public async Task Send_Command_With_Response_Works()
29+
{
30+
// Arrange
31+
var command = new GetUserCommand(1);
32+
33+
// Act
34+
var result = await _mediator.SendAsync<GetUserCommand, User>(command);
35+
36+
// Assert
37+
var expectedResult = new User(
38+
Id: 1,
39+
Name: "Ralph",
40+
Email: "[email protected]");
41+
Assert.Equal(result, expectedResult);
42+
Assert.True(_testDependency.Called);
43+
}
44+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using OpenMediator.Buses;
3+
using OpenMediator.Shared.Integration.Test;
4+
using OpenMediator.Shared.Integration.Test.Commands;
5+
6+
namespace OpenMediator.Integration.Test.Commands;
7+
8+
public sealed class CommandTest
9+
{
10+
private readonly IServiceProvider _serviceProvider;
11+
private readonly IMediatorBus _mediator;
12+
private TestDependency _testDependency;
13+
14+
public CommandTest()
15+
{
16+
_testDependency = new TestDependency();
17+
var services = new ServiceCollection();
18+
services.AddOpenMediator(config =>
19+
{
20+
config.RegisterCommandsFromAssembly(typeof(GetUserCommandHandler).Assembly);
21+
});
22+
services.AddSingleton(_testDependency);
23+
_serviceProvider = services.BuildServiceProvider();
24+
_mediator = _serviceProvider.GetService<IMediatorBus>()!;
25+
}
26+
27+
[Fact]
28+
public async Task Send_Command_Works()
29+
{
30+
// Arrange
31+
var command = new CreateUserCommand(1, "UserTest");
32+
33+
// Act
34+
await _mediator.SendAsync(command);
35+
36+
// Assert
37+
Assert.True(_testDependency.Called);
38+
}
39+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using OpenMediator.Buses;
3+
using OpenMediator.Shared.Integration.Test;
4+
using OpenMediator.Shared.Integration.Test.Commands;
5+
6+
namespace OpenMediator.Integration.Test.MediatorMiddleware;
7+
8+
public sealed class CustomMediatorMiddlewareTest
9+
{
10+
private readonly IServiceProvider _serviceProvider;
11+
private readonly IMediatorBus _mediator;
12+
private TestDependency _testDependency;
13+
14+
public CustomMediatorMiddlewareTest()
15+
{
16+
_testDependency = new TestDependency();
17+
var services = new ServiceCollection();
18+
services.AddOpenMediator(config =>
19+
{
20+
config.RegisterCommandsFromAssembly(typeof(GetUserCommandHandler).Assembly);
21+
config.RegisterMiddleware<CustomMediatorMiddleware>();
22+
});
23+
services.AddSingleton(_testDependency);
24+
_serviceProvider = services.BuildServiceProvider();
25+
_mediator = _serviceProvider.GetService<IMediatorBus>()!;
26+
}
27+
28+
[Fact]
29+
public async Task Send_Command_With_Response_Works()
30+
{
31+
// Arrange
32+
var command = new GetUserCommand(1);
33+
34+
// Act
35+
var result = await _mediator.SendAsync<GetUserCommand, User>(command);
36+
37+
// Assert
38+
var expectedResult = new User(
39+
Id: 1,
40+
Name: "Ralph",
41+
Email: "[email protected]");
42+
Assert.Equal(result, expectedResult);
43+
Assert.True(_testDependency.Called);
44+
Assert.Equal(3, _testDependency.Counter);
45+
}
46+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.4" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
13+
<PackageReference Include="xunit" Version="2.9.3" />
14+
<PackageReference Include="coverlet.collector" Version="6.0.4">
15+
<PrivateAssets>all</PrivateAssets>
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
</PackageReference>
18+
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\..\src\OpenMediator.csproj" />
26+
<ProjectReference Include="..\OpenMediator.Shared.Integration.Test\OpenMediator.Shared.Integration.Test.csproj" />
27+
</ItemGroup>
28+
29+
<ItemGroup>
30+
<Using Include="Xunit" />
31+
</ItemGroup>
32+
33+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace OpenMediator.Shared.Integration.Test.Commands;
2+
3+
public record CreateUserCommand(int Id, string Name) : ICommand;
4+
5+
public record CreateUserCommandHandler(TestDependency testDependency) : ICommandHandler<CreateUserCommand>
6+
{
7+
public async Task HandleAsync(CreateUserCommand command)
8+
{
9+
await Task.Delay(500);
10+
testDependency.Called = true;
11+
}
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace OpenMediator.Shared.Integration.Test.Commands;
2+
3+
public record GetUserCommand(int Id) : ICommand<User>;
4+
5+
public record User(int Id, string Name, string Email);
6+
7+
public record GetUserCommandHandler(TestDependency testDependency) : ICommandHandler<GetUserCommand, User>
8+
{
9+
public async Task<User> HandleAsync(GetUserCommand command)
10+
{
11+
await Task.Delay(500);
12+
testDependency.Called = true;
13+
testDependency.Counter++;
14+
15+
return new User(
16+
Id: 1,
17+
Name: "Ralph",
18+
Email: "[email protected]");
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using OpenMediator.Middlewares;
2+
3+
namespace OpenMediator.Shared.Integration.Test;
4+
5+
public class CustomMediatorMiddleware(TestDependency testDependency) : IMediatorMiddleware
6+
{
7+
public async Task ExecuteAsync<TCommand>(TCommand command, Func<Task> next)
8+
where TCommand : ICommand
9+
{
10+
testDependency.Called = true;
11+
12+
// Do something before the command
13+
testDependency.Counter++;
14+
Task.Delay(500).Wait();
15+
16+
await next();
17+
18+
// Do something after the command
19+
testDependency.Counter++;
20+
Task.Delay(500).Wait();
21+
}
22+
23+
public Task ExecuteAsync<TCommand, TResponse>(TCommand command, Func<Task<TResponse>> next) where TCommand : ICommand<TResponse>
24+
{
25+
// Do something before the command
26+
testDependency.Counter++;
27+
Task.Delay(500).Wait();
28+
29+
var result = next();
30+
31+
// Do something after the command
32+
testDependency.Counter++;
33+
Task.Delay(500).Wait();
34+
35+
return result;
36+
}
37+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\src\OpenMediator.csproj" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace OpenMediator.Shared.Integration.Test;
2+
3+
public sealed class TestDependency
4+
{
5+
public bool Called { get; set; }
6+
public int Counter { get; set; }
7+
}

0 commit comments

Comments
 (0)