public RedirectToRouteResult RemoveFromCart(CartRepository cart, int productId, string returnUrl)
 {
     Product product = _repo.Products.SingleOrDefault(p => p.ProductID == productId);
     if (product != default(Product))
         cart.RemoveLine(product);
     return RedirectToAction("Index", new { returnUrl });
 }
 public ViewResult Index(CartRepository cart, string returnUrl)
 {
     return View(new CartIndexViewModel
     {
         Cart = cart,
         ReturnUrl = returnUrl
     });
 }
        public ViewResult CheckOut(CartRepository cart, ShippingDetails shippingDetails)
        {
            if (cart.CartLineCollections.Count == 0)
                ModelState.AddModelError("", "Koszyk jest pusty");

            if (ModelState.IsValid)
            {
                _proc.ProcessOrder(cart, shippingDetails);
                cart.Clear();
                return View("Completed");
            }
            return View(shippingDetails);
        }
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (controllerContext.HttpContext.Session != null)
                cart = (CartRepository)controllerContext.HttpContext.Session[sessionKey];

            if (cart == null)
            {
                cart = new CartRepository();
                controllerContext.HttpContext.Session[sessionKey] = cart;
            }

            return cart;
        }
Example #5
0
        public void Calculate_Cart_List()
        {
            Product p1 = new Product { Name = "P1", ProductID = 1 , Price = 100M};
            Product p2 = new Product { Name = "P2", ProductID = 2, Price = 50M };

            CartRepository target = new CartRepository();

            target.AddItem(p1, 2);
            target.AddItem(p2, 3);

            var result = target.ComputeTotalValue();

            Assert.AreEqual(result, new decimal(100 * 2 + 50 * 3));
        }
Example #6
0
        public void Adding_Product_To_Cart_Goes_To_Cart_Screen()
        {
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] {
                new Product { ProductID = 1, Name = "P1", Category = "Jabłka" }
            }.AsQueryable());

            CartRepository cart = new CartRepository();

            CartController controller = new CartController(mock.Object,null);

            RedirectToRouteResult result = controller.AddToCart(cart, 2, "myUrl");

            Assert.AreEqual(result.RouteValues["action"],"Index");
            Assert.AreEqual(result.RouteValues["returnUrl"], "myUrl");
        }
Example #7
0
        public void Cannot_Checkout_Empty_Cart()
        {
            // przygotowanie - tworzenie imitacji procesora zamówień
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
            // przygotowanie - tworzenie pustego koszyka
            CartRepository cart = new CartRepository();
            // przygotowanie - tworzenie danych do wysyłki
            ShippingDetails shippingDetails = new ShippingDetails();
            // przygotowanie - tworzenie egzemplarza kontrolera
            CartController target = new CartController(null, mock.Object);

            // działanie
            ViewResult result = target.CheckOut(cart, shippingDetails);

            // asercje - sprawdzenie, czy zamówienie zostało przekazane do procesora
            mock.Verify(m => m.ProcessOrder(It.IsAny<CartRepository>(), It.IsAny<ShippingDetails>()),
                Times.Never());
            // asercje - sprawdzenie, czy metoda zwraca domyślny widok
            Assert.AreEqual("", result.ViewName);
            // asercje - sprawdzenie, czy przekazujemy prawidłowy model do widoku
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
Example #8
0
        public void Cannot_Checkout_Invalid_ShippingDetails()
        {
            // przygotowanie - tworzenie imitacji procesora zamówień
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
            // przygotowanie - tworzenie koszyka z produktem
            CartRepository cart = new CartRepository();
            cart.AddItem(new Product(), 1);

            // przygotowanie - tworzenie egzemplarza kontrolera
            CartController target = new CartController(null, mock.Object);
            // przygotowanie - dodanie błędu do modelu
            target.ModelState.AddModelError("error", "error");

            // działanie - próba zakończenia zamówienia
            ViewResult result = target.CheckOut(cart, new ShippingDetails());

            // asercje - sprawdzenie, czy zamówienie zostało przekazane do procesora
            mock.Verify(m => m.ProcessOrder(It.IsAny<CartRepository>(), It.IsAny<ShippingDetails>()),
                Times.Never());
            // asercje - sprawdzenie, czy metoda zwraca domyślny widok
            Assert.AreEqual("", result.ViewName);
            // asercje - sprawdzenie, czy przekazujemy prawidłowy model do widoku
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
 public PartialViewResult Summary(CartRepository cart)
 {
     return PartialView(cart);
 }
Example #10
0
        public void Can_View_Cart_Contents()
        {
            CartRepository cart = new CartRepository();

            CartController controller = new CartController(null,null);

            CartIndexViewModel result = (CartIndexViewModel)controller.Index(cart, "myUrl").ViewData.Model;

            Assert.AreSame(result.Cart, cart);
            Assert.AreEqual(result.ReturnUrl, "myUrl");
        }
Example #11
0
        public void Can_Remove_Lines()
        {
            Product p1 = new Product { Name = "P1", ProductID = 1 };
            Product p2 = new Product { Name = "P2", ProductID = 2 };
            Product p3 = new Product { Name = "P3", ProductID = 3 };

            CartRepository target = new CartRepository();

            target.AddItem(p1, 1);
            target.AddItem(p2, 2);
            target.AddItem(p1, 10);
            target.AddItem(p3, 5);

            var results = target.CartLineCollections;

            Assert.AreEqual(results.Count, 3);
            Assert.AreEqual(results[0].Product, p1);
            Assert.AreEqual(results[1].Product, p2);
            Assert.AreEqual(results[2].Product, p3);

            target.RemoveLine(p2);

            results = target.CartLineCollections;

            Assert.AreEqual(results.Count, 2);
            Assert.AreEqual(results[0].Product, p1);
            Assert.AreEqual(results[1].Product, p3);
            Assert.IsFalse(results.Any(p => p.Product.ProductID == p2.ProductID));
        }
Example #12
0
        public void Can_Clear_Contents()
        {
            Product p1 = new Product { Name = "P1", ProductID = 1 };
            Product p2 = new Product { Name = "P2", ProductID = 2 };

            CartRepository target = new CartRepository();

            target.AddItem(p1, 1);
            target.AddItem(p2, 1);

            var results = target.CartLineCollections.ToArray();

            Assert.AreEqual(results.Length, 2);

            target.Clear();
            Assert.AreEqual(target.CartLineCollections.Count, 0);
        }
Example #13
0
        public void Can_Add_To_Cart()
        {
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] {
                new Product { ProductID = 1, Name = "P1", Category = "Jab" }
            }.AsQueryable());

            CartRepository cart = new CartRepository();

            CartController controller = new CartController(mock.Object, null);

            controller.AddToCart(cart, 1, null);

            Assert.AreEqual(cart.CartLineCollections.Count, 1);
            Assert.AreEqual(cart.CartLineCollections[0].Product.ProductID, 1);
        }
Example #14
0
        public void Can_Add_Quantity_For_Existing_Lines()
        {
            Product p1 = new Product { Name = "P1", ProductID = 1 };
            Product p2 = new Product { Name = "P2", ProductID = 2 };

            CartRepository target = new CartRepository();

            target.AddItem(p1, 1);
            target.AddItem(p2, 2);
            target.AddItem(p1, 10);

            var results = target.CartLineCollections;

            Assert.AreEqual(results.Count, 2);
            Assert.AreEqual(results[0].Quantity, 11);
            Assert.AreEqual(results[1].Quantity, 2);
        }
Example #15
0
        public void Can_Add_New_Lines()
        {
            Product p1 = new Product { Name = "P1", ProductID = 1 };
            Product p2 = new Product {Name = "P2", ProductID = 2 };

            CartRepository target = new CartRepository();

            target.AddItem(p1, 1);
            target.AddItem(p2, 1);

            var results = target.CartLineCollections;

            Assert.AreEqual(results.Count, 2);
            Assert.AreEqual(results[0].Product, p1);
            Assert.AreEqual(results[1].Product, p2);
        }