// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. namespace System.Data.Entity.Config { using Moq; using Xunit; public class InternalConfigurationTests { public class AddAppConfigResolver { [Fact] public void AddAppConfigResolver_adds_a_resolver_to_the_app_config_chain() { var mockAppConfigChain = new Mock(); var resolver = new Mock().Object; new InternalConfiguration( mockAppConfigChain.Object, new Mock().Object, new RootDependencyResolver()). AddAppConfigResolver(resolver); mockAppConfigChain.Verify(m => m.Add(resolver)); } } public class RegisterSingleton { [Fact] public void Adds_a_singleton_resolver() { var mockNormalChain = new Mock(); new InternalConfiguration( new Mock().Object, mockNormalChain.Object, new RootDependencyResolver()).RegisterSingleton(new object(), null); mockNormalChain.Verify(m => m.Add(It.IsAny>())); } } public class GetService { [Fact] public void Queries_resolvers_for_service() { var mockNormalChain = new Mock(); new InternalConfiguration( new Mock().Object, mockNormalChain.Object, new RootDependencyResolver()).GetService(42); mockNormalChain.Verify(m => m.GetService(typeof(object), 42)); } } public class AddDependencyResolver { [Fact] public void AddDependencyResolver_adds_a_resolver_to_the_normal_chain() { var mockNormalChain = new Mock(); var resolver = new Mock().Object; new InternalConfiguration( new Mock().Object, mockNormalChain.Object, new RootDependencyResolver()).AddDependencyResolver(resolver); mockNormalChain.Verify(m => m.Add(resolver)); } [Fact] public void AddDependencyResolver_adds_a_resolver_to_the_app_config_chain_when_override_flag_is_used() { var mockAppConfigChain = new Mock(); var resolver = new Mock().Object; new InternalConfiguration( mockAppConfigChain.Object, new Mock().Object, new RootDependencyResolver()).AddDependencyResolver(resolver, overrideConfigFile: true); mockAppConfigChain.Verify(m => m.Add(resolver)); } } public class DependencyResolver { [Fact] public void DependencyResolver_returns_the_dependency_resolver_in_use() { var mockAppConfigChain = new Mock(); var mockNormalChain = new Mock(); var config = new InternalConfiguration( mockAppConfigChain.Object, mockNormalChain.Object, new RootDependencyResolver()); var resolver = (CompositeResolver)config.DependencyResolver; Assert.Same(mockAppConfigChain.Object, resolver.First); Assert.Same(mockNormalChain.Object, resolver.Second); } } public class RootResolver { [Fact] public void RootResolver_returns_the_root_resolver() { var rootResolver = new RootDependencyResolver(); var config = new InternalConfiguration(new Mock().Object, new Mock().Object, rootResolver); Assert.Same(rootResolver, config.RootResolver); } [Fact] public void RootResolver_is_added_to_the_non_app_config_resolver_chain() { var normalChain = new ResolverChain(); var mockRootResolver = new Mock(); new InternalConfiguration(new Mock().Object, normalChain, mockRootResolver.Object); normalChain.GetService("Foo"); mockRootResolver.Verify(m => m.GetService(typeof(object), "Foo")); } } public class SwitchInRootResolver { [Fact] public void SwitchInRootResolver_swicthes_in_given_root_resolver() { var configuration = new DbConfiguration().InternalConfiguration; var mockRootResolver = new Mock(); configuration.SwitchInRootResolver(mockRootResolver.Object); Assert.Same(mockRootResolver.Object, configuration.RootResolver); configuration.DependencyResolver.GetService("Foo"); mockRootResolver.Verify(m => m.GetService(typeof(object), "Foo")); } [Fact] public void SwitchInRootResolver_leaves_other_resolvers_intact() { var configuration = new DbConfiguration().InternalConfiguration; var mockResolver = new Mock(); configuration.AddDependencyResolver(mockResolver.Object); configuration.SwitchInRootResolver(new Mock().Object); configuration.DependencyResolver.GetService("Foo"); mockResolver.Verify(m => m.GetService(typeof(object), "Foo")); } } public class ResolverSnapshot { [Fact] public void ResolverSnapshot_returns_copy_of_resolver_chain() { var configuration = new DbConfiguration().InternalConfiguration; var resolver1 = new Mock(); configuration.AddDependencyResolver(resolver1.Object); var snapshot = configuration.ResolverSnapshot; var resolver2 = new Mock(); configuration.AddDependencyResolver(resolver2.Object); snapshot.GetService("Foo"); resolver1.Verify(m => m.GetService(typeof(object), "Foo"), Times.Once()); resolver2.Verify(m => m.GetService(typeof(object), "Foo"), Times.Never()); } [Fact] public void ResolverSnapshot_returns_resolvers_in_correct_order() { var callOrder = ""; var configResolver = new Mock(); configResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Config"); var configChain = new ResolverChain(); configChain.Add(configResolver.Object); var rootResolver = new Mock(); rootResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Root"); var configuration = new DbConfiguration(new InternalConfiguration(configChain, new ResolverChain(), rootResolver.Object)).InternalConfiguration; var normalResolver = new Mock(); normalResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Normal"); configuration.AddDependencyResolver(normalResolver.Object); var overrideResolver = new Mock(); overrideResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Override"); configuration.AddDependencyResolver(overrideResolver.Object, overrideConfigFile: true); configuration.ResolverSnapshot.GetService("Foo"); Assert.Equal(" Override Config Normal Root", callOrder); } } } }