/** * {@inheritdoc} */ public function execute() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'execute'); if (!$pluginInfo) { return parent::execute(); } else { return $this->___callPlugins('execute', func_get_args(), $pluginInfo); } }
/** * Run test execute method * * @param int|bool $categoryId * @param int $storeId * @param int|null $parentId * @return void * * @dataProvider dataProviderExecute * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecute($categoryId, $storeId, $parentId) { $rootCategoryId = \Magento\Catalog\Model\Category::TREE_ROOT_ID; $products = [['any_product']]; $postData = ['general-data', 'parent' => $parentId, 'category_products' => json_encode($products)]; if (isset($storeId)) { $postData['store_id'] = $storeId; } /** * @var \Magento\Backend\Model\View\Result\Redirect * |\PHPUnit_Framework_MockObject_MockObject $resultRedirectMock */ $resultRedirectMock = $this->getMock('Magento\\Backend\\Model\\View\\Result\\Redirect', [], [], '', false); /** * @var \Magento\Framework\View\Element\Messages * |\PHPUnit_Framework_MockObject_MockObject $blockMock */ $blockMock = $this->getMock('Magento\\Framework\\View\\Element\\Messages', ['setMessages', 'getGroupedHtml'], [], '', false); /** * @var \Magento\Catalog\Model\Category * |\PHPUnit_Framework_MockObject_MockObject $categoryMock */ $categoryMock = $this->getMock('Magento\\Catalog\\Model\\Category', ['setStoreId', 'load', 'getPath', 'getResource', 'setPath', 'setParentId', 'setData', 'addData', 'setAttributeSetId', 'getDefaultAttributeSetId', 'getProductsReadonly', 'setPostedProducts', 'getId', 'validate', 'unsetData', 'save', 'toArray'], [], '', false); /** * @var \Magento\Catalog\Model\Category * |\PHPUnit_Framework_MockObject_MockObject $parentCategoryMock */ $parentCategoryMock = $this->getMock('Magento\\Catalog\\Model\\Category', ['setStoreId', 'load', 'getPath', 'setPath', 'setParentId', 'setData', 'addData', 'setAttributeSetId', 'getDefaultAttributeSetId', 'getProductsReadonly', 'setPostedProducts', 'getId'], [], '', false); /** * @var \Magento\Backend\Model\Auth\Session * |\PHPUnit_Framework_MockObject_MockObject $sessionMock */ $sessionMock = $this->getMock('Magento\\Backend\\Model\\Auth\\Session', [], [], '', false); /** * @var \Magento\Framework\Registry * |\PHPUnit_Framework_MockObject_MockObject $registryMock */ $registryMock = $this->getMock('Magento\\Framework\\Registry', ['register'], [], '', false); /** * @var \Magento\Cms\Model\Wysiwyg\Config * |\PHPUnit_Framework_MockObject_MockObject $wysiwygConfigMock */ $wysiwygConfigMock = $this->getMock('Magento\\Cms\\Model\\Wysiwyg\\Config', ['setStoreId'], [], '', false); /** * @var \Magento\Store\Model\StoreManagerInterface * |\PHPUnit_Framework_MockObject_MockObject $storeManagerMock */ $storeManagerMock = $this->getMockForAbstractClass('Magento\\Store\\Model\\StoreManagerInterface', [], '', false, true, true, ['getStore', 'getRootCategoryId']); /** * @var \Magento\Framework\View\Layout * |\PHPUnit_Framework_MockObject_MockObject $layoutMock */ $layoutMock = $this->getMockForAbstractClass('Magento\\Framework\\View\\Layout', [], '', false, true, true, ['getMessagesBlock']); /** * @var \Magento\Framework\Controller\Result\Json * |\PHPUnit_Framework_MockObject_MockObject $resultJsonMock */ $resultJsonMock = $this->getMock('Magento\\Cms\\Model\\Wysiwyg\\Config', ['setData'], [], '', false); /** * @var \Magento\Framework\Message\Collection * |\PHPUnit_Framework_MockObject_MockObject $messagesMock */ $messagesMock = $this->getMock('Magento\\Framework\\Message\\Collection', [], [], '', false); $this->resultRedirectFactoryMock->expects($this->once())->method('create')->will($this->returnValue($resultRedirectMock)); $this->requestMock->expects($this->atLeastOnce())->method('getParam')->will($this->returnValueMap([['id', false, $categoryId], ['store', null, $storeId], ['parent', null, $parentId]])); $this->objectManagerMock->expects($this->atLeastOnce())->method('create')->will($this->returnValue($categoryMock)); $this->objectManagerMock->expects($this->atLeastOnce())->method('get')->will($this->returnValueMap([['Magento\\Backend\\Model\\Auth\\Session', $sessionMock], ['Magento\\Framework\\Registry', $registryMock], ['Magento\\Cms\\Model\\Wysiwyg\\Config', $wysiwygConfigMock], ['Magento\\Store\\Model\\StoreManagerInterface', $storeManagerMock]])); $categoryMock->expects($this->once())->method('setStoreId')->with($storeId); $registryMock->expects($this->any())->method('register')->will($this->returnValueMap([['category', $categoryMock], ['current_category', $categoryMock]])); $wysiwygConfigMock->expects($this->once())->method('setStoreId')->with($storeId); $this->requestMock->expects($this->atLeastOnce())->method('getPost')->will($this->returnValueMap([['use_config', ['attribute']], ['use_default', ['default-attribute']], ['return_session_messages_only', true]])); $this->requestMock->expects($this->atLeastOnce())->method('getPostValue')->willReturn($postData); $categoryMock->expects($this->once())->method('addData')->with($postData); $categoryMock->expects($this->at(0))->method('getId')->will($this->returnValue($categoryId)); if (!$parentId) { if ($storeId) { $storeManagerMock->expects($this->once())->method('getStore')->with($storeId)->will($this->returnSelf()); $storeManagerMock->expects($this->once())->method('getRootCategoryId')->will($this->returnValue($rootCategoryId)); $parentId = $rootCategoryId; } } $categoryMock->expects($this->any())->method('load')->will($this->returnValue($parentCategoryMock)); $parentCategoryMock->expects($this->once())->method('getPath')->will($this->returnValue('parent_category_path')); $categoryMock->expects($this->once())->method('setPath')->with('parent_category_path'); $categoryMock->expects($this->once())->method('setParentId')->with($parentId); $categoryMock->expects($this->atLeastOnce())->method('setData')->will($this->returnValueMap([['attribute', null, true], ['default-attribute', false, true], ['use_post_data_config', ['attribute'], true]])); $categoryMock->expects($this->once())->method('getDefaultAttributeSetId')->will($this->returnValue('default-attribute')); $categoryMock->expects($this->once())->method('setAttributeSetId')->with('default-attribute'); $categoryMock->expects($this->once())->method('getProductsReadonly')->will($this->returnValue(false)); $categoryMock->expects($this->once())->method('setPostedProducts')->with($products); $this->eventManagerMock->expects($this->once())->method('dispatch')->with('catalog_category_prepare_save', ['category' => $categoryMock, 'request' => $this->requestMock]); $categoryResource = $this->getMock('Magento\\Catalog\\Model\\ResourceModel\\Category', [], [], '', false); $categoryMock->expects($this->once())->method('getResource')->will($this->returnValue($categoryResource)); $categoryMock->expects($this->once())->method('validate')->will($this->returnValue(true)); $categoryMock->expects($this->once())->method('unsetData')->with('use_post_data_config'); $categoryMock->expects($this->once())->method('save'); $this->messageManagerMock->expects($this->once())->method('addSuccess')->with(__('You saved the category.')); $categoryMock->expects($this->at(1))->method('getId')->will($this->returnValue(111)); $this->layoutFactoryMock->expects($this->once())->method('create')->will($this->returnValue($layoutMock)); $layoutMock->expects($this->once())->method('getMessagesBlock')->will($this->returnValue($blockMock)); $this->messageManagerMock->expects($this->once())->method('getMessages')->with(true)->will($this->returnValue($messagesMock)); $blockMock->expects($this->once())->method('setMessages')->with($messagesMock); $blockMock->expects($this->once())->method('getGroupedHtml')->will($this->returnValue('grouped-html')); $this->resultJsonFactoryMock->expects($this->once())->method('create')->will($this->returnValue($resultJsonMock)); $categoryMock->expects($this->once())->method('toArray')->will($this->returnValue(['category-data'])); $resultJsonMock->expects($this->once())->method('setData')->with(['messages' => 'grouped-html', 'error' => false, 'category' => ['category-data']])->will($this->returnValue('result-execute')); $this->assertEquals('result-execute', $this->save->execute()); }