/** * @expectedException \Magento\Framework\Exception\NoSuchEntityException */ public function testUpdateNoSuchEntityException() { $productSku = 'oneSku'; $optionId = 3; $this->productRepository->expects($this->once())->method('get')->with($this->equalTo($productSku))->will($this->returnValue($this->product)); $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)); $this->productType->expects($this->once())->method('getOptionsCollection')->with($this->equalTo($this->product))->will($this->returnValue($this->optionCollection)); $this->optionCollection->expects($this->once())->method('setIdFilter')->with($this->equalTo($optionId)); $this->optionCollection->expects($this->once())->method('getFirstItem')->will($this->returnValue($this->optionModel)); $this->optionConverter->expects($this->once())->method('getModelFromData')->with($this->equalTo($this->option), $this->equalTo($this->optionModel))->will($this->returnValue($this->optionModel)); $this->optionModel->expects($this->once())->method('getId'); $this->model->update($productSku, $optionId, $this->option); }
/** * Update bundle-related attributes of product. * * @param string $sku * @param Product $updatedProduct * @return string */ public function update($sku, Product $updatedProduct) { /** * @var Product $existingProduct */ $existingProduct = $this->productRepository->get($sku, true); if ($existingProduct->getTypeId() != ProductType::TYPE_BUNDLE) { return $sku; } /** * @var Option[] $existingProductOptions */ $existingProductOptions = $this->optionReadService->getList($sku); /** * @var Option[] $newProductOptions */ /** * @var AttributeValue $newProductOptionsAttrValue */ $newProductOptionsAttrValue = $updatedProduct->getCustomAttribute('bundle_product_options'); if (is_null($newProductOptionsAttrValue) || !is_array($newProductOptionsAttrValue->getValue())) { $newProductOptions = array(); } else { $newProductOptions = $newProductOptionsAttrValue->getValue(); } /** * @var Option[] $optionsToDelete */ $optionsToDelete = array_udiff($existingProductOptions, $newProductOptions, array($this, 'compareOptions')); foreach ($optionsToDelete as $option) { $this->optionWriteService->remove($sku, $option->getId()); } /** @var Option[] $optionsToUpdate */ $optionsToUpdate = array_uintersect($existingProductOptions, $newProductOptions, array($this, 'compareOptions')); foreach ($optionsToUpdate as $option) { $this->optionWriteService->update($sku, $option->getId(), $option); } /** * @var Option[] $optionsToAdd */ $optionsToAdd = array_udiff($newProductOptions, $existingProductOptions, array($this, 'compareOptions')); foreach ($optionsToAdd as $option) { $this->optionWriteService->add($sku, $option); } return $sku; }