From 1f85f51ecdd6f37c2f0fdf72fb18823b4c4ad72f Mon Sep 17 00:00:00 2001 From: Stephan Date: Fri, 22 Apr 2016 09:58:02 +0200 Subject: [PATCH] Fixing tests - services test object --- .../Services/ThreadSafetyServiceTest.cs | 2 +- .../TestHelpers/BaseUmbracoApplicationTest.cs | 2 +- .../TestHelpers/ServiceContextHelper.cs | 136 ----------------- src/Umbraco.Tests/TestHelpers/TestObjects.cs | 137 ++++++++++++++++++ src/Umbraco.Tests/Umbraco.Tests.csproj | 1 - 5 files changed, 139 insertions(+), 139 deletions(-) delete mode 100644 src/Umbraco.Tests/TestHelpers/ServiceContextHelper.cs diff --git a/src/Umbraco.Tests/Services/ThreadSafetyServiceTest.cs b/src/Umbraco.Tests/Services/ThreadSafetyServiceTest.cs index df7788e85f..aea2ce3db4 100644 --- a/src/Umbraco.Tests/Services/ThreadSafetyServiceTest.cs +++ b/src/Umbraco.Tests/Services/ThreadSafetyServiceTest.cs @@ -55,7 +55,7 @@ namespace Umbraco.Tests.Services //we need a new Database object for each thread. var repositoryFactory = new RepositoryFactory(Container); var evtMsgs = new TransientMessagesFactory(); - ApplicationContext.Services = ServiceContextHelper.GetServiceContext( + ApplicationContext.Services = TestObjects.GetServiceContext( repositoryFactory, _uowProvider, new FileUnitOfWorkProvider(), diff --git a/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs b/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs index 3db6cd3be5..8ca2e8499b 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs @@ -220,7 +220,7 @@ namespace Umbraco.Tests.TestHelpers //assign the db context new DatabaseContext(new DefaultDatabaseFactory(Core.Configuration.GlobalSettings.UmbracoConnectionName, TestObjects.GetDefaultSqlSyntaxProviders(Logger), Logger), Logger), //assign the service context - ServiceContextHelper.GetServiceContext( + TestObjects.GetServiceContext( Container.GetInstance(), new NPocoUnitOfWorkProvider(Logger), new FileUnitOfWorkProvider(), diff --git a/src/Umbraco.Tests/TestHelpers/ServiceContextHelper.cs b/src/Umbraco.Tests/TestHelpers/ServiceContextHelper.cs deleted file mode 100644 index 468c4b19b3..0000000000 --- a/src/Umbraco.Tests/TestHelpers/ServiceContextHelper.cs +++ /dev/null @@ -1,136 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using Umbraco.Core; -using Umbraco.Core.Events; -using Umbraco.Core.IO; -using Umbraco.Core.Logging; -using Umbraco.Core.Persistence; -using Umbraco.Core.Persistence.UnitOfWork; -using Umbraco.Core.Publishing; -using Umbraco.Core.Services; -using Umbraco.Core.Strings; -using Umbraco.Web.Services; - -namespace Umbraco.Tests.TestHelpers -{ - class ServiceContextHelper - { - //NOTE: Should be used sparingly for integration tests only - for unit tests you can just mock the services to be passed to the - // ctor of the ServiceContext. - public static ServiceContext GetServiceContext(RepositoryFactory repositoryFactory, - IDatabaseUnitOfWorkProvider dbUnitOfWorkProvider, - IUnitOfWorkProvider fileUnitOfWorkProvider, - IPublishingStrategy publishingStrategy, - CacheHelper cache, - ILogger logger, - IEventMessagesFactory eventMessagesFactory, - IEnumerable urlSegmentProviders) - { - if (repositoryFactory == null) throw new ArgumentNullException(nameof(repositoryFactory)); - if (dbUnitOfWorkProvider == null) throw new ArgumentNullException(nameof(dbUnitOfWorkProvider)); - if (fileUnitOfWorkProvider == null) throw new ArgumentNullException(nameof(fileUnitOfWorkProvider)); - if (publishingStrategy == null) throw new ArgumentNullException(nameof(publishingStrategy)); - if (cache == null) throw new ArgumentNullException(nameof(cache)); - if (logger == null) throw new ArgumentNullException(nameof(logger)); - if (eventMessagesFactory == null) throw new ArgumentNullException(nameof(eventMessagesFactory)); - - var provider = dbUnitOfWorkProvider; - var fileProvider = fileUnitOfWorkProvider; - - var migrationEntryService = new Lazy(() => new MigrationEntryService(provider, repositoryFactory, logger, eventMessagesFactory)); - var externalLoginService = new Lazy(() => new ExternalLoginService(provider, repositoryFactory, logger, eventMessagesFactory)); - var publicAccessService = new Lazy(() => new PublicAccessService(provider, repositoryFactory, logger, eventMessagesFactory)); - var taskService = new Lazy(() => new TaskService(provider, repositoryFactory, logger, eventMessagesFactory)); - var domainService = new Lazy(() => new DomainService(provider, repositoryFactory, logger, eventMessagesFactory)); - var auditService = new Lazy(() => new AuditService(provider, repositoryFactory, logger, eventMessagesFactory)); - - var localizedTextService = new Lazy(() => new LocalizedTextService( - new Lazy(() => - { - var mainLangFolder = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Umbraco + "/config/lang/")); - var appPlugins = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.AppPlugins)); - var configLangFolder = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Config + "/lang/")); - - var pluginLangFolders = appPlugins.Exists == false - ? Enumerable.Empty() - : appPlugins.GetDirectories() - .SelectMany(x => x.GetDirectories("Lang")) - .SelectMany(x => x.GetFiles("*.xml", SearchOption.TopDirectoryOnly)) - .Where(x => Path.GetFileNameWithoutExtension(x.FullName).Length == 5) - .Select(x => new LocalizedTextServiceSupplementaryFileSource(x, false)); - - //user defined langs that overwrite the default, these should not be used by plugin creators - var userLangFolders = configLangFolder.Exists == false - ? Enumerable.Empty() - : configLangFolder - .GetFiles("*.user.xml", SearchOption.TopDirectoryOnly) - .Where(x => Path.GetFileNameWithoutExtension(x.FullName).Length == 10) - .Select(x => new LocalizedTextServiceSupplementaryFileSource(x, true)); - - return new LocalizedTextServiceFileSources( - logger, - cache.RuntimeCache, - mainLangFolder, - pluginLangFolders.Concat(userLangFolders)); - - }), - logger)); - - var userService = new Lazy(() => new UserService(provider, repositoryFactory, logger, eventMessagesFactory)); - var dataTypeService = new Lazy(() => new DataTypeService(provider, repositoryFactory, logger, eventMessagesFactory)); - var contentService = new Lazy(() => new ContentService(provider, repositoryFactory, logger, eventMessagesFactory, publishingStrategy, dataTypeService.Value, userService.Value, urlSegmentProviders)); - var notificationService = new Lazy(() => new NotificationService(provider, userService.Value, contentService.Value, repositoryFactory, logger)); - var serverRegistrationService = new Lazy(() => new ServerRegistrationService(provider, repositoryFactory, logger, eventMessagesFactory)); - var memberGroupService = new Lazy(() => new MemberGroupService(provider, repositoryFactory, logger, eventMessagesFactory)); - var memberService = new Lazy(() => new MemberService(provider, repositoryFactory, logger, eventMessagesFactory, memberGroupService.Value, dataTypeService.Value)); - var mediaService = new Lazy(() => new MediaService(provider, repositoryFactory, logger, eventMessagesFactory, dataTypeService.Value, userService.Value, urlSegmentProviders)); - var contentTypeService = new Lazy(() => new ContentTypeService(provider, repositoryFactory, logger, eventMessagesFactory, contentService.Value, mediaService.Value)); - var fileService = new Lazy(() => new FileService(fileProvider, provider, repositoryFactory, logger, eventMessagesFactory)); - var localizationService = new Lazy(() => new LocalizationService(provider, repositoryFactory, logger, eventMessagesFactory)); - - var memberTypeService = new Lazy(() => new MemberTypeService(provider, repositoryFactory, logger, eventMessagesFactory, memberService.Value)); - var entityService = new Lazy(() => new EntityService( - provider, repositoryFactory, logger, eventMessagesFactory, - contentService.Value, contentTypeService.Value, mediaService.Value, dataTypeService.Value, memberService.Value, memberTypeService.Value, - //TODO: Consider making this an isolated cache instead of using the global one - cache.RuntimeCache)); - - var macroService = new Lazy(() => new MacroService(provider, repositoryFactory, logger, eventMessagesFactory)); - var packagingService = new Lazy(() => new PackagingService(logger, contentService.Value, contentTypeService.Value, mediaService.Value, macroService.Value, dataTypeService.Value, fileService.Value, localizationService.Value, entityService.Value, userService.Value, repositoryFactory, provider, urlSegmentProviders)); - var relationService = new Lazy(() => new RelationService(provider, repositoryFactory, logger, eventMessagesFactory, entityService.Value)); - var treeService = new Lazy(() => new ApplicationTreeService(logger, cache)); - var tagService = new Lazy(() => new TagService(provider, repositoryFactory, logger, eventMessagesFactory)); - var sectionService = new Lazy(() => new SectionService(userService.Value, treeService.Value, provider, cache)); - - return new ServiceContext( - migrationEntryService, - publicAccessService, - taskService, - domainService, - auditService, - localizedTextService, - tagService, - contentService, - userService, - memberService, - mediaService, - contentTypeService, - dataTypeService, - fileService, - localizationService, - packagingService, - serverRegistrationService, - entityService, - relationService, - treeService, - sectionService, - macroService, - memberTypeService, - memberGroupService, - notificationService, - externalLoginService); - } - } -} diff --git a/src/Umbraco.Tests/TestHelpers/TestObjects.cs b/src/Umbraco.Tests/TestHelpers/TestObjects.cs index 4454be3444..1eb4437a1f 100644 --- a/src/Umbraco.Tests/TestHelpers/TestObjects.cs +++ b/src/Umbraco.Tests/TestHelpers/TestObjects.cs @@ -1,12 +1,21 @@ using System; using System.Collections.Generic; using System.Data.Common; +using System.IO; +using System.Linq; using Moq; using NPoco; using Umbraco.Core; +using Umbraco.Core.Events; +using Umbraco.Core.IO; using Umbraco.Core.Logging; using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.SqlSyntax; +using Umbraco.Core.Persistence.UnitOfWork; +using Umbraco.Core.Publishing; +using Umbraco.Core.Services; +using Umbraco.Core.Strings; +using Umbraco.Web.Services; namespace Umbraco.Tests.TestHelpers { @@ -60,5 +69,133 @@ namespace Umbraco.Tests.TestHelpers var connection = TestObjects.GetDbConnection(); return new UmbracoDatabase(connection, syntax, DatabaseType.SqlServer2008, dbProviderFactory, logger); } + + /// + /// Gets a ServiceContext. + /// + /// A repository factory. + /// A database unit of work provider. + /// A file unit of work provider. + /// A publishing strategy. + /// A cache. + /// A logger. + /// An event messages factory. + /// Some url segment providers. + /// A ServiceContext. + /// Should be used sparingly for integration tests only - for unit tests + /// just mock the services to be passed to the ctor of the ServiceContext. + public static ServiceContext GetServiceContext(RepositoryFactory repositoryFactory, + IDatabaseUnitOfWorkProvider dbUnitOfWorkProvider, + IUnitOfWorkProvider fileUnitOfWorkProvider, + IPublishingStrategy publishingStrategy, + CacheHelper cache, + ILogger logger, + IEventMessagesFactory eventMessagesFactory, + IEnumerable urlSegmentProviders) + { + if (repositoryFactory == null) throw new ArgumentNullException(nameof(repositoryFactory)); + if (dbUnitOfWorkProvider == null) throw new ArgumentNullException(nameof(dbUnitOfWorkProvider)); + if (fileUnitOfWorkProvider == null) throw new ArgumentNullException(nameof(fileUnitOfWorkProvider)); + if (publishingStrategy == null) throw new ArgumentNullException(nameof(publishingStrategy)); + if (cache == null) throw new ArgumentNullException(nameof(cache)); + if (logger == null) throw new ArgumentNullException(nameof(logger)); + if (eventMessagesFactory == null) throw new ArgumentNullException(nameof(eventMessagesFactory)); + + var provider = dbUnitOfWorkProvider; + var fileProvider = fileUnitOfWorkProvider; + + var migrationEntryService = new Lazy(() => new MigrationEntryService(provider, repositoryFactory, logger, eventMessagesFactory)); + var externalLoginService = new Lazy(() => new ExternalLoginService(provider, repositoryFactory, logger, eventMessagesFactory)); + var publicAccessService = new Lazy(() => new PublicAccessService(provider, repositoryFactory, logger, eventMessagesFactory)); + var taskService = new Lazy(() => new TaskService(provider, repositoryFactory, logger, eventMessagesFactory)); + var domainService = new Lazy(() => new DomainService(provider, repositoryFactory, logger, eventMessagesFactory)); + var auditService = new Lazy(() => new AuditService(provider, repositoryFactory, logger, eventMessagesFactory)); + + var localizedTextService = new Lazy(() => new LocalizedTextService( + new Lazy(() => + { + var mainLangFolder = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Umbraco + "/config/lang/")); + var appPlugins = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.AppPlugins)); + var configLangFolder = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Config + "/lang/")); + + var pluginLangFolders = appPlugins.Exists == false + ? Enumerable.Empty() + : appPlugins.GetDirectories() + .SelectMany(x => x.GetDirectories("Lang")) + .SelectMany(x => x.GetFiles("*.xml", SearchOption.TopDirectoryOnly)) + .Where(x => Path.GetFileNameWithoutExtension(x.FullName).Length == 5) + .Select(x => new LocalizedTextServiceSupplementaryFileSource(x, false)); + + //user defined langs that overwrite the default, these should not be used by plugin creators + var userLangFolders = configLangFolder.Exists == false + ? Enumerable.Empty() + : configLangFolder + .GetFiles("*.user.xml", SearchOption.TopDirectoryOnly) + .Where(x => Path.GetFileNameWithoutExtension(x.FullName).Length == 10) + .Select(x => new LocalizedTextServiceSupplementaryFileSource(x, true)); + + return new LocalizedTextServiceFileSources( + logger, + cache.RuntimeCache, + mainLangFolder, + pluginLangFolders.Concat(userLangFolders)); + + }), + logger)); + + var userService = new Lazy(() => new UserService(provider, repositoryFactory, logger, eventMessagesFactory)); + var dataTypeService = new Lazy(() => new DataTypeService(provider, repositoryFactory, logger, eventMessagesFactory)); + var contentService = new Lazy(() => new ContentService(provider, repositoryFactory, logger, eventMessagesFactory, publishingStrategy, dataTypeService.Value, userService.Value, urlSegmentProviders)); + var notificationService = new Lazy(() => new NotificationService(provider, userService.Value, contentService.Value, repositoryFactory, logger)); + var serverRegistrationService = new Lazy(() => new ServerRegistrationService(provider, repositoryFactory, logger, eventMessagesFactory)); + var memberGroupService = new Lazy(() => new MemberGroupService(provider, repositoryFactory, logger, eventMessagesFactory)); + var memberService = new Lazy(() => new MemberService(provider, repositoryFactory, logger, eventMessagesFactory, memberGroupService.Value, dataTypeService.Value)); + var mediaService = new Lazy(() => new MediaService(provider, repositoryFactory, logger, eventMessagesFactory, dataTypeService.Value, userService.Value, urlSegmentProviders)); + var contentTypeService = new Lazy(() => new ContentTypeService(provider, repositoryFactory, logger, eventMessagesFactory, contentService.Value, mediaService.Value)); + var fileService = new Lazy(() => new FileService(fileProvider, provider, repositoryFactory, logger, eventMessagesFactory)); + var localizationService = new Lazy(() => new LocalizationService(provider, repositoryFactory, logger, eventMessagesFactory)); + + var memberTypeService = new Lazy(() => new MemberTypeService(provider, repositoryFactory, logger, eventMessagesFactory, memberService.Value)); + var entityService = new Lazy(() => new EntityService( + provider, repositoryFactory, logger, eventMessagesFactory, + contentService.Value, contentTypeService.Value, mediaService.Value, dataTypeService.Value, memberService.Value, memberTypeService.Value, + //TODO: Consider making this an isolated cache instead of using the global one + cache.RuntimeCache)); + + var macroService = new Lazy(() => new MacroService(provider, repositoryFactory, logger, eventMessagesFactory)); + var packagingService = new Lazy(() => new PackagingService(logger, contentService.Value, contentTypeService.Value, mediaService.Value, macroService.Value, dataTypeService.Value, fileService.Value, localizationService.Value, entityService.Value, userService.Value, repositoryFactory, provider, urlSegmentProviders)); + var relationService = new Lazy(() => new RelationService(provider, repositoryFactory, logger, eventMessagesFactory, entityService.Value)); + var treeService = new Lazy(() => new ApplicationTreeService(logger, cache)); + var tagService = new Lazy(() => new TagService(provider, repositoryFactory, logger, eventMessagesFactory)); + var sectionService = new Lazy(() => new SectionService(userService.Value, treeService.Value, provider, cache)); + + return new ServiceContext( + migrationEntryService, + publicAccessService, + taskService, + domainService, + auditService, + localizedTextService, + tagService, + contentService, + userService, + memberService, + mediaService, + contentTypeService, + dataTypeService, + fileService, + localizationService, + packagingService, + serverRegistrationService, + entityService, + relationService, + treeService, + sectionService, + macroService, + memberTypeService, + memberGroupService, + notificationService, + externalLoginService); + } } } diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 644cc7cdb7..b876bddc05 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -186,7 +186,6 @@ -