102 lines
3.1 KiB
C#
102 lines
3.1 KiB
C#
using EnvelopeGenerator.Application;
|
|
using EnvelopeGenerator.Application.Receivers.Commands;
|
|
using EnvelopeGenerator.Infrastructure;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
namespace EnvelopeGenerator.Tests.Application;
|
|
|
|
public class Fake
|
|
{
|
|
public static Host CreateHost() => Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
|
|
.ConfigureAppConfiguration((context, config) =>
|
|
{
|
|
// add appsettings.json
|
|
config.SetBasePath(Directory.GetCurrentDirectory());
|
|
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
|
|
})
|
|
.ConfigureServices((context, services) =>
|
|
{
|
|
IConfiguration configuration = context.Configuration;
|
|
|
|
// add Application and Infrastructure services
|
|
#pragma warning disable CS0618
|
|
services.AddEnvelopeGeneratorServices(configuration);
|
|
services.AddEnvelopeGeneratorInfrastructureServices(
|
|
(sp, options) => options.UseInMemoryDatabase("EnvelopeGeneratorTestDb"),
|
|
context.Configuration
|
|
);
|
|
#pragma warning restore CS0618
|
|
})
|
|
.Build()
|
|
.ToFake();
|
|
|
|
public class Host : IHost
|
|
{
|
|
#region Root
|
|
private readonly IHost _root;
|
|
|
|
public Host(IHost root)
|
|
{
|
|
_root = root;
|
|
}
|
|
|
|
public IServiceProvider Services => _root.Services;
|
|
|
|
public void Dispose()
|
|
{
|
|
_root.Dispose();
|
|
}
|
|
|
|
public Task StartAsync(CancellationToken cancel = default)
|
|
{
|
|
return _root.StartAsync(cancel);
|
|
}
|
|
|
|
public Task StopAsync(CancellationToken cancel = default)
|
|
{
|
|
return _root.StopAsync(cancel);
|
|
}
|
|
#endregion
|
|
|
|
public IMediator Mediator => Services.GetRequiredService<IMediator>();
|
|
|
|
#region Sample Receivers
|
|
public List<(int Id, string EmailAddress)> _sampleReceivers = new();
|
|
|
|
public IEnumerable<(int Id, string EmailAddress)> SampleReceivers => _sampleReceivers;
|
|
|
|
public async Task AddSampleReceivers()
|
|
{
|
|
var mediator = Mediator;
|
|
var emails = new[]
|
|
{
|
|
"max.mueller@email.de",
|
|
"anna.schmidt@email.de",
|
|
"lukas.schneider@email.de",
|
|
"sophia.fischer@email.de",
|
|
"jonas.weber@email.de",
|
|
"lea.hoffmann@email.de",
|
|
"felix.wagner@email.de",
|
|
"mia.becker@email.de",
|
|
"paul.schulz@email.de",
|
|
"lena.koch@email.de"
|
|
};
|
|
foreach (var email in emails)
|
|
{
|
|
var cmd = new CreateReceiverCommand { EmailAddress = email };
|
|
var (Id, _) = await mediator.Send(cmd);
|
|
_sampleReceivers.Add((Id, email));
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|
|
public static class Extensions
|
|
{
|
|
public static Fake.Host ToFake(this IHost host) => new(host);
|
|
} |