protected function setUp() { $this->productBuilder = $this->getMock('Magento\\Catalog\\Controller\\Adminhtml\\Product\\Builder', ['build'], [], '', false); $this->product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['addData', 'getSku', 'getTypeId', 'getStoreId', '__sleep', '__wakeup', 'getAttributes', 'setAttributeSetId'])->getMock(); $this->product->expects($this->any())->method('getTypeId')->will($this->returnValue('simple')); $this->product->expects($this->any())->method('getStoreId')->will($this->returnValue('1')); $this->product->expects($this->any())->method('getAttributes')->will($this->returnValue([])); $this->productBuilder->expects($this->any())->method('build')->will($this->returnValue($this->product)); $this->resultPage = $this->getMockBuilder('Magento\\Backend\\Model\\View\\Result\\Page')->disableOriginalConstructor()->getMock(); $resultPageFactory = $this->getMockBuilder('Magento\\Framework\\View\\Result\\PageFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $resultPageFactory->expects($this->any())->method('create')->willReturn($this->resultPage); $this->resultForward = $this->getMockBuilder('Magento\\Backend\\Model\\View\\Result\\Forward')->disableOriginalConstructor()->getMock(); $resultForwardFactory = $this->getMockBuilder('Magento\\Backend\\Model\\View\\Result\\ForwardFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $resultForwardFactory->expects($this->any())->method('create')->willReturn($this->resultForward); $this->resultPage->expects($this->any())->method('getLayout')->willReturn($this->layout); $this->resultRedirectFactory = $this->getMock('Magento\\Backend\\Model\\View\\Result\\RedirectFactory', ['create'], [], '', false); $this->resultRedirect = $this->getMock('Magento\\Backend\\Model\\View\\Result\\Redirect', [], [], '', false); $this->resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirect); $this->initializationHelper = $this->getMock('Magento\\Catalog\\Controller\\Adminhtml\\Product\\Initialization\\Helper', [], [], '', false); $this->productFactory = $this->getMockBuilder('Magento\\Catalog\\Model\\ProductFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->productFactory->expects($this->any())->method('create')->willReturn($this->product); $this->resultJson = $this->getMock('Magento\\Framework\\Controller\\Result\\Json', [], [], '', false); $this->resultJsonFactory = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\JsonFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->resultJsonFactory->expects($this->any())->method('create')->willReturn($this->resultJson); $additionalParams = ['resultRedirectFactory' => $this->resultRedirectFactory]; $this->action = (new ObjectManagerHelper($this))->getObject('Magento\\Catalog\\Controller\\Adminhtml\\Product\\Validate', ['context' => $this->initContext($additionalParams), 'productBuilder' => $this->productBuilder, 'resultPageFactory' => $resultPageFactory, 'resultForwardFactory' => $resultForwardFactory, 'initializationHelper' => $this->initializationHelper, 'resultJsonFactory' => $this->resultJsonFactory, 'productFactory' => $this->productFactory]); }
protected function setUp() { $this->product = $this->getMock('Magento\\Catalog\\Model\\Product', [], [], '', false); $this->productFactory = $this->getMock('Magento\\Catalog\\Model\\ProductFactory', ['create'], [], '', false); $this->productFactory->expects($this->any())->method('create')->will($this->returnValue($this->product)); $this->visibility = $this->getMock('Magento\\Catalog\\Model\\Product\\Visibility', [], [], '', false); $this->timezone = $this->getMock('Magento\\Framework\\Stdlib\\DateTime\\Timezone', [], [], '', false); $this->objectManagerHelper = new ObjectManagerHelper($this); $this->newProducts = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Rss\\Product\\NewProducts', ['productFactory' => $this->productFactory, 'visibility' => $this->visibility, 'localeDate' => $this->timezone]); }
public function testExecuteNullProduct() { $this->productMock->expects($this->once())->method('load')->with(59)->willReturn($this->productMock); $this->productModelFactoryMock->expects($this->once())->method('create')->willReturn($this->productMock); $this->swatchHelperMock->expects($this->once())->method('getAttributesFromConfigurable')->with($this->productMock)->willReturn([$this->attributeMock]); $this->swatchHelperMock->expects($this->once())->method('loadVariationByFallback')->with($this->productMock, ['size' => 454])->willReturn(null); $this->swatchHelperMock->expects($this->once())->method('getProductMediaGallery')->with($this->productMock)->willReturn($this->mediaGallery); $this->jsonMock->expects($this->once())->method('setData')->with($this->mediaGallery)->will($this->returnSelf()); $result = $this->controller->execute(); $this->assertInstanceOf('\\Magento\\Framework\\Controller\\Result\\Json', $result); }
protected function setUp() { $this->objectManagerHelper = new ObjectManagerHelper($this); $this->mathDivision = $this->getMock('\\Magento\\Framework\\Math\\Division', ['getExactDivision'], [], '', false); $this->localeFormat = $this->getMockForAbstractClass('\\Magento\\Framework\\Locale\\FormatInterface', ['getNumber']); $this->localeFormat->expects($this->any())->method('getNumber')->willReturn($this->qty); $this->object = $this->objectManagerHelper->getObject('Magento\\Framework\\DataObject'); $this->objectFactory = $this->getMock('\\Magento\\Framework\\DataObject\\Factory', ['create'], [], '', false); $this->objectFactory->expects($this->any())->method('create')->willReturn($this->object); $this->product = $this->getMock('Magento\\Catalog\\Model\\Product', ['load', 'isComposite', '__wakeup', 'isSaleable'], [], '', false); $this->productFactory = $this->getMock('Magento\\Catalog\\Model\\ProductFactory', ['create'], [], '', false); $this->productFactory->expects($this->any())->method('create')->willReturn($this->product); $this->stockStateProvider = $this->objectManagerHelper->getObject('Magento\\CatalogInventory\\Model\\StockStateProvider', ['mathDivision' => $this->mathDivision, 'localeFormat' => $this->localeFormat, 'objectFactory' => $this->objectFactory, 'productFactory' => $this->productFactory, 'qtyCheckApplicable' => $this->qtyCheckApplicable]); }
/** * Test for afterImportData() * Covers afterImportData() + protected methods used inside except related to generateUrls() ones. * generateUrls will be covered separately. * * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::afterImportData * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::_populateForUrlGeneration * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::isGlobalScope * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::populateGlobalProduct * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::addProductToImport * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testAfterImportData() { $newSku = ['entity_id' => 'value']; $websiteId = 'websiteId value'; $productsCount = count($this->products); $websiteMock = $this->getMock('\\Magento\\Store\\Model\\Website', ['getStoreIds'], [], '', false); $storeIds = [1, Store::DEFAULT_STORE_ID]; $websiteMock->expects($this->once())->method('getStoreIds')->willReturn($storeIds); $this->storeManager->expects($this->once())->method('getWebsite')->with($websiteId)->willReturn($websiteMock); $this->importProduct->expects($this->exactly($productsCount))->method('getNewSku')->withConsecutive([$this->products[0][ImportProduct::COL_SKU]], [$this->products[1][ImportProduct::COL_SKU]])->willReturn($newSku); $this->importProduct->expects($this->exactly($productsCount))->method('getProductCategories')->withConsecutive([$this->products[0][ImportProduct::COL_SKU]], [$this->products[1][ImportProduct::COL_SKU]]); $getProductWebsitesCallsCount = $productsCount * 2; $this->importProduct->expects($this->exactly($getProductWebsitesCallsCount))->method('getProductWebsites')->willReturn([$newSku['entity_id'] => $websiteId]); $map = [[$this->products[0][ImportProduct::COL_STORE], $this->products[0][ImportProduct::COL_STORE]], [$this->products[1][ImportProduct::COL_STORE], $this->products[1][ImportProduct::COL_STORE]]]; $this->importProduct->expects($this->exactly(1))->method('getStoreIdByCode')->will($this->returnValueMap($map)); $product = $this->getMock('\\Magento\\Catalog\\Model\\Product', ['getId', 'setId', 'getSku', 'setStoreId', 'getStoreId'], [], '', false); $product->expects($this->exactly($productsCount))->method('setId')->with($newSku['entity_id']); $product->expects($this->any())->method('getId')->willReturn($newSku['entity_id']); $product->expects($this->exactly($productsCount))->method('getSku')->will($this->onConsecutiveCalls($this->products[0]['sku'], $this->products[1]['sku'])); $product->expects($this->exactly($productsCount))->method('getStoreId')->will($this->onConsecutiveCalls($this->products[0][ImportProduct::COL_STORE], $this->products[1][ImportProduct::COL_STORE])); $product->expects($this->once())->method('setStoreId')->with($this->products[1][ImportProduct::COL_STORE]); $this->catalogProductFactory->expects($this->exactly($productsCount))->method('create')->willReturn($product); $this->connection->expects($this->exactly(4))->method('quoteInto')->withConsecutive(['(store_id = ?', $storeIds[0]], [' AND entity_id = ?)', $newSku['entity_id']]); $productUrls = ['url 1', 'url 2']; $importMock = $this->getImportMock(['generateUrls', 'canonicalUrlRewriteGenerate', 'categoriesUrlRewriteGenerate', 'currentUrlRewritesRegenerate', 'cleanOverriddenUrlKey']); $importMock->expects($this->once())->method('generateUrls')->willReturn($productUrls); $this->urlPersist->expects($this->once())->method('replace')->with($productUrls); $importMock->afterImportData($this->observer); }
/** * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function setUp() { $this->_eavConfig = $this->getMock('Magento\\Eav\\Model\\Config', array('getEntityType', 'getEntityTypeId'), array(), '', false); $this->_eavConfig->expects($this->atLeastOnce())->method('getEntityType')->with($this->equalTo('catalog_product'))->will($this->returnSelf()); $this->_eavConfig->expects($this->atLeastOnce())->method('getEntityTypeId')->will($this->returnValue('1')); $this->_optionModel = $this->getMock('\\Magento\\CatalogImportExport\\Model\\Import\\Product\\Option', array(), array(), '', false); $this->_optionFactory = $this->getMock('\\Magento\\CatalogImportExport\\Model\\Import\\Product\\OptionFactory', array('create'), array(), '', false); $this->_optionFactory->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->_optionModel)); $this->_storeManager = $this->getMock('\\Magento\\Store\\Model\\StoreManager', array('getWebsites', 'getStores'), array(), '', false); $this->_storeManager->expects($this->atLeastOnce())->method('getWebsites')->will($this->returnValue(array())); $this->_storeManager->expects($this->atLeastOnce())->method('getStores')->will($this->returnValue(array())); $this->_setCol = $this->getMock('\\Magento\\Eav\\Model\\Resource\\Entity\\Attribute\\Set\\Collection', array('setEntityTypeFilter'), array(), '', false); $this->_setCol->expects($this->atLeastOnce())->method('setEntityTypeFilter')->with($this->equalTo('1'))->will($this->returnValue(array())); $this->_setColFactory = $this->getMock('\\Magento\\Eav\\Model\\Resource\\Entity\\Attribute\\Set\\CollectionFactory', array('create'), array(), '', false); $this->_setColFactory->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->_setCol)); $this->_importConfig = $this->getMock('\\Magento\\ImportExport\\Model\\Import\\Config', array('getEntityTypes'), array(), '', false); $this->_importConfig->expects($this->atLeastOnce())->method('getEntityTypes')->with('catalog_product')->will($this->returnValue(array())); $this->_categoryCol = $this->getMock('\\Magento\\Catalog\\Model\\Resource\\Category\\Collection', array('addNameToResult'), array(), '', false); $this->_categoryCol->expects($this->atLeastOnce())->method('addNameToResult')->will($this->returnValue(array())); $this->_categoryColFactory = $this->getMock('\\Magento\\Catalog\\Model\\Resource\\Category\\CollectionFactory', array('create'), array(), '', false); $this->_categoryColFactory->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->_categoryCol)); $this->_product = $this->getMock('\\Magento\\Catalog\\Model\\Product', array('getProductEntitiesInfo', '__wakeup'), array(), '', false); $this->_product->expects($this->atLeastOnce())->method('getProductEntitiesInfo')->with($this->equalTo(array('entity_id', 'type_id', 'attribute_set_id', 'sku')))->will($this->returnValue(array())); $this->_productFactory = $this->getMock('\\Magento\\Catalog\\Model\\ProductFactory', array('create'), array(), '', false); $this->_productFactory->expects($this->atLeastOnce())->method('create')->will($this->returnValue($this->_product)); $this->_customerGroupService = $this->getMock('Magento\\Customer\\Service\\V1\\CustomerGroupService', array('getGroups'), array(), '', false); $this->_customerGroupService->expects($this->atLeastOnce())->method('getGroups')->will($this->returnValue(array())); $objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); $this->_model = $objectManager->getObject('\\Magento\\CatalogImportExport\\Model\\Import\\Product', array('config' => $this->_eavConfig, 'optionFactory' => $this->_optionFactory, 'storeManager' => $this->_storeManager, 'setColFactory' => $this->_setColFactory, 'importConfig' => $this->_importConfig, 'categoryColFactory' => $this->_categoryColFactory, 'productFactory' => $this->_productFactory, 'customerGroupService' => $this->_customerGroupService)); }
/** * @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(); $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(); $parentProductMock->expects($this->once())->method('getNewVariationsAttributeSetId')->willReturn('new_attr_set_id'); $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)); }
/** * Test for method _toHtml * * @return void */ public function testToHtml() { $productMock = $this->getMock('Magento\\Catalog\\Model\\Product', ['getProductUrl', 'getDescription', 'getAllowedPriceInRss', 'getName', '__wakeup', 'getResourceCollection'], [], '', false); $productCollectionMock = $this->getMock('Magento\\Catalog\\Model\\Resource\\Product\\CollectionFactory', ['addPriceDataFieldFilter', 'addPriceData', 'addAttributeToSelect', 'addAttributeToSort', 'getSelect'], [], '', false); $rssObjMock = $this->getMock('Magento\\Rss\\Model\\Rss', [], [], '', false); $productUrl = '<a href="http://product.url">Product Url</a>'; $imgThumbSrc = 'http://source-for-thumbnail'; $productTitle = 'Product title'; $basePriceFormatted = '<span class="price">$10.00</span>'; $finalPriceFormatted = '<span class="price">$20.00</span>'; $productDescription = '<table><tr>' . '<td><a href="' . $productUrl . '"><img src="' . $imgThumbSrc . '" alt="" border="0" align="left" height="75" width="75" /></a></td>' . '<td style="text-decoration:none;"><p>Price: ' . $basePriceFormatted . ' Special Price: ' . $finalPriceFormatted . '</p></td></tr></table>'; $expectedData = ['title' => $productTitle, 'link' => $productUrl, 'description' => $productDescription]; $expectedResult = new \Magento\Framework\Object(['rss_feed' => '<xml>Feed of the rss</xml>']); $this->addMocks(); $this->productFactoryMock->expects($this->once())->method('create')->will($this->returnValue($productMock)); $productMock->expects($this->once())->method('getResourceCollection')->will($this->returnValue($productCollectionMock)); $productCollectionMock->expects($this->once())->method('addPriceDataFieldFilter')->will($this->returnSelf()); $productCollectionMock->expects($this->once())->method('addPriceData')->will($this->returnSelf()); $productCollectionMock->expects($this->once())->method('addAttributeToSelect')->will($this->returnSelf()); $productCollectionMock->expects($this->once())->method('addAttributeToSort')->will($this->returnSelf()); $this->rssFactoryMock->expects($this->once())->method('create')->will($this->returnValue($rssObjMock)); $productMock->expects($this->exactly(2))->method('getProductUrl')->will($this->returnValue($productUrl)); $this->imageHelperMock->expects($this->once())->method('resize')->will($this->returnValue($imgThumbSrc)); $productMock->expects($this->any())->method('getAllowedPriceInRss')->will($this->returnValue(true)); $productMock->expects($this->once())->method('getName')->will($this->returnValue($productTitle)); $this->priceCurrencyMock->expects($this->exactly(2))->method('convertAndFormat')->will($this->returnValueMap([[10, true, PriceCurrencyInterface::DEFAULT_PRECISION, null, null, $basePriceFormatted], [20, true, PriceCurrencyInterface::DEFAULT_PRECISION, null, null, $finalPriceFormatted]])); $rssObjMock->expects($this->once())->method('_addEntry')->with($expectedData)->will($this->returnSelf()); $rssObjMock->expects($this->once())->method('createRssXml')->will($this->returnValue($expectedResult)); $this->assertEquals($expectedResult, $this->block->toHtml()); }
protected function setUp() { $this->stockItemMock = $this->getMockBuilder('\\Magento\\CatalogInventory\\Model\\Stock\\Item')->disableOriginalConstructor()->setMethods(['getItemId', 'getProductId', 'setIsInStock', 'setStockStatusChangedAutomaticallyFlag', 'getStockStatusChangedAutomaticallyFlag', 'getManageStock', 'setLowStockDate', 'setStockStatusChangedAuto', 'hasStockStatusChangedAutomaticallyFlag', 'setQty', 'getWebsiteId', 'setWebsiteId', 'getStockId', 'setStockId'])->getMock(); $this->stockConfigurationMock = $this->getMockBuilder('Magento\\CatalogInventory\\Api\\StockConfigurationInterface')->disableOriginalConstructor()->getMock(); $this->stockStateProviderMock = $this->getMockBuilder('Magento\\CatalogInventory\\Model\\Spi\\StockStateProviderInterface')->disableOriginalConstructor()->getMock(); $this->stockItemResourceMock = $this->getMockBuilder('Magento\\CatalogInventory\\Model\\Resource\\Stock\\Item')->disableOriginalConstructor()->getMock(); $this->stockItemFactoryMock = $this->getMockBuilder('Magento\\CatalogInventory\\Api\\Data\\StockItemInterfaceFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock(); $this->stockItemCollectionMock = $this->getMockBuilder('Magento\\CatalogInventory\\Api\\Data\\StockItemCollectionInterfaceFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock(); $this->productFactoryMock = $this->getMockBuilder('Magento\\Catalog\\Model\\ProductFactory')->disableOriginalConstructor()->setMethods(['load', 'create'])->getMock(); $this->productMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['load', 'getId', 'getTypeId', '__wakeup'])->getMock(); $this->productFactoryMock->expects($this->any())->method('create')->willReturn($this->productMock); $this->queryBuilderFactoryMock = $this->getMockBuilder('Magento\\Framework\\DB\\QueryBuilderFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock(); $this->mapperMock = $this->getMockBuilder('Magento\\Framework\\DB\\MapperFactory')->disableOriginalConstructor()->getMock(); $this->localeDateMock = $this->getMockBuilder('Magento\\Framework\\Stdlib\\DateTime\\TimezoneInterface')->disableOriginalConstructor()->getMock(); $this->indexProcessorMock = $this->getMock('Magento\\CatalogInventory\\Model\\Indexer\\Stock\\Processor', ['reindexRow'], [], '', false); $this->model = new StockItemRepository($this->stockConfigurationMock, $this->stockStateProviderMock, $this->stockItemResourceMock, $this->stockItemFactoryMock, $this->stockItemCollectionMock, $this->productFactoryMock, $this->queryBuilderFactoryMock, $this->mapperMock, $this->localeDateMock, $this->indexProcessorMock); }
protected function setUp() { $this->objectManagerHelper = new ObjectManagerHelper($this); $this->product = $this->getMock('Magento\\Catalog\\Model\\Product', ['__wakeup', 'getIdBySku'], [], '', false); $this->product->expects($this->any())->method('getIdBySku')->willReturn($this->productId); //getIdBySku $this->productFactory = $this->getMock('Magento\\Catalog\\Model\\ProductFactory', ['create'], [], '', false); $this->productFactory->expects($this->any())->method('create')->will($this->returnValue($this->product)); $this->stock = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockInterface', ['__wakeup'], '', false); $this->stockItem = $this->getMockBuilder('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface')->setMethods(['setProductId', 'getData', 'addData', 'getItemId'])->disableOriginalConstructor()->getMockForAbstractClass(); $this->stockStatus = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockStatusInterface', ['__wakeup'], '', false); $this->stockRegistryProvider = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Model\\Spi\\StockRegistryProviderInterface', ['getStock', 'getStockItem', 'getStockStatus'], '', false); $this->stockRegistryProvider->expects($this->any())->method('getStock')->will($this->returnValue($this->stock)); $this->stockRegistryProvider->expects($this->any())->method('getStockItem')->will($this->returnValue($this->stockItem)); $this->stockRegistryProvider->expects($this->any())->method('getStockStatus')->will($this->returnValue($this->stockStatus)); $this->stockItemRepository = $this->getMockForAbstractClass('\\Magento\\CatalogInventory\\Api\\StockItemRepositoryInterface', ['save'], '', false); $this->stockItemRepository->expects($this->any())->method('save')->will($this->returnValue($this->stockItem)); $this->stockRegistry = $this->objectManagerHelper->getObject('\\Magento\\CatalogInventory\\Model\\StockRegistry', ['stockRegistryProvider' => $this->stockRegistryProvider, 'productFactory' => $this->productFactory, 'stockItemRepository' => $this->stockItemRepository]); }
public function testAddErrorMessageWhenProductWithoutStores() { $form = $this->getMock('Magento\\Framework\\Data\\Form', [], [], '', false); $form->expects($this->any())->method('getElement')->will($this->returnValue($this->getMockForAbstractClass('\\Magento\\Framework\\Data\\Form\\Element\\AbstractElement', [], '', false))); $this->formFactory->expects($this->once())->method('create')->will($this->returnValue($form)); $fieldset = $this->getMock('Magento\\Framework\\Data\\Form\\Element\\Fieldset', [], [], '', false); $form->expects($this->once())->method('addFieldset')->will($this->returnValue($fieldset)); $storeElement = $this->getMock('Magento\\Framework\\Data\\Form\\Element\\AbstractElement', ['setAfterElementHtml', 'setValues'], [], '', false); $fieldset->expects($this->at(2))->method('addField')->with('store_id', 'select', ['label' => 'Store', 'title' => 'Store', 'name' => 'store_id', 'required' => true, 'value' => 0])->willReturn($storeElement); $product = $this->getMock('Magento\\Catalog\\Model\\Product', [], [], '', false); $product->expects($this->any())->method('getId')->willReturn('product_id'); $product->expects($this->once())->method('getStoreIds')->willReturn([]); $this->productFactory->expects($this->once())->method('create')->willReturn($product); $this->categoryFactory->expects($this->once())->method('create')->willReturn($this->getMock('Magento\\Catalog\\Model\\Category', [], [], '', false)); $storeElement->expects($this->once())->method('setAfterElementHtml'); $storeElement->expects($this->once())->method('setValues')->with([]); $this->layout->expects($this->once())->method('createBlock')->willReturn($this->getMock('Magento\\Framework\\Data\\Form\\Element\\Renderer\\RendererInterface')); $this->form->toHtml(); }
/** * Set up test * * @return void */ protected function setUp() { $this->resource = $this->getMock('Magento\\Framework\\App\\ResourceConnection', ['getConnection', 'getTableName'], [], '', false); $this->ruleCollectionFactory = $this->getMock('Magento\\CatalogRule\\Model\\ResourceModel\\Rule\\CollectionFactory', ['create', 'addFieldToFilter'], [], '', false); $this->backend = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\AbstractBackend', [], [], '', false); $this->select = $this->getMock('Magento\\Framework\\DB\\Select', [], [], '', false); $this->metadataPool = $this->getMock('Magento\\Framework\\EntityManager\\MetadataPool', [], [], '', false); $metadata = $this->getMockBuilder('Magento\\Framework\\EntityManager\\EntityMetadata')->disableOriginalConstructor()->getMock(); $this->metadataPool->expects($this->any())->method('getMetadata')->willReturn($metadata); $this->connection = $this->getMock('Magento\\Framework\\DB\\Adapter\\AdapterInterface'); $this->db = $this->getMock('Zend_Db_Statement_Interface', [], [], '', false); $this->website = $this->getMock('Magento\\Store\\Model\\Website', [], [], '', false); $this->storeManager = $this->getMock('Magento\\Store\\Model\\StoreManagerInterface', [], [], '', false); $this->combine = $this->getMock('Magento\\Rule\\Model\\Condition\\Combine', [], [], '', false); $this->rules = $this->getMock('Magento\\CatalogRule\\Model\\Rule', [], [], '', false); $this->logger = $this->getMock('Psr\\Log\\LoggerInterface', [], [], '', false); $this->attribute = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute', [], [], '', false); $this->priceCurrency = $this->getMock('Magento\\Framework\\Pricing\\PriceCurrencyInterface'); $this->dateFormat = $this->getMock('Magento\\Framework\\Stdlib\\DateTime', [], [], '', false); $this->dateTime = $this->getMock('Magento\\Framework\\Stdlib\\DateTime\\DateTime', [], [], '', false); $this->eavConfig = $this->getMock('Magento\\Eav\\Model\\Config', ['getAttribute'], [], '', false); $this->product = $this->getMock('Magento\\Catalog\\Model\\Product', [], [], '', false); $this->productFactory = $this->getMock('Magento\\Catalog\\Model\\ProductFactory', ['create'], [], '', false); $this->connection->expects($this->any())->method('select')->will($this->returnValue($this->select)); $this->connection->expects($this->any())->method('query')->will($this->returnValue($this->db)); $this->select->expects($this->any())->method('distinct')->will($this->returnSelf()); $this->select->expects($this->any())->method('where')->will($this->returnSelf()); $this->select->expects($this->any())->method('from')->will($this->returnSelf()); $this->select->expects($this->any())->method('order')->will($this->returnSelf()); $this->resource->expects($this->any())->method('getConnection')->will($this->returnValue($this->connection)); $this->resource->expects($this->any())->method('getTableName')->will($this->returnArgument(0)); $this->storeManager->expects($this->any())->method('getWebsites')->will($this->returnValue([$this->website])); $this->storeManager->expects($this->any())->method('getWebsite')->will($this->returnValue($this->website)); $this->rules->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->rules->expects($this->any())->method('getWebsiteIds')->will($this->returnValue([1])); $this->rules->expects($this->any())->method('getCustomerGroupIds')->will($this->returnValue([1])); $this->ruleCollectionFactory->expects($this->any())->method('create')->will($this->returnSelf()); $this->ruleCollectionFactory->expects($this->any())->method('addFieldToFilter')->will($this->returnValue([$this->rules])); $this->product->expects($this->any())->method('load')->will($this->returnSelf()); $this->product->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->product->expects($this->any())->method('getWebsiteIds')->will($this->returnValue([1])); $this->rules->expects($this->any())->method('validate')->with($this->product)->willReturn(true); $this->attribute->expects($this->any())->method('getBackend')->will($this->returnValue($this->backend)); $this->productFactory->expects($this->any())->method('create')->will($this->returnValue($this->product)); $this->indexBuilder = new \Magento\CatalogRule\Model\Indexer\IndexBuilder($this->ruleCollectionFactory, $this->priceCurrency, $this->resource, $this->storeManager, $this->logger, $this->eavConfig, $this->dateFormat, $this->dateTime, $this->productFactory); $this->setProperties($this->indexBuilder, ['metadataPool' => $this->metadataPool]); }
/** * @expectedException \Magento\Framework\Exception\InputException * @expectedExceptionMessage Products "5" and "4" have the same set of attribute values. */ public function testAroundSaveWithLinksWithDuplicateAttributes() { $links = [4, 5]; $attributeCode = 'color'; $attributeId = 23; $this->option->expects(static::once())->method('getAttributeId')->willReturn($attributeId); $this->product->expects(static::once())->method('getTypeId')->willReturn(Configurable::TYPE_CODE); $this->result->expects(static::once())->method('getExtensionAttributes')->willReturn($this->extensionAttributes); $this->extensionAttributes->expects(static::once())->method('getConfigurableProductOptions')->willReturn([$this->option]); $this->extensionAttributes->expects(static::once())->method('getConfigurableProductLinks')->willReturn($links); $this->productAttributeRepository->expects(static::once())->method('get')->willReturn($this->eavAttribute); $this->eavAttribute->expects(static::once())->method('getAttributeCode')->willReturn($attributeCode); $product = $this->getMockBuilder(Product::class)->disableOriginalConstructor()->setMethods(['load', 'getData', '__wakeup'])->getMock(); $this->productFactory->expects(static::exactly(2))->method('create')->willReturn($product); $product->expects(static::exactly(2))->method('load')->willReturnSelf(); $product->expects(static::exactly(4))->method('getData')->with($attributeCode)->willReturn($attributeId); $this->plugin->aroundSave($this->productRepository, $this->closure, $this->product); }
/** * Test for afterImportData() * Covers afterImportData() + protected methods used inside * * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::afterImportData * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::_populateForUrlGeneration * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::isGlobalScope * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::populateGlobalProduct * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::addProductToImport * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testAfterImportData() { $newSku = [['entity_id' => 'value'], ['entity_id' => 'value3']]; $websiteId = 'websiteId value'; $productsCount = count($this->products); $websiteMock = $this->getMock('\\Magento\\Store\\Model\\Website', ['getStoreIds'], [], '', false); $storeIds = [1, Store::DEFAULT_STORE_ID]; $websiteMock->expects($this->once())->method('getStoreIds')->willReturn($storeIds); $this->storeManager->expects($this->once())->method('getWebsite')->with($websiteId)->willReturn($websiteMock); $this->importProduct->expects($this->exactly($productsCount))->method('getNewSku')->withConsecutive([$this->products[0][ImportProduct::COL_SKU]], [$this->products[1][ImportProduct::COL_SKU]])->will($this->onConsecutiveCalls($newSku[0], $newSku[1])); $this->importProduct->expects($this->exactly($productsCount))->method('getProductCategories')->withConsecutive([$this->products[0][ImportProduct::COL_SKU]], [$this->products[1][ImportProduct::COL_SKU]])->willReturn([]); $getProductWebsitesCallsCount = $productsCount * 2; $this->importProduct->expects($this->exactly($getProductWebsitesCallsCount))->method('getProductWebsites')->willReturnOnConsecutiveCalls([$newSku[0]['entity_id'] => $websiteId], [$newSku[0]['entity_id'] => $websiteId], [$newSku[1]['entity_id'] => $websiteId], [$newSku[1]['entity_id'] => $websiteId]); $map = [[$this->products[0][ImportProduct::COL_STORE], $this->products[0][ImportProduct::COL_STORE]], [$this->products[1][ImportProduct::COL_STORE], $this->products[1][ImportProduct::COL_STORE]]]; $this->importProduct->expects($this->exactly(1))->method('getStoreIdByCode')->will($this->returnValueMap($map)); $product = $this->getMock('\\Magento\\Catalog\\Model\\Product', ['getId', 'setId', 'getSku', 'setStoreId', 'getStoreId'], [], '', false); $product->expects($this->exactly($productsCount))->method('setId')->withConsecutive([$newSku[0]['entity_id']], [$newSku[1]['entity_id']]); $product->expects($this->any())->method('getId')->willReturnOnConsecutiveCalls($newSku[0]['entity_id'], $newSku[0]['entity_id'], $newSku[0]['entity_id'], $newSku[0]['entity_id'], $newSku[0]['entity_id'], $newSku[1]['entity_id'], $newSku[1]['entity_id'], $newSku[1]['entity_id']); $product->expects($this->exactly($productsCount))->method('getSku')->will($this->onConsecutiveCalls($this->products[0]['sku'], $this->products[1]['sku'])); $product->expects($this->exactly($productsCount))->method('getStoreId')->will($this->onConsecutiveCalls($this->products[0][ImportProduct::COL_STORE], $this->products[1][ImportProduct::COL_STORE])); $product->expects($this->exactly($productsCount))->method('setStoreId')->withConsecutive([$this->products[0][ImportProduct::COL_STORE]], [$this->products[1][ImportProduct::COL_STORE]]); $this->catalogProductFactory->expects($this->exactly($productsCount))->method('create')->willReturn($product); $this->connection->expects($this->exactly(4))->method('quoteInto')->withConsecutive(['(store_id = ?', $storeIds[0]], [' AND entity_id = ?)', $newSku[0]['entity_id']], ['(store_id = ?', $storeIds[0]], [' AND entity_id = ?)', $newSku[1]['entity_id']]); $this->connection->expects($this->once())->method('fetchAll')->willReturn([]); $this->select->expects($this->any())->method('from')->willReturnSelf(); $this->select->expects($this->any())->method('where')->willReturnSelf(); $this->urlFinder->expects($this->any())->method('findAllByData')->willReturn([]); $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')->willReturn('urlPathWithSuffix'); $this->productUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('urlPath'); $this->productUrlPathGenerator->expects($this->any())->method('getCanonicalUrlPath')->willReturn('canonicalUrlPath'); $this->urlRewrite->expects($this->any())->method('setStoreId')->willReturnSelf(); $this->urlRewrite->expects($this->any())->method('setEntityId')->willReturnSelf(); $this->urlRewrite->expects($this->any())->method('setEntityType')->willReturnSelf(); $this->urlRewrite->expects($this->any())->method('setRequestPath')->willReturnSelf(); $this->urlRewrite->expects($this->any())->method('setTargetPath')->willReturnSelf(); $this->urlRewrite->expects($this->any())->method('getTargetPath')->willReturn('targetPath'); $this->urlRewrite->expects($this->any())->method('getStoreId')->willReturnOnConsecutiveCalls(0, 'not global'); $this->urlRewriteFactory->expects($this->any())->method('create')->willReturn($this->urlRewrite); $productUrls = ['targetPath-0' => $this->urlRewrite, 'targetPath-not global' => $this->urlRewrite]; $this->urlPersist->expects($this->once())->method('replace')->with($productUrls); $this->import->execute($this->observer); }
/** * @param int|\Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $itemId * @param \Magento\Framework\Object $buyRequest * @param null|array|\Magento\Framework\Object $param * @throws \Magento\Framework\Model\Exception * * @dataProvider updateItemDataProvider */ public function testUpdateItem($itemId, $buyRequest, $param) { $storeId = 1; $productId = 1; $stores = [(new \Magento\Framework\Object())->setId($storeId)]; $newItem = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item')->disableOriginalConstructor()->getMock(); $newItem->expects($this->any())->method('setProductId')->will($this->returnSelf()); $newItem->expects($this->any())->method('setWishlistId')->will($this->returnSelf()); $newItem->expects($this->any())->method('setStoreId')->will($this->returnSelf()); $newItem->expects($this->any())->method('setOptions')->will($this->returnSelf()); $newItem->expects($this->any())->method('setProduct')->will($this->returnSelf()); $newItem->expects($this->any())->method('setQty')->will($this->returnSelf()); $newItem->expects($this->any())->method('getItem')->will($this->returnValue(2)); $this->itemFactory->expects($this->once())->method('create')->will($this->returnValue($newItem)); $this->storeManager->expects($this->any())->method('getStores')->will($this->returnValue($stores)); $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($stores[0])); $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $product->expects($this->any())->method('getId')->will($this->returnValue($productId)); $instanceType = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Type\\AbstractType')->disableOriginalConstructor()->getMock(); $instanceType->expects($this->once())->method('processConfiguration')->will($this->returnValue($this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock())); $newProduct = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $newProduct->expects($this->any())->method('setStoreId')->with($storeId)->will($this->returnSelf()); $newProduct->expects($this->once())->method('load')->with($productId)->will($this->returnSelf()); $newProduct->expects($this->once())->method('getTypeInstance')->will($this->returnValue($instanceType)); $item = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item')->disableOriginalConstructor()->getMock(); $item->expects($this->once())->method('getProduct')->will($this->returnValue($product)); $items = $this->getMockBuilder('Magento\\Wishlist\\Model\\Resource\\Item\\Collection')->disableOriginalConstructor()->getMock(); $items->expects($this->once())->method('addWishlistFilter')->will($this->returnSelf()); $items->expects($this->once())->method('addStoreFilter')->will($this->returnSelf()); $items->expects($this->once())->method('setVisibilityFilter')->will($this->returnSelf()); $items->expects($this->once())->method('getItemById')->will($this->returnValue($item)); $items->expects($this->any())->method('getIterator')->will($this->returnValue(new \ArrayIterator([$item]))); $this->itemsFactory->expects($this->any())->method('create')->will($this->returnValue($items)); $this->productFactory->expects($this->once())->method('create')->will($this->returnValue($newProduct)); $this->assertInstanceOf('Magento\\Wishlist\\Model\\Wishlist', $this->wishlist->updateItem($itemId, $buyRequest, $param)); }
public function testPopulateToUrlGenerationReturnProduct() { $rowData = [\Magento\CatalogImportExport\Model\Import\Product::COL_SKU => 'value']; $newSku = ['entity_id' => 'new sku value']; $expectedRowData = [\Magento\CatalogImportExport\Model\Import\Product::COL_SKU => 'value', 'entity_id' => $newSku['entity_id']]; $productMock = $this->getMock('\\Magento\\Catalog\\Model\\Product', ['addData'], [], '', false); $productMock->expects($this->once())->method('addData')->with($expectedRowData); $this->catalogProductFactory->expects($this->once())->method('create')->willReturn($productMock); $this->skuProcessor->expects($this->once())->method('getNewSku')->willReturn($newSku); $result = $this->importProduct->_populateToUrlGeneration($rowData); $this->assertEquals($productMock, $result); }