示例#1
0
 /**
  * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  */
 public function testAddCouldNotSaveException()
 {
     $productSku = 'oneSku';
     $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));
     $storeId = 1;
     $this->store->expects($this->once())->method('getId')->will($this->returnValue($storeId));
     $this->optionModel->expects($this->once())->method('setStoreId')->with($this->equalTo($storeId));
     $this->optionModel->expects($this->once())->method('save')->will($this->returnCallback(function () {
         throw new \Exception();
     }));
     $this->optionConverter->expects($this->once())->method('createModelFromData')->with($this->equalTo($this->option), $this->equalTo($this->product))->will($this->returnValue($this->optionModel));
     $this->assertEquals($this->option, $this->model->add($productSku, $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;
 }