public function testCreateProductDataFromModel() { $productModelMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $attrCodes = ['sku', 'price', 'status', 'updatedAt', 'entity_id']; $this->productBuilder->expects($this->once())->method('getCustomAttributesCodes')->will($this->returnValue($attrCodes)); $attributes = [ProductDataObject::SKU => ProductDataObject::SKU . 'value', ProductDataObject::PRICE => ProductDataObject::PRICE . 'value', ProductDataObject::STATUS => ProductDataObject::STATUS . 'dataValue']; $this->productBuilder->expects($this->once())->method('populateWithArray')->with($attributes); $this->productBuilder->expects($this->once())->method('getData')->will($this->returnValue($attributes)); $this->productBuilder->expects($this->once())->method('create')->will($this->returnValue(new ProductDataObject($this->productBuilder))); $dataUsingMethodCallback = $this->returnCallback(function ($attrCode) { if (in_array($attrCode, ['sku', 'price', 'entity_id'])) { return $attrCode . 'value'; } return null; }); $productModelMock->expects($this->exactly(count($attrCodes)))->method('getDataUsingMethod')->will($dataUsingMethodCallback); $dataCallback = $this->returnCallback(function ($attrCode) { if ($attrCode == 'status') { return $attrCode . 'dataValue'; } return null; }); $productModelMock->expects($this->exactly(2))->method('getData')->will($dataCallback); $this->converter = new Converter($this->productBuilder); $productData = $this->converter->createProductDataFromModel($productModelMock); $this->assertEquals(ProductDataObject::SKU . 'value', $productData->getSku()); $this->assertEquals(ProductDataObject::PRICE . 'value', $productData->getPrice()); $this->assertEquals(ProductDataObject::STATUS . 'dataValue', $productData->getStatus()); $this->assertEquals(null, $productData->getUpdatedAt()); }
/** * {@inheritdoc} */ public function load($id, \Magento\Catalog\Service\V1\Data\ProductBuilder $productBuilder) { /** @var \Magento\Catalog\Model\Product */ $product = $this->productRepository->get($id); if ($product->getTypeId() != ProductType::TYPE_BUNDLE) { return; } $productBuilder->setCustomAttribute('bundle_product_options', $this->optionReadService->getList($product->getSku())); }
public function testLoadBundleProduct() { $productId = 'test_id'; $productSku = 'test_sku'; $this->productRepository->expects($this->once())->method('get')->with($productId)->will($this->returnValue($this->product)); $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)); $this->product->expects($this->once())->method('getSku')->will($this->returnValue($productSku)); $optionCustomAttributeValue = ['a', 'b']; $this->optionReadService->expects($this->once())->method('getList')->with($productSku)->will($this->returnValue($optionCustomAttributeValue)); $this->productBuilder->expects($this->at(0))->method('setCustomAttribute')->with('bundle_product_options', $optionCustomAttributeValue); $this->model->load($productId, $this->productBuilder); }
/** * Loads the values from a product model * * @param \Magento\Catalog\Model\Product $productModel * @return void */ protected function _populateBuilderWithAttributes(\Magento\Catalog\Model\Product $productModel) { $attributes = array(); foreach ($this->productBuilder->getCustomAttributesCodes() as $attrCode) { $value = $productModel->getDataUsingMethod($attrCode); $value = $value ? $value : $productModel->getData($attrCode); if (null !== $value) { if ($attrCode != 'entity_id') { $attributes[$attrCode] = $value; } } } $this->productBuilder->populateWithArray($attributes); return; }
/** * Loads the values from a product model * * @param \Magento\Catalog\Model\Product $productModel * @return void */ protected function populateBuilderWithAttributes(\Magento\Catalog\Model\Product $productModel) { $attributes = array(); foreach ($productModel->getAttributes() as $attribute) { $attrCode = $attribute->getAttributeCode(); $value = $productModel->getDataUsingMethod($attrCode) ?: $productModel->getData($attrCode); if (null !== $value) { if ($attrCode != 'entity_id') { $attributes[$attrCode] = $value; } } } $attributes[ProductDataObject::STORE_ID] = $productModel->getStoreId(); $this->productBuilder->populateWithArray($attributes); return; }
/** * @magentoDataFixture Magento/Bundle/_files/product.php * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php * @magentoAppIsolation enabled */ public function testUpdateBundleProduct() { // get existing bundle product $savedProduct = $this->productService->get('bundle-product'); /** @var Link $newLink */ $newLink = $this->linkBuilder->setSku('simple2')->create(); /** @var Link[] $links */ $links = array($newLink); /** @var Option $newOption */ $newOption = $this->optionBuilder->setProductLinks($links)->create(); /** @var Product bundleProduct */ $updatedBundleProduct = $this->productBuilder->populate($savedProduct)->setCustomAttribute('bundle_product_options', array($newOption))->setCustomAttribute('price_view', 'test')->setCustomAttribute('price', 10)->create(); $this->assertEquals('bundle-product', $this->productService->update('bundle-product', $updatedBundleProduct)); $this->productRepository->get('bundle-product')->unsetData('_cache_instance_options_collection'); // load and confirm number of links and options $savedProduct = $this->productService->get('bundle-product'); /** @var Option[] $updatedOptions */ $savedOptions = $savedProduct->getCustomAttribute('bundle_product_options')->getValue(); $this->assertTrue(is_array($savedOptions)); $this->assertEquals(1, count($savedOptions)); $option = $savedOptions[0]; $linkedProducts = $option->getProductLinks(); $this->assertTrue(is_array($linkedProducts)); $this->assertEquals(1, count($linkedProducts)); $link = $linkedProducts[0]; $this->assertEquals('simple2', $link->getSku()); }
/** * @param array $configurableAttributeData * @dataProvider productVariationDataProvider */ public function testGenerateVariation($configurableAttributeData) { $attributeCode = 'code'; $attribute = $this->getMockBuilder('Magento\\Catalog\\Service\\V1\\Data\\Eav\\AttributeMetadata')->disableOriginalConstructor()->getMock(); $attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attributeCode)); $this->attributeReadService->expects($this->once())->method('info')->with($configurableAttributeData['attribute_id'])->will($this->returnValue($attribute)); $options = null; $this->variationMatrix->expects($this->any())->method('getVariations')->with([$configurableAttributeData['attribute_id'] => ["attribute_id" => $configurableAttributeData['attribute_id'], "values" => $configurableAttributeData['values'], "options" => $options, "attribute_code" => $attributeCode]])->will($this->returnValue([[$configurableAttributeData['attribute_id'] => ['value' => '14', 'label' => 'dd', 'price' => ['index' => 14, 'price' => 10]]]])); $product = $this->getMockBuilder('Magento\\Catalog\\Service\\V1\\Data\\Product')->disableOriginalConstructor()->getMock(); $product->expects($this->any())->method('getPrice')->will($this->returnValue(100)); $configurableAttribute = $this->getMockBuilder('Magento\\ConfigurableProduct\\Service\\V1\\Data\\Option')->disableOriginalConstructor()->getMock(); $configurableAttribute->expects($this->any())->method('__toArray')->will($this->returnValue($configurableAttributeData)); $configurableAttribute->expects($this->any())->method('getAttributeId')->will($this->returnValue($configurableAttributeData['attribute_id'])); $this->productBuilder->expects($this->any())->method('populute')->with($product); $this->productBuilder->expects($this->once())->method('setCustomAttribute')->with($attributeCode, 14); $this->productBuilder->expects($this->once())->method('setPrice')->with(110); $this->productBuilder->expects($this->once())->method('create')->will($this->returnValue($product)); $result = $this->object->generateVariation($product, [$configurableAttribute]); $this->assertCount(1, $result); $this->assertEquals([$product], $result); }
/** * Populate product with variation of attributes * * @param Product $product * @param array $variations * @param array $attributes * @return array */ private function populateProductVariation(Product $product, $variations, $attributes) { $products = []; foreach ($variations as $attributeId => $variation) { $price = $product->getPrice(); $this->productBuilder->populate($product); $suffix = ''; foreach ($variation as $attributeId => $valueInfo) { $suffix .= '-' . $valueInfo['value']; $this->productBuilder->setCustomAttribute($attributes[$attributeId]['attribute_code'], $valueInfo['value']); $priceInfo = $valueInfo['price']; $price += (!empty($priceInfo['is_percent']) ? $product->getPrice() / 100.0 : 1.0) * $priceInfo['price']; } $this->productBuilder->setPrice($price); $this->productBuilder->setName($product->getName() . $suffix); $this->productBuilder->setSku($product->getSku() . $suffix); $this->productBuilder->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE); $products[] = $this->productBuilder->create(); } return $products; }
public function testGetCustomAttributes() { $expectedAttributesCodes = ['attribute_code_1', 'attribute_code_2']; $this->assertEquals($expectedAttributesCodes, $this->_productBuilder->getCustomAttributesCodes()); }