public void create_order_invokes_factory_create_order(
            OrderData data)
        {
            var cart = new CartDataBuilder()
                .WithId(data.CartId)
                .Build();

            var user = new UserDataBuilder()
                .WithId(data.UserId)
                .Build();

            cartRepository.GetCartById(data.CartId)
                .Returns(cart);

            userRepository.GetUserById(data.UserId)
                .Returns(user);

            sut.CreateOrder(data);

            factory.Received()
                .CreateOrder(user,
                             cart,
                             data.Address,
                             data.City,
                             data.Zip);
        }
        public void save_invoke_session_save(
            [Frozen]ISession session,
            CartNHibRepository sut)
        {
            var cart = new CartDataBuilder()
                .Build();

            sut.Save(cart);

            session.Received()
                .Save(cart);
        }
        public void save_new_returns_cart_with_new_id()
        {
            var cart = new CartDataBuilder()
                .WithEmptyId()
                .Build();
            var sut = new Global(Consts.TEST_APP_DATA)
                    .GetKernel().Get<ICartRepository>();

            var actual = sut.Save(cart);

            actual.Id.Should().NotBeEmpty();
        }
        public void get_by_id_returns_cart_with_corresponding_id()
        {
            var cart = new CartDataBuilder()
                .WithEmptyId()
                .Build();

            var sut = new Global(Consts.TEST_APP_DATA)
                    .GetKernel().Get<ICartRepository>();

            var savedCart = sut.Save(cart);

            var actual = sut.GetCartById(savedCart.Id.Value);

            actual
                .ShouldBeEquivalentTo(savedCart);
        }
        public void order_factory_should_order_with_proper_user_and_order_items(
            OrderFactory sut,
            User user,
            string address,
            string city,
            string zip)
        {
            var products = new ProductDataFactory()
                .CreateSavedProductsList(5);

            var cart = new CartDataBuilder()
                .WithSomeId()
                .WithProducts(products)
                .Build();

            var actual = sut.CreateOrder(user,
                                         cart,
                                         address,
                                         city,
                                         zip);

            actual
                .Should()
                .NotBeNull();

            actual
                .Should()
                .BeOfType<ShopOrder>();

            actual.User
                .ShouldBeEquivalentTo(user);
            actual.OrderItems
                .ShouldAllBeEquivalentTo(cart.Items);

            actual.Address
                .Should().Be(address);
            actual.City
                .Should().Be(city);
            actual.Zip
               .Should().Be(zip);
        }
        public void add_product_to_cart_returns_cart_containing_proprt_cart_item(
            Guid cartId,
            Product product)
        {
            var productId = product.Id.Value;

            productRepository.GetById(productId)
                .Returns(product);

            var cart = new CartDataBuilder()
                .WithId(cartId)
                .Build();

            cartRepository.GetCartById(cartId)
                      .Returns(cart);

            var actual = sut.AddProductToCart(cartId, productId);

            actual.Items
                .Should()
                .Contain(item => item.Product == product);
        }
        public void sucessful_scenario_of_order_creation(
            OrderData data)
        {
            // Arrange
            var products = new ProductDataFactory()
                .CreateSavedProductsList(3);
            productRepository.Save(products);

            var cart = new CartDataBuilder()
                .WithProducts(products)
                .Build();
            cartRepository.Save(cart);

            var user = new UserDataBuilder()
                .WithId(data.UserId)
                .Build();
            userRepository.CreateUser(user);

            data.CartId = cart.Id.Value;
            data.UserId = user.Id;

            //Act
            var actual = sut.CreateOrder(data);

            //Assert
            actual.User
                .ShouldBeEquivalentTo(user);
            //actual.OrderItems
            //    .ShouldAllBeEquivalentTo(cart.Items);

            actual.Address
                .Should().Be(data.Address);
            actual.City
                .Should().Be(data.City);
            actual.Zip
               .Should().Be(data.Zip);
        }
        public void add_product_to_empty_cart_invoke_repo_save(
            Guid productId)
        {
            var cart = new CartDataBuilder()
                .Build();

            factory.CreateCart()
                .Returns(cart);

            sut.AddProductToCart(null, productId);

            cartRepository.Received()
                      .Save(cart);
        }
        public void get_subtotal_for_not_empty_cart_returns_sum_of_products_prices(
            Guid cartId)
        {
            var products = new ProductDataFactory()
                .CreateSavedProductsList(3);

            var cart = new CartDataBuilder()
                .WithId(cartId)
                .WithProducts(products)
                .Build();

            cartRepository.GetCartById(cartId)
                      .Returns(cart);

            var expected = products.Sum(p => p.Price);

            sut.GetSubtotal(cartId)
                .Should()
                .Be(expected);
        }
        public void get_subtotal_for_empty_cart_returns_0(
            Guid cartId)
        {
            var cart = new CartDataBuilder()
                .WithId(cartId)
                .Build();

            cartRepository.GetCartById(cartId)
                      .Returns(cart);

            sut.GetSubtotal(cartId)
                .Should()
                .Be(0);
        }
        public void get_subtotal_for_cart_containing_same_products_returns_correct_sum(
            Guid cartId,
            Product multipleProduct,
            Product product)
        {
            var cart = new CartDataBuilder()
                .WithId(cartId)
                .WithProduct(multipleProduct, 2)
                .WithProduct(product)
                .Build();

            cartRepository.GetCartById(cartId)
                      .Returns(cart);

            var expected = multipleProduct.Price * 2 + product.Price;

            sut.GetSubtotal(cartId)
                .Should()
                .Be(expected);
        }
        public void add_product_to_not_empty_cart_not_invoke_factory_create_cart(
            Guid cartId,
            Product product)
        {
            var cart = new CartDataBuilder()
                .WithId(cartId)
                .WithProduct(product)
                .Build();

            cartRepository.GetCartById(cartId)
                      .Returns(cart);

            sut.AddProductToCart(cartId, product.Id.Value);

            factory.DidNotReceive()
                   .CreateCart();
        }
        public void add_product_to_nonempty_cart_invoke_repo_get_by_id(
            Product product,
            Guid cartId)
        {
            var cart = new CartDataBuilder()
                .WithId(cartId)
                .Build();

            cartRepository.GetCartById(cartId)
                      .Returns(cart);

            sut.AddProductToCart(cartId, product.Id.Value);

            cartRepository.Received()
                      .GetCartById(cartId);
        }
        public void add_product_to_existung_cart_invoke_repo_save(
            Guid productId,
            Guid cartId)
        {
            var cart = new CartDataBuilder()
                .WithId(cartId)
                .Build();

            cartRepository.GetCartById(cartId)
                      .Returns(cart);

            sut.AddProductToCart(cartId, productId);

            cartRepository.Received()
                      .Save(cart);
        }
        public void post_product_to_empty_cart_returns_cart_with_this_product(
            Product product)
        {
            var cart = new CartDataBuilder()
                .WithProduct(product)
                .Build();

            service.AddProductToCart(null, product.Id.Value)
                .Returns(cart);

            var data = new AddToCartData
            {
                CartId = null,
                ProductId = product.Id.Value
            };

            sut.Post(data)
                .Items
                .Should()
                .Contain(item => item.Product == product);
        }