Ejemplo n.º 1
0
        public void Init()
        {
            var builder = new ContainerBuilder();

            // register the stuff we'll need
            // we mock the INotifier: we don't really need to test that anyway
            builder.RegisterInstance(new Mock <INotifier>().Object).As <INotifier>();
            // register the deafault GDPR provider
            builder.RegisterType <GDPRKeyFeaturesProvider>().As <IKeyFeaturesProvider>();
            // register the provider from CommunicationGateway
            builder.RegisterType <ContactGDPRKeyFeaturesProvider>().As <IKeyFeaturesProvider>();
            // register the providers from the Mobile module
            builder.RegisterType <MobileGDPRKeyFeaturesProvider>().As <IKeyFeaturesProvider>();
            builder.RegisterType <SmsGatewayGDPRKeyFeaturesProvider>().As <IKeyFeaturesProvider>();
            builder.RegisterType <SmsExtensionGDPRKeyFeaturesProvider>().As <IKeyFeaturesProvider>();
            builder.RegisterType <PushGatewayGDPRKeyFeaturesProvider>().As <IKeyFeaturesProvider>();
            // register the providers for the OpenAuthentication module
            builder.RegisterType <OpenAuthKeyFeaturesProvider>().As <IKeyFeaturesProvider>();

            // We mock the IFeatureManager: normally, when there is a request to enable a feature it goes through
            // a bunch of methods before it's actually enabled and the events raised. Since we want to test the behaviour
            // of the event handler and the related provider, we mock all that away.
            var featureManager = new Mock <IFeatureManager>();

            //
            featureManager.Setup(fm => fm.GetDisabledFeatures())
            .Returns(() => {
                var enabled = EnabledFeatures ?? new List <string>();
                return(AllKeyFeaturesForTheTest().Where(fd => !enabled.Contains(fd.Id)));
            });

            featureManager.Setup(fm => fm.EnableFeatures(It.IsAny <IEnumerable <string> >()))
            .Returns <IEnumerable <string> >(names => {
                EnabledFeatures = EnabledFeatures ?? new List <string>();
                EnabledFeatures.AddRange(names);
                var handler = _container.Resolve <IFeatureEventHandler>();
                foreach (var name in names)
                {
                    handler.Enabled(new Feature()
                    {
                        Descriptor = new FeatureDescriptor {
                            Id = name
                        }
                    });
                }
                return(names);
            });

            _featureManager = featureManager.Object;
            builder.RegisterInstance(_featureManager).As <IFeatureManager>();

            // Register the handler we are testing
            builder.RegisterType <GDPRFeaturesEventHandler>().As <IFeatureEventHandler>();

            EnabledFeatures = new List <string>();

            _container = builder.Build();
        }