/** * removeProduct. * * @dataProvider dataRemoveProduct * @group cart */ public function testRemoveProduct($quantity, $productId, $quantityExpected) { /** * @var ProductInterface $product */ $product = $this->getMock('Elcodi\\Component\\Product\\Entity\\Interfaces\\ProductInterface'); $productToRemove = $this->getMock('Elcodi\\Component\\Product\\Entity\\Interfaces\\ProductInterface'); $product->expects($this->any())->method('getId')->willReturn('1001'); $cart = new Cart(); $cartLine = new CartLine(); $cartLine->setPurchasable($product); $cart->setCartLines(new ArrayCollection([$cartLine])); $cartLine->setCart($cart); $this->assertCount(1, $cart->getCartLines()); $productToRemove->expects($this->any())->method('getId')->willReturn($productId); $this->cartManager->removePurchasable($cart, $productToRemove, $quantity); $this->assertCount($quantityExpected, $cart->getCartLines()); }
/** * Check CartLine integrity. * * When a purchasable is not enabled or its quantity is <=0, * the line is discarded and a ElcodiCartEvents::CART_INCONSISTENT * event is fired. * * A further check on stock availability is performed so that when * $quantity is greater that the available units, $quantity for this * CartLine is set to Purchasable::$stock number * * @param CartLineInterface $cartLine Cart line * * @return CartLineInterface CartLine */ private function validateCartLine(CartLineInterface $cartLine) { $cart = $cartLine->getCart(); $purchasable = $cartLine->getPurchasable(); $realStockAvailable = $this->purchasableStockValidator->isStockAvailable($purchasable, $cartLine->getQuantity(), $this->useStock); if (false === $realStockAvailable || $realStockAvailable === 0) { $this->cartManager->silentRemoveLine($cart, $cartLine); /** * An inconsistent cart event is dispatched. */ $this->cartEventDispatcher->dispatchCartInconsistentEvent($cart, $cartLine); return $cartLine; } if (is_int($realStockAvailable)) { $cartLine->setQuantity($realStockAvailable); } return $cartLine; }
/** * Check CartLine integrity * * When a purchasable is not enabled or its quantity is <=0, * the line is discarded and a ElcodiCartEvents::CART_INCONSISTENT * event is fired. * * A further check on stock availability is performed so that when * $quantity is greater that the available units, $quantity for this * CartLine is set to Purchasable::$stock number * * @param CartLineInterface $cartLine Cart line * * @return CartLineInterface CartLine */ protected function checkCartLine(CartLineInterface $cartLine) { $cart = $cartLine->getCart(); $purchasable = $cartLine->getPurchasable(); if (!$purchasable instanceof PurchasableInterface || !$purchasable->isEnabled() || $cartLine->getQuantity() <= 0) { $this->cartManager->silentRemoveLine($cart, $cartLine); /** * An inconsistent cart event is dispatched */ $this->cartEventDispatcher->dispatchCartInconsistentEvent($cart, $cartLine); } /** * We cannot exceed available stock for a given purchasable * when setting CartLine::$quantity */ if ($cartLine->getQuantity() > $purchasable->getStock()) { $cartLine->setQuantity($purchasable->getStock()); } return $cartLine; }
/** * Check CartLine integrity * * When a purchasable is not enabled or its quantity is <=0, * the line is discarded and a ElcodiCartEvents::CART_INCONSISTENT * event is fired. * * A further check on stock availability is performed so that when * $quantity is greater that the available units, $quantity for this * CartLine is set to Purchasable::$stock number * * @param CartLineInterface $cartLine Cart line * * @return CartLineInterface CartLine */ private function checkCartLine(CartLineInterface $cartLine) { $cart = $cartLine->getCart(); $purchasable = $cartLine->getPurchasable(); if (!$purchasable instanceof PurchasableInterface || !$purchasable->isEnabled() || $this->useStock && $cartLine->getQuantity() <= 0) { $this->cartManager->silentRemoveLine($cart, $cartLine); /** * An inconsistent cart event is dispatched */ $this->cartEventDispatcher->dispatchCartInconsistentEvent($cart, $cartLine); } /** * We cannot exceed available stock for a given purchasable * when setting CartLine::$quantity * * This checking has sense when the Product has not infinite stock */ if ($this->useStock && $cartLine->getProduct()->getStock() !== ElcodiProductStock::INFINITE_STOCK && $cartLine->getQuantity() > $purchasable->getStock()) { $cartLine->setQuantity($purchasable->getStock()); } return $cartLine; }
/** * addProduct * * @dataProvider dataAddProduct * @group cart */ public function testAddProduct($quantity, $productCreated, $quantityExpected) { $cartLine = new CartLine(); $this->cartLineFactory->expects($this->exactly(intval($productCreated)))->method('create')->will($this->returnValue($cartLine)); /** * @var ProductInterface $product */ $product = $this->getMock('Elcodi\\Component\\Product\\Entity\\Interfaces\\ProductInterface'); $cart = new Cart(); $cart->setCartLines(new ArrayCollection()); $this->cartManager->addProduct($cart, $product, $quantity); if ($productCreated) { $createdLine = $cart->getCartLines()->first(); $createdProduct = $createdLine->getProduct(); $this->assertCount(1, $cart->getCartLines()); $this->assertInstanceOf('Elcodi\\Component\\Cart\\Entity\\Interfaces\\CartLineInterface', $createdLine); $this->assertSame($createdProduct, $product); $this->assertEquals($createdLine->getQuantity(), $quantityExpected); } else { $this->assertCount(0, $cart->getCartLines()); } }