using System.Reflection; using AutoFixture; using AutoFixture.Kernel; using AutoMapper; using Bit.Core.Settings; using Bit.Infrastructure.EFIntegration.Test.Helpers; using Bit.Infrastructure.EntityFramework.AdminConsole.Models; using Bit.Infrastructure.EntityFramework.AdminConsole.Models.Provider; using Bit.Infrastructure.EntityFramework.Auth.Models; using Bit.Infrastructure.EntityFramework.Models; using Bit.Infrastructure.EntityFramework.Repositories; using Bit.Infrastructure.EntityFramework.Vault.Models; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NSubstitute; namespace Bit.Infrastructure.EFIntegration.Test.AutoFixture; internal class ServiceScopeFactoryBuilder : ISpecimenBuilder { private DbContextOptions _options { get; set; } public ServiceScopeFactoryBuilder(DbContextOptions options) { _options = options; } public object Create(object request, ISpecimenContext context) { var fixture = new Fixture(); var serviceProvider = Substitute.For(); var dbContext = new DatabaseContext(_options); serviceProvider.GetService(typeof(DatabaseContext)).Returns(dbContext); var serviceScope = Substitute.For(); serviceScope.ServiceProvider.Returns(serviceProvider); var serviceScopeFactory = Substitute.For(); serviceScopeFactory.CreateScope().Returns(serviceScope); return serviceScopeFactory; } } public class EfRepositoryListBuilder : ISpecimenBuilder where T : BaseEntityFrameworkRepository { public object Create(object request, ISpecimenContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var t = request as ParameterInfo; if (t == null || t.ParameterType != typeof(List)) { return new NoSpecimen(); } var list = new List(); foreach (var option in DatabaseOptionsFactory.Options) { var fixture = new Fixture(); fixture.Customize(x => x.FromFactory(new ServiceScopeFactoryBuilder(option))); fixture.Customize(x => x.FromFactory(() => new MapperConfiguration(cfg => { cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); cfg.AddProfile(); }) .CreateMapper())); fixture.Customize>(x => x.FromFactory(() => Substitute.For>())); var repo = fixture.Create(); list.Add(repo); } return list; } } public class IgnoreVirtualMembersCustomization : ISpecimenBuilder { public object Create(object request, ISpecimenContext context) { if (context == null) { throw new ArgumentNullException("context"); } var pi = request as PropertyInfo; if (pi == null) { return new NoSpecimen(); } if (pi.GetGetMethod().IsVirtual && pi.DeclaringType != typeof(GlobalSettings)) { return null; } return new NoSpecimen(); } }