Adds public ctors for ApplicationContext, ServiceContext, DatabaseContext, RepositoryFactory, makes IDatabaseFactory public, makes an interfaces for IEntityService, adds EnsureContext methods for ApplicationContext - to set the singleton.

This commit is contained in:
Shannon
2013-10-09 13:17:19 +11:00
parent 49f10ccd32
commit c6389852ea
19 changed files with 432 additions and 121 deletions

View File

@@ -35,12 +35,9 @@ namespace Umbraco.Core
/// <param name="dbContext"></param>
/// <param name="serviceContext"></param>
/// <param name="enableCache"></param>
internal ApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext, bool enableCache)
public ApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext, bool enableCache)
: this(enableCache)
{
if (dbContext == null) throw new ArgumentNullException("dbContext");
if (serviceContext == null) throw new ArgumentNullException("serviceContext");
_databaseContext = dbContext;
_services = serviceContext;
}
@@ -57,7 +54,7 @@ namespace Umbraco.Core
/// Constructor used to specify if we will enable application cache or not
/// </summary>
/// <param name="enableCache"></param>
internal ApplicationContext(bool enableCache)
public ApplicationContext(bool enableCache)
{
//create a new application cache from the HttpRuntime.Cache
ApplicationCache = HttpRuntime.Cache == null
@@ -65,7 +62,55 @@ namespace Umbraco.Core
: new CacheHelper(HttpRuntime.Cache, enableCache);
}
/// <summary>
/// <summary>
/// A method used to set and/or ensure that a global ApplicationContext singleton is created.
/// </summary>
/// <param name="appContext">
/// The instance to set on the global application singleton
/// </param>
/// <param name="replaceContext">If set to true and the singleton is already set, it will be replaced</param>
/// <returns></returns>
/// <remarks>
/// This is NOT thread safe
/// </remarks>
public static ApplicationContext EnsureContext(ApplicationContext appContext, bool replaceContext)
{
if (ApplicationContext.Current != null)
{
if (!replaceContext)
return ApplicationContext.Current;
}
ApplicationContext.Current = appContext;
return ApplicationContext.Current;
}
/// <summary>
/// A method used to create and ensure that a global ApplicationContext singleton is created.
/// </summary>
/// <param name="enableCache"></param>
/// <param name="replaceContext">
/// If set to true will replace the current singleton instance - This should only be used for unit tests or on app
/// startup if for some reason the boot manager is not the umbraco boot manager.
/// </param>
/// <param name="dbContext"></param>
/// <param name="serviceContext"></param>
/// <returns></returns>
/// <remarks>
/// This is NOT thread safe
/// </remarks>
public static ApplicationContext EnsureContext(DatabaseContext dbContext, ServiceContext serviceContext, bool enableCache, bool replaceContext)
{
if (ApplicationContext.Current != null)
{
if (!replaceContext)
return ApplicationContext.Current;
}
var ctx = new ApplicationContext(dbContext, serviceContext, enableCache);
ApplicationContext.Current = ctx;
return ApplicationContext.Current;
}
/// <summary>
/// Singleton accessor
/// </summary>
public static ApplicationContext Current { get; internal set; }

View File

@@ -28,7 +28,7 @@ namespace Umbraco.Core
private string _providerName;
private DatabaseSchemaResult _result;
internal DatabaseContext(IDatabaseFactory factory)
public DatabaseContext(IDatabaseFactory factory)
{
_factory = factory;
}

View File

@@ -5,7 +5,7 @@ namespace Umbraco.Core.Persistence
/// <summary>
/// Used to create the UmbracoDatabase for use in the DatabaseContext
/// </summary>
internal interface IDatabaseFactory : IDisposable
public interface IDatabaseFactory : IDisposable
{
UmbracoDatabase CreateDatabase();
}

View File

@@ -17,7 +17,7 @@ namespace Umbraco.Core.Persistence
}
internal RepositoryFactory(bool disableAllCache)
public RepositoryFactory(bool disableAllCache)
{
_disableAllCache = disableAllCache;
}

View File

@@ -10,7 +10,7 @@ using Umbraco.Core.Persistence.UnitOfWork;
namespace Umbraco.Core.Services
{
public class EntityService : IService
public class EntityService : IService, IEntityService
{
private readonly IDatabaseUnitOfWorkProvider _uowProvider;
private readonly RepositoryFactory _repositoryFactory;

View File

@@ -0,0 +1,146 @@
using System;
using System.Collections.Generic;
using Umbraco.Core.Models;
using Umbraco.Core.Models.EntityBase;
namespace Umbraco.Core.Services
{
public interface IEntityService
{
/// <summary>
/// Gets an UmbracoEntity by its Id, and optionally loads the complete object graph.
/// </summary>
/// <returns>
/// By default this will load the base type <see cref="IUmbracoEntity"/> with a minimum set of properties.
/// </returns>
/// <param name="id">Id of the object to retrieve</param>
/// <param name="loadBaseType">Optional bool to load the complete object graph when set to <c>False</c>.</param>
/// <returns>An <see cref="IUmbracoEntity"/></returns>
IUmbracoEntity Get(int id, bool loadBaseType = true);
/// <summary>
/// Gets an UmbracoEntity by its Id and UmbracoObjectType, and optionally loads the complete object graph.
/// </summary>
/// <returns>
/// By default this will load the base type <see cref="IUmbracoEntity"/> with a minimum set of properties.
/// </returns>
/// <param name="id">Id of the object to retrieve</param>
/// <param name="umbracoObjectType">UmbracoObjectType of the entity to retrieve</param>
/// <param name="loadBaseType">Optional bool to load the complete object graph when set to <c>False</c>.</param>
/// <returns>An <see cref="IUmbracoEntity"/></returns>
IUmbracoEntity Get(int id, UmbracoObjectTypes umbracoObjectType, bool loadBaseType = true);
/// <summary>
/// Gets an UmbracoEntity by its Id and specified Type. Optionally loads the complete object graph.
/// </summary>
/// <returns>
/// By default this will load the base type <see cref="IUmbracoEntity"/> with a minimum set of properties.
/// </returns>
/// <typeparam name="T">Type of the model to retrieve. Must be based on an <see cref="IUmbracoEntity"/></typeparam>
/// <param name="id">Id of the object to retrieve</param>
/// <param name="loadBaseType">Optional bool to load the complete object graph when set to <c>False</c>.</param>
/// <returns>An <see cref="IUmbracoEntity"/></returns>
IUmbracoEntity Get<T>(int id, bool loadBaseType = true) where T : IUmbracoEntity;
/// <summary>
/// Gets the parent of entity by its id
/// </summary>
/// <param name="id">Id of the entity to retrieve the Parent for</param>
/// <returns>An <see cref="IUmbracoEntity"/></returns>
IUmbracoEntity GetParent(int id);
/// <summary>
/// Gets the parent of entity by its id and UmbracoObjectType
/// </summary>
/// <param name="id">Id of the entity to retrieve the Parent for</param>
/// <param name="umbracoObjectType">UmbracoObjectType of the parent to retrieve</param>
/// <returns>An <see cref="IUmbracoEntity"/></returns>
IUmbracoEntity GetParent(int id, UmbracoObjectTypes umbracoObjectType);
/// <summary>
/// Gets a collection of children by the parents Id
/// </summary>
/// <param name="parentId">Id of the parent to retrieve children for</param>
/// <returns>An enumerable list of <see cref="IUmbracoEntity"/> objects</returns>
IEnumerable<IUmbracoEntity> GetChildren(int parentId);
/// <summary>
/// Gets a collection of children by the parents Id and UmbracoObjectType
/// </summary>
/// <param name="parentId">Id of the parent to retrieve children for</param>
/// <param name="umbracoObjectType">UmbracoObjectType of the children to retrieve</param>
/// <returns>An enumerable list of <see cref="IUmbracoEntity"/> objects</returns>
IEnumerable<IUmbracoEntity> GetChildren(int parentId, UmbracoObjectTypes umbracoObjectType);
/// <summary>
/// Gets a collection of descendents by the parents Id
/// </summary>
/// <param name="id">Id of entity to retrieve descendents for</param>
/// <returns>An enumerable list of <see cref="IUmbracoEntity"/> objects</returns>
IEnumerable<IUmbracoEntity> GetDescendents(int id);
/// <summary>
/// Gets a collection of descendents by the parents Id
/// </summary>
/// <param name="id">Id of entity to retrieve descendents for</param>
/// <param name="umbracoObjectType">UmbracoObjectType of the descendents to retrieve</param>
/// <returns>An enumerable list of <see cref="IUmbracoEntity"/> objects</returns>
IEnumerable<IUmbracoEntity> GetDescendents(int id, UmbracoObjectTypes umbracoObjectType);
/// <summary>
/// Gets a collection of the entities at the root, which corresponds to the entities with a Parent Id of -1.
/// </summary>
/// <param name="umbracoObjectType">UmbracoObjectType of the root entities to retrieve</param>
/// <returns>An enumerable list of <see cref="IUmbracoEntity"/> objects</returns>
IEnumerable<IUmbracoEntity> GetRootEntities(UmbracoObjectTypes umbracoObjectType);
/// <summary>
/// Gets a collection of all <see cref="IUmbracoEntity"/> of a given type.
/// </summary>
/// <typeparam name="T">Type of the entities to retrieve</typeparam>
/// <returns>An enumerable list of <see cref="IUmbracoEntity"/> objects</returns>
IEnumerable<IUmbracoEntity> GetAll<T>() where T : IUmbracoEntity;
/// <summary>
/// Gets a collection of all <see cref="IUmbracoEntity"/> of a given type.
/// </summary>
/// <param name="umbracoObjectType">UmbracoObjectType of the entities to return</param>
/// <returns>An enumerable list of <see cref="IUmbracoEntity"/> objects</returns>
IEnumerable<IUmbracoEntity> GetAll(UmbracoObjectTypes umbracoObjectType);
/// <summary>
/// Gets a collection of <see cref="IUmbracoEntity"/>
/// </summary>
/// <param name="objectTypeId">Guid id of the UmbracoObjectType</param>
/// <returns>An enumerable list of <see cref="IUmbracoEntity"/> objects</returns>
IEnumerable<IUmbracoEntity> GetAll(Guid objectTypeId);
/// <summary>
/// Gets the UmbracoObjectType from the integer id of an IUmbracoEntity.
/// </summary>
/// <param name="id">Id of the entity</param>
/// <returns><see cref="UmbracoObjectTypes"/></returns>
UmbracoObjectTypes GetObjectType(int id);
/// <summary>
/// Gets the UmbracoObjectType from an IUmbracoEntity.
/// </summary>
/// <param name="entity"><see cref="IUmbracoEntity"/></param>
/// <returns><see cref="UmbracoObjectTypes"/></returns>
UmbracoObjectTypes GetObjectType(IUmbracoEntity entity);
/// <summary>
/// Gets the Type of an entity by its Id
/// </summary>
/// <param name="id">Id of the entity</param>
/// <returns>Type of the entity</returns>
Type GetEntityType(int id);
/// <summary>
/// Gets the Type of an entity by its <see cref="UmbracoObjectTypes"/>
/// </summary>
/// <param name="umbracoObjectType"><see cref="UmbracoObjectTypes"/></param>
/// <returns>Type of the entity</returns>
Type GetEntityType(UmbracoObjectTypes umbracoObjectType);
}
}

View File

@@ -13,10 +13,10 @@ namespace Umbraco.Core.Services
{
private readonly IDatabaseUnitOfWorkProvider _uowProvider;
private readonly RepositoryFactory _repositoryFactory;
private readonly EntityService _entityService;
private readonly IEntityService _entityService;
public RelationService(IDatabaseUnitOfWorkProvider uowProvider, RepositoryFactory repositoryFactory,
EntityService entityService)
IEntityService entityService)
{
_uowProvider = uowProvider;
_repositoryFactory = repositoryFactory;

View File

@@ -22,7 +22,7 @@ namespace Umbraco.Core.Services
private Lazy<ILocalizationService> _localizationService;
private Lazy<PackagingService> _packagingService;
private Lazy<ServerRegistrationService> _serverRegistrationService;
private Lazy<EntityService> _entityService;
private Lazy<IEntityService> _entityService;
private Lazy<RelationService> _relationService;
private Lazy<IMemberTypeService> _memberTypeService;
@@ -36,23 +36,19 @@ namespace Umbraco.Core.Services
/// <param name="fileService"></param>
/// <param name="localizationService"></param>
/// <param name="packagingService"></param>
/// <param name="serverRegistrationService"></param>
/// <param name="entityService"></param>
/// <param name="relationService"></param>
/// <param name="memberTypeService"></param>
public ServiceContext(Lazy<IContentService> contentService, Lazy<IMediaService> mediaService, Lazy<IContentTypeService> contentTypeService, Lazy<IDataTypeService> dataTypeService, Lazy<IFileService> fileService, Lazy<ILocalizationService> localizationService, Lazy<PackagingService> packagingService, Lazy<ServerRegistrationService> serverRegistrationService, Lazy<EntityService> entityService, Lazy<RelationService> relationService, Lazy<IMemberTypeService> memberTypeService)
public ServiceContext(IContentService contentService, IMediaService mediaService, IContentTypeService contentTypeService, IDataTypeService dataTypeService, IFileService fileService, ILocalizationService localizationService, PackagingService packagingService, IEntityService entityService, RelationService relationService)
{
_contentService = contentService;
_mediaService = mediaService;
_contentTypeService = contentTypeService;
_dataTypeService = dataTypeService;
_fileService = fileService;
_localizationService = localizationService;
_packagingService = packagingService;
_serverRegistrationService = serverRegistrationService;
_entityService = entityService;
_relationService = relationService;
_memberTypeService = memberTypeService;
_contentService = new Lazy<IContentService>(() => contentService);
_mediaService = new Lazy<IMediaService>(() => mediaService);
_contentTypeService = new Lazy<IContentTypeService>(() => contentTypeService);
_dataTypeService = new Lazy<IDataTypeService>(() => dataTypeService);
_fileService = new Lazy<IFileService>(() => fileService);
_localizationService = new Lazy<ILocalizationService>(() => localizationService);
_packagingService = new Lazy<PackagingService>(() => packagingService);
_entityService = new Lazy<IEntityService>(() => entityService);
_relationService = new Lazy<RelationService>(() => relationService);
}
/// <summary>
@@ -112,7 +108,7 @@ namespace Umbraco.Core.Services
_packagingService = new Lazy<PackagingService>(() => new PackagingService(_contentService.Value, _contentTypeService.Value, _mediaService.Value, _dataTypeService.Value, _fileService.Value, _localizationService.Value, repositoryFactory.Value, provider));
if (_entityService == null)
_entityService = new Lazy<EntityService>(() => new EntityService(provider, repositoryFactory.Value, _contentService.Value, _contentTypeService.Value, _mediaService.Value, _dataTypeService.Value));
_entityService = new Lazy<IEntityService>(() => new EntityService(provider, repositoryFactory.Value, _contentService.Value, _contentTypeService.Value, _mediaService.Value, _dataTypeService.Value));
if (_relationService == null)
_relationService = new Lazy<RelationService>(() => new RelationService(provider, repositoryFactory.Value, _entityService.Value));
@@ -132,7 +128,7 @@ namespace Umbraco.Core.Services
/// <summary>
/// Gets the <see cref="EntityService"/>
/// </summary>
public EntityService EntityService
public IEntityService EntityService
{
get { return _entityService.Value; }
}

View File

@@ -714,6 +714,7 @@
<Compile Include="Services\IContentService.cs" />
<Compile Include="Services\IContentTypeService.cs" />
<Compile Include="Services\IDataTypeService.cs" />
<Compile Include="Services\IEntityService.cs" />
<Compile Include="Services\IFileService.cs" />
<Compile Include="Services\ILocalizationService.cs" />
<Compile Include="Services\IMediaService.cs" />

View File

@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Services;
using Moq;
using Umbraco.Web;
namespace Umbraco.Tests
{
[TestFixture]
public class MockTests
{
[Test]
public void Can_Create_Empty_App_Context()
{
var appCtx = new ApplicationContext(false);
Assert.Pass();
}
[Test]
public void Can_Create_Service_Context()
{
var svcCtx = new ServiceContext(
new Mock<IContentService>().Object,
new Mock<IMediaService>().Object,
new Mock<IContentTypeService>().Object,
new Mock<IDataTypeService>().Object,
new Mock<IFileService>().Object,
new Mock<ILocalizationService>().Object,
new PackagingService(
new Mock<IContentService>().Object,
new Mock<IContentTypeService>().Object,
new Mock<IMediaService>().Object,
new Mock<IDataTypeService>().Object,
new Mock<IFileService>().Object,
new Mock<ILocalizationService>().Object,
new RepositoryFactory(true),
new Mock<IDatabaseUnitOfWorkProvider>().Object),
new Mock<IEntityService>().Object,
new RelationService(
new Mock<IDatabaseUnitOfWorkProvider>().Object,
new RepositoryFactory(true),
new Mock<IEntityService>().Object));
Assert.Pass();
}
[Test]
public void Can_Create_Db_Context()
{
var dbCtx = new DatabaseContext(new Mock<IDatabaseFactory>().Object);
Assert.Pass();
}
[Test]
public void Can_Create_App_Context_With_Services()
{
var appCtx = new ApplicationContext(
new DatabaseContext(new Mock<IDatabaseFactory>().Object),
new ServiceContext(
new Mock<IContentService>().Object,
new Mock<IMediaService>().Object,
new Mock<IContentTypeService>().Object,
new Mock<IDataTypeService>().Object,
new Mock<IFileService>().Object,
new Mock<ILocalizationService>().Object,
new PackagingService(
new Mock<IContentService>().Object,
new Mock<IContentTypeService>().Object,
new Mock<IMediaService>().Object,
new Mock<IDataTypeService>().Object,
new Mock<IFileService>().Object,
new Mock<ILocalizationService>().Object,
new RepositoryFactory(true),
new Mock<IDatabaseUnitOfWorkProvider>().Object),
new Mock<IEntityService>().Object,
new RelationService(
new Mock<IDatabaseUnitOfWorkProvider>().Object,
new RepositoryFactory(true),
new Mock<IEntityService>().Object)),
false);
Assert.Pass();
}
[Test]
public void Can_Assign_App_Context_Singleton()
{
var appCtx = new ApplicationContext(false);
var result = ApplicationContext.EnsureContext(appCtx, true);
Assert.AreEqual(appCtx, result);
}
[Test]
public void Does_Not_Overwrite_App_Context_Singleton()
{
ApplicationContext.EnsureContext(new ApplicationContext(false), true);
var appCtx = new ApplicationContext(false);
var result = ApplicationContext.EnsureContext(appCtx, false);
Assert.AreNotEqual(appCtx, result);
}
[NUnit.Framework.Ignore("Need to fix more stuff up, this is ignore because an exception occurs because it wants to ensure we have a resolver initialized - need to make that process better for testability")]
[Test]
public void Can_Get_Umbraco_Context()
{
var appCtx = new ApplicationContext(false);
ApplicationContext.EnsureContext(appCtx, true);
var umbCtx = UmbracoContext.EnsureContext(
new Mock<HttpContextBase>().Object,
appCtx,
true);
Assert.AreEqual(umbCtx, UmbracoContext.Current);
}
}
}

View File

@@ -1,35 +1,35 @@
using System.Linq;
using NUnit.Framework;
using Umbraco.Core.Auditing;
using Umbraco.Core.Models.Rdbms;
using Umbraco.Tests.TestHelpers;
namespace Umbraco.Tests.Auditing
{
[TestFixture]
public class AuditTests : BaseDatabaseFactoryTest
{
[SetUp]
public override void Initialize()
{
base.Initialize();
}
[Test]
public void Can_Add_Audit_Entry()
{
Audit.Add(AuditTypes.System, "This is a System audit trail", 0, -1);
var dtos = DatabaseContext.Database.Fetch<LogDto>("WHERE id > -1");
Assert.That(dtos.Any(), Is.True);
Assert.That(dtos.First().Comment, Is.EqualTo("This is a System audit trail"));
}
[TearDown]
public override void TearDown()
{
base.TearDown();
}
}
using System.Linq;
using NUnit.Framework;
using Umbraco.Core.Auditing;
using Umbraco.Core.Models.Rdbms;
using Umbraco.Tests.TestHelpers;
namespace Umbraco.Tests.Persistence.Auditing
{
[TestFixture]
public class AuditTests : BaseDatabaseFactoryTest
{
[SetUp]
public override void Initialize()
{
base.Initialize();
}
[Test]
public void Can_Add_Audit_Entry()
{
Audit.Add(AuditTypes.System, "This is a System audit trail", 0, -1);
var dtos = DatabaseContext.Database.Fetch<LogDto>("WHERE id > -1");
Assert.That(dtos.Any(), Is.True);
Assert.That(dtos.First().Comment, Is.EqualTo("This is a System audit trail"));
}
[TearDown]
public override void TearDown()
{
base.TearDown();
}
}
}

View File

@@ -3,8 +3,8 @@ using System.Web.Routing;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Tests.Stubs;
using Umbraco.Tests.TestHelpers;
using Umbraco.Tests.TestHelpers.Stubs;
using Umbraco.Web;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;

View File

@@ -3,9 +3,9 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Umbraco.Tests.Stubs;
using Umbraco.Tests.TestHelpers;
using System.Configuration;
using Umbraco.Tests.TestHelpers.Stubs;
using Umbraco.Web;
using Umbraco.Web.PublishedCache.XmlPublishedCache;
using Umbraco.Web.Routing;

View File

@@ -19,7 +19,6 @@ using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;
using Umbraco.Tests.Stubs;
using Umbraco.Web;
using Umbraco.Web.PublishedCache;
using Umbraco.Web.PublishedCache.XmlPublishedCache;

View File

@@ -4,7 +4,7 @@ using System.Web.Routing;
using NUnit.Framework;
using Umbraco.Core.Configuration;
using Umbraco.Core.Models;
using Umbraco.Tests.Stubs;
using Umbraco.Tests.TestHelpers.Stubs;
using Umbraco.Web;
using Umbraco.Web.PublishedCache.XmlPublishedCache;
using Umbraco.Web.Routing;

View File

@@ -12,7 +12,6 @@ using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;
using Umbraco.Tests.Stubs;
using Umbraco.Web;
using Umbraco.Web.Routing;
using umbraco.BusinessLogic;

View File

@@ -1,12 +1,12 @@
using Umbraco.Web.Routing;
namespace Umbraco.Tests.Stubs
{
internal class FakeLastChanceFinder : IContentFinder
{
public bool TryFindContent(PublishedContentRequest docRequest)
{
return false;
}
}
using Umbraco.Web.Routing;
namespace Umbraco.Tests.TestHelpers.Stubs
{
internal class FakeLastChanceFinder : IContentFinder
{
public bool TryFindContent(PublishedContentRequest docRequest)
{
return false;
}
}
}

View File

@@ -1,40 +1,40 @@
using System;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.SessionState;
using Umbraco.Core;
namespace Umbraco.Tests.Stubs
{
/// <summary>
/// Used in place of the UmbracoControllerFactory which relies on BuildManager which throws exceptions in a unit test context
/// </summary>
internal class TestControllerFactory : IControllerFactory
{
public IController CreateController(RequestContext requestContext, string controllerName)
{
var types = TypeFinder.FindClassesOfType<ControllerBase>(new[] { Assembly.GetExecutingAssembly() });
var controllerTypes = types.Where(x => x.Name.Equals(controllerName + "Controller", StringComparison.InvariantCultureIgnoreCase));
var t = controllerTypes.SingleOrDefault();
if (t == null)
return null;
return Activator.CreateInstance(t) as IController;
}
public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
{
return SessionStateBehavior.Disabled;
}
public void ReleaseController(IController controller)
{
controller.DisposeIfDisposable();
}
}
using System;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.SessionState;
using Umbraco.Core;
namespace Umbraco.Tests.TestHelpers.Stubs
{
/// <summary>
/// Used in place of the UmbracoControllerFactory which relies on BuildManager which throws exceptions in a unit test context
/// </summary>
internal class TestControllerFactory : IControllerFactory
{
public IController CreateController(RequestContext requestContext, string controllerName)
{
var types = TypeFinder.FindClassesOfType<ControllerBase>(new[] { Assembly.GetExecutingAssembly() });
var controllerTypes = types.Where(x => x.Name.Equals(controllerName + "Controller", StringComparison.InvariantCultureIgnoreCase));
var t = controllerTypes.SingleOrDefault();
if (t == null)
return null;
return Activator.CreateInstance(t) as IController;
}
public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
{
return SessionStateBehavior.Disabled;
}
public void ReleaseController(IController controller)
{
controller.DisposeIfDisposable();
}
}
}

View File

@@ -144,7 +144,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AttemptTests.cs" />
<Compile Include="Auditing\AuditTests.cs" />
<Compile Include="MockTests.cs" />
<Compile Include="Persistence\Auditing\AuditTests.cs" />
<Compile Include="BootManagers\CoreBootManagerTests.cs" />
<Compile Include="BusinessLogic\DictionaryTest.cs" />
<Compile Include="Cache\CacheHelperTests.cs" />
@@ -355,7 +356,7 @@
<Compile Include="Routing\NiceUrlProviderTests.cs" />
<Compile Include="Routing\RenderRouteHandlerTests.cs" />
<Compile Include="Routing\RouteTestExtensions.cs" />
<Compile Include="Stubs\TestControllerFactory.cs" />
<Compile Include="TestHelpers\Stubs\TestControllerFactory.cs" />
<Compile Include="TestHelpers\BaseUsingSqlCeSyntax.cs" />
<Compile Include="TestHelpers\BaseWebTest.cs" />
<Compile Include="TestHelpers\BaseDatabaseTest.cs" />
@@ -391,7 +392,7 @@
<Compile Include="Resolvers\PackageActionsResolverTests.cs" />
<Compile Include="PluginManagerExtensions.cs" />
<Compile Include="PluginManagerTests.cs" />
<Compile Include="Stubs\FakeLastChanceFinder.cs" />
<Compile Include="TestHelpers\Stubs\FakeLastChanceFinder.cs" />
<Compile Include="TestHelpers\TestHelper.cs" />
<Compile Include="EnumerableExtensionsTests.cs" />
<Compile Include="IO\AbstractFileSystemTests.cs" />