public void CalculateBasketTotal_MultipleItems_ReturnsSumOfItemCostMultipliedByItemQuantity()
        {
            // Arrange
            var item1 = new BasketItem {
                Name = "Bread", Cost = 1.00M, Quantity = 1
            };
            var item2 = new BasketItem {
                Name = "Butter", Cost = 0.80M, Quantity = 1
            };
            var item3 = new BasketItem {
                Name = "Milk", Cost = 1.15M, Quantity = 1
            };

            var basket = new Basket();

            basket.AddBasketItem(item1);
            basket.AddBasketItem(item2);
            basket.AddBasketItem(item3);

            decimal expected = 2.95M;

            // Act
            var result = basket.CalculateBasketTotal();

            // Assert
            Assert.AreEqual(expected, result);
        }
        public void AddBasketItem_DuplicateItem_ExistingItemQuantityIsIncremented()
        {
            // Arrange
            var item = new BasketItem {
                Name = "Butter", Cost = 0.80M, Quantity = 1
            };
            var item2 = new BasketItem {
                Name = "Butter", Cost = 0.80M, Quantity = 1
            };
            var basket = new Basket();

            var expected = new List <BasketItem>
            {
                new BasketItem {
                    Name = "Butter", Cost = 0.80M, Quantity = 2
                }
            };

            // Act
            basket.AddBasketItem(item);
            basket.AddBasketItem(item2);
            var result = basket.BasketItems;

            // Assert
            Assert.AreEqual(expected.Count, result.Count, "Number of actual elements do not match the expected number of elements");

            Assert.IsTrue(expected.All(e => result.Count(a => a.Name == e.Name) == 1) &&
                          expected.All(e => result.Count(a => a.Cost == e.Cost) == 1) &&
                          expected.All(e => result.Count(a => a.Quantity == e.Quantity) == 1), "Actual items do not match expected items");
        }
Esempio n. 3
0
        public bool AddToBasket(HttpContextBase httpContext, int productId, int quantity)
        {
            bool success = true;

            Basket     basket = GetBasket(httpContext);
            BasketItem item   = basket.BasketItems.FirstOrDefault(i => i.ProductId == productId);

            if (item == null)
            {
                item = new BasketItem()
                {
                    BasketId  = basket.BasketId,
                    ProductId = productId,
                    Quantity  = quantity
                };
                basket.AddBasketItem(item);
            }
            else
            {
                item.Quantity = item.Quantity + quantity;
            }
            baskets.Commit();

            return(success);
        }
Esempio n. 4
0
 private void PopulateWithProducts(Basket basket, Product product, int count)
 {
     for (int i = 0; i < count; i++)
     {
         basket.AddBasketItem(product);
     }
 }
Esempio n. 5
0
        public void ShouldAddSeveralBasketItems()
        {
            // Arrange
            var product = CreateTestProduct();

            // Sut
            var basket = new Basket(_discountProvider.Object, _giftProvider.Object);

            // Act
            basket.AddBasketItem(product);
            basket.AddBasketItem(product);
            basket.AddBasketItem(product);

            // Assert
            Assert.AreEqual(3, basket.Products.Count, "Should have 3 products");
        }
Esempio n. 6
0
        public void ShouldCallGiftProviderAndAddGiftWhenAddItem()
        {
            // Arrange
            var productId = "Some-Id";
            var product   = new Product(productId, "Test", 23);
            var gift      = CreateTestProduct("gift");
            var gifts     = new List <Product> {
                gift
            };

            _giftProvider.Setup(p => p.GetGiftProducts(It.IsAny <Basket>())).Returns(gifts);

            // Sut
            var basket = new Basket(_discountProvider.Object, _giftProvider.Object);

            // Act
            basket.AddBasketItem(product);

            // Assert
            _giftProvider.VerifyAll();
            var items = basket.Products;
            var expectedProductCount = 2; // product + gift

            Assert.AreEqual(expectedProductCount, items.Count, $"Amount of products in basket is not as expected. Expected: {expectedProductCount}, Actual: {items.Count}");
        }
        public bool AddProductToBasket(HttpContextBase httpContext, int pId, int CurrQty)
        {
            bool       success = true;
            Basket     bkt     = FetchBasket(httpContext);
            BasketItem bim     = bkt.BasketItems.FirstOrDefault(i => i.ProductId == pId);

            if (bim == null)
            {
                BasketItem Item = new BasketItem()
                {
                    BasketId  = bkt.BasketId,
                    ProductId = pId,
                    Quantity  = CurrQty
                };
                bkt.AddBasketItem(Item);
            }
            else
            {
                bim.Quantity += CurrQty;
            }

            baskets.Commit();

            return(success);
        }
Esempio n. 8
0
        public void CalculateBasketWithBeansAndBreadDiscount()
        {
            var basket = new Basket();

            basket.AddBasketItem(Products.GetProduct("Beans"));
            basket.AddBasketItem(Products.GetProduct("Beans"));
            basket.AddBasketItem(Products.GetProduct("Bread"));

            var calculator = new BasketCalculator(Products, Promotions, basket);

            calculator.CalculateBasket(new DateTime(2020, 6, 18));

            calculator.SubTotal.ShouldBe(new decimal(2.10));
            calculator.DiscountsItems.Count.ShouldBe(1);
            calculator.DiscountsItems[0].Amount.ShouldBe(new decimal(.40));
            calculator.Total.ShouldBe(new decimal(1.70));
        }
Esempio n. 9
0
        public void CalculateBasketWithOutAppleDiscountAsPromotionExpired()
        {
            var basket = new Basket();

            basket.AddBasketItem(Products.GetProduct("Apples"));
            basket.AddBasketItem(Products.GetProduct("Milk"));
            basket.AddBasketItem(Products.GetProduct("Bread"));

            var calculator = new BasketCalculator(Products, Promotions, basket);

            //Use an date in the future after promotion ended
            calculator.CalculateBasket(new DateTime(2021, 6, 18));

            calculator.SubTotal.ShouldBe(new decimal(3.10));
            calculator.DiscountsItems.Count.ShouldBe(0);
            calculator.Total.ShouldBe(new decimal(3.10));
        }
Esempio n. 10
0
        public void ShouldThrowWhenAddNull()
        {
            // Sut
            var basket = new Basket(_discountProvider.Object, _giftProvider.Object);

            // Act, Assert
            Assert.ThrowsException <ArgumentNullException>(() => basket.AddBasketItem(null));
        }
        private void AddItemsToBasket(Guid productId, Basket basket)
        {
            var product = _productRepository.GetById(productId);

            if (product != null)
            {
                basket.AddBasketItem(product);
            }
        }
Esempio n. 12
0
        public void ShouldThrowWhenAddInvalidBasketItem()
        {
            // Arrange
            var product = new Product(null, "Test", 23);

            // Sut
            var basket = new Basket(_discountProvider.Object, _giftProvider.Object);

            // Act, Assert
            Assert.ThrowsException <ArgumentNullException>(() => basket.AddBasketItem(product));
        }
Esempio n. 13
0
        public ActionResult Update([CurrentBasket] Basket basket, [EntityBind(Fetch = false)] BasketItem basketItem)
        {
            if (!basketItem.Size.IsInStock)
            {
                Message = RenderIndexViewWithError(basketItem.Size);
                return(this.RedirectToAction <ProductController>(c => c.Item(basketItem.Size.Product.UrlName)));
            }

            basket.AddBasketItem(basketItem);
            return(this.RedirectToAction(c => c.Index()));
        }
Esempio n. 14
0
        public void ShouldAddBasketItem()
        {
            // Arrange
            var productId = "Some-Id";
            var product   = new Product(productId, "Test", 23);

            // Sut
            var basket = new Basket(_discountProvider.Object, _giftProvider.Object);

            // Act
            basket.AddBasketItem(product);

            // Assert
            Assert.AreEqual(1, basket.Products.Count, "Should have one product");
            Assert.AreEqual(productId, basket.Products.First().Id, $"Ids are not equal, Should be {productId}");
        }
        public void AddBasketItem_ItemWithZeroQuantity_ItemIsNotAdded()
        {
            // Arrange
            var item = new BasketItem {
                Name = "Butter", Cost = 0.80M, Quantity = 0
            };

            var basket = new Basket();

            // Act
            basket.AddBasketItem(item);

            var result = basket.BasketItems;

            // Assert
            Assert.AreEqual(0, result.Count, "Number of actual elements do not match the expected number of elements");
        }
        public void CalculateBasketTotal_SingleItem_ReturnsItemCostMultipliedByItemQuantity()
        {
            // Arrange
            var item = new BasketItem {
                Name = "Butter", Cost = 0.80M, Quantity = 2
            };

            var basket = new Basket();

            basket.AddBasketItem(item);

            decimal expected = 1.60M;

            // Act
            var result = basket.CalculateBasketTotal();

            // Assert
            Assert.AreEqual(expected, result);
        }