Ejemplo n.º 1
0
        /// <summary>
        /// Removes an entire CartLine
        /// </summary>
        /// <param name="product">The Product entity that is being removed from the cart (any given product has only one CartLine)</param>
        public void RemoveLine(Product product)
        {
            _lineCollection.RemoveAll(l => l.Product.ProductId == product.ProductId);

            CartLine nullIfCartIsEmpty = _lineCollection.FirstOrDefault();
            if (nullIfCartIsEmpty == null) ResetCartEstablishmentId();
        }
Ejemplo n.º 2
0
        public void CanRetrieveImageData()
        {
            // Arrange - create a Product with image data
            var product = new Product
                {
                   ProductId = 2, Name = "Test", ImageData = new byte[] { }, ImageMimeType = "image/png" 
                };

            // Arrange - create the mock repository
            var localMock = new Mock<IProductRepository>();
            localMock.Setup(m => m.Products).Returns(
                new[]
                    {
                       new Product { ProductId = 1, Name = "P1" }, product, new Product { ProductId = 3, Name = "P3" } 
                    }.
                    AsQueryable());

            // Arrange - create the controller
            var controller = new ProductController(localMock.Object);

            // Action - call the GetImage action method
            ActionResult result = controller.GetImage(2);

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(FileResult));
            Assert.AreEqual(product.ImageMimeType, ((FileResult)result).ContentType);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Adds a CartLine to the cart.  The CartLine will contain a product with the appropriate quantity.
        /// The first CartLine added to a cart dictates the cart's _establishmentId.
        /// </summary>
        /// <param name="product">The product entity being added to the cart</param>
        /// <param name="quantity">The quantity of the product being added to the cart</param>
        public void AddItem(Product product, int quantity)
        {
            if (_establishmentId == 0) _establishmentId = product.EstablishmentId;

            if (product.EstablishmentId != _establishmentId)
            {
                // TODO: Add HttpException handling in place of Exception below
                throw new Exception("Product cannot go in cart because it does not match the establishment that other objects in this cart have");
            }

            CartLine line = _lineCollection.FirstOrDefault(p => p.Product.ProductId == product.ProductId);
            if (line == null)
            {
                _lineCollection.Add(new CartLine {Product = product, Quantity = quantity});
            }
            else
            {
                line.Quantity += quantity;
            }
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        public void CanDeleteValidProducts()
        {
            // Arrange - create a product
            var product = new Product { ProductId = 2, Name = "Test" };

            // Arrange - create a local mock repository
            var localMock = new Mock<IProductRepository>();
            localMock.Setup(m => m.Products).Returns(new Product[]
                {
                    new Product {ProductId = 1, Name = "P1"},
                    product,
                    new Product {ProductId = 3, Name = "P3"}
                }.AsQueryable());

            // Arrange - create a controller
            var controller = new AdminController(localMock.Object);

            // Action - delete the product
            controller.Delete(product.ProductId);

            // assert - ensure that the repository Delete method was called with the correct Product
            localMock.Verify(m => m.DeleteProduct(product));
        }
Ejemplo n.º 10
0
        public void CannotSaveInvalidChanges()
        {
            // Arrange - create a controller
            var controller = new AdminController(this._mockRepository.Object);
            // Arrange - create a product
            Product product = new Product { Name = "Test" };
            // Arrange - add an error to the model state
            controller.ModelState.AddModelError("error", "error");

            // Action - try to save the product
            ActionResult result = controller.Edit(product, null);

            // Assert - check that the repository was called
            this._mockRepository.Verify(m => m.SaveProduct(It.IsAny<Product>()), Times.Never());
            // Assert - check the method result type
            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }
Ejemplo n.º 11
0
        public void CanSaveValidChanges()
        {
            // Arrange - create a controller
            var controller = new AdminController(this._mockRepository.Object);
            // Arrange - create a product
            Product product = new Product { Name = "Test" };

            // Action - try to save the product
            ActionResult result = controller.Edit(product, null);

            // Assert - check that the repository was called
            this._mockRepository.Verify(m => m.SaveProduct(product));
            // Assert - check the method result type
            Assert.IsNotInstanceOfType(result, typeof(ViewResult));
        }
Ejemplo n.º 12
0
        public ActionResult Edit(Product product, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                if (image != null)
                {
                    product.ImageMimeType = image.ContentType;
                    product.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(product.ImageData, 0, image.ContentLength);
                }

                _productRepository.SaveProduct(product);
                TempData["message"] = string.Format("Product {0} has been saved", product.Name);
                return RedirectToAction("Index");
            }
            else
            {
                // there is something wrong with the data values
                return View(product);
            }
        }
Ejemplo n.º 13
0
        public void SaveProduct(Product product)
        {
            if (product.ProductId == 0)
            {
                context.Products.Add(product);
            }
            else
            {
                context.Entry(product).State = EntityState.Modified;
            }

            context.SaveChanges();
        }
Ejemplo n.º 14
0
 public void DeleteProduct(Product product)
 {
     context.Products.Remove(product);
     context.SaveChanges();
 }