// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace System.Data.Entity.Infrastructure.DependencyResolution
{
    using System.Linq;
    using Moq;
    using Xunit;

    public class DbDependencyResolverExtensionsTests
    {
        public interface IPilkington
        {
        }

        [Fact]
        public void Generic_GetService_with_name_calls_resolver_with_correct_type_and_name()
        {
            var karl = new Mock<IPilkington>().Object;
            var mockResolver = CreateMockResolver(karl);

            Assert.Same(karl, mockResolver.Object.GetService<IPilkington>("Karl"));

            mockResolver.Verify(m => m.GetService(typeof(IPilkington), "Karl"), Times.Once());
        }

        [Fact]
        public void Generic_GetService_without_name_calls_resolver_with_correct_type_and_null_name()
        {
            var karl = new Mock<IPilkington>().Object;
            var mockResolver = CreateMockResolver(karl);

            Assert.Same(karl, mockResolver.Object.GetService<IPilkington>());

            mockResolver.Verify(m => m.GetService(typeof(IPilkington), null), Times.Once());
        }

        [Fact]
        public void Non_generic_GetService_without_name_calls_resolver_with_given_type_and_null_name()
        {
            var karl = new Mock<IPilkington>().Object;
            var mockResolver = CreateMockResolver(karl);

            Assert.Same(karl, mockResolver.Object.GetService(typeof(IPilkington)));

            mockResolver.Verify(m => m.GetService(typeof(IPilkington), null), Times.Once());
        }

        [Fact]
        public void Generic_GetServices_with_name_calls_resolver_with_correct_type_and_name()
        {
            var karl = new Mock<IPilkington>().Object;
            var mockResolver = CreateMockResolver(karl);

            Assert.Same(karl, mockResolver.Object.GetServices<IPilkington>("Karl").Single());

            mockResolver.Verify(m => m.GetServices(typeof(IPilkington), "Karl"), Times.Once());
        }

        [Fact]
        public void Generic_GetServices_without_name_calls_resolver_with_correct_type_and_null_name()
        {
            var karl = new Mock<IPilkington>().Object;
            var mockResolver = CreateMockResolver(karl);

            Assert.Same(karl, mockResolver.Object.GetServices<IPilkington>().Single());

            mockResolver.Verify(m => m.GetServices(typeof(IPilkington), null), Times.Once());
        }

        [Fact]
        public void Non_generic_GetServices_without_name_calls_resolver_with_given_type_and_null_name()
        {
            var karl = new Mock<IPilkington>().Object;
            var mockResolver = CreateMockResolver(karl);

            Assert.Same(karl, mockResolver.Object.GetServices(typeof(IPilkington)).Single());

            mockResolver.Verify(m => m.GetServices(typeof(IPilkington), null), Times.Once());
        }

        [Fact]
        public void Get_methods_verify_resolver_and_type_are_non_null()
        {
            Assert.Equal(
                "resolver",
                Assert.Throws<ArgumentNullException>(() => DbDependencyResolverExtensions.GetService<IPilkington>(null, "Karl")).ParamName);

            Assert.Equal(
                "resolver",
                Assert.Throws<ArgumentNullException>(() => DbDependencyResolverExtensions.GetService<IPilkington>(null)).ParamName);

            Assert.Equal(
                "resolver",
                Assert.Throws<ArgumentNullException>(() => DbDependencyResolverExtensions.GetService(null, typeof(IPilkington))).ParamName);

            Assert.Equal(
                "resolver",
                Assert.Throws<ArgumentNullException>(() => DbDependencyResolverExtensions.GetServices<IPilkington>(null, "Karl")).ParamName);

            Assert.Equal(
                "resolver",
                Assert.Throws<ArgumentNullException>(() => DbDependencyResolverExtensions.GetServices<IPilkington>(null)).ParamName);

            Assert.Equal(
                "resolver",
                Assert.Throws<ArgumentNullException>(() => DbDependencyResolverExtensions.GetServices(null, typeof(IPilkington))).ParamName);

            Assert.Equal(
                "type",
                Assert.Throws<ArgumentNullException>(() => new Mock<IDbDependencyResolver>().Object.GetService(null)).ParamName);

            Assert.Equal(
                "type",
                Assert.Throws<ArgumentNullException>(() => new Mock<IDbDependencyResolver>().Object.GetServices(null)).ParamName);
        }

        private static Mock<IDbDependencyResolver> CreateMockResolver(IPilkington karl)
        {
            var mockResolver = new Mock<IDbDependencyResolver>();
            mockResolver.Setup(m => m.GetService(It.IsAny<Type>(), It.IsAny<string>())).Returns(karl);
            mockResolver.Setup(m => m.GetServices(It.IsAny<Type>(), It.IsAny<string>())).Returns(new object[] { karl });

            return mockResolver;
        }
    }
}
