/** * Retrieve synchronize process state and it's parameters in json format * * @return \Magento\Framework\Controller\Result\JSON * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute() { $result = []; $flag = $this->_getSyncFlag(); if ($flag) { $state = $flag->getState(); switch ($state) { case \Magento\Core\Model\File\Storage\Flag::STATE_INACTIVE: $flagData = $flag->getFlagData(); if (is_array($flagData)) { if (isset($flagData['destination']) && !empty($flagData['destination'])) { $result['destination'] = $flagData['destination']; } } $state = \Magento\Core\Model\File\Storage\Flag::STATE_INACTIVE; break; case \Magento\Core\Model\File\Storage\Flag::STATE_RUNNING: if (!$flag->getLastUpdate() || time() <= strtotime($flag->getLastUpdate()) + \Magento\Core\Model\File\Storage\Flag::FLAG_TTL) { $flagData = $flag->getFlagData(); if (is_array($flagData) && isset($flagData['source']) && !empty($flagData['source']) && isset($flagData['destination']) && !empty($flagData['destination'])) { $result['message'] = __('Synchronizing %1 to %2', $flagData['source'], $flagData['destination']); } else { $result['message'] = __('Synchronizing...'); } break; } else { $flagData = $flag->getFlagData(); if (is_array($flagData) && !(isset($flagData['timeout_reached']) && $flagData['timeout_reached'])) { $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical(new \Magento\Framework\Exception(__('The timeout limit for response from synchronize process was reached.'))); $state = \Magento\Core\Model\File\Storage\Flag::STATE_FINISHED; $flagData['has_errors'] = true; $flagData['timeout_reached'] = true; $flag->setState($state)->setFlagData($flagData)->save(); } } // fall-through intentional // fall-through intentional // fall-through intentional // fall-through intentional case \Magento\Core\Model\File\Storage\Flag::STATE_FINISHED: case \Magento\Core\Model\File\Storage\Flag::STATE_NOTIFIED: $flagData = $flag->getFlagData(); if (!isset($flagData['has_errors'])) { $flagData['has_errors'] = false; } $result['has_errors'] = $flagData['has_errors']; break; default: $state = \Magento\Core\Model\File\Storage\Flag::STATE_INACTIVE; break; } } else { $state = \Magento\Core\Model\File\Storage\Flag::STATE_INACTIVE; } $result['state'] = $state; /** @var \Magento\Framework\Controller\Result\JSON $resultJson */ $resultJson = $this->resultJsonFactory->create(); return $resultJson->setData($result); }
public function testExecuteException() { $response = ['error' => true, 'message' => 'Cannot add new comment.']; $e = new \Exception('test'); $this->requestMock->expects($this->once())->method('getParam')->will($this->throwException($e)); $this->resultJsonFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->resultJsonMock)); $this->resultJsonMock->expects($this->once())->method('setData')->with($response); $this->assertSame($this->resultJsonMock, $this->controller->execute()); }
public function testExecuteNoComment() { $message = 'The Comment Text field cannot be empty.'; $response = ['error' => true, 'message' => $message]; $data = []; $this->requestMock->expects($this->once())->method('getPost')->with('comment')->willReturn($data); $this->resultJsonFactoryMock->expects($this->once())->method('create')->willReturn($this->resultJsonMock); $this->resultJsonMock->expects($this->once())->method('setData')->with($response)->willReturnSelf(); $this->assertInstanceOf('Magento\\Framework\\Controller\\Result\\JSON', $this->controller->execute()); }
public function testExecuteException() { $message = 'Cannot update item quantity.'; $response = ['error' => true, 'message' => $message]; $orderMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->setMethods(['load', 'getId', 'canInvoice'])->getMock(); $orderMock->expects($this->once())->method('load')->will($this->returnSelf()); $orderMock->expects($this->once())->method('getId')->willReturn(null); $this->objectManagerMock->expects($this->at(0))->method('create')->with('Magento\\Sales\\Model\\Order')->willReturn($orderMock); $this->titleMock->expects($this->never())->method('prepend')->with('Invoices'); /** @var \Magento\Framework\Controller\Result\JSON|\PHPUnit_Framework_MockObject_MockObject */ $resultJsonMock = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\JSON')->disableOriginalConstructor()->setMethods([])->getMock(); $resultJsonMock->expects($this->once())->method('setData')->with($response); $this->resultJsonFactoryMock->expects($this->once())->method('create')->will($this->returnValue($resultJsonMock)); $this->assertSame($resultJsonMock, $this->controller->execute()); }
/** * Run test execute method * * @param int|bool $categoryId * @param int $storeId * @param int|string $activeTabId * @param int|null $parentId * @return void * * @dataProvider dataProviderExecute * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecute($categoryId, $storeId, $activeTabId, $parentId) { $rootCategoryId = 896; $products = [['any_product']]; $postData = ['general' => ['general-data'], 'category_products' => json_encode($products)]; /** * @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', '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', ['setActiveTabId'], [], '', 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\Framework\Store\StoreManagerInterface * |\PHPUnit_Framework_MockObject_MockObject $storeManagerMock */ $storeManagerMock = $this->getMockForAbstractClass('Magento\\Framework\\Store\\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], ['active_tab_id', null, $activeTabId], ['parent', null, $parentId]])); $this->objectManagerMock->expects($this->atLeastOnce())->method('create')->will($this->returnValueMap([['Magento\\Catalog\\Model\\Category', $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\\Framework\\Store\\StoreManagerInterface', $storeManagerMock]])); $categoryMock->expects($this->once())->method('setStoreId')->with($storeId); if ($activeTabId) { $sessionMock->expects($this->once())->method('setActiveTabId')->with($activeTabId); } else { $sessionMock->expects($this->never())->method('setActiveTabId'); } $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([[$postData], ['use_config', ['attribute']], ['use_default', ['default-attribute']], ['return_session_messages_only', true]])); $categoryMock->expects($this->once())->method('addData')->with($postData['general']); $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]); $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()); }