/** * {@inheritdoc} */ public function getList($productSku) { $options = []; $product = $this->getProduct($productSku); foreach ($this->getConfigurableAttributesCollection($product) as $option) { $options[] = $this->optionConverter->convertFromModel($option); } return $options; }
public function testGet() { $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(ConfigurableType::TYPE_CODE)); $this->productType->expects($this->once())->method('getConfigurableAttributeCollection')->with($this->equalTo($this->product))->will($this->returnValue($this->configurableAttributeCollection)); $this->configurableAttributeCollection->expects($this->once())->method('addFieldToFilter')->with(self::ATTRIBUTE_ID_FIELD_NAME, $optionId); $this->configurableAttributeCollection->expects($this->once())->method('getFirstItem')->will($this->returnValue($this->option)); $this->option->expects($this->once())->method('getId')->will($this->returnValue($optionId)); $this->attributeResource->expects($this->once())->method('getIdFieldName')->will($this->returnValue(self::ATTRIBUTE_ID_FIELD_NAME)); $this->configurableAttributeCollection->expects($this->once())->method('getResource')->will($this->returnValue($this->attributeResource)); $this->optionConverter->expects($this->once())->method('convertFromModel')->with($this->equalTo($this->option))->will($this->returnValue($this->metadata)); $this->assertEquals($this->metadata, $this->model->get($productSku, $optionId)); }
/** * {@inheritdoc} */ public function update($productSku, $optionId, Option $option) { $product = $this->getProduct($productSku); $configurableAttribute = $this->configurableAttributeFactory->create(); $configurableAttribute->load($optionId); if (!$configurableAttribute->getId() || $configurableAttribute->getProductId() != $product->getId()) { throw new NoSuchEntityException('Option with id "%1" not found', [$optionId]); } $configurableAttribute = $this->optionConverter->getModelFromData($option, $configurableAttribute); try { $configurableAttribute->save(); } catch (\Exception $e) { throw new CouldNotSaveException('Could not update option with id "%1"', [$optionId]); } return true; }
/** * #@expectedException \Magento\Framework\Exception\CouldNotSaveException */ public function testUpdateCouldNotSaveException() { $productSku = 'productSku'; $optionId = 3; $this->productRepositoryMock->expects($this->once())->method('get')->with($this->equalTo($productSku))->will($this->returnValue($this->productMock)); $this->productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(ConfigurableType::TYPE_CODE)); $this->productMock->expects($this->any())->method('getId')->will($this->returnValue($optionId)); $this->confAttributeFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->attributeMock)); $this->attributeMock->expects($this->once())->method('load')->with($this->equalTo($optionId)); $this->attributeMock->expects($this->any())->method('getId')->will($this->returnValue($optionId)); $this->attributeMock->expects($this->any())->method('getProductId')->will($this->returnValue($optionId)); $this->attributeMock->expects($this->any())->method('save')->will($this->returnCallback(function () { throw new \Exception(); })); $this->optionConverterMock->expects($this->once())->method('getModelFromData')->with($this->equalTo($this->optionMock), $this->equalTo($this->attributeMock))->will($this->returnValue($this->attributeMock)); $this->writeService->update($productSku, $optionId, $this->optionMock); }
public function testGetModelFromData() { $data = ['data']; $id = 33; $this->configurableAttribute->expects($this->any())->method('getData')->will($this->returnValue($data)); $this->configurableAttribute->expects($this->once())->method('setData')->with($this->equalTo($data)); $this->configurableAttribute->expects($this->once())->method('addData')->with($this->equalTo($data)); $this->configurableAttribute->expects($this->any())->method('getId')->will($this->returnValue($id)); $this->configurableAttribute->expects($this->once())->method('setId')->with($this->equalTo($id)); $this->configurableAttribute->expects($this->any())->method('getAttributeId')->will($this->returnValue($id)); $this->configurableAttribute->expects($this->once())->method('setAttributeId')->with($this->equalTo($id)); $this->configurableAttribute->expects($this->any())->method('getValues')->will($this->returnValue($data)); $this->configurableAttribute->expects($this->any())->method('setValues')->with($this->equalTo($data)); $this->option->expects($this->any())->method('getValues')->will($this->returnValue([$this->value])); $this->option->expects($this->any())->method('__toArray')->will($this->returnValue($data)); $this->valueConverter->expects($this->any())->method('convertArrayFromData')->with($this->equalTo($this->value))->will($this->returnValue($data[0])); $result = $this->converter->getModelFromData($this->option, $this->configurableAttribute); $this->assertEquals($this->configurableAttribute, $result); }