Files
Umbraco-CMS/src/Umbraco.Tests.UnitTests/AutoFixture/AutoMoqDataAttribute.cs

108 lines
5.3 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Reflection;
using AutoFixture;
using AutoFixture.AutoMoq;
2020-06-25 11:22:59 +02:00
using AutoFixture.Kernel;
using AutoFixture.NUnit3;
using Microsoft.AspNetCore.Http;
2020-06-25 11:22:59 +02:00
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Moq;
2020-09-03 13:02:29 +02:00
using Umbraco.Core;
using Umbraco.Core.BackOffice;
2020-09-03 13:02:29 +02:00
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.Hosting;
2020-08-24 10:52:48 +02:00
using Umbraco.Tests.Common.Builders;
2020-06-25 11:22:59 +02:00
using Umbraco.Web.BackOffice.Controllers;
using Umbraco.Web.BackOffice.Routing;
2020-08-21 13:53:27 +02:00
using Umbraco.Web.Common.Install;
using Umbraco.Web.Common.Security;
using Umbraco.Web.WebApi;
2020-07-01 17:42:39 +02:00
namespace Umbraco.Tests.UnitTests.AutoFixture
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, AllowMultiple = true)]
public class InlineAutoMoqDataAttribute : InlineAutoDataAttribute
{
/// <summary>
/// Uses AutoFixture to automatically mock (using Moq) the injected types. E.g when injecting interfaces.
/// AutoFixture is used to generate concrete types. If the concrete type required some types injected, the
/// [Frozen] can be used to ensure the same variable is injected and available as parameter for the test
/// </summary>
public InlineAutoMoqDataAttribute(params object[] arguments) : base(() => AutoMoqDataAttribute.AutoMockCustomizations.Default, arguments)
{
}
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor)]
public class AutoMoqDataAttribute : AutoDataAttribute
{
2020-07-09 11:00:29 +02:00
/// <summary>
/// Uses AutoFixture to automatically mock (using Moq) the injected types. E.g when injecting interfaces.
/// AutoFixture is used to generate concrete types. If the concrete type required some types injected, the
/// [Frozen] can be used to ensure the same variable is injected and available as parameter for the test
/// </summary>
public AutoMoqDataAttribute() : base(() => AutoMockCustomizations.Default)
{
}
internal static class AutoMockCustomizations
{
public static IFixture Default => new Fixture().Customize(new UmbracoCustomization());
private class UmbracoCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customize<BackOfficeIdentityUser>(
u => u.FromFactory<string ,string, string>(
2020-09-21 21:06:24 +02:00
(a,b,c) => BackOfficeIdentityUser.CreateNew(new GlobalSettings(),a,b,c)));
2020-06-25 11:22:59 +02:00
fixture
.Customize(new ConstructorCustomization(typeof(UsersController), new GreedyConstructorQuery()))
2020-08-21 13:53:27 +02:00
.Customize(new ConstructorCustomization(typeof(InstallController), new GreedyConstructorQuery()))
2020-08-24 08:45:41 +02:00
.Customize(new ConstructorCustomization(typeof(PreviewController), new GreedyConstructorQuery()))
.Customize(new ConstructorCustomization(typeof(BackOfficeController), new GreedyConstructorQuery()))
.Customize(new ConstructorCustomization(typeof(BackOfficeUserManager), new GreedyConstructorQuery()));
2020-06-25 11:22:59 +02:00
fixture.Customize(new AutoMoqCustomization());
2020-07-09 11:00:29 +02:00
// When requesting an IUserStore ensure we actually uses a IUserLockoutStore
2020-06-25 11:22:59 +02:00
fixture.Customize<IUserStore<BackOfficeIdentityUser>>(cc => cc.FromFactory(() => Mock.Of<IUserLockoutStore<BackOfficeIdentityUser>>()));
2020-08-21 13:53:27 +02:00
fixture.Customize<ConfigConnectionString>(
u => u.FromFactory<string, string, string>(
(a, b, c) => new ConfigConnectionString(a, b, c)));
fixture.Customize<IUmbracoVersion>(
u => u.FromFactory(
() => new UmbracoVersion()));
fixture.Customize<BackOfficeAreaRoutes>(u => u.FromFactory(
() => new BackOfficeAreaRoutes(
Options.Create(new GlobalSettings()),
Mock.Of<IHostingEnvironment>(x => x.ToAbsolute(It.IsAny<string>()) == "/umbraco" && x.ApplicationVirtualPath == string.Empty),
Mock.Of<IRuntimeState>(x => x.Level == RuntimeLevel.Run),
new UmbracoApiControllerTypeCollection(Array.Empty<Type>()))));
fixture.Customize<PreviewRoutes>(u => u.FromFactory(
() => new PreviewRoutes(
Options.Create(new GlobalSettings()),
Mock.Of<IHostingEnvironment>(x => x.ToAbsolute(It.IsAny<string>()) == "/umbraco" && x.ApplicationVirtualPath == string.Empty),
Mock.Of<IRuntimeState>(x => x.Level == RuntimeLevel.Run))));
var connectionStrings = new ConnectionStrings();
fixture.Customize<ConnectionStrings>(x => x.FromFactory(() => connectionStrings ));
2020-06-25 11:22:59 +02:00
var httpContextAccessor = new HttpContextAccessor { HttpContext = new DefaultHttpContext() };
fixture.Customize<HttpContext>(x => x.FromFactory(() => httpContextAccessor.HttpContext));
fixture.Customize<IHttpContextAccessor>(x => x.FromFactory(() => httpContextAccessor));
2020-06-25 11:22:59 +02:00
}
}
}
}
}