/** * @param $parentsIds * @param $validationResult * @param $runValidateAmount * @param $result * @dataProvider dataProviderForValidateWithValidConfigurableProduct * @return void */ public function testAroundValidateWithValidConfigurableProduct($parentsIds, $validationResult, $runValidateAmount, $result) { $closureMock = function () { return false; }; $this->productMock->expects($this->once())->method('getId')->willReturn('product_id'); $this->configurableMock->expects($this->once())->method('getParentIdsByChild')->with('product_id')->willReturn($parentsIds); $this->ruleMock->expects($this->exactly($runValidateAmount))->method('getConditions')->willReturn($this->ruleConditionsMock); $this->ruleConditionsMock->expects($this->exactly($runValidateAmount))->method('validateByEntityId')->willReturnMap($validationResult); $this->assertEquals($result, $this->validation->aroundValidate($this->ruleMock, $closureMock, $this->productMock)); }
/** * @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', '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(['getSetAttributes'])->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(); $this->configurableProduct->expects($this->once())->method('getUsedProductAttributes')->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); $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('getSetAttributes')->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('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)); }
protected function getAttributesFromConfigurable() { $this->productMock->expects($this->atLeastOnce())->method('getTypeInstance')->willReturn($this->configurableMock); $confAttribute = $this->getMock('\\Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable\\Attribute', [], [], '', false); $this->configurableMock->expects($this->any())->method('getConfigurableAttributes')->with($this->productMock)->willReturn([$confAttribute, $confAttribute]); $confAttribute->expects($this->any())->method('__call')->with('getProductAttribute')->willReturn($this->attributeMock); }
/** * @covers \Magento\ConfigurableProduct\Helper\Product\Options\Loader::load */ public function testLoad() { $option = ['value_index' => 23]; $this->product->expects(static::once())->method('getTypeInstance')->willReturn($this->configurable); $attribute = $this->getMockBuilder(Attribute::class)->disableOriginalConstructor()->setMethods(['getOptions', 'setValues'])->getMock(); $attributes = [$attribute]; $iterator = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock(); $iterator->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator($attributes)); $this->configurable->expects(static::once())->method('getConfigurableAttributeCollection')->with($this->product)->willReturn($iterator); $attribute->expects(static::once())->method('getOptions')->willReturn([$option]); $optionValue = $this->getMockForAbstractClass(OptionValueInterface::class); $this->optionValueFactory->expects(static::once())->method('create')->willReturn($optionValue); $optionValue->expects(static::once())->method('setValueIndex')->with($option['value_index']); $attribute->expects(static::once())->method('setValues')->with([$optionValue]); $options = $this->loader->load($this->product); static::assertSame([$attribute], $options); }
/** * @expectedException \Magento\Framework\Exception\NoSuchEntityException */ public function testRemoveNoSuchEntityException() { $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->productTypeMock->expects($this->once())->method('getConfigurableAttributeCollection')->with($this->equalTo($this->productMock))->will($this->returnValue($this->attributeCollectionMock)); $this->attributeCollectionMock->expects($this->once())->method('getItemById')->with($this->equalTo($optionId))->will($this->returnValue(null)); $this->writeService->remove($productSku, $optionId); }
public function testPrepareAttributeSet() { $productMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['getNewVariationsAttributeSetId'])->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(); $productMock->expects($this->once())->method('getNewVariationsAttributeSetId')->willReturn('new_attr_set_id'); $this->configurableProduct->expects($this->once())->method('getUsedProductAttributes')->with($productMock)->willReturn([$attributeMock]); $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->model->prepareAttributeSet($productMock); }
/** * @expectedException \Magento\Framework\Exception\NoSuchEntityException */ public function testRemoveChildInvalidChildSku() { $productSku = 'configurable'; $childSku = 'simple_10'; $this->product->expects($this->any())->method('getTypeId')->will($this->returnValue(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE)); $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product)); $option = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['getSku', 'getId', '__wakeup'])->disableOriginalConstructor()->getMock(); $option->expects($this->any())->method('getSku')->will($this->returnValue($childSku . '_invalid')); $option->expects($this->any())->method('getId')->will($this->returnValue(10)); $this->productType->expects($this->once())->method('getUsedProducts')->will($this->returnValue([$option])); $this->service->removeChild($productSku, $childSku); }
/** * @expectedException \Magento\Framework\Exception\NoSuchEntityException * @expectedExceptionMessage Requested option doesn't exist: 3 */ public function testGetNoSuchEntityException() { $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)->will($this->returnSelf()); $this->configurableAttributeCollection->expects($this->once())->method('getFirstItem')->will($this->returnValue($this->option)); $this->option->expects($this->once())->method('getId')->will($this->returnValue(null)); $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->model->get($productSku, $optionId); }
/** * @covers \Magento\ConfigurableProduct\Helper\Product\Options\Factory::create */ public function testCreate() { $attributeId = 90; $valueIndex = 12; $item = ['attribute_id' => $attributeId, 'values' => [['value_index' => $valueIndex]]]; $data = [$item]; $attribute = $this->getMockBuilder(Attribute::class)->disableOriginalConstructor()->setMethods(['setValues', 'setData', '__wakeup'])->getMock(); $this->attributeFactory->expects(static::once())->method('create')->willReturn($attribute); $eavAttribute = $this->getMockBuilder(EavAttribute::class)->disableOriginalConstructor()->getMock(); $this->productAttributeRepository->expects(static::once())->method('get')->with($attributeId)->willReturn($eavAttribute); $this->configurable->expects(static::once())->method('canUseAttribute')->with($eavAttribute)->willReturn(true); $option = $this->getMock(OptionValueInterface::class); $option->expects(static::once())->method('setValueIndex')->with($valueIndex)->willReturnSelf(); $this->optionValueFactory->expects(static::once())->method('create')->willReturn($option); $attribute->expects(static::once())->method('setData')->with($item); $attribute->expects(static::once())->method('setValues')->with([$option]); $result = $this->factory->create($data); static::assertSame([$attribute], $result); }