public void LazyCollectionBuilderHandlesTypesAndProducers()
    {
        var container   = CreateRegister();
        var composition = new UmbracoBuilder(container, Mock.Of <IConfiguration>(), TestHelper.GetMockedTypeLoader());

        composition.WithCollectionBuilder <TestCollectionBuilder>()
        .Add <TransientObject3>()
        .Add <TransientObject2>()
        .Add <TransientObject3>()
        .Add(() => new[] { typeof(TransientObject1) });

        var factory = composition.CreateServiceProvider();

        var values = factory.GetRequiredService <TestCollection>();

        Assert.AreEqual(3, values.Count());
        Assert.IsTrue(values.Select(x => x.GetType())
                      .ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) }));

        var other = factory.GetRequiredService <TestCollection>();

        Assert.AreNotSame(values, other); // transient
        var o1 = other.FirstOrDefault(x => x is TransientObject1);

        Assert.IsFalse(values.Contains(o1)); // transient
    }
    public void LazyCollectionBuilderThrowsOnIllegalTypes()
    {
        var container   = CreateRegister();
        var composition = new UmbracoBuilder(container, Mock.Of <IConfiguration>(), TestHelper.GetMockedTypeLoader());

        composition.WithCollectionBuilder <TestCollectionBuilder>()
        .Add <TransientObject3>()

        // illegal, does not implement the interface!
        ////.Add<TransientObject4>()

        // legal so far...
        .Add(() => new[] { typeof(TransientObject4) });

        Assert.Throws <InvalidOperationException>(() =>
        {
            // but throws here when trying to register the types, right before creating the factory
            var factory = composition.CreateServiceProvider();
        });
    }
        public virtual void Initialize()
        {
            var services = TestHelper.GetRegister();

            var ioHelper   = TestHelper.IOHelper;
            var logger     = new ProfilingLogger(Mock.Of <ILogger <ProfilingLogger> >(), Mock.Of <IProfiler>());
            var typeFinder = TestHelper.GetTypeFinder();
            var typeLoader = new TypeLoader(typeFinder, NoAppCache.Instance,
                                            new DirectoryInfo(ioHelper.MapPath(Constants.SystemDirectories.TempData)),
                                            Mock.Of <ILogger <TypeLoader> >(),
                                            logger,
                                            false);

            var composition = new UmbracoBuilder(services, Mock.Of <IConfiguration>(), TestHelper.GetMockedTypeLoader());


            services.AddUnique <ILogger>(_ => Mock.Of <ILogger>());
            services.AddUnique <ILoggerFactory>(_ => NullLoggerFactory.Instance);
            services.AddUnique <IProfiler>(_ => Mock.Of <IProfiler>());
            services.AddUnique(typeLoader);

            composition.WithCollectionBuilder <MapperCollectionBuilder>()
            .AddCoreMappers();

            services.AddUnique <ISqlContext>(_ => SqlContext);

            var factory = Current.Factory = TestHelper.CreateServiceProvider(composition);

            var pocoMappers = new NPoco.MapperCollection {
                new PocoMapper()
            };
            var pocoDataFactory = new FluentPocoDataFactory((type, iPocoDataFactory) => new PocoDataBuilder(type, pocoMappers).Init());
            var sqlSyntax       = new SqlCeSyntaxProvider(Options.Create(new GlobalSettings()));

            SqlContext = new SqlContext(sqlSyntax, DatabaseType.SQLCe, pocoDataFactory, new Lazy <IMapperCollection>(() => factory.GetRequiredService <IMapperCollection>()));
            Mappers    = factory.GetRequiredService <IMapperCollection>();

            SetUp();
        }
Esempio n. 4
0
        public void Requires2B()
        {
            IServiceCollection register   = MockRegister();
            TypeLoader         typeLoader = MockTypeLoader();
            IServiceProvider   factory    = MockFactory();
            var composition = new UmbracoBuilder(register, Mock.Of <IConfiguration>(), TestHelper.GetMockedTypeLoader());

            Type[] types     = new[] { typeof(Composer9), typeof(Composer2), typeof(Composer4) };
            var    composers = new ComposerGraph(composition, types, Enumerable.Empty <Attribute>(), Mock.Of <ILogger <ComposerGraph> >());

            Composed.Clear();
            composers.Compose();
            ComponentCollectionBuilder builder = composition.WithCollectionBuilder <ComponentCollectionBuilder>();

            builder.RegisterWith(register);
            ComponentCollection components = builder.CreateCollection(factory);

            Assert.AreEqual(3, Composed.Count);
            Assert.AreEqual(typeof(Composer4), Composed[0]);
            Assert.AreEqual(typeof(Composer2), Composed[1]);
            Assert.AreEqual(typeof(Composer9), Composed[2]);
        }
Esempio n. 5
0
        public virtual void Setup()
        {
            IServiceCollection container  = TestHelper.GetServiceCollection();
            TypeLoader         typeLoader = TestHelper.GetMockedTypeLoader();

            var composition = new UmbracoBuilder(container, Mock.Of <IConfiguration>(), TestHelper.GetMockedTypeLoader());

            composition.WithCollectionBuilder <MapperCollectionBuilder>()
            .AddCoreMappers();

            composition.Services.AddUnique(_ => SqlContext);

            IServiceProvider factory = composition.CreateServiceProvider();
            var pocoMappers          = new NPoco.MapperCollection
            {
                new NullableDateMapper()
            };
            var pocoDataFactory = new FluentPocoDataFactory((type, iPocoDataFactory) => new PocoDataBuilder(type, pocoMappers).Init());
            var sqlSyntax       = new SqlServerSyntaxProvider(Options.Create(new GlobalSettings()));

            SqlContext = new SqlContext(sqlSyntax, DatabaseType.SqlServer2012, pocoDataFactory, factory.GetRequiredService <IMapperCollection>());
            Mappers    = factory.GetRequiredService <IMapperCollection>();
        }
Esempio n. 6
0
        public void Boot1A()
        {
            IServiceCollection register = MockRegister();
            var composition             = new UmbracoBuilder(register, Mock.Of <IConfiguration>(), TestHelper.GetMockedTypeLoader());

            Type[] types     = TypeArray <Composer1, Composer2, Composer4>();
            var    composers = new ComposerGraph(composition, types, Enumerable.Empty <Attribute>(), Mock.Of <ILogger <ComposerGraph> >());

            Composed.Clear();

            // 2 is Core and requires 4
            // 3 is User
            // => reorder components accordingly
            composers.Compose();
            AssertTypeArray(TypeArray <Composer1, Composer4, Composer2>(), Composed);

            IServiceProvider factory = MockFactory(m =>
            {
                m.Setup(x => x.GetService(It.Is <Type>(t => t == typeof(ISomeResource)))).Returns(() => new SomeResource());
                m.Setup(x => x.GetService(It.IsAny <Type>())).Returns <Type>((type) =>
                {
                    if (type == typeof(Composer1))
                    {
                        return(new Composer1());
                    }

                    if (type == typeof(Composer5))
                    {
                        return(new Composer5());
                    }

                    if (type == typeof(Component5))
                    {
                        return(new Component5(new SomeResource()));
                    }

                    if (type == typeof(IProfilingLogger))
                    {
                        return(new ProfilingLogger(Mock.Of <ILogger <ProfilingLogger> >(), Mock.Of <IProfiler>()));
                    }

                    if (type == typeof(ILogger <ComponentCollection>))
                    {
                        return(Mock.Of <ILogger <ComponentCollection> >());
                    }

                    throw new NotSupportedException(type.FullName);
                });
            });

            ComponentCollectionBuilder builder = composition.WithCollectionBuilder <ComponentCollectionBuilder>();

            builder.RegisterWith(register);
            ComponentCollection components = builder.CreateCollection(factory);

            Assert.IsEmpty(components);
            components.Initialize();
            Assert.IsEmpty(Initialized);
            components.Terminate();
            Assert.IsEmpty(Terminated);
        }
Esempio n. 7
0
        public void SimpleConverter3Test()
        {
            var register = new ServiceCollection();

            var composition = new UmbracoBuilder(register, Mock.Of <IConfiguration>(), TestHelper.GetMockedTypeLoader());

            composition.WithCollectionBuilder <PropertyValueConverterCollectionBuilder>()
            .Append <SimpleConverter3A>()
            .Append <SimpleConverter3B>();

            IPublishedModelFactory factory = new PublishedModelFactory(
                new[]
            {
                typeof(PublishedSnapshotTestObjects.TestElementModel1),
                typeof(PublishedSnapshotTestObjects.TestElementModel2),
                typeof(PublishedSnapshotTestObjects.TestContentModel1),
                typeof(PublishedSnapshotTestObjects.TestContentModel2)
            }, Mock.Of <IPublishedValueFallback>());

            register.AddTransient(f => factory);

            var cacheMock    = new Mock <IPublishedContentCache>();
            var cacheContent = new Dictionary <int, IPublishedContent>();

            cacheMock.Setup(x => x.GetById(It.IsAny <int>())).Returns <int>(id =>
                                                                            cacheContent.TryGetValue(id, out IPublishedContent content) ? content : null);
            var publishedSnapshotMock = new Mock <IPublishedSnapshot>();

            publishedSnapshotMock.Setup(x => x.Content).Returns(cacheMock.Object);
            var publishedSnapshotAccessorMock = new Mock <IPublishedSnapshotAccessor>();
            var localPublishedSnapshot        = publishedSnapshotMock.Object;

            publishedSnapshotAccessorMock.Setup(x => x.TryGetPublishedSnapshot(out localPublishedSnapshot)).Returns(true);
            register.AddTransient(f => publishedSnapshotAccessorMock.Object);

            IServiceProvider registerFactory            = composition.CreateServiceProvider();
            PropertyValueConverterCollection converters =
                registerFactory.GetRequiredService <PropertyValueConverterCollection>();

            var serializer          = new ConfigurationEditorJsonSerializer();
            var dataTypeServiceMock = new Mock <IDataTypeService>();
            var dataType1           = new DataType(
                new VoidEditor(
                    Mock.Of <IDataValueEditorFactory>()),
                serializer)
            {
                Id = 1
            };
            var dataType2 = new DataType(
                new VoidEditor(
                    "2",
                    Mock.Of <IDataValueEditorFactory>()),
                serializer)
            {
                Id = 2
            };

            dataTypeServiceMock.Setup(x => x.GetAll()).Returns(new[] { dataType1, dataType2 });

            var contentTypeFactory = new PublishedContentTypeFactory(factory, converters, dataTypeServiceMock.Object);

            IEnumerable <IPublishedPropertyType> CreatePropertyTypes(IPublishedContentType contentType, int i)
            {
                yield return(contentTypeFactory.CreatePropertyType(contentType, "prop" + i, i));
            }

            IPublishedContentType elementType1 =
                contentTypeFactory.CreateContentType(Guid.NewGuid(), 1000, "element1", t => CreatePropertyTypes(t, 1));
            IPublishedContentType elementType2 =
                contentTypeFactory.CreateContentType(Guid.NewGuid(), 1001, "element2", t => CreatePropertyTypes(t, 2));
            IPublishedContentType contentType1 =
                contentTypeFactory.CreateContentType(Guid.NewGuid(), 1002, "content1", t => CreatePropertyTypes(t, 1));
            IPublishedContentType contentType2 =
                contentTypeFactory.CreateContentType(Guid.NewGuid(), 1003, "content2", t => CreatePropertyTypes(t, 2));

            var element1 = new PublishedElement(
                elementType1,
                Guid.NewGuid(),
                new Dictionary <string, object> {
                { "prop1", "val1" }
            },
                false);
            var element2 = new PublishedElement(
                elementType2,
                Guid.NewGuid(),
                new Dictionary <string, object> {
                { "prop2", "1003" }
            },
                false);
            var cnt1 = new InternalPublishedContent(contentType1)
            {
                Id         = 1003,
                Properties = new[]
                {
                    new InternalPublishedProperty {
                        Alias = "prop1", SolidHasValue = true, SolidValue = "val1"
                    }
                }
            };
            var cnt2 = new InternalPublishedContent(contentType1)
            {
                Id         = 1004,
                Properties = new[]
                {
                    new InternalPublishedProperty {
                        Alias = "prop2", SolidHasValue = true, SolidValue = "1003"
                    }
                }
            };

            IPublishedModelFactory publishedModelFactory = registerFactory.GetRequiredService <IPublishedModelFactory>();

            cacheContent[cnt1.Id] = cnt1.CreateModel(publishedModelFactory);
            cacheContent[cnt2.Id] = cnt2.CreateModel(publishedModelFactory);

            // can get the actual property Clr type
            // ie ModelType gets properly mapped by IPublishedContentModelFactory
            // must test ModelClrType with special equals 'cos they are not ref-equals
            Assert.IsTrue(ModelType.Equals(
                              typeof(IEnumerable <>).MakeGenericType(ModelType.For("content1")),
                              contentType2.GetPropertyType("prop2").ModelClrType));
            Assert.AreEqual(
                typeof(IEnumerable <PublishedSnapshotTestObjects.TestContentModel1>),
                contentType2.GetPropertyType("prop2").ClrType);

            // can create a model for an element
            IPublishedElement model1 = factory.CreateModel(element1);

            Assert.IsInstanceOf <PublishedSnapshotTestObjects.TestElementModel1>(model1);
            Assert.AreEqual("val1", ((PublishedSnapshotTestObjects.TestElementModel1)model1).Prop1);

            // can create a model for a published content
            IPublishedElement model2 = factory.CreateModel(element2);

            Assert.IsInstanceOf <PublishedSnapshotTestObjects.TestElementModel2>(model2);
            var mmodel2 = (PublishedSnapshotTestObjects.TestElementModel2)model2;

            // and get direct property
            Assert.IsInstanceOf <PublishedSnapshotTestObjects.TestContentModel1[]>(
                model2.Value(Mock.Of <IPublishedValueFallback>(), "prop2"));
            Assert.AreEqual(
                1,
                ((PublishedSnapshotTestObjects.TestContentModel1[])model2.Value(Mock.Of <IPublishedValueFallback>(), "prop2")).Length);

            // and get model property
            Assert.IsInstanceOf <IEnumerable <PublishedSnapshotTestObjects.TestContentModel1> >(mmodel2.Prop2);
            Assert.IsInstanceOf <PublishedSnapshotTestObjects.TestContentModel1[]>(mmodel2.Prop2);
            PublishedSnapshotTestObjects.TestContentModel1 mmodel1 = mmodel2.Prop2.First();

            // and we get what we want
            Assert.AreSame(cacheContent[mmodel1.Id], mmodel1);
        }