protected function setUp() { $this->objectManager = new ObjectManager($this); $this->productLinkFactoryMock = $this->getMockBuilder(ProductLinkInterfaceFactory::class)->disableOriginalConstructor()->getMock(); $this->productRepositoryMock = $this->getMockBuilder(ProductRepository::class)->disableOriginalConstructor()->getMock(); $this->requestMock = $this->getMockBuilder(RequestInterface::class)->setMethods(['getPost'])->getMockForAbstractClass(); $this->storeMock = $this->getMockBuilder(StoreInterface::class)->setMethods(['getWebsite'])->getMockForAbstractClass(); $this->websiteMock = $this->getMockBuilder(WebsiteInterface::class)->getMockForAbstractClass(); $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)->getMockForAbstractClass(); $this->dateFilterMock = $this->getMockBuilder(DateFilter::class)->disableOriginalConstructor()->getMock(); $this->stockFilterMock = $this->getMockBuilder(StockDataFilter::class)->disableOriginalConstructor()->getMock(); $this->productMock = $this->getMockBuilder(Product::class)->setMethods(['setData', 'addData', 'getId', 'setWebsiteIds', 'isLockedAttribute', 'lockAttribute', 'getAttributes', 'unlockAttribute', 'getOptionsReadOnly', 'setOptions', 'setCanSaveCustomOptions', '__sleep', '__wakeup', 'getSku', 'getProductLinks'])->disableOriginalConstructor()->getMock(); $this->customOptionFactoryMock = $this->getMockBuilder(ProductCustomOptionInterfaceFactory::class)->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->customOptionMock = $this->getMockBuilder(ProductCustomOptionInterface::class)->disableOriginalConstructor()->getMockForAbstractClass(); $this->productLinksMock = $this->getMockBuilder(ProductLinks::class)->disableOriginalConstructor()->getMock(); $this->customOptionFactoryMock->expects($this->any())->method('create')->with(['data' => ['is_delete' => false]])->willReturn($this->customOptionMock); $this->productLinksMock->expects($this->any())->method('initializeLinks')->willReturn($this->productMock); $this->helper = $this->objectManager->getObject(Helper::class, ['request' => $this->requestMock, 'storeManager' => $this->storeManagerMock, 'stockFilter' => $this->stockFilterMock, 'productLinks' => $this->productLinksMock, 'dateFilter' => $this->dateFilterMock, 'customOptionFactory' => $this->customOptionFactoryMock, 'productLinkFactory' => $this->productLinkFactoryMock, 'productRepository' => $this->productRepositoryMock]); }
/** * @covers \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper::initialize * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testInitialize() { $this->websiteMock->expects($this->once())->method('getId')->willReturn($this->websiteId); $this->storeMock->expects($this->once())->method('getWebsite')->willReturn($this->websiteMock); $this->storeManagerMock->expects($this->once())->method('getStore')->with(true)->willReturn($this->storeMock); $this->customOptionMock->expects($this->once())->method('setProductSku'); $this->customOptionMock->expects($this->once())->method('setOptionId'); $optionsData = ['option1' => ['is_delete' => true, 'name' => 'name1', 'price' => 'price1'], 'option2' => ['is_delete' => false, 'name' => 'name1', 'price' => 'price1']]; $productData = ['stock_data' => ['stock_data'], 'options' => $optionsData]; $attributeNonDate = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)->disableOriginalConstructor()->getMock(); $attributeDate = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)->disableOriginalConstructor()->getMock(); $attributeNonDateBackEnd = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend::class)->disableOriginalConstructor()->getMock(); $attributeDateBackEnd = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Backend\Datetime::class)->disableOriginalConstructor()->getMock(); $attributeNonDate->expects($this->any())->method('getBackend')->willReturn($attributeNonDateBackEnd); $attributeDate->expects($this->any())->method('getBackend')->willReturn($attributeDateBackEnd); $this->productMock->expects($this->any())->method('getProductLinks')->willReturn([]); $attributeNonDateBackEnd->expects($this->any())->method('getType')->willReturn('non-datetime'); $attributeDateBackEnd->expects($this->any())->method('getType')->willReturn('datetime'); $attributesArray = [$attributeNonDate, $attributeDate]; $useDefaults = ['attributeCode1', 'attributeCode2']; $this->requestMock->expects($this->at(0))->method('getPost')->with('product')->willReturn($productData); $this->requestMock->expects($this->at(1))->method('getPost')->with('use_default')->willReturn($useDefaults); $this->linkResolverMock->expects($this->once())->method('getLinks')->willReturn([]); $this->stockFilterMock->expects($this->once())->method('filter')->with(['stock_data'])->willReturn(['stock_data']); $this->storeManagerMock->expects($this->once())->method('hasSingleStore')->willReturn(true); $this->productMock->expects($this->once())->method('isLockedAttribute')->with('media')->willReturn(true); $this->productMock->expects($this->once())->method('unlockAttribute')->with('media'); $this->productMock->expects($this->any())->method('getProductLinks')->willReturn([]); $this->productMock->expects($this->once())->method('lockAttribute')->with('media'); $this->productMock->expects($this->once())->method('getAttributes')->willReturn($attributesArray); $productData['category_ids'] = []; $productData['website_ids'] = []; unset($productData['options']); $this->productMock->expects($this->once())->method('addData')->with($productData); $this->productMock->expects($this->once())->method('getSku')->willReturn('sku'); $this->productMock->expects($this->once())->method('setWebsiteIds')->with([$this->websiteId]); $this->productMock->expects($this->any())->method('getOptionsReadOnly')->willReturn(false); $this->customOptionFactoryMock->expects($this->any())->method('create')->with(['data' => $optionsData['option2']])->willReturn($this->customOptionMock); $this->productMock->expects($this->once())->method('setOptions')->with([$this->customOptionMock]); $this->assertEquals($this->productMock, $this->helper->initialize($this->productMock)); }