mirror of https://github.com/Squidex/squidex.git
4 changed files with 129 additions and 0 deletions
@ -0,0 +1,41 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Squidex.Infrastructure.Commands |
|||
{ |
|||
public sealed class CustomCommandMiddlewareRunner : ICommandMiddleware |
|||
{ |
|||
private readonly IEnumerable<ICustomCommandMiddleware> extensions; |
|||
|
|||
public CustomCommandMiddlewareRunner(IEnumerable<ICustomCommandMiddleware> extensions) |
|||
{ |
|||
Guard.NotNull(extensions, nameof(extensions)); |
|||
|
|||
this.extensions = extensions.Reverse().ToList(); |
|||
} |
|||
|
|||
public async Task HandleAsync(CommandContext context, Func<Task> next) |
|||
{ |
|||
foreach (var handler in extensions) |
|||
{ |
|||
next = Join(handler, context, next); |
|||
} |
|||
|
|||
await next(); |
|||
} |
|||
|
|||
private static Func<Task> Join(ICommandMiddleware handler, CommandContext context, Func<Task> next) |
|||
{ |
|||
return () => handler.HandleAsync(context, next); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
namespace Squidex.Infrastructure.Commands |
|||
{ |
|||
public interface ICustomCommandMiddleware : ICommandMiddleware |
|||
{ |
|||
} |
|||
} |
|||
@ -0,0 +1,72 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using FakeItEasy; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Infrastructure.Commands |
|||
{ |
|||
public class CustomCommandMiddlewareRunnerTests |
|||
{ |
|||
public sealed class Command : ICommand |
|||
{ |
|||
public List<int> Values { get; set; } = new List<int>(); |
|||
|
|||
public long ExpectedVersion { get; set; } |
|||
} |
|||
|
|||
public sealed class CustomMiddleware : ICustomCommandMiddleware |
|||
{ |
|||
private readonly int value; |
|||
|
|||
public CustomMiddleware(int value) |
|||
{ |
|||
this.value = value; |
|||
} |
|||
|
|||
public Task HandleAsync(CommandContext context, Func<Task> next) |
|||
{ |
|||
if (context.Command is Command command) |
|||
{ |
|||
command.Values.Add(value); |
|||
} |
|||
|
|||
return next(); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_run_extensions_in_right_order() |
|||
{ |
|||
var command = new Command(); |
|||
var context = new CommandContext(command, A.Fake<ICommandBus>()); |
|||
|
|||
var sut = new CustomCommandMiddlewareRunner(new[] |
|||
{ |
|||
new CustomMiddleware(10), |
|||
new CustomMiddleware(12), |
|||
new CustomMiddleware(14) |
|||
}); |
|||
|
|||
var isNextCalled = false; |
|||
|
|||
await sut.HandleAsync(context, () => |
|||
{ |
|||
isNextCalled = true; |
|||
|
|||
Assert.Equal(new[] { 10, 12, 14 }, command.Values); |
|||
|
|||
return Task.CompletedTask; |
|||
}); |
|||
|
|||
Assert.True(isNextCalled); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue