using Elsa.Extensions; using Elsa.Features.Services; using Elsa.Workflows.Core.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Xunit.Abstractions; namespace Elsa.Testing.Shared; /// /// A builder that can be used to configure an for testing purposes. /// public class TestApplicationBuilder { private readonly ITestOutputHelper _testOutputHelper; private readonly ServiceCollection _services; private Action _configureElsa; /// /// Initializes a new instance of the class. /// /// The test output helper. public TestApplicationBuilder(ITestOutputHelper testOutputHelper) { _testOutputHelper = testOutputHelper; _services = new ServiceCollection(); _services .AddSingleton(testOutputHelper) .AddSingleton() .AddLogging(logging => logging.AddProvider(new XunitLoggerProvider(testOutputHelper)).SetMinimumLevel(LogLevel.Debug)); _configureElsa += elsa => elsa .UseWorkflows(workflows => workflows .WithStandardOutStreamProvider(_ => new StandardOutStreamProvider(new XunitConsoleTextWriter(_testOutputHelper))) ); } /// /// Builds the . /// /// The . public IServiceProvider Build() { _services.AddElsa(_configureElsa); return _services.BuildServiceProvider(); } /// /// Configures Elsa. /// /// The configuration delegate. /// The . public TestApplicationBuilder ConfigureElsa(Action configure) { _configureElsa += configure; return this; } /// /// Configures the service provider. /// /// The configuration delegate. /// The . public TestApplicationBuilder ConfigureServices(Action configure) { configure(_services); return this; } /// /// Configures the service provider to use a to capture standard output. /// /// The capturing text writer. /// The . public TestApplicationBuilder WithCapturingTextWriter(CapturingTextWriter capturingTextWriter) { var combinedTextWriter = new CombinedTextWriter(capturingTextWriter, new XunitConsoleTextWriter(_testOutputHelper)); var provider = new StandardOutStreamProvider(combinedTextWriter); ConfigureElsa(elsa => elsa.UseWorkflows(workflows => workflows.WithStandardOutStreamProvider(_ => provider))); return this; } }