Beispiel #1
0
        public void CanPlaceOrder()
        {
            //Arrange
            IRepository <Order>    orderContext    = new MockRepository <Order>();
            IRepository <Product>  productContext  = new MockRepository <Product>();
            IRepository <Cart>     cartContext     = new MockRepository <Cart>();
            IRepository <CartItem> cartItemContext = new MockRepository <CartItem>();

            productContext.Insert(new Product {
                Id = "1", Price = 10
            });
            productContext.Insert(new Product {
                Id = "2", Price = 20
            });

            ICartService cartService = new CartService(productContext, cartContext, cartItemContext);

            cartService.AddToCart(context.Object, "1");
            cartService.AddToCart(context.Object, "2");

            IOrderService orderService = new OrderService(orderContext);

            //Act
            orderService.CreateOrder(new Order()
            {
                OrderStatus = "Order Created"
            }, cartService.GetCartItems(context.Object));

            //Assert
            Assert.AreEqual(orderContext.Collection().ToList()[0].Items.Count, 2);
        }
        public async Task RemoveFromCart_WithCartAmountingToTwo_ShouldReturnCartItemsCount1()
        {
            //Arrange
            var db = this.SetDb();

            await this.SeedProducts(db);

            var productRepo = new Repository <Product>(db);
            var wrapper     = new TestCartSessionWrapper();
            var cartManager = new CartManager(wrapper);

            var cartService = new CartService(productRepo, cartManager);

            await cartService.AddToCart(1);

            await cartService.AddToCart(2);

            //Act
            await cartService.RemoveFromCart(1);

            var expected = 1;
            var actual   = cartManager.GetCartItem().Count;

            //Assert
            Assert.Equal(expected, actual);
        }
        public async Task GetCartItems_WithTwoItems_ShouldReturnItemCount2()
        {
            //Arrange
            var db = this.SetDb();

            await this.SeedProducts(db);

            var productRepo = new Repository <Product>(db);
            var wrapper     = new TestCartSessionWrapper();
            var cartManager = new CartManager(wrapper);

            var cartService = new CartService(productRepo, cartManager);

            await cartService.AddToCart(1);

            await cartService.AddToCart(2);

            //Act
            var result = cartService.GetCartItems();

            var expectedResultCount = 2;
            var actualResultCount   = result.CartItems.Count;

            //Assert
            Assert.Equal(expectedResultCount, actualResultCount);
        }
Beispiel #4
0
        public void GetCartSummary()
        {
            //Arrange
            MockRepository <Product>  productContext  = new MockRepository <Product>();
            MockRepository <Cart>     cartContext     = new MockRepository <Cart>();
            MockRepository <CartItem> cartItemContext = new MockRepository <CartItem>();

            productContext.Insert(new Product {
                Id = "1", Price = 10
            });
            productContext.Insert(new Product {
                Id = "2", Price = 20
            });

            //Act
            CartService cart = new CartService(productContext, cartContext, cartItemContext);

            cart.AddToCart(context.Object, "1");
            cart.AddToCart(context.Object, "2");
            cart.AddToCart(context.Object, "2");

            //Assert
            CartSummaryViewModel result = cart.GetCartSummary(context.Object);

            Assert.IsTrue(result.CartCount == 3 && result.CartTotal == 50);
        }
Beispiel #5
0
 public void AddQuantityLineItemIfExists()
 {
     using (var context = new StoreContext(_options))
     {
         var cartService    = new CartService(context);
         var firstLineItem  = cartService.AddToCart(null, 1, 1);
         var secondLineItem = cartService.AddToCart(firstLineItem.SessionId, 1, 1);
         Assert.Single(secondLineItem.Items);
         Assert.Equal(2, secondLineItem.Items[0].Quantity);
     }
 }
Beispiel #6
0
        public void UpdateQuantity_Should_Update_Quantity_Of_Item_In_Cart()
        {
            var cartService = new CartService();
            var cartItems   = cartService.GetCartItems();

            Assert.IsTrue(cartItems.Count == 0);

            var item = new Item()
            {
                Id       = 1,
                SKU      = "3",
                Quantity = 100
            };

            cartService.AddToCart(item);
            cartItems = cartService.GetCartItems();

            Assert.AreEqual(1, cartItems.Count);

            item.Quantity = 50;
            cartService.UpdateQuantity(item);

            item = cartService.GetCartItem(item.Id);

            Assert.AreEqual(50, item.Quantity);

            // because of static data, I have had to clean up after each test
            CartData.EmptyCart();
        }
        public async Task AddToCart_WithValidProductId_ShouldAddNewItemToCart()
        {
            //Arrange
            var db = this.SetDb();

            await this.SeedProducts(db);

            var productRepo = new Repository <Product>(db);
            var wrapper     = new TestCartSessionWrapper();
            var cartManager = new CartManager(wrapper);

            var cartService = new CartService(productRepo, cartManager);

            //Act
            await cartService.AddToCart(1);

            var expectedNumber   = 1;
            var expectedItemName = "Chocolate Peanut Cake";
            var actualItemName   = cartManager.GetCartItem().First().Product.Name;
            var actualNumber     = cartManager.GetCartItem().Count;

            //Assert
            Assert.Equal(expectedNumber, actualNumber);
            Assert.Equal(expectedItemName, actualItemName);
        }
Beispiel #8
0
        public void CartService_AddToCart_Increment_Quantity()
        {
            const int EXPECTED_PRODUCT_ID  = 5;
            const int EXPECTED_ITEMS_COUNT = 3;

            var cart = new Cart
            {
                Items = new List <CartItem> {
                    new CartItem {
                        ProductId = EXPECTED_PRODUCT_ID, Quantity = EXPECTED_ITEMS_COUNT - 1
                    }
                }
            };

            var product_data_mock = new Mock <IProductData>();

            var cart_store_mock = new Mock <ICartStore>();

            cart_store_mock.Setup(c => c.Cart).Returns(cart);

            var cart_service = new CartService(product_data_mock.Object, cart_store_mock.Object);

            cart_service.AddToCart(EXPECTED_PRODUCT_ID);

            Assert.Equal(1, cart.Items.Count);
            Assert.Equal(EXPECTED_PRODUCT_ID, cart.Items.First().ProductId);
        }
Beispiel #9
0
        public void CartService_AddToCart_WorkCorrect()
        {
            var cart = new Cart
            {
                Items = new List <CartItem>()
            };

            var product_data_mock = new Mock <IProductData>();

            var cart_store_mock = new Mock <ICartStore>();

            cart_store_mock
            .Setup(c => c.Cart)
            .Returns(cart);

            var cart_service = new CartService(product_data_mock.Object, cart_store_mock.Object);

            const int expected_id = 5;

            cart_service.AddToCart(expected_id);

            Assert.Equal(1, cart.ItemsCount);
            Assert.Single(cart.Items);
            Assert.Equal(expected_id, cart.Items[0].ProductId);
        }
Beispiel #10
0
        public void CartServiceAddToCartWorksCorrect()
        {
            // Arrange
            // подготовим пустую корзину
            var cart = new Cart
            {
                CartItems = new List <CartItem>()
            };

            var productData = new Mock <IProductService>();
            var cartStore   = new Mock <ICartStore>();

            cartStore
            .Setup(c => c.Cart)
            .Returns(cart);

            var cartService = new CartService(productData.Object, cartStore.Object);

            // Act
            cartService.AddToCart(5);

            // Assert
            Assert.Equal(1, cart.ItemsCount);
            Assert.Equal(1, cart.CartItems.Count);
            Assert.Equal(5, cart.CartItems[0].ProductId);
        }
        public void ShouldAddProductToCart()
        {
            var mockDB = new Mock <ICart>();

            mockDB.Setup(c => c.ToCart(GetProduct(), GetCart(), 1))
            .Returns(true);

            var mockProductService = new Mock <IProductService>();

            mockProductService.Setup(p => p.FindById(new ProductFindRequest
            {
                ProductId = 2
            })).Returns(new ProductFindResponse
            {
                FoundProduct = GetProduct()
            });

            mockDB.Setup(c => c.ReadById(GetCartById()))
            .Returns(GetCart());

            _cartValidation = new CartValidation(_addProductToCartValidation, _cartCreateRequestValidation, _cartFindRequestValidation, _cartRemoveRequestValidation, _removeFromCartRequestValidation, _cartClearRequestValidation);

            _victim = new CartService(mockDB.Object, mockProductService.Object, _cartValidation);

            var actual = _victim.AddToCart(AddItemToCart()).HasAdded;

            Assert.True(actual);
        }
        public void CartService_AddToCart_Increment_Quantity()
        {
            var cart = new Cart()
            {
                Items = new List <CartItem>()
                {
                    new CartItem()
                    {
                        ProductId = 5, Quantity = 2
                    }
                }
            };

            var productData = new Mock <IProductData>();
            var cartStore   = new Mock <ICartStore>();

            cartStore.Setup(c => c.Cart).Returns(cart);

            var cartService = new CartService(productData.Object, cartStore.Object);

            cartService.AddToCart(5);

            Assert.Single(cart.Items);
            Assert.Equal(3, cart.ItemsCount);
        }
Beispiel #13
0
        public async Task <IHttpActionResult> AddToCart(CartDetailsModel cartDetails)
        {
            if (cartDetails == null)
            {
                return(BadRequest("Please provide valid inputs!"));
            }

            CommonResponse validatedResponse = await AuthService.ValidateUserAndToken();

            if (!validatedResponse.IsError)
            {
                if (await CartService.AddToCart(cartDetails))
                {
                    var cartDetailsList = await CartService.GetCartDetails(cartDetails);

                    if (cartDetailsList.Count > 0)
                    {
                        return(Ok(cartDetailsList));
                    }
                    else
                    {
                        return(BadRequest("No Inventory record Exists!"));
                    }
                }
                else
                {
                    return(BadRequest("Transaction Cannot be Processed!"));
                }
            }
            else
            {
                return(Unauthorized());
            }
        }//end of add
Beispiel #14
0
        public void CartService_AddToCart_Increment_Quantity()
        {
            const int expected_product_id  = 5;
            const int expected_items_count = 3;

            var cart = new Entities.ViewModels.Cart
            {
                Items = new List <CartItem>
                {
                    new CartItem {
                        ProductId = expected_product_id, Quantity = expected_items_count - 1
                    }
                }
            };

            var product_data_mock = new Mock <IProductData>();
            var cart_store_mock   = new Mock <ICartStore>();

            cart_store_mock.Setup(c => c.Cart).Returns(cart);

            var cart_service = new CartService(product_data_mock.Object, cart_store_mock.Object);

            cart_service.AddToCart(expected_product_id);

            Assert.Equal(1, cart.Items.Count);
            Assert.Equal(expected_items_count, cart.ItemsCount);
        }
Beispiel #15
0
        public void CartServiceAddToCartIncrementQuantity()
        {
            // Arrange
            // подготовим корзину с товарами
            var cart = new Cart
            {
                CartItems = new List <CartItem>
                {
                    new CartItem
                    {
                        ProductId = 5,
                        Quantity  = 2
                    }
                }
            };

            var productData = new Mock <IProductService>();
            var cartStore   = new Mock <ICartStore>();

            cartStore
            .Setup(c => c.Cart)
            .Returns(cart);

            var cartService = new CartService(productData.Object, cartStore.Object);

            // Act
            cartService.AddToCart(5);

            // Assert
            Assert.Equal(1, cart.CartItems.Count);
            Assert.Equal(3, cart.ItemsCount);
        }
        public async Task AddToCart_WhitInValidProductId_ShouldNotAddItem()
        {
            //Arrange
            var db = this.SetDb();

            await this.SeedProducts(db);

            var productRepo = new Repository <Product>(db);
            var wrapper     = new TestCartSessionWrapper();
            var cartManager = new CartManager(wrapper);

            var cartService = new CartService(productRepo, cartManager);

            //Arrange
            try
            {
                await cartService.AddToCart(3);
            }
            catch (Exception)
            { }

            var expectedCartCount = 0;
            var actualCartCount   = cartManager.GetCartItem().Count;

            //Assert
            Assert.Equal(expectedCartCount, actualCartCount);
        }
Beispiel #17
0
        public ActionResult Add(int id = 0)
        {
            var product = _products.GetProduct(id);

            _cart.AddToCart(product);

            return(RedirectToAction("Details", "Cart"));
        }
Beispiel #18
0
 public void CreateCartIfNotFound()
 {
     using (var context = new StoreContext(_options))
     {
         var cartService = new CartService(context);
         var result      = cartService.AddToCart(null, 1, 1);
         Assert.NotEqual(Guid.Empty, result.SessionId);
     }
 }
Beispiel #19
0
 public void AddQuantityTwoLineItem()
 {
     using (var context = new StoreContext(_options))
     {
         var cartService = new CartService(context);
         var cart        = cartService.AddToCart(null, 1, 2);
         Assert.Equal(2, cart.Items[0].Quantity);
     }
 }
Beispiel #20
0
        public IActionResult AddToCart(int ID)
        {
            var book = _bookService.GetAllBooks().Find(x => x.ID == ID);

            _cartService.AddToCart(book);
            _db.SaveChanges();

            return(View(book));
        }
Beispiel #21
0
        public async Task <IActionResult> AddToCart(int ID)
        {
            var user = await _userManager.GetUserAsync(User);

            var userId = user.Id;

            _cartService.AddToCart(userId, ID);

            return(RedirectToAction("Index", "Home"));
        }
        public async Task <IActionResult> Patch([FromBody] Cart model)
        {
            string message = _cartService.Validate(model);

            if (!string.IsNullOrEmpty(message))
            {
                return(BadRequest(new { message }));
            }
            return(Ok(await _cartService.AddToCart(model)));
        }
Beispiel #23
0
 public void AddLineItemIfNotExists()
 {
     using (var context = new StoreContext(_options))
     {
         var cartService = new CartService(context);
         var result      = cartService.AddToCart(null, 1, 1);
         Assert.Single(result.Items);
         Assert.Equal(1, result.Items[0].Quantity);
     }
 }
        public IActionResult AddToCart(int bookId)
        {
            //Sækir bók eftir id tölu
            var bookAdded = _bookService.GetBookById(bookId);

            //Bætir bók í körfu sem er skráð á innskráðann notenda
            _cartService.AddToCart(bookAdded, this.HttpContext);

            return(Redirect("/ShoppingCart"));
        }
Beispiel #25
0
        static void Main(string[] args)
        {
            IAccount myAccount = new Account()
            {
                Currency = Currency.ILS, Name = "Basel", PaymentMethods = new List <ICard>()
                {
                    new MasterCard {
                        Number = "20506070", Password = "******", Provider = "PayPal", Amount = 300, Currency = Currency.USD
                    }
                }
            };

            IStore store           = new Store();
            ICart  myCart          = new Cart();
            var    myPaymentMethod = myAccount.PaymentMethods.Single();

            ILoggingService        loggingService        = new LoggingService();
            ICartService           cartService           = new CartService();
            IStoreLocalizerService storeLocalizerService = new StoreLocalizerService();

            myAccount.IsLoggedIn = true;

            //fill My Store
            store.AddItem(new Item(id: 1, name: "MacBook Pro", category: ItemCategory.Electronics, price: 100), 10);
            store.AddItem(new Item(id: 1, name: "MacBook Pro", category: ItemCategory.Electronics, price: 100), 5);
            store.AddItem(new Item(id: 2, name: "T-Shirt", category: ItemCategory.Clothes, price: 7), 5);

            //localize store
            store = storeLocalizerService.LocalizeStore(store, myAccount.Currency);

            //init my cart
            myCart.AddPaymentMethod(myPaymentMethod);

            //fill my cart
            cartService.AddToCart(store, myCart, 1, 3);
            cartService.AddToCart(store, myCart, 2, 8);

            //Process Payment
            loggingService.Log($"My Balance: {myPaymentMethod.Amount}");
            cartService.ProcessPaymentFor(store, myCart, myAccount);
            loggingService.Log($"My Balance: {myPaymentMethod.Amount}");
        }
Beispiel #26
0
        public JsonResult AddToCart(int albumId, int toCartNum)
        {
            if (albumId > 0 && toCartNum > 0)
            {
                var cartId = cartService.GetCartId(this.HttpContext);
                cartService.AddToCart(cartId, albumId, toCartNum);
                return(Json(new { MessageType = 1, MessageContent = "添加成功" }));
            }

            return(Json(new { MessageType = 0, MessageContent = "添加失败" }));
        }
        public IActionResult AddToCart(int bookId)
        {
            var bookAdded = _bookService.GetBookById(bookId);
            var cart      = CartService.GetCart(this.HttpContext);

            _cartService.AddToCart(bookAdded, this.HttpContext);

            string referer = Request.Headers["Referer"].ToString();

            return(Redirect(referer));
        }
Beispiel #28
0
        public void CartService_AddToCart_WorkCorrect()
        {
            _Cart.Items.Clear();

            const int expected_id = 5;

            _CartService.AddToCart(expected_id);

            Assert.Equal(1, _Cart.ItemsCount);
            Assert.Single(_Cart.Items);
            Assert.Equal(expected_id, _Cart.Items[0].ProductId);
        }
Beispiel #29
0
        public object AddToCart()
        {
            var product = _productService.ById(_routeValues.GetValue("productId").ParseInt());

            _cartService.AddToCart(_chat, product.Id);

            return(new StringViewModel(
                       _i18NService["notifications.product-added-to-cart", new Values {
                                        ["name"] = product.Name
                                    }]
                       ));
        }
Beispiel #30
0
        public void GetCartItems_Should_Return_All_Items_In_Cart()
        {
            var cartService = new CartService();
            var cartItems   = cartService.GetCartItems();

            Assert.IsTrue(cartItems.Count == 0);

            var item1 = new Item()
            {
                Id       = 1,
                SKU      = "3",
                Quantity = 100
            };

            var item2 = new Item()
            {
                Id       = 2,
                SKU      = "1",
                Quantity = 100
            };

            var item3 = new Item()
            {
                Id       = 3,
                SKU      = "3",
                Quantity = 100
            };

            cartService.AddToCart(item1);
            cartService.AddToCart(item2);
            cartService.AddToCart(item3);

            cartItems = cartService.GetCartItems();

            Assert.AreEqual(3, cartItems.Count);

            // because of static data, I have had to clean up after each test
            CartData.EmptyCart();
        }