/** * @param CatalogProduct $product * @param array $identities * @return string[] */ public function afterGetIdentities(CatalogProduct $product, array $identities) { foreach ($this->type->getParentIdsByChild($product->getId()) as $parentId) { $identities[] = CatalogProduct::CACHE_TAG . '_' . $parentId; } return $identities; }
public function testAfterGetIdentities() { $baseIdentities = ['SomeCacheId', 'AnotherCacheId']; $id = 12345; $parentIds = [1, 2, 5, 100500]; $expectedIdentities = ['SomeCacheId', 'AnotherCacheId', Product::CACHE_TAG . '_' . 1, Product::CACHE_TAG . '_' . 2, Product::CACHE_TAG . '_' . 5, Product::CACHE_TAG . '_' . 100500]; $this->product->expects($this->once())->method('getId')->will($this->returnValue($id)); $this->type->expects($this->once())->method('getParentIdsByChild')->with($id)->will($this->returnValue($parentIds)); $identities = $this->plugin->afterGetIdentities($this->product, $baseIdentities); $this->assertEquals($expectedIdentities, $identities); }
/** * {@inheritdoc} */ public function getList($productSku) { $product = $this->getProduct($productSku); $optionCollection = $this->type->getOptionsCollection($product); /** @var \Magento\Bundle\Service\V1\Data\Product\Option[] $optionDtoList */ $optionDtoList = []; /** @var \Magento\Bundle\Model\Option $option */ foreach ($optionCollection as $option) { $optionDtoList[] = $this->optionConverter->createDataFromModel($option, $product); } return $optionDtoList; }
public function testGetIdentities() { $identities = array('id1', 'id2'); $productMock = $this->getMock('Magento\\Catalog\\Model\\Product', array(), array(), '', false); $optionMock = $this->getMock('\\Magento\\Bundle\\Model\\Option', array('getSelections', '__wakeup'), array(), '', false); $optionCollectionMock = $this->getMock('Magento\\Bundle\\Model\\Resource\\Option\\Collection', array(), array(), '', false); $cacheKey = '_cache_instance_options_collection'; $productMock->expects($this->once())->method('getIdentities')->will($this->returnValue($identities)); $productMock->expects($this->once())->method('hasData')->with($cacheKey)->will($this->returnValue(true)); $productMock->expects($this->once())->method('getData')->with($cacheKey)->will($this->returnValue($optionCollectionMock)); $optionCollectionMock->expects($this->once())->method('getItems')->will($this->returnValue(array($optionMock))); $optionMock->expects($this->exactly(2))->method('getSelections')->will($this->returnValue(array($productMock))); $this->assertEquals($identities, $this->_model->getIdentities($productMock)); }
/** * {@inheritdoc} */ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, \Magento\Bundle\Api\Data\OptionInterface $option) { $option->setStoreId($this->storeManager->getStore()->getId()); $option->setParentId($product->getId()); $optionId = $option->getOptionId(); $linksToAdd = []; if (!$optionId) { $option->setDefaultTitle($option->getTitle()); if (is_array($option->getProductLinks())) { $linksToAdd = $option->getProductLinks(); } } else { $optionCollection = $this->type->getOptionsCollection($product); /** @var \Magento\Bundle\Model\Option $existingOption */ $existingOption = $optionCollection->getItemById($option->getOptionId()); if (!isset($existingOption) || !$existingOption->getOptionId()) { throw new NoSuchEntityException(__('Requested option doesn\'t exist')); } $option->setData(array_merge($existingOption->getData(), $option->getData())); $this->updateOptionSelection($product, $option); } try { $this->optionResource->save($option); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not save option'), $e); } /** @var \Magento\Bundle\Api\Data\LinkInterface $linkedProduct */ foreach ($linksToAdd as $linkedProduct) { $this->linkManagement->addChild($product, $option->getOptionId(), $linkedProduct); } return $option->getOptionId(); }
/** * {@inheritdoc} */ public function deleteById($sku, $optionId) { $product = $this->getProduct($sku); $optionCollection = $this->type->getOptionsCollection($product); $optionCollection->setIdFilter($optionId); return $this->delete($optionCollection->getFirstItem()); }
public function testSave() { $options = ['some_option' => ['option_id' => '', 'delete' => false]]; $selections = ['some_option' => [123 => ['selection_id' => '', 'delete' => false]]]; $resource = $this->getMockBuilder('Magento\\Bundle\\Model\\Resource\\Bundle')->disableOriginalConstructor()->getMock(); $this->bundleFactory->expects($this->once())->method('create')->willReturn($resource); $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->setMethods(['getStoreId', 'getOrigData', 'getData', 'getBundleOptionsData', 'getBundleSelectionsData'])->disableOriginalConstructor()->getMock(); $product->expects($this->once())->method('getBundleOptionsData')->willReturn($options); $product->expects($this->once())->method('getBundleSelectionsData')->willReturn($selections); $option = $this->getMockBuilder('Magento\\Bundle\\Model\\Resource\\Option\\Collection')->setMethods(['setData', 'setParentId', 'setStoreId', 'isDeleted', 'save', 'getOptionId'])->disableOriginalConstructor()->getMock(); $option->expects($this->once())->method('setData')->willReturnSelf(); $option->expects($this->once())->method('setParentId')->willReturnSelf(); $option->expects($this->once())->method('setStoreId')->willReturnSelf(); $this->bundleOptionFactory->expects($this->once())->method('create')->will($this->returnValue($option)); $selection = $this->getMockBuilder('Magento\\Bundle\\Model\\Selection')->setMethods(['setData', 'setOptionId', 'setParentProductId', 'setWebsiteId', 'save'])->disableOriginalConstructor()->getMock(); $selection->expects($this->once())->method('setData')->willReturnSelf(); $selection->expects($this->once())->method('setOptionId')->willReturnSelf(); $selection->expects($this->once())->method('setParentProductId')->willReturnSelf(); $selection->expects($this->once())->method('setWebsiteId')->willReturnSelf(); $selection->expects($this->once())->method('setParentProductId')->willReturnSelf(); $this->bundleModelSelection->expects($this->once())->method('create')->willReturn($selection); $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->setMethods(['getWebsiteId', '__wakeup'])->disableOriginalConstructor()->getMock(); $this->storeManager->expects($this->once())->method('getStore')->will($this->returnValue($store)); $store->expects($this->once())->method('getWebsiteId')->will($this->returnValue(10)); $this->model->save($product); }
public function testGetList() { $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)); $this->productType->expects($this->once())->method('getOptionsCollection')->with($this->equalTo($this->product))->will($this->returnValue([$this->optionModel])); $this->optionConverter->expects($this->once())->method('createDataFromModel')->with($this->equalTo($this->optionModel), $this->equalTo($this->product))->will($this->returnValue($this->option)); $this->assertEquals([$this->option], $this->model->getList($productSku)); }
/** * @expectedException \Magento\Framework\Exception\NoSuchEntityException */ public function testRemoveNoSuchEntityException() { $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->optionModel->expects($this->once())->method('getId'); $this->model->remove($productSku, $optionId); }
public function testIsSalableNoManageStock() { $option1 = $this->getRequiredOptionMock(10, 10); $option2 = $this->getRequiredOptionMock(20, 10); $stockItem = $this->getStockItem(true); $this->stockRegistry->method('getStockItem')->willReturn($stockItem); $this->stockState->expects($this->at(0))->method('getStockQty')->with(10)->willReturn(10); $this->stockState->expects($this->at(1))->method('getStockQty')->with(20)->willReturn(10); $optionCollectionMock = $this->getOptionCollectionMock([$option1, $option2]); $selectionCollectionMock = $this->getSelectionCollectionMock([$option1, $option2]); $product = new \Magento\Framework\Object(['is_salable' => true, '_cache_instance_options_collection' => $optionCollectionMock, 'status' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED, '_cache_instance_selections_collection10_20' => $selectionCollectionMock]); $this->assertTrue($this->model->isSalable($product)); }
public function testHasOptions() { $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['_wakeup', 'hasData', 'getData', 'setData', 'getId', 'getStoreId'])->getMock(); $optionCollection = $this->getMockBuilder('Magento\\Bundle\\Model\\ResourceModel\\Option\\Collection')->disableOriginalConstructor()->setMethods(['getAllIds'])->getMock(); $selectionCollection = $this->getMockBuilder('\\Magento\\Bundle\\Model\\ResourceModel\\Selection\\Collection')->disableOriginalConstructor()->getMock(); $product->expects($this->once())->method('getStoreId')->willReturn('storeId'); $product->expects($this->once())->method('setData')->with('_cache_instance_store_filter', 'storeId')->willReturnSelf(); $product->expects($this->any())->method('hasData')->willReturn(true); $product->expects($this->at(3))->method('getData')->with('_cache_instance_options_collection')->willReturn($optionCollection); $optionCollection->expects($this->once())->method('getAllIds')->willReturn(['ids']); $product->expects($this->at(5))->method('getData')->with('_cache_instance_selections_collectionids')->willReturn([$selectionCollection]); $this->assertTrue($this->model->hasOptions($product)); }
/** * {@inheritdoc} */ public function update($productSku, $optionId, \Magento\Bundle\Service\V1\Data\Product\Option $option) { $product = $this->getProduct($productSku); $optionCollection = $this->type->getOptionsCollection($product); $optionCollection->setIdFilter($optionId); /** @var \Magento\Bundle\Model\Option $optionModel */ $optionModel = $optionCollection->getFirstItem(); $updateOption = $this->optionConverter->getModelFromData($option, $optionModel); if (!$updateOption->getId()) { throw new NoSuchEntityException('Requested option doesn\'t exist'); } $updateOption->setStoreId($this->storeManager->getStore()->getId()); /** * @var Link[] $existingProductLinks */ $existingProductLinks = $optionModel->getProductLinks(); if (!is_array($existingProductLinks)) { $existingProductLinks = array(); } /** * @var Link[] $newProductLinks */ $newProductLinks = $option->getProductLinks(); if (is_null($newProductLinks)) { $newProductLinks = array(); } /** * @var Link[] $linksToDelete */ $linksToDelete = array_udiff($existingProductLinks, $newProductLinks, array($this, 'compareLinks')); foreach ($linksToDelete as $link) { $this->linkWriteService->removeChild($productSku, $option->getId(), $link->getSku()); } /** * @var Link[] $linksToAdd */ $linksToAdd = array_udiff($newProductLinks, $existingProductLinks, array($this, 'compareLinks')); foreach ($linksToAdd as $link) { $this->linkWriteService->addChild($productSku, $option->getId(), $link); } try { $updateOption->save(); } catch (\Exception $e) { throw new CouldNotSaveException('Could not save option', [], $e); } return true; }
/** * {@inheritdoc} */ public function update($productSku, $optionId, \Magento\Bundle\Service\V1\Data\Product\Option $option) { $product = $this->getProduct($productSku); $optionCollection = $this->type->getOptionsCollection($product); $optionCollection->setIdFilter($optionId); /** @var \Magento\Bundle\Model\Option $optionModel */ $optionModel = $optionCollection->getFirstItem(); $updateOption = $this->optionConverter->getModelFromData($option, $optionModel); if (!$updateOption->getId()) { throw new NoSuchEntityException('Requested option doesn\'t exist'); } $updateOption->setStoreId($this->storeManager->getStore()->getId()); try { $updateOption->save(); } catch (\Exception $e) { throw new CouldNotSaveException('Could not save option', [], $e); } return true; }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.NPathComplexity) */ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, \Magento\Bundle\Api\Data\OptionInterface $option) { $option->setStoreId($this->storeManager->getStore()->getId()); $option->setParentId($product->getId()); if (!$option->getOptionId()) { $option->setDefaultTitle($option->getTitle()); $linksToAdd = is_array($option->getProductLinks()) ? $option->getProductLinks() : []; } else { $optionCollection = $this->type->getOptionsCollection($product); $optionCollection->setIdFilter($option->getOptionId()); /** @var \Magento\Bundle\Model\Option $existingOption */ $existingOption = $optionCollection->getFirstItem(); if (!$existingOption->getOptionId()) { throw new NoSuchEntityException(__('Requested option doesn\'t exist')); } $option->setData(array_merge($existingOption->getData(), $option->getData())); /** @var \Magento\Bundle\Api\Data\LinkInterface[] $existingLinks */ $existingLinks = is_array($existingOption->getProductLinks()) ? $existingOption->getProductLinks() : []; /** @var \Magento\Bundle\Api\Data\LinkInterface[] $newProductLinks */ $newProductLinks = is_array($option->getProductLinks()) ? $option->getProductLinks() : []; /** @var \Magento\Bundle\Api\Data\LinkInterface[] $linksToDelete */ $linksToDelete = array_udiff($existingLinks, $newProductLinks, [$this, 'compareLinks']); foreach ($linksToDelete as $link) { $this->linkManagement->removeChild($product->getSku(), $option->getOptionId(), $link->getSku()); } /** @var \Magento\Bundle\Api\Data\LinkInterface[] $linksToAdd */ $linksToAdd = array_udiff($newProductLinks, $existingLinks, [$this, 'compareLinks']); } try { $this->optionResource->save($option); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not save option'), $e); } /** @var \Magento\Bundle\Api\Data\LinkInterface $linkedProduct */ foreach ($linksToAdd as $linkedProduct) { $this->linkManagement->addChild($product, $option->getOptionId(), $linkedProduct); } return $option->getOptionId(); }