public function testHandleWithOriginalProductAttributes() { $originalAttributes = [['id' => 1, 'values' => [['value_index' => 0, 'is_percent' => 10, 'pricing_value' => 50]]]]; $this->productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE)); $this->productTypeMock->expects($this->once())->method('getConfigurableAttributesAsArray')->with($this->productMock)->will($this->returnValue($originalAttributes)); $attributeData = [['attribute_id' => 1, 'values' => [['value_index' => 0, 'pricing_value' => 10, 'is_percent' => 1], ['value_index' => 1, 'pricing_value' => 100, 'is_percent' => 200]]]]; $this->productMock->expects($this->once())->method('getConfigurableAttributesData')->will($this->returnValue($attributeData)); $expected = [['attribute_id' => 1, 'values' => [['value_index' => 0, 'pricing_value' => 50, 'is_percent' => 10], ['value_index' => 1, 'pricing_value' => 0, 'is_percent' => 0]]]]; $this->productMock->expects($this->once())->method('setConfigurableAttributesData')->with($expected); $this->model->handle($this->productMock); }
/** * @expectedException \Magento\Framework\Exception\StateException * @expectedExceptionMessage Product has been already attached */ public function testAddChildStateException() { $productSku = 'configurable-sku'; $childSku = 'simple-sku'; $configurable = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $configurable->expects($this->any())->method('getId')->will($this->returnValue(666)); $simplee = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $simplee->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->productRepository->expects($this->at(0))->method('get')->with($productSku)->will($this->returnValue($configurable)); $this->productRepository->expects($this->at(1))->method('get')->with($childSku)->will($this->returnValue($simplee)); $this->configurableType->expects($this->once())->method('getChildrenIds')->with(666)->will($this->returnValue([0 => [1, 2, 3]])); $this->assertTrue(true, $this->service->addChild($productSku, $childSku)); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGenerateSimpleProducts() { $productsData = [6 => ['image' => 'image.jpg', 'name' => 'config-red', 'configurable_attribute' => '{"new_attr":"6"}', 'sku' => 'config-red', 'quantity_and_stock_status' => ['qty' => ''], 'weight' => '333']]; $stockData = ['manage_stock' => '0', 'use_config_enable_qty_increments' => '1', 'use_config_qty_increments' => '1', 'use_config_manage_stock' => 0, 'is_decimal_divided' => 0]; $parentProductMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['__wakeup', 'hasData', 'getData', 'getNewVariationsAttributeSetId', 'getStockData', 'getQuantityAndStockStatus', 'getWebsiteIds'])->disableOriginalConstructor()->getMock(); $newSimpleProductMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['__wakeup', 'save', 'getId', 'setStoreId', 'setTypeId', 'setAttributeSetId', 'getTypeInstance', 'getStoreId', 'addData', 'setWebsiteIds', 'setStatus', 'setVisibility'])->disableOriginalConstructor()->getMock(); $attributeMock = $this->getMockBuilder('\\Magento\\Eav\\Model\\Entity\\Attribute')->setMethods(['isInSet', 'setAttributeSetId', 'setAttributeGroupId', 'save'])->disableOriginalConstructor()->getMock(); $attributeSetMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\Set')->setMethods(['load', 'addSetInfo', 'getDefaultGroupId'])->disableOriginalConstructor()->getMock(); $eavEntityMock = $this->getMockBuilder('\\Magento\\Eav\\Model\\Entity')->setMethods(['setType', 'getTypeId'])->disableOriginalConstructor()->getMock(); $productTypeMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Type')->setMethods(['getEditableAttributes'])->disableOriginalConstructor()->getMock(); $editableAttributeMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute')->setMethods(['getIsUnique', 'getAttributeCode', 'getFrontend', 'getIsVisible'])->disableOriginalConstructor()->getMock(); $frontendAttributeMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\Frontend')->setMethods(['getInputType'])->disableOriginalConstructor()->getMock(); $parentProductMock->expects($this->once())->method('hasData')->with('_cache_instance_used_product_attributes')->willReturn(true); $parentProductMock->expects($this->once())->method('getData')->with('_cache_instance_used_product_attributes')->willReturn([$attributeMock]); $parentProductMock->expects($this->any())->method('getNewVariationsAttributeSetId')->willReturn('new_attr_set_id'); $this->_attributeSetFactory->expects($this->once())->method('create')->willReturn($attributeSetMock); $attributeSetMock->expects($this->once())->method('load')->with('new_attr_set_id')->willReturnSelf(); $this->_entityFactoryMock->expects($this->once())->method('create')->willReturn($eavEntityMock); $eavEntityMock->expects($this->once())->method('setType')->with('catalog_product')->willReturnSelf(); $eavEntityMock->expects($this->once())->method('getTypeId')->willReturn('type_id'); $attributeSetMock->expects($this->once())->method('addSetInfo')->with('type_id', [$attributeMock]); $attributeMock->expects($this->once())->method('isInSet')->with('new_attr_set_id')->willReturn(false); $attributeMock->expects($this->once())->method('setAttributeSetId')->with('new_attr_set_id')->willReturnSelf(); $attributeSetMock->expects($this->once())->method('getDefaultGroupId')->with('new_attr_set_id')->willReturn('default_group_id'); $attributeMock->expects($this->once())->method('setAttributeGroupId')->with('default_group_id')->willReturnSelf(); $attributeMock->expects($this->once())->method('save')->willReturnSelf(); $this->_productFactoryMock->expects($this->once())->method('create')->willReturn($newSimpleProductMock); $this->_coreDataMock->expects($this->once())->method('jsonDecode')->with('{"new_attr":"6"}')->willReturn(['new_attr' => 6]); $newSimpleProductMock->expects($this->once())->method('setStoreId')->with(0)->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('setTypeId')->with('simple')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('setAttributeSetId')->with('new_attr_set_id')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('getTypeInstance')->willReturn($productTypeMock); $productTypeMock->expects($this->once())->method('getEditableAttributes')->with($newSimpleProductMock)->willReturn([$editableAttributeMock]); $editableAttributeMock->expects($this->once())->method('getIsUnique')->willReturn(false); $editableAttributeMock->expects($this->once())->method('getAttributeCode')->willReturn('some_code'); $editableAttributeMock->expects($this->any())->method('getFrontend')->willReturn($frontendAttributeMock); $frontendAttributeMock->expects($this->any())->method('getInputType')->willReturn('input_type'); $editableAttributeMock->expects($this->any())->method('getIsVisible')->willReturn(false); $parentProductMock->expects($this->once())->method('getStockData')->willReturn($stockData); $parentProductMock->expects($this->once())->method('getQuantityAndStockStatus')->willReturn(['is_in_stock' => 1]); $newSimpleProductMock->expects($this->once())->method('getStoreId')->willReturn('store_id'); $this->_stockConfiguration->expects($this->once())->method('getManageStock')->with('store_id')->willReturn(1); $newSimpleProductMock->expects($this->once())->method('addData')->willReturnSelf(); $parentProductMock->expects($this->once())->method('getWebsiteIds')->willReturn('website_id'); $newSimpleProductMock->expects($this->once())->method('setWebsiteIds')->with('website_id')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('setStatus')->with(1)->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('setVisibility')->with(1)->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('save')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('getId')->willReturn('product_id'); $this->assertEquals(['product_id'], $this->_model->generateSimpleProducts($parentProductMock, $productsData)); }
/** * @param int $productStore * @param int $attributeStore * * @dataProvider getConfigurableAttributesAsArrayDataProvider */ public function testGetConfigurableAttributesAsArray($productStore, $attributeStore) { $attributeSource = $this->getMockForAbstractClass('Magento\\Eav\\Model\\Entity\\Attribute\\Source\\AbstractSource', array(), '', false, true, true, array('getAllOptions')); $attributeSource->expects($this->any())->method('getAllOptions')->will($this->returnValue([])); $attributeFrontend = $this->getMockForAbstractClass('Magento\\Eav\\Model\\Entity\\Attribute\\Frontend\\AbstractFrontend', array(), '', false, true, true, array('getLabel')); $attributeFrontend->expects($this->any())->method('getLabel')->will($this->returnValue('Label')); $eavAttribute = $this->getMock('Magento\\Catalog\\Model\\Resource\\Eav\\Attribute', array('getFrontend', 'getSource', 'getStoreLabel', '__wakeup', 'setStoreId', '__sleep'), array(), '', false); $eavAttribute->expects($this->any())->method('getFrontend')->will($this->returnValue($attributeFrontend)); $eavAttribute->expects($this->any())->method('getSource')->will($this->returnValue($attributeSource)); $eavAttribute->expects($this->any())->method('getStoreLabel')->will($this->returnValue('Store Label')); $eavAttribute->expects($this->any())->method('setStoreId')->with($attributeStore); $attribute = $this->getMockBuilder('\\Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable\\Attribute')->disableOriginalConstructor()->setMethods(['getProductAttribute', '__wakeup', '__sleep'])->getMock(); $attribute->expects($this->any())->method('getProductAttribute')->will($this->returnValue($eavAttribute)); $product = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['getStoreId', 'getData', 'hasData', '__wakeup', '__sleep'])->disableOriginalConstructor()->getMock(); $product->expects($this->any())->method('getStoreId')->will($this->returnValue($productStore)); $product->expects($this->any())->method('hasData')->will($this->returnValueMap([['_cache_instance_configurable_attributes', 1]])); $product->expects($this->any())->method('getData')->will($this->returnValueMap([['_cache_instance_configurable_attributes', null, [$attribute]]])); $result = $this->_model->getConfigurableAttributesAsArray($product); $this->assertCount(1, $result); }
public function testgetUsedProducts() { $attributeCollection = $this->getMockBuilder('\\Magento\\ConfigurableProduct\\Model\\Resource\\Product\\Type\\Configurable\\Attribute\\Collection')->setMethods(['setProductFilter', 'addFieldToFilter', 'walk'])->disableOriginalConstructor()->getMock(); $attributeCollection->expects($this->any())->method('setProductFilter')->will($this->returnSelf()); $this->_attributeCollectionFactory->expects($this->any())->method('create')->will($this->returnValue($attributeCollection)); $product = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['dataHasChangedFor', 'getConfigurableAttributesData', 'getStoreId', 'getId', 'getData', 'hasData', 'getAssociatedProductIds', '__wakeup', '__sleep'])->disableOriginalConstructor()->getMock(); $attributeData = [1 => ['id' => 1, 'code' => 'someattr', 'attribute_id' => 111, 'position' => 0, 'label' => 'Some Super Attribute', 'values' => []]]; $product->expects($this->any())->method('getConfigurableAttributesData')->will($this->returnValue($attributeData)); $product->expects($this->any())->method('getStoreId')->will($this->returnValue(5)); $product->expects($this->any())->method('getId')->will($this->returnValue(1)); $product->expects($this->any())->method('getAssociatedProductIds')->will($this->returnValue([2])); $product->expects($this->any())->method('hasData')->will($this->returnValueMap([['_cache_instance_used_product_attribute_ids', 1], ['_cache_instance_products', 0], ['_cache_instance_configurable_attributes', 1]])); $product->expects($this->any())->method('getData')->will($this->returnValue(1)); $productCollection = $this->getMockBuilder('Magento\\ConfigurableProduct\\Model\\Resource\\Product\\Type\\Configurable\\Product\\Collection')->setMethods(['setFlag', 'setProductFilter', 'addStoreFilter', 'addAttributeToSelect', 'addFilterByRequiredOptions', 'setStoreId'])->disableOriginalConstructor()->getMock(); $productCollection->expects($this->any())->method('addAttributeToSelect')->will($this->returnSelf()); $productCollection->expects($this->any())->method('setProductFilter')->will($this->returnSelf()); $productCollection->expects($this->any())->method('setFlag')->will($this->returnSelf()); $productCollection->expects($this->any())->method('addFilterByRequiredOptions')->will($this->returnSelf()); $productCollection->expects($this->any())->method('setStoreId')->with(5)->will($this->returnValue([])); $this->_productCollectionFactory->expects($this->any())->method('create')->will($this->returnValue($productCollection)); $this->_model->getUsedProducts($product); }