public function testPrepare() { $qty = 3000000000; $customPrice = 400000000; $this->itemMock->expects($this->any())->method('addQty')->will($this->returnValue($qty)); $this->itemMock->expects($this->any())->method('setCustomPrice')->will($this->returnValue($customPrice)); $this->itemMock->expects($this->any())->method('setOriginalCustomPrice')->will($this->returnValue($customPrice)); $this->itemMock->expects($this->any())->method('addQty')->will($this->returnValue($qty)); $this->productMock->expects($this->any())->method('getCartQty')->will($this->returnValue($qty)); $this->objectMock->expects($this->any())->method('getCustomPrice')->will($this->returnValue($customPrice)); $this->processor->prepare($this->itemMock, $this->objectMock, $this->productMock); }
/** * Advanced func to add product to quote - processing mode can be specified there. * Returns error message if product type instance can't prepare product. * * @param mixed $product * @param null|float|\Magento\Framework\Object $request * @param null|string $processMode * @return \Magento\Sales\Model\Quote\Item|string * @throws \Magento\Framework\Model\Exception */ public function addProduct(\Magento\Catalog\Model\Product $product, $request = null, $processMode = \Magento\Catalog\Model\Product\Type\AbstractType::PROCESS_MODE_FULL) { if ($request === null) { $request = 1; } if (is_numeric($request)) { $request = $this->objectFactory->create(['qty' => $request]); } if (!$request instanceof \Magento\Framework\Object) { throw new \Magento\Framework\Model\Exception(__('We found an invalid request for adding product to quote.')); } $cartCandidates = $product->getTypeInstance()->prepareForCartAdvanced($request, $product, $processMode); /** * Error message */ if (is_string($cartCandidates)) { return $cartCandidates; } /** * If prepare process return one object */ if (!is_array($cartCandidates)) { $cartCandidates = array($cartCandidates); } $parentItem = null; $errors = array(); $item = null; $items = array(); foreach ($cartCandidates as $candidate) { // Child items can be sticked together only within their parent $stickWithinParent = $candidate->getParentProductId() ? $parentItem : null; $candidate->setStickWithinParent($stickWithinParent); $item = $this->getItemByProduct($candidate); if (!$item) { $item = $this->itemProcessor->init($candidate, $request); // Add only item that is not in quote already $this->addItem($item); } $items[] = $item; /** * As parent item we should always use the item of first added product */ if (!$parentItem) { $parentItem = $item; } if ($parentItem && $candidate->getParentProductId()) { $item->setParentItem($parentItem); } $this->itemProcessor->prepare($item, $request, $candidate); // collect errors instead of throwing first one if ($item->getHasError()) { $message = $item->getMessage(); if (!in_array($message, $errors)) { // filter duplicate messages $errors[] = $message; } } } if (!empty($errors)) { throw new \Magento\Framework\Model\Exception(implode("\n", $errors)); } $this->_eventManager->dispatch('sales_quote_product_add_after', ['items' => $items]); return $item; }