V10: fix build warnings in test projects (#12509)
* Run code cleanup * Dotnet format benchmarks project * Fix up Test.Common * Run dotnet format + manual cleanup * Run code cleanup for unit tests * Run dotnet format * Fix up errors * Manual cleanup of Unit test project * Update tests/Umbraco.Tests.Benchmarks/HexStringBenchmarks.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Integration/Testing/TestDbMeta.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Benchmarks/TypeFinderBenchmarks.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Integration/Umbraco.Core/Events/EventAggregatorTests.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Fix according to review * Fix after merge * Fix errors Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk> Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> Co-authored-by: Zeegaan <nge@umbraco.dk>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
@@ -7,13 +6,10 @@ using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.Cache;
|
||||
using Umbraco.Cms.Core.Composing;
|
||||
using Umbraco.Cms.Core.Configuration.Models;
|
||||
using Umbraco.Cms.Core.DependencyInjection;
|
||||
using Umbraco.Cms.Core.IO;
|
||||
using Umbraco.Cms.Core.Scoping;
|
||||
using Umbraco.Cms.Core.Services;
|
||||
using Umbraco.Cms.Core.Strings;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
@@ -27,175 +23,174 @@ using Umbraco.Cms.Tests.Integration.DependencyInjection;
|
||||
using Umbraco.Cms.Tests.Integration.Extensions;
|
||||
using Umbraco.Cms.Web.Common.Hosting;
|
||||
using Umbraco.Extensions;
|
||||
using IScopeProvider = Umbraco.Cms.Infrastructure.Scoping.IScopeProvider;
|
||||
using Constants = Umbraco.Cms.Core.Constants;
|
||||
|
||||
namespace Umbraco.Cms.Tests.Integration.Testing
|
||||
namespace Umbraco.Cms.Tests.Integration.Testing;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract class for integration tests
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This will use a Host Builder to boot and install Umbraco ready for use
|
||||
/// </remarks>
|
||||
public abstract class UmbracoIntegrationTest : UmbracoIntegrationTestBase
|
||||
{
|
||||
private IHost _host;
|
||||
|
||||
protected IServiceProvider Services => _host.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract class for integration tests
|
||||
/// Gets the <see cref="IScopeProvider" />
|
||||
/// </summary>
|
||||
protected IScopeProvider ScopeProvider => Services.GetRequiredService<IScopeProvider>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IScopeAccessor" />
|
||||
/// </summary>
|
||||
protected IScopeAccessor ScopeAccessor => Services.GetRequiredService<IScopeAccessor>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ILoggerFactory" />
|
||||
/// </summary>
|
||||
protected ILoggerFactory LoggerFactory => Services.GetRequiredService<ILoggerFactory>();
|
||||
|
||||
protected AppCaches AppCaches => Services.GetRequiredService<AppCaches>();
|
||||
|
||||
protected IIOHelper IOHelper => Services.GetRequiredService<IIOHelper>();
|
||||
|
||||
protected IShortStringHelper ShortStringHelper => Services.GetRequiredService<IShortStringHelper>();
|
||||
|
||||
protected GlobalSettings GlobalSettings => Services.GetRequiredService<IOptions<GlobalSettings>>().Value;
|
||||
|
||||
protected IMapperCollection Mappers => Services.GetRequiredService<IMapperCollection>();
|
||||
|
||||
protected UserBuilder UserBuilderInstance { get; } = new();
|
||||
|
||||
protected UserGroupBuilder UserGroupBuilderInstance { get; } = new();
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
InMemoryConfiguration[Constants.Configuration.ConfigUnattended + ":" + nameof(UnattendedSettings.InstallUnattended)] = "true";
|
||||
var hostBuilder = CreateHostBuilder();
|
||||
|
||||
_host = hostBuilder.Build();
|
||||
UseTestDatabase(_host.Services);
|
||||
_host.Start();
|
||||
|
||||
if (TestOptions.Boot)
|
||||
{
|
||||
Services.GetRequiredService<IUmbracoContextFactory>().EnsureUmbracoContext();
|
||||
}
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDownAsync() => _host.StopAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Create the Generic Host and execute startup ConfigureServices/Configure calls
|
||||
/// </summary>
|
||||
private IHostBuilder CreateHostBuilder()
|
||||
{
|
||||
var hostBuilder = Host.CreateDefaultBuilder()
|
||||
.ConfigureUmbracoDefaults()
|
||||
|
||||
// IMPORTANT: We Cannot use UseStartup, there's all sorts of threads about this with testing. Although this can work
|
||||
// if you want to setup your tests this way, it is a bit annoying to do that as the WebApplicationFactory will
|
||||
// create separate Host instances. So instead of UseStartup, we just call ConfigureServices/Configure ourselves,
|
||||
// and in the case of the UmbracoTestServerTestBase it will use the ConfigureWebHost to Configure the IApplicationBuilder directly.
|
||||
.ConfigureAppConfiguration((context, configBuilder) =>
|
||||
{
|
||||
context.HostingEnvironment = TestHelper.GetWebHostEnvironment();
|
||||
configBuilder.Sources.Clear();
|
||||
configBuilder.AddInMemoryCollection(InMemoryConfiguration);
|
||||
configBuilder.AddConfiguration(GlobalSetupTeardown.TestConfiguration);
|
||||
|
||||
Configuration = configBuilder.Build();
|
||||
})
|
||||
.ConfigureServices((_, services) =>
|
||||
{
|
||||
ConfigureServices(services);
|
||||
ConfigureTestServices(services);
|
||||
services.AddUnique(CreateLoggerFactory());
|
||||
|
||||
if (!TestOptions.Boot)
|
||||
{
|
||||
// If boot is false, we don't want the CoreRuntime hosted service to start
|
||||
// So we replace it with a Mock
|
||||
services.AddUnique(Mock.Of<IRuntime>());
|
||||
}
|
||||
});
|
||||
|
||||
return hostBuilder;
|
||||
}
|
||||
|
||||
protected void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<TestUmbracoDatabaseFactoryProvider>();
|
||||
var webHostEnvironment = TestHelper.GetWebHostEnvironment();
|
||||
services.AddRequiredNetCoreServices(TestHelper, webHostEnvironment);
|
||||
|
||||
// We register this service because we need it for IRuntimeState, if we don't this breaks 900 tests
|
||||
services.AddSingleton<IConflictingRouteService, TestConflictingRouteService>();
|
||||
|
||||
services.AddLogger(webHostEnvironment, Configuration);
|
||||
|
||||
// Add it!
|
||||
var hostingEnvironment = TestHelper.GetHostingEnvironment();
|
||||
var typeLoader = services.AddTypeLoader(
|
||||
GetType().Assembly,
|
||||
hostingEnvironment,
|
||||
TestHelper.ConsoleLoggerFactory,
|
||||
AppCaches.NoCache,
|
||||
Configuration,
|
||||
TestHelper.Profiler);
|
||||
var builder = new UmbracoBuilder(services, Configuration, typeLoader, TestHelper.ConsoleLoggerFactory, TestHelper.Profiler, AppCaches.NoCache, hostingEnvironment);
|
||||
|
||||
builder.AddConfiguration()
|
||||
.AddUmbracoCore()
|
||||
.AddWebComponents()
|
||||
.AddRuntimeMinifier()
|
||||
.AddBackOfficeAuthentication()
|
||||
.AddBackOfficeIdentity()
|
||||
.AddMembersIdentity()
|
||||
.AddExamine()
|
||||
.AddUmbracoSqlServerSupport()
|
||||
.AddUmbracoSqliteSupport()
|
||||
.AddTestServices(TestHelper);
|
||||
|
||||
if (TestOptions.Mapper)
|
||||
{
|
||||
// TODO: Should these just be called from within AddUmbracoCore/AddWebComponents?
|
||||
builder
|
||||
.AddCoreMappingProfiles()
|
||||
.AddWebMappingProfiles();
|
||||
}
|
||||
|
||||
services.AddSignalR();
|
||||
services.AddMvc();
|
||||
|
||||
CustomTestSetup(builder);
|
||||
|
||||
builder.Build();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hook for altering UmbracoBuilder setup
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This will use a Host Builder to boot and install Umbraco ready for use
|
||||
/// Can also be used for registering test doubles.
|
||||
/// </remarks>
|
||||
public abstract class UmbracoIntegrationTest : UmbracoIntegrationTestBase
|
||||
protected virtual void CustomTestSetup(IUmbracoBuilder builder)
|
||||
{
|
||||
private IHost _host;
|
||||
|
||||
protected IServiceProvider Services => _host.Services;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
InMemoryConfiguration[Core.Constants.Configuration.ConfigUnattended + ":" + nameof(UnattendedSettings.InstallUnattended)] = "true";
|
||||
IHostBuilder hostBuilder = CreateHostBuilder();
|
||||
|
||||
_host = hostBuilder.Build();
|
||||
UseTestDatabase(_host.Services);
|
||||
_host.Start();
|
||||
|
||||
if (TestOptions.Boot)
|
||||
{
|
||||
Services.GetRequiredService<IUmbracoContextFactory>().EnsureUmbracoContext();
|
||||
}
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDownAsync() => _host.StopAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Create the Generic Host and execute startup ConfigureServices/Configure calls
|
||||
/// </summary>
|
||||
private IHostBuilder CreateHostBuilder()
|
||||
{
|
||||
IHostBuilder hostBuilder = Host.CreateDefaultBuilder()
|
||||
.ConfigureUmbracoDefaults()
|
||||
|
||||
// IMPORTANT: We Cannot use UseStartup, there's all sorts of threads about this with testing. Although this can work
|
||||
// if you want to setup your tests this way, it is a bit annoying to do that as the WebApplicationFactory will
|
||||
// create separate Host instances. So instead of UseStartup, we just call ConfigureServices/Configure ourselves,
|
||||
// and in the case of the UmbracoTestServerTestBase it will use the ConfigureWebHost to Configure the IApplicationBuilder directly.
|
||||
.ConfigureAppConfiguration((context, configBuilder) =>
|
||||
{
|
||||
context.HostingEnvironment = TestHelper.GetWebHostEnvironment();
|
||||
configBuilder.Sources.Clear();
|
||||
configBuilder.AddInMemoryCollection(InMemoryConfiguration);
|
||||
configBuilder.AddConfiguration(GlobalSetupTeardown.TestConfiguration);
|
||||
|
||||
Configuration = configBuilder.Build();
|
||||
})
|
||||
.ConfigureServices((_, services) =>
|
||||
{
|
||||
ConfigureServices(services);
|
||||
ConfigureTestServices(services);
|
||||
services.AddUnique(CreateLoggerFactory());
|
||||
|
||||
if (!TestOptions.Boot)
|
||||
{
|
||||
// If boot is false, we don't want the CoreRuntime hosted service to start
|
||||
// So we replace it with a Mock
|
||||
services.AddUnique(Mock.Of<IRuntime>());
|
||||
}
|
||||
});
|
||||
|
||||
return hostBuilder;
|
||||
}
|
||||
|
||||
protected void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<TestUmbracoDatabaseFactoryProvider>();
|
||||
IWebHostEnvironment webHostEnvironment = TestHelper.GetWebHostEnvironment();
|
||||
services.AddRequiredNetCoreServices(TestHelper, webHostEnvironment);
|
||||
|
||||
// We register this service because we need it for IRuntimeState, if we don't this breaks 900 tests
|
||||
services.AddSingleton<IConflictingRouteService, TestConflictingRouteService>();
|
||||
|
||||
services.AddLogger(webHostEnvironment, Configuration);
|
||||
|
||||
// Add it!
|
||||
Core.Hosting.IHostingEnvironment hostingEnvironment = TestHelper.GetHostingEnvironment();
|
||||
TypeLoader typeLoader = services.AddTypeLoader(
|
||||
GetType().Assembly,
|
||||
hostingEnvironment,
|
||||
TestHelper.ConsoleLoggerFactory,
|
||||
AppCaches.NoCache,
|
||||
Configuration,
|
||||
TestHelper.Profiler);
|
||||
var builder = new UmbracoBuilder(services, Configuration, typeLoader, TestHelper.ConsoleLoggerFactory, TestHelper.Profiler, AppCaches.NoCache, hostingEnvironment);
|
||||
|
||||
builder.AddConfiguration()
|
||||
.AddUmbracoCore()
|
||||
.AddWebComponents()
|
||||
.AddRuntimeMinifier()
|
||||
.AddBackOfficeAuthentication()
|
||||
.AddBackOfficeIdentity()
|
||||
.AddMembersIdentity()
|
||||
.AddExamine()
|
||||
.AddUmbracoSqlServerSupport()
|
||||
.AddUmbracoSqliteSupport()
|
||||
.AddTestServices(TestHelper);
|
||||
|
||||
if (TestOptions.Mapper)
|
||||
{
|
||||
// TODO: Should these just be called from within AddUmbracoCore/AddWebComponents?
|
||||
builder
|
||||
.AddCoreMappingProfiles()
|
||||
.AddWebMappingProfiles();
|
||||
}
|
||||
|
||||
services.AddSignalR();
|
||||
services.AddMvc();
|
||||
|
||||
CustomTestSetup(builder);
|
||||
|
||||
builder.Build();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hook for altering UmbracoBuilder setup
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Can also be used for registering test doubles.
|
||||
/// </remarks>
|
||||
protected virtual void CustomTestSetup(IUmbracoBuilder builder)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hook for registering test doubles.
|
||||
/// </summary>
|
||||
protected virtual void ConfigureTestServices(IServiceCollection services)
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual T GetRequiredService<T>() => Services.GetRequiredService<T>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IScopeProvider"/>
|
||||
/// </summary>
|
||||
protected IScopeProvider ScopeProvider => Services.GetRequiredService<IScopeProvider>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IScopeAccessor"/>
|
||||
/// </summary>
|
||||
protected IScopeAccessor ScopeAccessor => Services.GetRequiredService<IScopeAccessor>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ILoggerFactory"/>
|
||||
/// </summary>
|
||||
protected ILoggerFactory LoggerFactory => Services.GetRequiredService<ILoggerFactory>();
|
||||
|
||||
protected AppCaches AppCaches => Services.GetRequiredService<AppCaches>();
|
||||
|
||||
protected IIOHelper IOHelper => Services.GetRequiredService<IIOHelper>();
|
||||
|
||||
protected IShortStringHelper ShortStringHelper => Services.GetRequiredService<IShortStringHelper>();
|
||||
|
||||
protected GlobalSettings GlobalSettings => Services.GetRequiredService<IOptions<GlobalSettings>>().Value;
|
||||
|
||||
protected IMapperCollection Mappers => Services.GetRequiredService<IMapperCollection>();
|
||||
|
||||
protected UserBuilder UserBuilderInstance { get; } = new ();
|
||||
|
||||
protected UserGroupBuilder UserGroupBuilderInstance { get; } = new ();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hook for registering test doubles.
|
||||
/// </summary>
|
||||
protected virtual void ConfigureTestServices(IServiceCollection services)
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual T GetRequiredService<T>() => Services.GetRequiredService<T>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user