2020-04-22 19:46:41 -04:00
|
|
|
|
using ApplicationTests.Fixtures;
|
2020-04-25 20:01:26 -04:00
|
|
|
|
using ApplicationTests.Mocks;
|
2020-04-22 19:46:41 -04:00
|
|
|
|
using FakeItEasy;
|
|
|
|
|
using IW4MAdmin;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-05-16 12:54:01 -04:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
2020-04-25 20:01:26 -04:00
|
|
|
|
using SharedLibraryCore.Database;
|
2020-04-22 19:46:41 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using SharedLibraryCore.Services;
|
2020-05-16 12:54:01 -04:00
|
|
|
|
using System;
|
2020-04-22 19:46:41 -04:00
|
|
|
|
|
|
|
|
|
namespace ApplicationTests
|
|
|
|
|
{
|
2020-05-04 17:50:02 -04:00
|
|
|
|
static class DependencyInjectionExtensions
|
2020-04-22 19:46:41 -04:00
|
|
|
|
{
|
2020-05-04 17:50:02 -04:00
|
|
|
|
public static IServiceCollection BuildBase(this IServiceCollection serviceCollection, IEventHandler eventHandler = null)
|
2020-04-22 19:46:41 -04:00
|
|
|
|
{
|
2020-05-04 17:50:02 -04:00
|
|
|
|
|
|
|
|
|
if (eventHandler == null)
|
|
|
|
|
{
|
2020-05-16 12:54:01 -04:00
|
|
|
|
eventHandler = new EventHandlerMock();
|
|
|
|
|
serviceCollection.AddSingleton(eventHandler as EventHandlerMock);
|
2020-05-04 17:50:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-16 12:54:01 -04:00
|
|
|
|
else if (eventHandler is EventHandlerMock mockEventHandler)
|
2020-05-04 17:50:02 -04:00
|
|
|
|
{
|
|
|
|
|
serviceCollection.AddSingleton(mockEventHandler);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 19:46:41 -04:00
|
|
|
|
var manager = A.Fake<IManager>();
|
|
|
|
|
var logger = A.Fake<ILogger>();
|
2020-04-25 20:01:26 -04:00
|
|
|
|
|
2020-04-22 19:46:41 -04:00
|
|
|
|
A.CallTo(() => manager.GetLogger(A<long>.Ignored))
|
|
|
|
|
.Returns(logger);
|
|
|
|
|
|
|
|
|
|
serviceCollection.AddSingleton(logger)
|
|
|
|
|
.AddSingleton(manager)
|
2020-04-25 20:01:26 -04:00
|
|
|
|
.AddSingleton<IDatabaseContextFactory, DatabaseContextFactoryMock>()
|
2020-04-22 19:46:41 -04:00
|
|
|
|
.AddSingleton(A.Fake<IRConConnectionFactory>())
|
|
|
|
|
.AddSingleton(A.Fake<IRConConnection>())
|
|
|
|
|
.AddSingleton(A.Fake<ITranslationLookup>())
|
|
|
|
|
.AddSingleton(A.Fake<IRConParser>())
|
|
|
|
|
.AddSingleton(A.Fake<IParserRegexFactory>())
|
2020-05-04 17:50:02 -04:00
|
|
|
|
.AddSingleton<DataFileLoader>()
|
|
|
|
|
.AddSingleton(A.Fake<IGameLogReaderFactory>())
|
2020-08-17 22:21:11 -04:00
|
|
|
|
.AddSingleton(A.Fake<IMetaService>())
|
2020-05-16 12:54:01 -04:00
|
|
|
|
.AddSingleton(eventHandler)
|
|
|
|
|
.AddSingleton(ConfigurationGenerators.CreateApplicationConfiguration())
|
|
|
|
|
.AddSingleton(ConfigurationGenerators.CreateCommandConfiguration())
|
|
|
|
|
.AddSingleton<IConfigurationHandler<ApplicationConfiguration>, ApplicationConfigurationHandlerMock>();
|
2020-04-22 19:46:41 -04:00
|
|
|
|
|
|
|
|
|
serviceCollection.AddSingleton(_sp => new IW4MServer(_sp.GetRequiredService<IManager>(), ConfigurationGenerators.CreateServerConfiguration(),
|
2020-08-17 22:21:11 -04:00
|
|
|
|
_sp.GetRequiredService<ITranslationLookup>(), _sp.GetRequiredService<IRConConnectionFactory>(), _sp.GetRequiredService<IGameLogReaderFactory>(), _sp.GetRequiredService<IMetaService>())
|
2020-04-22 19:46:41 -04:00
|
|
|
|
{
|
|
|
|
|
RconParser = _sp.GetRequiredService<IRConParser>()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return serviceCollection;
|
|
|
|
|
}
|
2020-05-16 12:54:01 -04:00
|
|
|
|
|
|
|
|
|
public static IServiceProvider SetupTestHooks(this IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
var mgr = serviceProvider.GetRequiredService<IManager>();
|
|
|
|
|
A.CallTo(() => mgr.GetApplicationSettings())
|
|
|
|
|
.Returns(serviceProvider.GetRequiredService<IConfigurationHandler<ApplicationConfiguration>>());
|
|
|
|
|
|
|
|
|
|
return serviceProvider;
|
|
|
|
|
}
|
2020-04-22 19:46:41 -04:00
|
|
|
|
}
|
|
|
|
|
}
|