Esempio n. 1
0
        public RedirectToRouteResult RemoveFromCart(Cart cart, int productId, string returnUrl)
        {
            Product product = this._repository.Products.FirstOrDefault(p => p.ProductId == productId);

            if (product != null)
            {
                cart.RemoveLine(product);
            }

            return RedirectToAction("Index", new { returnUrl });
        }
Esempio n. 2
0
        public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl)
        {
            Product product = this._repository.Products.FirstOrDefault(p => p.ProductId == productId);

            if (product != null)
            {
                cart.AddItem(product, 1);
            }

            return RedirectToAction("Index", new { returnUrl });
        }
Esempio n. 3
0
        public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
        {
            if (!cart.Lines.Any())
            {
                ModelState.AddModelError(string.Empty, Resources.EmptyCartError);
            }

            if (ModelState.IsValid)
            {
                _cartApplicationService.Process(cart, shippingDetails);
                return View("Completed");
            }

            return View(shippingDetails);
        }
Esempio n. 4
0
        public object BindModel(ControllerContext controllerContext,
                                ModelBindingContext bindingContext)
        {
            // get the Cart from the session

            // ReSharper disable PossibleNullReferenceException
            var cart = (Cart) controllerContext.HttpContext.Session[SessionKey];
            // ReSharper restore PossibleNullReferenceException

            if (cart == null)
            {
                cart = new Cart();
                controllerContext.HttpContext.Session[SessionKey] = cart;
            }
            return cart;
        }
Esempio n. 5
0
        public void CanAddNewLines()
        {
            // Arrange - create some test Products
            var p1 = new Product {ProductId = 1, Name = "P1"};
            var p2 = new Product {ProductId = 2, Name = "P2"};
            // Arrange - create a new Cart
            var target = new Cart();

            // Action
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            CartLine[] results = target.Lines.ToArray();

            // Assert
            Assert.AreEqual(results.Length, 2);
            Assert.AreEqual(results[0].Product, p1);
            Assert.AreEqual(results[1].Product, p2);
        }
Esempio n. 6
0
        public void CanAddQuantityForExistingLines()
        {
            // Arrange - create some test products
            var p1 = new Product {ProductId = 1, Name = "P1"};
            var p2 = new Product {ProductId = 2, Name = "P2"};
            // Arrange - create a new cart
            var target = new Cart();

            // Action
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 10);
            CartLine[] results = target.Lines.OrderBy(c => c.Product.ProductId).ToArray();

            // Assert
            Assert.AreEqual(results.Length, 2);
            Assert.AreEqual(results[0].Quantity, 11);
            Assert.AreEqual(results[1].Quantity, 1);
        }
Esempio n. 7
0
        public void CanRemoveLine()
        {
            // Arrange - create some test products
            var p1 = new Product {ProductId = 1, Name = "P1"};
            var p2 = new Product {ProductId = 2, Name = "P2"};
            var p3 = new Product {ProductId = 3, Name = "P3"};
            // Arrange - create a new cart
            var target = new Cart();

            // Arrange - add some products to the cart
            target.AddItem(p1, 1);
            target.AddItem(p2, 3);
            target.AddItem(p3, 5);
            target.AddItem(p2, 1);

            // Action
            target.RemoveLine(p2);

            // Assert
            Assert.AreEqual(target.Lines.Count(c => c.Product == p2), 0);
            Assert.AreEqual(target.Lines.Count(), 2);
        }
Esempio n. 8
0
        private Order ProcessTheOrder(Cart cart, ShippingDetails shippingDetails)
        {
            var order = new Order();
            var orderDetailsList = new List<OrderDetail>();

            try
            {
                foreach (var item in cart.Lines)
                {
                    var orderDetail = new OrderDetail
                    {
                        OrderId = order.OrderId,
                        ProductName = item.Product.Name,
                        Quantity = item.Quantity,
                        UnitPrice = item.Product.Price
                    };
                    order.TotalCost += orderDetail.UnitPrice*orderDetail.Quantity;
                    //TODO: Make Unit Test
                    orderDetailsList.Add(orderDetail);
                }
            }
            catch (Exception)
            {
                // TODO: Add HttpException handling in place of Exception below
                throw new Exception("Erorr building list of oder details -> cart.Lines was probably null.");
            }

            var firstCartLineProduct = cart.Lines.FirstOrDefault();
            int establishmentId = firstCartLineProduct.Product.EstablishmentId;

            order.OrderDetails = orderDetailsList;
            order.EstablishmentId = establishmentId;
            order.CustomerName = shippingDetails.Name;
            order.TimeProcessed = DateTime.Now;
            return order;
        }
Esempio n. 9
0
        public void CanViewCartContents()
        {
            // Arrange - create a Cart
            var cart = new Cart();
            // Arrange - create the controller
            var target = new CartController(null, null);

            // Action - call the Index action method
            var result
                = (CartIndexViewModel) target.Index(cart, "myUrl").ViewData.Model;

            // Assert
            Assert.AreSame(result.Cart, cart);
            Assert.AreEqual(result.ReturnUrl, "myUrl");
        }
Esempio n. 10
0
 public ViewResult Index(Cart cart, string returnUrl)
 {
     return View(new CartIndexViewModel { Cart = cart, ReturnUrl = returnUrl });
 }
Esempio n. 11
0
 public void Process(Cart cart, ShippingDetails shippingDetails)
 {
     var order = ProcessTheOrder(cart, shippingDetails);
     orderRepository.CreateOrder(order);
     cart.Clear();
 }
Esempio n. 12
0
        public void CalculateCartTotal()
        {
            // Arrange - create some test products
            var p1 = new Product {ProductId = 1, Name = "P1", Price = 100M};
            var p2 = new Product {ProductId = 2, Name = "P2", Price = 50M};
            // Arrange - create a new cart
            var target = new Cart();

            // Action
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 3);
            decimal result = target.ComputeTotalValue();

            // Assert
            Assert.AreEqual(result, 450M);
        }
Esempio n. 13
0
 public ViewResult Summary(Cart cart)
 {
     return View(cart);
 }
Esempio n. 14
0
        public void CanAddToCart()
        {
            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new[]
                {
                    new Product {ProductId = 1, Name = "P1", CategoryId = 1},
                }.AsQueryable());
            // Arrange - create a Cart
            var cart = new Cart();
            // Arrange - create the controller
            var target = new CartController(mock.Object, null);

            // Action - add a product to the cart
            target.AddToCart(cart, 1, null);

            // Assert
            Assert.AreEqual(cart.Lines.Count(), 1);
            Assert.AreEqual(cart.Lines.ToArray()[0].Product.ProductId, 1);
        }
Esempio n. 15
0
        public void CannotAddNewProductFromDifferentEstablishment()
        {
            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new[]
                {
                    new Product {ProductId = 1, Name = "P1", EstablishmentId = 1},
                    new Product {ProductId = 2, Name = "P2", EstablishmentId = 2}
                }.AsQueryable());
            // Arrange - create a Cart
            var cart = new Cart();
            // Arrange - create the controller
            var target = new CartController(mock.Object, null);

            // Action - add a product to the cart
            target.AddToCart(cart, 1, null);
            try
            {
                target.AddToCart(cart, 2, null);
                Assert.Fail(); // If we get to this line we have failed.
            }
            catch (Exception)
            {
                // If we add a throw; here the test does not run.
            }


            // Assert
            Assert.AreEqual(cart.Lines.Count(), 1);
            Assert.AreEqual(cart.Lines.ToArray()[0].Product.ProductId, 1);
        }
Esempio n. 16
0
        public void CanClearContents()
        {
            // Arrange - create some test products
            var p1 = new Product {ProductId = 1, Name = "P1", Price = 100M};
            var p2 = new Product {ProductId = 2, Name = "P2", Price = 50M};
            // Arrange - create a new cart
            var target = new Cart();

            // Arrange - add some items
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);

            // Action - reset the cart
            target.Clear();

            // Assert
            Assert.AreEqual(target.Lines.Count(), 0);
        }
Esempio n. 17
0
        public void AddingProductToCartGoesToCartScreen()
        {
            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new[]
                {
                    new Product {ProductId = 1, Name = "P1", CategoryId = 1},
                }.AsQueryable());
            // Arrange - create a Cart
            var cart = new Cart();
            // Arrange - create the controller
            var target = new CartController(mock.Object, null);

            // Action - add a product to the cart
            RedirectToRouteResult result = target.AddToCart(cart, 2, "myUrl");

            // Assert
            Assert.AreEqual(result.RouteValues["action"], "Index");
            Assert.AreEqual(result.RouteValues["returnUrl"], "myUrl");
        }
Esempio n. 18
0
        public void CannotCheckoutEmptyCart()
        {
            // Arrange - create a mock order processor
            var mock = new Mock<IOrderProcessor>();
            // Arrange - create an empty cart
            var cart = new Cart();
            // Arrange - create shipping details
            var shippingDetails = new ShippingDetails();

            // Arrange - create an instance of the controller
            var controller = new CartController(null, null);

            // Action
            ViewResult result = controller.Checkout(cart, shippingDetails);

            // Assert - check that the order hasn't been passed on to the processor
            mock.Verify(m => m.CreateOrder(It.IsAny<Order>()),
                        Times.Never());
            // Assert - check that the method is returning the default view
            Assert.AreEqual("", result.ViewName);
            // Assert - check that we are passing an invalid model to the view
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
Esempio n. 19
0
        public void CanCheckoutAndSubmitOrder()
        {
            // Arrange - create a mock order processor
            var mockRepository = new Mock<IProductRepository>();
            var what = new Mock<ICartApplicationService>();
            // Arrange - create a cart with an item
            var cart = new Cart();
            var shippingDetails = new ShippingDetails
                {
                    Name = "Jason",
                    Line1 = "123 Fake",
                    City = "Corpus Christi",
                    State = "Texas",
                    Zip = "78414",
                    Country = "United States",
                    GiftWrap = false
                };
            cart.AddItem(new Product {ProductId = 1, Name = "P1", Price = 100M, EstablishmentId = 1, CategoryId = 1, Description = "none"}, 1);
            // Arrange - create an instance of the controller

            var controller = new CartController(mockRepository.Object, what.Object);

            // Action - try to checkout
            var result = controller.Checkout(cart, shippingDetails);

            // Assert - check that the order has been passed on to the processor
            what.Verify(m => m.Process(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()),
                        Times.Once());
            // Assert - check that the method is returning the Completed view
            Assert.AreEqual("Completed", result.ViewName);
            // Assert - check that we are passing a valid model to the view
            Assert.AreEqual(true, result.ViewData.ModelState.IsValid);
        }
Esempio n. 20
0
        public void CannotCheckoutInvalidShippingDetails()
        {
            // Arrange - create a mock order processor
            var mock = new Mock<IOrderProcessor>();
            // Arrange - create a cart with an item
            var cart = new Cart();
            cart.AddItem(new Product(), 1);
            // Arrange - create an instance of the controller
            var controller = new CartController(null, null);
            // Arrange - add an error to the model
            controller.ModelState.AddModelError("error", "error");

            // Action - try to checkout
            ViewResult result = controller.Checkout(cart, new ShippingDetails());

            // Assert - check that the order hasn't been passed on to the processor
            mock.Verify(m => m.CreateOrder(It.IsAny<Order>()),
                        Times.Never());
            // Assert - check that the method is returning the default view
            Assert.AreEqual("", result.ViewName);
            // Assert - check that we are passing an invalid model to the view
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }