Additional refactoring I have been meaning to get around to for a while.
This commit is contained in:
@@ -39,9 +39,9 @@ namespace Umbraco.Cms.Tests.Integration.DependencyInjection
|
||||
/// <summary>
|
||||
/// Uses/Replaces services with testing services
|
||||
/// </summary>
|
||||
public static IUmbracoBuilder AddTestServices(this IUmbracoBuilder builder, TestHelper testHelper, AppCaches appCaches = null)
|
||||
public static IUmbracoBuilder AddTestServices(this IUmbracoBuilder builder, TestHelper testHelper)
|
||||
{
|
||||
builder.Services.AddUnique(appCaches ?? AppCaches.NoCache);
|
||||
builder.Services.AddUnique(AppCaches.NoCache);
|
||||
builder.Services.AddUnique(Mock.Of<IUmbracoBootPermissionChecker>());
|
||||
builder.Services.AddUnique(testHelper.MainDom);
|
||||
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
// Copyright (c) Umbraco.
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Net.Http;
|
||||
@@ -17,7 +14,6 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.Cache;
|
||||
using Umbraco.Cms.Core.Composing;
|
||||
using Umbraco.Cms.Core.DependencyInjection;
|
||||
@@ -35,14 +31,17 @@ namespace Umbraco.Cms.Tests.Integration.TestServerTest
|
||||
{
|
||||
[TestFixture]
|
||||
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, Logger = UmbracoTestOptions.Logger.Console, Boot = true)]
|
||||
public abstract class UmbracoTestServerTestBase : UmbracoIntegrationTest
|
||||
public abstract class UmbracoTestServerTestBase : UmbracoIntegrationTestBase
|
||||
{
|
||||
[SetUp]
|
||||
public override void Setup()
|
||||
{
|
||||
InMemoryConfiguration["ConnectionStrings:" + Constants.System.UmbracoConnectionName] = null;
|
||||
InMemoryConfiguration["Umbraco:CMS:Hosting:Debug"] = "true";
|
||||
protected HttpClient Client { get; private set; }
|
||||
|
||||
protected LinkGenerator LinkGenerator { get; private set; }
|
||||
|
||||
protected WebApplicationFactory<UmbracoTestServerTestBase> Factory { get; private set; }
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
/*
|
||||
* It's worth noting that our usage of WebApplicationFactory is non-standard,
|
||||
* the intent is that your Startup.ConfigureServices is called just like
|
||||
@@ -53,9 +52,12 @@ namespace Umbraco.Cms.Tests.Integration.TestServerTest
|
||||
* This is currently a pain to refactor towards due to UmbracoBuilder+TypeFinder+TypeLoader setup but
|
||||
* we should get there one day.
|
||||
*
|
||||
* However we need to separate the testing framework we provide for downstream projects from our own tests.
|
||||
* We cannot use the Umbraco.Web.UI startup yet as that is not available downstream.
|
||||
*
|
||||
* See https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests
|
||||
*/
|
||||
var factory = new UmbracoWebApplicationFactory<UmbracoTestServerTestBase>(CreateHostBuilder, BeforeHostStart);
|
||||
var factory = new UmbracoWebApplicationFactory<UmbracoTestServerTestBase>(CreateHostBuilder);
|
||||
|
||||
// additional host configuration for web server integration tests
|
||||
Factory = factory.WithWebHostBuilder(builder =>
|
||||
@@ -71,7 +73,6 @@ namespace Umbraco.Cms.Tests.Integration.TestServerTest
|
||||
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(TestAuthHandler.TestAuthenticationScheme, options => { }));
|
||||
});
|
||||
|
||||
|
||||
Client = Factory.CreateClient(new WebApplicationFactoryClientOptions
|
||||
{
|
||||
AllowAutoRedirect = false
|
||||
@@ -80,57 +81,6 @@ namespace Umbraco.Cms.Tests.Integration.TestServerTest
|
||||
LinkGenerator = Factory.Services.GetRequiredService<LinkGenerator>();
|
||||
}
|
||||
|
||||
protected override IHostBuilder CreateHostBuilder()
|
||||
{
|
||||
/* It is important that ConfigureWebHost is called before ConfigureServices, this is consistent with the host setup
|
||||
* found in Program.cs and avoids nasty surprises.
|
||||
*
|
||||
* e.g. the registration for RefreshingRazorViewEngine requires that IWebHostEnvironment is registered
|
||||
* at the point in time that the service collection is snapshotted.
|
||||
*/
|
||||
IHostBuilder hostBuilder = Host.CreateDefaultBuilder()
|
||||
.ConfigureAppConfiguration((context, configBuilder) =>
|
||||
{
|
||||
context.HostingEnvironment = TestHelper.GetWebHostEnvironment();
|
||||
configBuilder.Sources.Clear();
|
||||
configBuilder.AddInMemoryCollection(InMemoryConfiguration);
|
||||
|
||||
Configuration = configBuilder.Build();
|
||||
})
|
||||
.ConfigureWebHost(builder =>
|
||||
{
|
||||
// need to configure the IWebHostEnvironment too
|
||||
builder.ConfigureServices((c, s) => c.HostingEnvironment = TestHelper.GetWebHostEnvironment());
|
||||
|
||||
// call startup
|
||||
builder.Configure(app => Configure(app));
|
||||
})
|
||||
.ConfigureServices((_, services) =>
|
||||
{
|
||||
ConfigureServices(services);
|
||||
ConfigureTestSpecificServices(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>());
|
||||
}
|
||||
})
|
||||
.UseDefaultServiceProvider(cfg =>
|
||||
{
|
||||
// These default to true *if* WebHostEnvironment.EnvironmentName == Development
|
||||
// When running tests, EnvironmentName used to be null on the mock that we register into services.
|
||||
// Enable opt in for tests so that validation occurs regardless of environment name.
|
||||
// Would be nice to have this on for UmbracoIntegrationTest also but requires a lot more effort to resolve issues.
|
||||
cfg.ValidateOnBuild = true;
|
||||
cfg.ValidateScopes = true;
|
||||
});
|
||||
|
||||
return hostBuilder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prepare a url before using <see cref="Client"/>.
|
||||
/// This returns the url but also sets the HttpContext.request into to use this url.
|
||||
@@ -139,7 +89,7 @@ namespace Umbraco.Cms.Tests.Integration.TestServerTest
|
||||
protected string PrepareApiControllerUrl<T>(Expression<Func<T, object>> methodSelector)
|
||||
where T : UmbracoApiController
|
||||
{
|
||||
string url = LinkGenerator.GetUmbracoApiService(methodSelector);
|
||||
var url = LinkGenerator.GetUmbracoApiService(methodSelector);
|
||||
return PrepareUrl(url);
|
||||
}
|
||||
|
||||
@@ -151,7 +101,7 @@ namespace Umbraco.Cms.Tests.Integration.TestServerTest
|
||||
protected string PrepareSurfaceControllerUrl<T>(Expression<Func<T, object>> methodSelector)
|
||||
where T : SurfaceController
|
||||
{
|
||||
string url = LinkGenerator.GetUmbracoSurfaceUrl(methodSelector);
|
||||
var url = LinkGenerator.GetUmbracoSurfaceUrl(methodSelector);
|
||||
return PrepareUrl(url);
|
||||
}
|
||||
|
||||
@@ -181,17 +131,67 @@ namespace Umbraco.Cms.Tests.Integration.TestServerTest
|
||||
return url;
|
||||
}
|
||||
|
||||
protected HttpClient Client { get; private set; }
|
||||
private IHostBuilder CreateHostBuilder()
|
||||
{
|
||||
IHostBuilder hostBuilder = Host.CreateDefaultBuilder()
|
||||
.ConfigureAppConfiguration((context, configBuilder) =>
|
||||
{
|
||||
context.HostingEnvironment = TestHelper.GetWebHostEnvironment();
|
||||
configBuilder.Sources.Clear();
|
||||
configBuilder.AddInMemoryCollection(InMemoryConfiguration);
|
||||
|
||||
protected LinkGenerator LinkGenerator { get; private set; }
|
||||
Configuration = configBuilder.Build();
|
||||
})
|
||||
/* It is important that ConfigureWebHost is called before ConfigureServices, this is consistent with the host setup
|
||||
* found in Program.cs and avoids nasty surprises.
|
||||
*
|
||||
* e.g. the registration for RefreshingRazorViewEngine requires that IWebHostEnvironment is registered
|
||||
* at the point in time that the service collection is snapshotted.
|
||||
*/
|
||||
.ConfigureWebHost(builder =>
|
||||
{
|
||||
// need to configure the IWebHostEnvironment too
|
||||
builder.ConfigureServices((c, s) => c.HostingEnvironment = TestHelper.GetWebHostEnvironment());
|
||||
|
||||
protected WebApplicationFactory<UmbracoTestServerTestBase> Factory { get; private set; }
|
||||
// call startup
|
||||
builder.Configure(Configure);
|
||||
})
|
||||
.ConfigureServices((_, services) =>
|
||||
{
|
||||
ConfigureServices(services);
|
||||
ConfigureTestSpecificServices(services);
|
||||
|
||||
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>());
|
||||
}
|
||||
})
|
||||
.UseDefaultServiceProvider(cfg =>
|
||||
{
|
||||
// These default to true *if* WebHostEnvironment.EnvironmentName == Development
|
||||
// When running tests, EnvironmentName used to be null on the mock that we register into services.
|
||||
// Enable opt in for tests so that validation occurs regardless of environment name.
|
||||
// Would be nice to have this on for UmbracoIntegrationTest also but requires a lot more effort to resolve issues.
|
||||
cfg.ValidateOnBuild = true;
|
||||
cfg.ValidateScopes = true;
|
||||
});
|
||||
|
||||
return hostBuilder;
|
||||
}
|
||||
|
||||
protected virtual IServiceProvider Services => Factory.Services;
|
||||
|
||||
protected virtual T GetRequiredService<T>() => Factory.Services.GetRequiredService<T>();
|
||||
|
||||
private void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddUnique(CreateLoggerFactory());
|
||||
services.AddTransient<TestUmbracoDatabaseFactoryProvider>();
|
||||
|
||||
Core.Hosting.IHostingEnvironment hostingEnvironment = TestHelper.GetHostingEnvironment();
|
||||
|
||||
TypeLoader typeLoader = services.AddTypeLoader(
|
||||
GetType().Assembly,
|
||||
hostingEnvironment,
|
||||
@@ -234,8 +234,10 @@ namespace Umbraco.Cms.Tests.Integration.TestServerTest
|
||||
.Build();
|
||||
}
|
||||
|
||||
public override void Configure(IApplicationBuilder app)
|
||||
private void Configure(IApplicationBuilder app)
|
||||
{
|
||||
UseTestDatabase(app);
|
||||
|
||||
app.UseUmbraco()
|
||||
.WithMiddleware(u =>
|
||||
{
|
||||
|
||||
@@ -18,11 +18,7 @@ namespace Umbraco.Cms.Tests.Integration.TestServerTest
|
||||
/// </summary>
|
||||
/// <param name="createHostBuilder">Method to create the IHostBuilder</param>
|
||||
/// <param name="beforeStart">Method to perform an action before IHost starts</param>
|
||||
public UmbracoWebApplicationFactory(Func<IHostBuilder> createHostBuilder, Action<IHost> beforeStart = null)
|
||||
{
|
||||
_createHostBuilder = createHostBuilder;
|
||||
_beforeStart = beforeStart;
|
||||
}
|
||||
public UmbracoWebApplicationFactory(Func<IHostBuilder> createHostBuilder) => _createHostBuilder = createHostBuilder;
|
||||
|
||||
protected override IHostBuilder CreateHostBuilder() => _createHostBuilder();
|
||||
|
||||
|
||||
@@ -1,23 +1,12 @@
|
||||
// Copyright (c) Umbraco.
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Serilog;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.Cache;
|
||||
using Umbraco.Cms.Core.Composing;
|
||||
@@ -29,14 +18,11 @@ using Umbraco.Cms.Core.Services;
|
||||
using Umbraco.Cms.Core.Strings;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
using Umbraco.Cms.Infrastructure.DependencyInjection;
|
||||
using Umbraco.Cms.Infrastructure.Persistence;
|
||||
using Umbraco.Cms.Infrastructure.Persistence.Mappers;
|
||||
using Umbraco.Cms.Infrastructure.Scoping;
|
||||
using Umbraco.Cms.Tests.Common.Builders;
|
||||
using Umbraco.Cms.Tests.Common.Testing;
|
||||
using Umbraco.Cms.Tests.Integration.DependencyInjection;
|
||||
using Umbraco.Cms.Tests.Integration.Extensions;
|
||||
using Umbraco.Cms.Tests.Integration.Implementations;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Tests.Integration.Testing
|
||||
@@ -47,123 +33,35 @@ namespace Umbraco.Cms.Tests.Integration.Testing
|
||||
/// <remarks>
|
||||
/// This will use a Host Builder to boot and install Umbraco ready for use
|
||||
/// </remarks>
|
||||
[SingleThreaded]
|
||||
[NonParallelizable]
|
||||
public abstract class UmbracoIntegrationTest
|
||||
public abstract class UmbracoIntegrationTest : UmbracoIntegrationTestBase
|
||||
{
|
||||
private List<Action> _testTeardown = null;
|
||||
private readonly List<Action> _fixtureTeardown = new List<Action>();
|
||||
private IHost _host;
|
||||
|
||||
public void OnTestTearDown(Action tearDown)
|
||||
{
|
||||
if (_testTeardown == null)
|
||||
{
|
||||
_testTeardown = new List<Action>();
|
||||
}
|
||||
|
||||
_testTeardown.Add(tearDown);
|
||||
}
|
||||
|
||||
public void OnFixtureTearDown(Action tearDown) => _fixtureTeardown.Add(tearDown);
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void FixtureTearDown()
|
||||
{
|
||||
foreach (Action a in _fixtureTeardown)
|
||||
{
|
||||
a();
|
||||
}
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public async Task TearDownAsync()
|
||||
{
|
||||
if (_testTeardown != null)
|
||||
{
|
||||
foreach (Action a in _testTeardown)
|
||||
{
|
||||
a();
|
||||
}
|
||||
}
|
||||
|
||||
_testTeardown = null;
|
||||
_firstTestInFixture = false;
|
||||
s_firstTestInSession = false;
|
||||
|
||||
// Ensure CoreRuntime stopped (now it's a HostedService)
|
||||
IHost host = Services?.GetService<IHost>();
|
||||
if (host is not null)
|
||||
{
|
||||
await host.StopAsync();
|
||||
host.Dispose();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public virtual void TearDown_Logging() =>
|
||||
TestContext.Progress.Write($" {TestContext.CurrentContext.Result.Outcome.Status}");
|
||||
protected IServiceProvider Services => _host.Services;
|
||||
|
||||
[SetUp]
|
||||
public virtual void SetUp_Logging() =>
|
||||
TestContext.Progress.Write($"Start test {s_testCount++}: {TestContext.CurrentContext.Test.Name}");
|
||||
|
||||
[SetUp]
|
||||
public virtual void Setup()
|
||||
public void Setup()
|
||||
{
|
||||
InMemoryConfiguration[Constants.Configuration.ConfigUnattended + ":" + nameof(UnattendedSettings.InstallUnattended)] = "true";
|
||||
IHostBuilder hostBuilder = CreateHostBuilder();
|
||||
|
||||
IHost host = hostBuilder.Build();
|
||||
BeforeHostStart(host);
|
||||
host.Start();
|
||||
_host = hostBuilder.Build();
|
||||
UseTestDatabase(_host.Services);
|
||||
_host.Start();
|
||||
|
||||
var app = new ApplicationBuilder(host.Services);
|
||||
Configure(app);
|
||||
}
|
||||
|
||||
protected virtual void BeforeHostStart(IHost host)
|
||||
{
|
||||
Services = host.Services;
|
||||
UseTestDatabase(Services);
|
||||
}
|
||||
|
||||
protected ILoggerFactory CreateLoggerFactory()
|
||||
{
|
||||
try
|
||||
if (TestOptions.Boot)
|
||||
{
|
||||
switch (TestOptions.Logger)
|
||||
{
|
||||
case UmbracoTestOptions.Logger.Mock:
|
||||
return NullLoggerFactory.Instance;
|
||||
case UmbracoTestOptions.Logger.Serilog:
|
||||
return Microsoft.Extensions.Logging.LoggerFactory.Create(builder =>
|
||||
{
|
||||
string path = Path.Combine(TestHelper.WorkingDirectory, "logs", "umbraco_integration_tests_.txt");
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.WriteTo.File(path, rollingInterval: RollingInterval.Day)
|
||||
.MinimumLevel.Debug()
|
||||
.CreateLogger();
|
||||
|
||||
builder.AddSerilog(Log.Logger);
|
||||
});
|
||||
case UmbracoTestOptions.Logger.Console:
|
||||
return Microsoft.Extensions.Logging.LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Debug));
|
||||
}
|
||||
Services.GetRequiredService<IUmbracoContextFactory>().EnsureUmbracoContext();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
|
||||
return NullLoggerFactory.Instance;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDownAsync() => _host.StopAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Create the Generic Host and execute startup ConfigureServices/Configure calls
|
||||
/// </summary>
|
||||
protected virtual IHostBuilder CreateHostBuilder()
|
||||
private IHostBuilder CreateHostBuilder()
|
||||
{
|
||||
IHostBuilder hostBuilder = Host.CreateDefaultBuilder()
|
||||
|
||||
@@ -171,7 +69,6 @@ namespace Umbraco.Cms.Tests.Integration.Testing
|
||||
// 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.
|
||||
// .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(GetType()); })
|
||||
.ConfigureAppConfiguration((context, configBuilder) =>
|
||||
{
|
||||
context.HostingEnvironment = TestHelper.GetWebHostEnvironment();
|
||||
@@ -184,7 +81,6 @@ namespace Umbraco.Cms.Tests.Integration.Testing
|
||||
{
|
||||
ConfigureServices(services);
|
||||
ConfigureTestSpecificServices(services);
|
||||
services.AddUnique(CreateLoggerFactory());
|
||||
|
||||
if (!TestOptions.Boot)
|
||||
{
|
||||
@@ -197,12 +93,9 @@ namespace Umbraco.Cms.Tests.Integration.Testing
|
||||
return hostBuilder;
|
||||
}
|
||||
|
||||
protected virtual void ConfigureTestSpecificServices(IServiceCollection services)
|
||||
{
|
||||
}
|
||||
|
||||
private void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddUnique(CreateLoggerFactory());
|
||||
services.AddSingleton(TestHelper.DbProviderFactoryCreator);
|
||||
services.AddTransient<TestUmbracoDatabaseFactoryProvider>();
|
||||
IWebHostEnvironment webHostEnvironment = TestHelper.GetWebHostEnvironment();
|
||||
@@ -232,7 +125,7 @@ namespace Umbraco.Cms.Tests.Integration.Testing
|
||||
.AddBackOfficeIdentity()
|
||||
.AddMembersIdentity()
|
||||
.AddExamine()
|
||||
.AddTestServices(TestHelper, GetAppCaches());
|
||||
.AddTestServices(TestHelper);
|
||||
|
||||
if (TestOptions.Mapper)
|
||||
{
|
||||
@@ -250,182 +143,12 @@ namespace Umbraco.Cms.Tests.Integration.Testing
|
||||
builder.Build();
|
||||
}
|
||||
|
||||
protected virtual AppCaches GetAppCaches() =>
|
||||
|
||||
// Disable caches for integration tests
|
||||
AppCaches.NoCache;
|
||||
|
||||
public virtual void Configure(IApplicationBuilder app)
|
||||
protected virtual void CustomTestSetup(IUmbracoBuilder builder)
|
||||
{
|
||||
if (TestOptions.Boot)
|
||||
{
|
||||
Services.GetRequiredService<IUmbracoContextFactory>().EnsureUmbracoContext();
|
||||
}
|
||||
|
||||
app.UseUmbracoCore(); // This no longer starts CoreRuntime, it's very fast
|
||||
}
|
||||
|
||||
private static readonly object s_dbLocker = new object();
|
||||
private static ITestDatabase s_dbInstance;
|
||||
private static TestDbMeta s_fixtureDbMeta;
|
||||
|
||||
protected void UseTestDatabase(IServiceProvider serviceProvider)
|
||||
{
|
||||
IRuntimeState state = serviceProvider.GetRequiredService<IRuntimeState>();
|
||||
TestUmbracoDatabaseFactoryProvider testDatabaseFactoryProvider = serviceProvider.GetRequiredService<TestUmbracoDatabaseFactoryProvider>();
|
||||
IUmbracoDatabaseFactory databaseFactory = serviceProvider.GetRequiredService<IUmbracoDatabaseFactory>();
|
||||
ILoggerFactory loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
|
||||
|
||||
// This will create a db, install the schema and ensure the app is configured to run
|
||||
SetupTestDatabase(testDatabaseFactoryProvider, databaseFactory, loggerFactory, state, TestHelper.WorkingDirectory);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get or create an instance of <see cref="ITestDatabase"/>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// There must only be ONE instance shared between all tests in a session
|
||||
/// </remarks>
|
||||
private static ITestDatabase GetOrCreateDatabase(string filesPath, ILoggerFactory loggerFactory, TestUmbracoDatabaseFactoryProvider dbFactory)
|
||||
{
|
||||
lock (s_dbLocker)
|
||||
{
|
||||
if (s_dbInstance != null)
|
||||
{
|
||||
return s_dbInstance;
|
||||
}
|
||||
|
||||
// TODO: pull from IConfiguration
|
||||
var settings = new TestDatabaseSettings
|
||||
{
|
||||
PrepareThreadCount = 4,
|
||||
EmptyDatabasesCount = 2,
|
||||
SchemaDatabaseCount = 4
|
||||
};
|
||||
|
||||
s_dbInstance = TestDatabaseFactory.Create(settings, filesPath, loggerFactory, dbFactory);
|
||||
|
||||
return s_dbInstance;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a LocalDb instance to use for the test
|
||||
/// </summary>
|
||||
private void SetupTestDatabase(
|
||||
TestUmbracoDatabaseFactoryProvider testUmbracoDatabaseFactoryProvider,
|
||||
IUmbracoDatabaseFactory databaseFactory,
|
||||
ILoggerFactory loggerFactory,
|
||||
IRuntimeState runtimeState,
|
||||
string workingDirectory)
|
||||
{
|
||||
if (TestOptions.Database == UmbracoTestOptions.Database.None)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// need to manually register this factory
|
||||
DbProviderFactories.RegisterFactory(Constants.DbProviderNames.SqlServer, SqlClientFactory.Instance);
|
||||
|
||||
string dbFilePath = Path.Combine(workingDirectory, "LocalDb");
|
||||
|
||||
ITestDatabase db = GetOrCreateDatabase(dbFilePath, loggerFactory, testUmbracoDatabaseFactoryProvider);
|
||||
|
||||
switch (TestOptions.Database)
|
||||
{
|
||||
case UmbracoTestOptions.Database.NewSchemaPerTest:
|
||||
|
||||
// New DB + Schema
|
||||
TestDbMeta newSchemaDbMeta = db.AttachSchema();
|
||||
|
||||
// Add teardown callback
|
||||
OnTestTearDown(() => db.Detach(newSchemaDbMeta));
|
||||
|
||||
ConfigureTestDatabaseFactory(newSchemaDbMeta, databaseFactory, runtimeState);
|
||||
|
||||
Assert.AreEqual(RuntimeLevel.Run, runtimeState.Level);
|
||||
|
||||
break;
|
||||
case UmbracoTestOptions.Database.NewEmptyPerTest:
|
||||
TestDbMeta newEmptyDbMeta = db.AttachEmpty();
|
||||
|
||||
// Add teardown callback
|
||||
OnTestTearDown(() => db.Detach(newEmptyDbMeta));
|
||||
|
||||
ConfigureTestDatabaseFactory(newEmptyDbMeta, databaseFactory, runtimeState);
|
||||
|
||||
Assert.AreEqual(RuntimeLevel.Install, runtimeState.Level);
|
||||
|
||||
break;
|
||||
case UmbracoTestOptions.Database.NewSchemaPerFixture:
|
||||
// Only attach schema once per fixture
|
||||
// Doing it more than once will block the process since the old db hasn't been detached
|
||||
// and it would be the same as NewSchemaPerTest even if it didn't block
|
||||
if (_firstTestInFixture)
|
||||
{
|
||||
// New DB + Schema
|
||||
TestDbMeta newSchemaFixtureDbMeta = db.AttachSchema();
|
||||
s_fixtureDbMeta = newSchemaFixtureDbMeta;
|
||||
|
||||
// Add teardown callback
|
||||
OnFixtureTearDown(() => db.Detach(newSchemaFixtureDbMeta));
|
||||
}
|
||||
|
||||
ConfigureTestDatabaseFactory(s_fixtureDbMeta, databaseFactory, runtimeState);
|
||||
|
||||
break;
|
||||
case UmbracoTestOptions.Database.NewEmptyPerFixture:
|
||||
// Only attach schema once per fixture
|
||||
// Doing it more than once will block the process since the old db hasn't been detached
|
||||
// and it would be the same as NewSchemaPerTest even if it didn't block
|
||||
if (_firstTestInFixture)
|
||||
{
|
||||
// New DB + Schema
|
||||
TestDbMeta newEmptyFixtureDbMeta = db.AttachEmpty();
|
||||
s_fixtureDbMeta = newEmptyFixtureDbMeta;
|
||||
|
||||
// Add teardown callback
|
||||
OnFixtureTearDown(() => db.Detach(newEmptyFixtureDbMeta));
|
||||
}
|
||||
|
||||
ConfigureTestDatabaseFactory(s_fixtureDbMeta, databaseFactory, runtimeState);
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(TestOptions), TestOptions, null);
|
||||
}
|
||||
}
|
||||
|
||||
private void ConfigureTestDatabaseFactory(TestDbMeta meta, IUmbracoDatabaseFactory factory, IRuntimeState state)
|
||||
{
|
||||
ILogger<UmbracoIntegrationTest> log = Services.GetRequiredService<ILogger<UmbracoIntegrationTest>>();
|
||||
log.LogInformation($"ConfigureTestDatabaseFactory - Using test database: [{meta.Name}] - IsEmpty: [{meta.IsEmpty}]");
|
||||
|
||||
// It's just been pulled from container and wasn't used to create test database
|
||||
Assert.IsFalse(factory.Configured);
|
||||
|
||||
factory.Configure(meta.ConnectionString, Constants.DatabaseProviders.SqlServer);
|
||||
state.DetermineRuntimeLevel();
|
||||
log.LogInformation($"ConfigureTestDatabaseFactory - Determined RuntimeLevel: [{state.Level}]");
|
||||
}
|
||||
|
||||
protected UmbracoTestAttribute TestOptions => TestOptionAttributeBase.GetTestOptions<UmbracoTestAttribute>();
|
||||
|
||||
protected virtual T GetRequiredService<T>() => Services.GetRequiredService<T>();
|
||||
|
||||
public Dictionary<string, string> InMemoryConfiguration { get; } = new Dictionary<string, string>();
|
||||
|
||||
public IConfiguration Configuration { get; protected set; }
|
||||
|
||||
public TestHelper TestHelper { get; } = new TestHelper();
|
||||
|
||||
protected virtual void CustomTestSetup(IUmbracoBuilder builder) { }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the DI container.
|
||||
/// </summary>
|
||||
protected IServiceProvider Services { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IScopeProvider"/>
|
||||
/// </summary>
|
||||
@@ -451,11 +174,8 @@ namespace Umbraco.Cms.Tests.Integration.Testing
|
||||
|
||||
protected IMapperCollection Mappers => Services.GetRequiredService<IMapperCollection>();
|
||||
|
||||
protected UserBuilder UserBuilderInstance { get; } = new UserBuilder();
|
||||
protected UserGroupBuilder UserGroupBuilderInstance { get; } = new UserGroupBuilder();
|
||||
protected UserBuilder UserBuilderInstance { get; } = new ();
|
||||
|
||||
private static bool s_firstTestInSession = true;
|
||||
private bool _firstTestInFixture = true;
|
||||
private static int s_testCount = 1;
|
||||
protected UserGroupBuilder UserGroupBuilderInstance { get; } = new ();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,243 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NUnit.Framework;
|
||||
using Serilog;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.Services;
|
||||
using Umbraco.Cms.Infrastructure.Persistence;
|
||||
using Umbraco.Cms.Tests.Common.Testing;
|
||||
using Umbraco.Cms.Tests.Integration.Implementations;
|
||||
|
||||
namespace Umbraco.Cms.Tests.Integration.Testing;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for all UmbracoIntegrationTests
|
||||
/// </summary>
|
||||
[SingleThreaded]
|
||||
[NonParallelizable]
|
||||
public abstract class UmbracoIntegrationTestBase
|
||||
{
|
||||
private static readonly object s_dbLocker = new ();
|
||||
private static ITestDatabase s_dbInstance;
|
||||
private static TestDbMeta s_fixtureDbMeta;
|
||||
private static int s_testCount = 1;
|
||||
|
||||
private bool _firstTestInFixture = true;
|
||||
private readonly Queue<Action> _testTeardown = new ();
|
||||
private readonly List<Action> _fixtureTeardown = new ();
|
||||
|
||||
protected Dictionary<string, string> InMemoryConfiguration { get; } = new ();
|
||||
|
||||
protected IConfiguration Configuration { get; set; }
|
||||
|
||||
protected UmbracoTestAttribute TestOptions =>
|
||||
TestOptionAttributeBase.GetTestOptions<UmbracoTestAttribute>();
|
||||
|
||||
protected TestHelper TestHelper { get; } = new ();
|
||||
|
||||
private void AddOnTestTearDown(Action tearDown) => _testTeardown.Enqueue(tearDown);
|
||||
|
||||
private void AddOnFixtureTearDown(Action tearDown) => _fixtureTeardown.Add(tearDown);
|
||||
|
||||
[SetUp]
|
||||
public void SetUp_Logging() =>
|
||||
TestContext.Progress.Write($"Start test {s_testCount++}: {TestContext.CurrentContext.Test.Name}");
|
||||
|
||||
[TearDown]
|
||||
public void TearDown_Logging() =>
|
||||
TestContext.Progress.Write($" {TestContext.CurrentContext.Result.Outcome.Status}");
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void FixtureTearDown()
|
||||
{
|
||||
foreach (Action a in _fixtureTeardown)
|
||||
{
|
||||
a();
|
||||
}
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_firstTestInFixture = false;
|
||||
|
||||
while (_testTeardown.TryDequeue(out Action a))
|
||||
{
|
||||
a();
|
||||
}
|
||||
}
|
||||
|
||||
protected ILoggerFactory CreateLoggerFactory()
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (TestOptions.Logger)
|
||||
{
|
||||
case UmbracoTestOptions.Logger.Mock:
|
||||
return NullLoggerFactory.Instance;
|
||||
case UmbracoTestOptions.Logger.Serilog:
|
||||
return LoggerFactory.Create(builder =>
|
||||
{
|
||||
var path = Path.Combine(TestHelper.WorkingDirectory, "logs", "umbraco_integration_tests_.txt");
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.WriteTo.File(path, rollingInterval: RollingInterval.Day)
|
||||
.MinimumLevel.Debug()
|
||||
.CreateLogger();
|
||||
|
||||
builder.AddSerilog(Log.Logger);
|
||||
});
|
||||
case UmbracoTestOptions.Logger.Console:
|
||||
return LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Debug));
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
|
||||
return NullLoggerFactory.Instance;
|
||||
}
|
||||
|
||||
protected virtual void ConfigureTestSpecificServices(IServiceCollection services)
|
||||
{
|
||||
}
|
||||
|
||||
protected void UseTestDatabase(IApplicationBuilder app)
|
||||
=> UseTestDatabase(app.ApplicationServices);
|
||||
|
||||
protected void UseTestDatabase(IServiceProvider serviceProvider)
|
||||
{
|
||||
IRuntimeState state = serviceProvider.GetRequiredService<IRuntimeState>();
|
||||
TestUmbracoDatabaseFactoryProvider testDatabaseFactoryProvider = serviceProvider.GetRequiredService<TestUmbracoDatabaseFactoryProvider>();
|
||||
IUmbracoDatabaseFactory databaseFactory = serviceProvider.GetRequiredService<IUmbracoDatabaseFactory>();
|
||||
ILoggerFactory loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
|
||||
|
||||
// This will create a db, install the schema and ensure the app is configured to run
|
||||
SetupTestDatabase(testDatabaseFactoryProvider, databaseFactory, loggerFactory, state, TestHelper.WorkingDirectory);
|
||||
}
|
||||
|
||||
private void ConfigureTestDatabaseFactory(TestDbMeta meta, IUmbracoDatabaseFactory factory, IRuntimeState state)
|
||||
{
|
||||
// It's just been pulled from container and wasn't used to create test database
|
||||
Assert.IsFalse(factory.Configured);
|
||||
|
||||
factory.Configure(meta.ConnectionString, Constants.DatabaseProviders.SqlServer);
|
||||
state.DetermineRuntimeLevel();
|
||||
}
|
||||
|
||||
private void SetupTestDatabase(
|
||||
TestUmbracoDatabaseFactoryProvider testUmbracoDatabaseFactoryProvider,
|
||||
IUmbracoDatabaseFactory databaseFactory,
|
||||
ILoggerFactory loggerFactory,
|
||||
IRuntimeState runtimeState,
|
||||
string workingDirectory)
|
||||
{
|
||||
if (TestOptions.Database == UmbracoTestOptions.Database.None)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// need to manually register this factory
|
||||
DbProviderFactories.RegisterFactory(Constants.DbProviderNames.SqlServer, SqlClientFactory.Instance);
|
||||
|
||||
var dbFilePath = Path.Combine(workingDirectory, "LocalDb");
|
||||
|
||||
ITestDatabase db = GetOrCreateDatabase(dbFilePath, loggerFactory, testUmbracoDatabaseFactoryProvider);
|
||||
|
||||
switch (TestOptions.Database)
|
||||
{
|
||||
case UmbracoTestOptions.Database.NewSchemaPerTest:
|
||||
|
||||
// New DB + Schema
|
||||
TestDbMeta newSchemaDbMeta = db.AttachSchema();
|
||||
|
||||
// Add teardown callback
|
||||
AddOnTestTearDown(() => db.Detach(newSchemaDbMeta));
|
||||
|
||||
ConfigureTestDatabaseFactory(newSchemaDbMeta, databaseFactory, runtimeState);
|
||||
|
||||
Assert.AreEqual(RuntimeLevel.Run, runtimeState.Level);
|
||||
|
||||
break;
|
||||
case UmbracoTestOptions.Database.NewEmptyPerTest:
|
||||
TestDbMeta newEmptyDbMeta = db.AttachEmpty();
|
||||
|
||||
// Add teardown callback
|
||||
AddOnTestTearDown(() => db.Detach(newEmptyDbMeta));
|
||||
|
||||
ConfigureTestDatabaseFactory(newEmptyDbMeta, databaseFactory, runtimeState);
|
||||
|
||||
Assert.AreEqual(RuntimeLevel.Install, runtimeState.Level);
|
||||
|
||||
break;
|
||||
case UmbracoTestOptions.Database.NewSchemaPerFixture:
|
||||
// Only attach schema once per fixture
|
||||
// Doing it more than once will block the process since the old db hasn't been detached
|
||||
// and it would be the same as NewSchemaPerTest even if it didn't block
|
||||
if (_firstTestInFixture)
|
||||
{
|
||||
// New DB + Schema
|
||||
TestDbMeta newSchemaFixtureDbMeta = db.AttachSchema();
|
||||
s_fixtureDbMeta = newSchemaFixtureDbMeta;
|
||||
|
||||
// Add teardown callback
|
||||
AddOnFixtureTearDown(() => db.Detach(newSchemaFixtureDbMeta));
|
||||
}
|
||||
|
||||
ConfigureTestDatabaseFactory(s_fixtureDbMeta, databaseFactory, runtimeState);
|
||||
|
||||
break;
|
||||
case UmbracoTestOptions.Database.NewEmptyPerFixture:
|
||||
// Only attach schema once per fixture
|
||||
// Doing it more than once will block the process since the old db hasn't been detached
|
||||
// and it would be the same as NewSchemaPerTest even if it didn't block
|
||||
if (_firstTestInFixture)
|
||||
{
|
||||
// New DB + Schema
|
||||
TestDbMeta newEmptyFixtureDbMeta = db.AttachEmpty();
|
||||
s_fixtureDbMeta = newEmptyFixtureDbMeta;
|
||||
|
||||
// Add teardown callback
|
||||
AddOnFixtureTearDown(() => db.Detach(newEmptyFixtureDbMeta));
|
||||
}
|
||||
|
||||
ConfigureTestDatabaseFactory(s_fixtureDbMeta, databaseFactory, runtimeState);
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(TestOptions), TestOptions, null);
|
||||
}
|
||||
}
|
||||
|
||||
private static ITestDatabase GetOrCreateDatabase(string filesPath, ILoggerFactory loggerFactory, TestUmbracoDatabaseFactoryProvider dbFactory)
|
||||
{
|
||||
lock (s_dbLocker)
|
||||
{
|
||||
if (s_dbInstance != null)
|
||||
{
|
||||
return s_dbInstance;
|
||||
}
|
||||
|
||||
// TODO: pull from IConfiguration
|
||||
var settings = new TestDatabaseSettings
|
||||
{
|
||||
PrepareThreadCount = 4,
|
||||
EmptyDatabasesCount = 2,
|
||||
SchemaDatabaseCount = 4
|
||||
};
|
||||
|
||||
s_dbInstance = TestDatabaseFactory.Create(settings, filesPath, loggerFactory, dbFactory);
|
||||
|
||||
return s_dbInstance;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,14 +24,7 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Core
|
||||
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
|
||||
public class RuntimeStateTests : UmbracoIntegrationTest
|
||||
{
|
||||
private protected IRuntimeState RuntimeState { get; private set; }
|
||||
|
||||
public override void Configure(IApplicationBuilder app)
|
||||
{
|
||||
base.Configure(app);
|
||||
|
||||
RuntimeState = Services.GetRequiredService<IRuntimeState>();
|
||||
}
|
||||
private IRuntimeState RuntimeState => Services.GetRequiredService<IRuntimeState>();
|
||||
|
||||
protected override void CustomTestSetup(IUmbracoBuilder builder)
|
||||
{
|
||||
|
||||
@@ -7,6 +7,7 @@ using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Castle.Core.Logging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
@@ -16,6 +17,7 @@ using Umbraco.Cms.Infrastructure.Persistence;
|
||||
using Umbraco.Cms.Tests.Common;
|
||||
using Umbraco.Cms.Tests.Common.Testing;
|
||||
using Umbraco.Cms.Tests.Integration.Testing;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Scoping
|
||||
{
|
||||
@@ -28,16 +30,19 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Scoping
|
||||
[SetUp]
|
||||
public void SetUp() => Assert.IsNull(ScopeProvider.AmbientScope); // gone
|
||||
|
||||
protected override AppCaches GetAppCaches()
|
||||
|
||||
protected override void ConfigureTestSpecificServices(IServiceCollection services)
|
||||
{
|
||||
// Need to have a mockable request cache for tests
|
||||
var appCaches = new AppCaches(
|
||||
NoAppCache.Instance,
|
||||
Mock.Of<IRequestCache>(x => x.IsAvailable == false),
|
||||
new IsolatedCaches(_ => NoAppCache.Instance));
|
||||
return appCaches;
|
||||
|
||||
services.AddUnique(appCaches);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void GivenUncompletedScopeOnChildThread_WhenTheParentCompletes_TheTransactionIsRolledBack()
|
||||
{
|
||||
|
||||
@@ -31,15 +31,15 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Scoping
|
||||
|
||||
private ILocalizationService LocalizationService => GetRequiredService<ILocalizationService>();
|
||||
|
||||
protected override AppCaches GetAppCaches()
|
||||
protected override void ConfigureTestSpecificServices(IServiceCollection services)
|
||||
{
|
||||
// this is what's created core web runtime
|
||||
var result = new AppCaches(
|
||||
var appCaches = new AppCaches(
|
||||
new DeepCloneAppCache(new ObjectCacheAppCache()),
|
||||
NoAppCache.Instance,
|
||||
new IsolatedCaches(type => new DeepCloneAppCache(new ObjectCacheAppCache())));
|
||||
|
||||
return result;
|
||||
services.AddUnique(appCaches);
|
||||
}
|
||||
|
||||
protected override void CustomTestSetup(IUmbracoBuilder builder)
|
||||
|
||||
@@ -8,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Core.Scoping;
|
||||
using Umbraco.Cms.Core.Services;
|
||||
using Umbraco.Cms.Tests.Common.Builders;
|
||||
using Umbraco.Cms.Tests.Common.Builders.Extensions;
|
||||
@@ -21,6 +22,8 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Web.BackOffice.Controllers
|
||||
[TestFixture]
|
||||
public class EntityControllerTests : UmbracoTestServerTestBase
|
||||
{
|
||||
private IScopeProvider ScopeProvider => GetRequiredService<IScopeProvider>();
|
||||
|
||||
[Test]
|
||||
public async Task GetUrlsByIds_MediaWithIntegerIds_ReturnsValidMap()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user