/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGetConfigurableOptions() { $context = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock(); /** @var Configurable|\PHPUnit_Framework_MockObject_MockObject $configurable */ $configurable = $this->getMockBuilder(Configurable::class)->setMethods(['getTable', 'getConnection'])->setConstructorArgs([$context, $this->relation])->getMock(); $reflection = new \ReflectionClass(Configurable::class); $reflectionProperty = $reflection->getProperty('metadataPool'); $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($configurable, $this->metadataPoolMock); /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */ $product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)->setMethods(['__sleep', '__wakeup', 'getData'])->disableOriginalConstructor()->getMock(); $this->metadataMock->expects($this->once())->method('getLinkField')->willReturn('link'); $product->expects($this->once())->method('getData')->with('link')->willReturn('getId value'); $configurable->expects($this->exactly(7))->method('getTable')->will($this->returnValueMap([['catalog_product_super_attribute', 'catalog_product_super_attribute value'], ['catalog_product_entity', 'catalog_product_entity value'], ['catalog_product_super_link', 'catalog_product_super_link value'], ['eav_attribute', 'eav_attribute value'], ['catalog_product_entity', 'catalog_product_entity value'], ['eav_attribute_option_value', 'eav_attribute_option_value value'], ['catalog_product_super_attribute_label', 'catalog_product_super_attribute_label value']])); $select = $this->getMock(\Magento\Framework\DB\Select::class, ['from', 'joinInner', 'joinLeft', 'where'], [], '', false); $select->expects($this->once())->method('from')->with(['super_attribute' => 'catalog_product_super_attribute value'], ['sku' => 'entity.sku', 'product_id' => 'product_entity.entity_id', 'attribute_code' => 'attribute.attribute_code', 'option_title' => 'option_value.value', 'super_attribute_label' => 'attribute_label.value'])->will($this->returnSelf()); $superAttribute = $this->getMock(\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute::class, ['getBackendTable', 'getAttributeId'], [], '', false); $superAttribute->expects($this->once())->method('getBackendTable')->willReturn('getBackendTable value'); $superAttribute->expects($this->once())->method('getAttributeId')->willReturn('getAttributeId value'); $attributes = [$superAttribute]; $select->expects($this->exactly(5))->method('joinInner')->will($this->returnSelf())->withConsecutive([['product_entity' => 'catalog_product_entity value'], 'product_entity.link = super_attribute.product_id', []], [['product_link' => 'catalog_product_super_link value'], 'product_link.parent_id = super_attribute.product_id', []], [['attribute' => 'eav_attribute value'], 'attribute.attribute_id = super_attribute.attribute_id', []], [['entity' => 'catalog_product_entity value'], 'entity.entity_id = product_link.product_id', []], [['entity_value' => 'getBackendTable value'], implode(' AND ', ['entity_value.attribute_id = super_attribute.attribute_id', 'entity_value.store_id = 0', 'entity_value.link = entity.link']), []]); $select->expects($this->exactly(2))->method('joinLeft')->will($this->returnSelf())->withConsecutive([['option_value' => 'eav_attribute_option_value value'], implode(' AND ', ['option_value.option_id = entity_value.value', 'option_value.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID]), []], [['attribute_label' => 'catalog_product_super_attribute_label value'], implode(' AND ', ['super_attribute.product_super_attribute_id = attribute_label.product_super_attribute_id', 'attribute_label.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID]), []]); $select->expects($this->once())->method('where')->will($this->returnSelf())->with('super_attribute.product_id = ?', 'getId value'); $readerAdapter = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)->setMethods(['select', 'fetchAll'])->disableOriginalConstructor()->getMockForAbstractClass(); $readerAdapter->expects($this->once())->method('select')->willReturn($select); $readerAdapter->expects($this->once())->method('fetchAll')->with($select)->willReturn('fetchAll value'); $configurable->expects($this->exactly(2))->method('getConnection')->willReturn($readerAdapter); $expectedAttributesOptionsData = ['getAttributeId value' => 'fetchAll value']; $actualAttributesOptionsData = $configurable->getConfigurableOptions($product, $attributes); $this->assertEquals($expectedAttributesOptionsData, $actualAttributesOptionsData); }
public function testRefreshSpecialPrices() { $idsToProcess = [1, 2, 3]; $this->metadataPool->expects($this->atLeastOnce())->method('getMetadata')->willReturn($this->metadataMock); $this->metadataMock->expects($this->atLeastOnce())->method('getLinkField')->willReturn('row_id'); $this->metadataMock->expects($this->atLeastOnce())->method('getIdentifierField')->willReturn('entity_id'); $selectMock = $this->getMock('Magento\\Framework\\DB\\Select', [], [], '', false); $selectMock->expects($this->any())->method('from')->will($this->returnSelf()); $selectMock->expects($this->any())->method('joinLeft')->will($this->returnSelf()); $selectMock->expects($this->any())->method('where')->will($this->returnSelf()); $connectionMock = $this->getMock('Magento\\Framework\\DB\\Adapter\\AdapterInterface', [], [], '', false); $connectionMock->expects($this->any())->method('select')->will($this->returnValue($selectMock)); $connectionMock->expects($this->any())->method('fetchCol')->will($this->returnValue($idsToProcess)); $this->_resourceMock->expects($this->once())->method('getConnection')->will($this->returnValue($connectionMock)); $this->_resourceMock->expects($this->any())->method('getTableName')->will($this->returnValue('category')); $storeMock = $this->getMock('\\Magento\\Store\\Model\\Store', [], [], '', false); $storeMock->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->_storeManagerMock->expects($this->once())->method('getStores')->with(true)->will($this->returnValue([$storeMock])); $this->_localeDateMock->expects($this->once())->method('scopeTimeStamp')->with($storeMock)->will($this->returnValue(32000)); $indexerMock = $this->getMock('Magento\\Indexer\\Model\\Indexer', [], [], '', false); $indexerMock->expects($this->exactly(2))->method('reindexList'); $this->_priceProcessorMock->expects($this->exactly(2))->method('getIndexer')->will($this->returnValue($indexerMock)); $attributeMock = $this->getMockForAbstractClass('Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute', [], '', false, true, true, ['__wakeup', 'getAttributeId']); $attributeMock->expects($this->any())->method('getAttributeId')->will($this->returnValue(1)); $this->_eavConfigMock->expects($this->any())->method('getAttribute')->will($this->returnValue($attributeMock)); $this->_model->execute(); }
public function testGetConfigurableAttributes() { $expectedData = [1]; $configurableAttributes = '_cache_instance_configurable_attributes'; /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */ $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->setMethods(['getData', 'hasData', 'setData', 'getIdentities', 'getId', 'getStoreId'])->disableOriginalConstructor()->getMock(); $product->expects($this->once())->method('hasData')->with($configurableAttributes)->willReturn(false); $product->expects($this->once())->method('setData')->willReturnSelf(); $product->expects($this->exactly(2))->method('getData')->willReturnMap([[$configurableAttributes, null, $expectedData], ['link', null, 1]]); $product->expects($this->once())->method('getIdentities')->willReturn([1, 2, 3]); $this->entityMetadata->expects($this->once())->method('getLinkField')->willReturn('link'); $attributeCollection = $this->getMockBuilder('Magento\\ConfigurableProduct\\Model\\ResourceModel\\Product\\Type\\Configurable\\Attribute\\Collection')->setMethods(['setProductFilter', 'orderByPosition', 'load'])->disableOriginalConstructor()->getMock(); $attributeCollection->expects($this->any())->method('setProductFilter')->will($this->returnSelf()); $attributeCollection->expects($this->any())->method('orderByPosition')->will($this->returnSelf()); $this->_attributeCollectionFactory->expects($this->any())->method('create')->willReturn($attributeCollection); $this->extensionAttributesJoinProcessorMock->expects($this->once())->method('process')->with($this->isInstanceOf('Magento\\ConfigurableProduct\\Model\\ResourceModel\\Product\\Type\\Configurable\\Attribute\\Collection')); $this->assertEquals($expectedData, $this->_model->getConfigurableAttributes($product)); }
public function testRemoveChild() { $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product)); $bundle = $this->getMock('\\Magento\\Bundle\\Model\\ResourceModel\\Bundle', [], [], '', false); $this->bundleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($bundle)); $productSku = 'productSku'; $optionId = 1; $productId = 1; $childSku = 'childSku'; $this->product->expects($this->any())->method('getTypeId')->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)); $this->getRemoveOptions(); $selection = $this->getMockBuilder('\\Magento\\Bundle\\Model\\Selection')->setMethods(['getSku', 'getOptionId', 'getSelectionId', 'getProductId', '__wakeup'])->disableOriginalConstructor()->getMock(); $selection->expects($this->any())->method('getSku')->will($this->returnValue($childSku)); $selection->expects($this->any())->method('getOptionId')->will($this->returnValue($optionId)); $selection->expects($this->any())->method('getSelectionId')->will($this->returnValue(55)); $selection->expects($this->any())->method('getProductId')->willReturn($productId); $this->option->expects($this->any())->method('getSelections')->will($this->returnValue([$selection])); $this->metadataMock->expects($this->any())->method('getLinkField')->willReturn($this->linkField); $this->product->expects($this->any())->method('getData')->with($this->linkField)->willReturn(3); $bundle->expects($this->once())->method('dropAllUnneededSelections')->with(3, []); $bundle->expects($this->once())->method('removeProductRelations')->with(3, [$productId]); //Params come in lowercase to method $this->assertTrue($this->model->removeChild($productSku, $optionId, $childSku)); }