From 0a0561182602a8706c23894dec3d5a6558a34f5c Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Thu, 29 Oct 2020 14:08:01 +0100 Subject: [PATCH] Migrated ScopedRepositoryTests Signed-off-by: Bjarke Berg --- .../Testing/UmbracoIntegrationTest.cs | 8 +- .../Scoping/ScopedRepositoryTests.cs | 88 ++++++++----------- src/Umbraco.Tests/Umbraco.Tests.csproj | 1 - 3 files changed, 43 insertions(+), 54 deletions(-) rename src/{Umbraco.Tests => Umbraco.Tests.Integration/Umbraco.Infrastructure}/Scoping/ScopedRepositoryTests.cs (82%) diff --git a/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs b/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs index 3430644795..d089c4a4e9 100644 --- a/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs +++ b/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs @@ -263,7 +263,7 @@ namespace Umbraco.Tests.Integration.Testing webHostEnvironment, register, GetType().Assembly, - AppCaches.NoCache, // Disable caches for integration tests + GetAppCaches(), TestHelper.GetLoggingConfiguration(), Configuration, CreateTestRuntime); @@ -280,6 +280,12 @@ namespace Umbraco.Tests.Integration.Testing CustomTestSetup(services); } + protected virtual AppCaches GetAppCaches() + { + // Disable caches for integration tests + return AppCaches.NoCache; + } + public virtual void Configure(IApplicationBuilder app) { //get the currently set options diff --git a/src/Umbraco.Tests/Scoping/ScopedRepositoryTests.cs b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Scoping/ScopedRepositoryTests.cs similarity index 82% rename from src/Umbraco.Tests/Scoping/ScopedRepositoryTests.cs rename to src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Scoping/ScopedRepositoryTests.cs index 7bffbbe2ed..41f80fc0c3 100644 --- a/src/Umbraco.Tests/Scoping/ScopedRepositoryTests.cs +++ b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Scoping/ScopedRepositoryTests.cs @@ -1,64 +1,49 @@ using System; using System.Collections.Generic; using System.Linq; -using Moq; -using NUnit.Framework; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; -using Umbraco.Core; +using NUnit.Framework; using Umbraco.Core.Cache; -using Umbraco.Core.Configuration.Models; using Umbraco.Core.Events; using Umbraco.Core.Models; using Umbraco.Core.Models.Membership; using Umbraco.Core.Scoping; +using Umbraco.Core.Services; +using Umbraco.Core.Services.Implement; using Umbraco.Core.Sync; -using Umbraco.Tests.Common.Builders; -using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.Integration.Testing; using Umbraco.Tests.Testing; using Umbraco.Web; using Umbraco.Web.Cache; -using Umbraco.Web.Composing; -namespace Umbraco.Tests.Scoping +namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Scoping { [TestFixture] - [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, WithApplication = true)] - public class ScopedRepositoryTests : TestWithDatabaseBase + [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] + public class ScopedRepositoryTests : UmbracoIntegrationTest { private DistributedCacheBinder _distributedCacheBinder; - private GlobalSettings _globalSettings; + private IUserService UserService => GetRequiredService(); + private ILocalizationService LocalizationService => GetRequiredService(); + private IServerMessenger ServerMessenger => GetRequiredService(); + private CacheRefresherCollection CacheRefresherCollection => GetRequiredService(); - public override void SetUp() - { - base.SetUp(); - - _globalSettings = new GlobalSettings(); - } - - protected override void Compose() - { - base.Compose(); - - // the cache refresher component needs to trigger to refresh caches - // but then, it requires a lot of plumbing ;( - // FIXME: and we cannot inject a DistributedCache yet - // so doing all this mess - Composition.RegisterUnique(); - Composition.RegisterUnique(f => Mock.Of()); - Composition.WithCollectionBuilder() - .Add(() => Composition.TypeLoader.GetCacheRefreshers()); - } + protected override Action CustomTestSetup => (services) + => services.Replace(ServiceDescriptor.Singleton(typeof(IServerMessenger), typeof(LocalServerMessenger))); protected override AppCaches GetAppCaches() { // this is what's created core web runtime - return new AppCaches( + var result = new AppCaches( new DeepCloneAppCache(new ObjectCacheAppCache()), NoAppCache.Instance, new IsolatedCaches(type => new DeepCloneAppCache(new ObjectCacheAppCache()))); - } + return result; + } [TearDown] public void Teardown() { @@ -70,11 +55,10 @@ namespace Umbraco.Tests.Scoping [TestCase(false)] public void DefaultRepositoryCachePolicy(bool complete) { - var scopeProvider = ScopeProvider; - var service = ServiceContext.UserService; - var globalCache = Current.AppCaches.IsolatedCaches.GetOrCreate(typeof(IUser)); - - var user = (IUser)new User(_globalSettings, "name", "email", "username", "rawPassword"); + var scopeProvider = (ScopeProvider)ScopeProvider; + var service = (UserService)UserService; + var globalCache = AppCaches.IsolatedCaches.GetOrCreate(typeof(IUser)); + var user = (IUser)new User(GlobalSettings, "name", "email", "username", "rawPassword"); service.Save(user); // global cache contains the entity @@ -86,13 +70,13 @@ namespace Umbraco.Tests.Scoping // get user again - else we'd modify the one that's in the cache user = service.GetUserById(user.Id); - _distributedCacheBinder = new DistributedCacheBinder(new DistributedCache(Current.ServerMessenger, Current.CacheRefreshers), Mock.Of(), Mock.Of>()); + _distributedCacheBinder = new DistributedCacheBinder(new DistributedCache(ServerMessenger, CacheRefresherCollection), GetRequiredService(), GetRequiredService>()); _distributedCacheBinder.BindEvents(true); Assert.IsNull(scopeProvider.AmbientScope); using (var scope = scopeProvider.CreateScope(repositoryCacheMode: RepositoryCacheMode.Scoped)) { - Assert.IsInstanceOf(scope); + Assert.IsInstanceOf(scope); Assert.IsNotNull(scopeProvider.AmbientScope); Assert.AreSame(scope, scopeProvider.AmbientScope); @@ -147,11 +131,11 @@ namespace Umbraco.Tests.Scoping [TestCase(false)] public void FullDataSetRepositoryCachePolicy(bool complete) { - var scopeProvider = ScopeProvider; - var service = ServiceContext.LocalizationService; - var globalCache = Current.AppCaches.IsolatedCaches.GetOrCreate(typeof (ILanguage)); + var scopeProvider = (ScopeProvider)ScopeProvider; + var service = LocalizationService; + var globalCache = AppCaches.IsolatedCaches.GetOrCreate(typeof (ILanguage)); - var lang = (ILanguage) new Language(_globalSettings, "fr-FR"); + var lang = (ILanguage) new Language(GlobalSettings, "fr-FR"); service.Save(lang); // global cache has been flushed, reload @@ -167,13 +151,13 @@ namespace Umbraco.Tests.Scoping Assert.AreEqual(lang.Id, globalCached.Id); Assert.AreEqual("fr-FR", globalCached.IsoCode); - _distributedCacheBinder = new DistributedCacheBinder(new DistributedCache(Current.ServerMessenger, Current.CacheRefreshers), Mock.Of(), Mock.Of>()); + _distributedCacheBinder = new DistributedCacheBinder(new DistributedCache(ServerMessenger, CacheRefresherCollection), GetRequiredService(), GetRequiredService>()); _distributedCacheBinder.BindEvents(true); Assert.IsNull(scopeProvider.AmbientScope); using (var scope = scopeProvider.CreateScope(repositoryCacheMode: RepositoryCacheMode.Scoped)) { - Assert.IsInstanceOf(scope); + Assert.IsInstanceOf(scope); Assert.IsNotNull(scopeProvider.AmbientScope); Assert.AreSame(scope, scopeProvider.AmbientScope); @@ -239,11 +223,11 @@ namespace Umbraco.Tests.Scoping [TestCase(false)] public void SingleItemsOnlyRepositoryCachePolicy(bool complete) { - var scopeProvider = ScopeProvider; - var service = ServiceContext.LocalizationService; - var globalCache = Current.AppCaches.IsolatedCaches.GetOrCreate(typeof (IDictionaryItem)); + var scopeProvider = (ScopeProvider)ScopeProvider; + var service = LocalizationService; + var globalCache = AppCaches.IsolatedCaches.GetOrCreate(typeof (IDictionaryItem)); - var lang = (ILanguage)new Language(_globalSettings, "fr-FR"); + var lang = (ILanguage)new Language(GlobalSettings, "fr-FR"); service.Save(lang); var item = (IDictionaryItem) new DictionaryItem("item-key"); @@ -259,13 +243,13 @@ namespace Umbraco.Tests.Scoping Assert.AreEqual(item.Id, globalCached.Id); Assert.AreEqual("item-key", globalCached.ItemKey); - _distributedCacheBinder = new DistributedCacheBinder(new DistributedCache(Current.ServerMessenger, Current.CacheRefreshers), Mock.Of(), Mock.Of>()); + _distributedCacheBinder = new DistributedCacheBinder(new DistributedCache(ServerMessenger, CacheRefresherCollection), GetRequiredService(), GetRequiredService>()); _distributedCacheBinder.BindEvents(true); Assert.IsNull(scopeProvider.AmbientScope); using (var scope = scopeProvider.CreateScope(repositoryCacheMode: RepositoryCacheMode.Scoped)) { - Assert.IsInstanceOf(scope); + Assert.IsInstanceOf(scope); Assert.IsNotNull(scopeProvider.AmbientScope); Assert.AreSame(scope, scopeProvider.AmbientScope); diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 7c2214a0ed..8ef268d214 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -175,7 +175,6 @@ -