/** * Set up test * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function setUp() { $this->contextMock = $this->getMock('Magento\\Backend\\App\\Action\\Context', ['getAuthorization', 'getSession', 'getActionFlag', 'getAuth', 'getView', 'getHelper', 'getBackendUrl', 'getFormKeyValidator', 'getLocaleResolver', 'getCanUseBaseUrl', 'getRequest', 'getResponse', 'getObjectManager', 'getMessageManager'], [], '', false); $this->response = $this->getMock('Magento\\Framework\\App\\ResponseInterface', ['setRedirect', 'sendResponse'], [], '', false); $this->view = $this->getMock('\\Magento\\Framework\\App\\ViewInterface', ['loadLayout', 'getPage', 'getConfig', 'getTitle', 'renderLayout', 'loadLayoutUpdates', 'getDefaultLayoutHandle', 'addPageLayoutHandles', 'generateLayoutBlocks', 'generateLayoutXml', 'getLayout', 'addActionLayoutHandles', 'setIsLayoutLoaded', 'isLayoutLoaded'], [], '', false); $this->session = $this->getMock('\\Magento\\Backend\\Model\\Session', ['setIsUrlNotice'], [], '', false); $this->session->expects($this->any())->method('setIsUrlNotice')->willReturn($this->objectManager); $this->actionFlag = $this->getMock('\\Magento\\Framework\\App\\ActionFlag', ['get'], [], '', false); $this->actionFlag->expects($this->any())->method("get")->willReturn($this->objectManager); $this->objectManager = $this->getMock('Magento\\Framework\\TestFramework\\Unit\\Helper\\ObjectManager', ['get'], [], '', false); $this->request = $this->getMockForAbstractClass('\\Magento\\Framework\\App\\RequestInterface', ['getParam', 'getRequest'], '', false); $this->response->expects($this->any())->method("setRedirect")->willReturn(1); $this->page = $this->getMock('\\Magento\\Framework\\View\\Result\\Page', [], [], '', false); $this->config = $this->getMock('\\Magento\\Framework\\View\\Result\\Page', [], [], '', false); $this->title = $this->getMock('\\Magento\\Framework\\View\\Page\\Title', [], [], '', false); $this->messageManager = $this->getMockForAbstractClass('\\Magento\\Framework\\Message\\ManagerInterface', ['addError', 'addSuccess'], '', false); $this->indexReg = $this->getMock('Magento\\Framework\\Indexer\\IndexerRegistry', ['get', 'setScheduled'], [], '', false); $this->helper = $this->getMock('\\Magento\\Backend\\Helper\\Data', ['getUrl'], [], '', false); $this->contextMock->expects($this->any())->method("getObjectManager")->willReturn($this->objectManager); $this->contextMock->expects($this->any())->method("getRequest")->willReturn($this->request); $this->contextMock->expects($this->any())->method("getResponse")->willReturn($this->response); $this->contextMock->expects($this->any())->method("getMessageManager")->willReturn($this->messageManager); $this->contextMock->expects($this->any())->method("getSession")->willReturn($this->session); $this->contextMock->expects($this->any())->method("getActionFlag")->willReturn($this->actionFlag); $this->contextMock->expects($this->any())->method("getHelper")->willReturn($this->helper); }
/** * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute */ public function testExecute() { $this->prepareExecute(); $this->resultPageFactoryMock->expects($this->once())->method('create')->willReturn($this->resultPageMock); $this->resultPageMock->expects($this->once())->method('setActiveMenu')->with('Magento_Customer::customer_manage'); $this->resultPageMock->expects($this->once())->method('getConfig')->willReturn($this->pageConfigMock); $this->pageConfigMock->expects($this->once())->method('getTitle')->willReturn($this->pageTitleMock); $this->pageTitleMock->expects($this->once())->method('prepend')->with('Customers'); $this->resultPageMock->expects($this->atLeastOnce())->method('addBreadcrumb')->withConsecutive(['Customers', 'Customers'], ['Manage Customers', 'Manage Customers']); $this->sessionMock->expects($this->once())->method('unsCustomerData'); $this->assertInstanceOf('Magento\\Framework\\View\\Result\\Page', $this->indexController->execute()); }
/** * Redirect into response section * * @return void */ protected function redirectSection() { $this->actionFlag->expects($this->once())->method('get')->with('', \Magento\Backend\App\AbstractAction::FLAG_IS_URLS_CHECKED)->will($this->returnValue(true)); $this->sessionMock->expects($this->once())->method('setIsUrlNotice')->with(true); $this->helperMock->expects($this->once())->method('getUrl')->will($this->returnValue('redirect-path')); $this->responseMock->expects($this->once())->method('setRedirect'); }
/** * Run test execute method * * @param int|bool $categoryId * @param int $storeId * @return void * * @dataProvider dataProviderExecute * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecute($categoryId, $storeId) { $rootCategoryId = 2; $this->requestMock->expects($this->atLeastOnce())->method('getParam')->will($this->returnValueMap([['id', false, $categoryId], ['store', null, $storeId]])); $this->requestMock->expects($this->atLeastOnce())->method('getQuery')->with('isAjax')->will($this->returnValue(false)); $this->mockInitCategoryCall(); $this->sessionMock->expects($this->once())->method('__call')->will($this->returnValue([])); if (!$categoryId) { if ($storeId) { $this->storeManagerInterfaceMock->expects($this->once())->method('getStore')->with($storeId)->will($this->returnSelf()); } else { $this->storeManagerInterfaceMock->expects($this->once())->method('getDefaultStoreView')->will($this->returnSelf()); } $this->storeManagerInterfaceMock->expects($this->once())->method('getRootCategoryId')->will($this->returnValue($rootCategoryId)); $categoryId = $rootCategoryId; } $this->requestMock->expects($this->atLeastOnce())->method('setParam')->with('id', $categoryId)->will($this->returnValue(true)); $this->categoryMock->expects($this->atLeastOnce())->method('getId')->will($this->returnValue($categoryId)); /** * @var \Magento\Framework\View\Element\Template * |\PHPUnit_Framework_MockObject_MockObject $blockMock */ $blockMock = $this->getMock('Magento\\Framework\\View\\Element\\Template', ['setStoreId'], [], '', false); $blockMock->expects($this->once())->method('setStoreId')->with($storeId); $this->resultPageMock->expects($this->once())->method('getLayout')->will($this->returnSelf()); $this->resultPageMock->expects($this->once())->method('getBlock')->willReturn($blockMock); $this->edit->execute(); }
/** * Prepare required values * * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function setUp() { $this->_request = $this->getMockBuilder('Magento\\Framework\\App\\Request\\Http')->disableOriginalConstructor()->getMock(); $this->_response = $this->getMockBuilder('Magento\\Framework\\App\\Response\\Http')->disableOriginalConstructor()->setMethods(['setRedirect', 'getHeader', '__wakeup'])->getMock(); $this->_response->expects($this->any())->method('getHeader')->with($this->equalTo('X-Frame-Options'))->will($this->returnValue(true)); $this->_objectManager = $this->getMockBuilder('Magento\\Framework\\App\\ObjectManager')->disableOriginalConstructor()->setMethods(['get', 'create'])->getMock(); $frontControllerMock = $this->getMockBuilder('Magento\\Framework\\App\\FrontController')->disableOriginalConstructor()->getMock(); $actionFlagMock = $this->getMockBuilder('Magento\\Framework\\App\\ActionFlag')->disableOriginalConstructor()->getMock(); $this->_session = $this->getMockBuilder('Magento\\Backend\\Model\\Session')->disableOriginalConstructor()->setMethods(['setIsUrlNotice', '__wakeup'])->getMock(); $this->_session->expects($this->any())->method('setIsUrlNotice'); $this->_helper = $this->getMockBuilder('Magento\\Backend\\Helper\\Data')->disableOriginalConstructor()->setMethods(['getUrl'])->getMock(); $this->messageManager = $this->getMockBuilder('Magento\\Framework\\Message\\Manager')->disableOriginalConstructor()->setMethods(['addSuccess', 'addMessage', 'addException'])->getMock(); $contextArgs = ['getHelper', 'getSession', 'getAuthorization', 'getTranslator', 'getObjectManager', 'getFrontController', 'getActionFlag', 'getMessageManager', 'getLayoutFactory', 'getEventManager', 'getRequest', 'getResponse', 'getTitle', 'getView']; $contextMock = $this->getMockBuilder('\\Magento\\Backend\\App\\Action\\Context')->disableOriginalConstructor()->setMethods($contextArgs)->getMock(); $contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->_request)); $contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->_response)); $contextMock->expects($this->any())->method('getObjectManager')->will($this->returnValue($this->_objectManager)); $contextMock->expects($this->any())->method('getFrontController')->will($this->returnValue($frontControllerMock)); $contextMock->expects($this->any())->method('getActionFlag')->will($this->returnValue($actionFlagMock)); $contextMock->expects($this->any())->method('getHelper')->will($this->returnValue($this->_helper)); $contextMock->expects($this->any())->method('getSession')->will($this->returnValue($this->_session)); $contextMock->expects($this->any())->method('getMessageManager')->will($this->returnValue($this->messageManager)); $this->titleMock = $this->getMockBuilder('\\Magento\\Framework\\View\\Page\\Title')->disableOriginalConstructor()->getMock(); $contextMock->expects($this->any())->method('getTitle')->will($this->returnValue($this->titleMock)); $this->viewInterfaceMock = $this->getMockBuilder('\\Magento\\Framework\\App\\ViewInterface')->disableOriginalConstructor()->getMock(); $this->viewInterfaceMock->expects($this->any())->method('loadLayout')->will($this->returnSelf()); $contextMock->expects($this->any())->method('getView')->will($this->returnValue($this->viewInterfaceMock)); $this->resultLayoutMock = $this->getMockBuilder('Magento\\Framework\\View\\Result\\Layout')->disableOriginalConstructor()->getMock(); $this->pageConfigMock = $this->getMockBuilder('Magento\\Framework\\View\\Page\\Config')->disableOriginalConstructor()->getMock(); $this->customerAccountManagement = $this->getMockBuilder('Magento\\Customer\\Api\\AccountManagementInterface')->getMock(); $this->resultLayoutFactoryMock = $this->getMockBuilder('Magento\\Framework\\View\\Result\\LayoutFactory')->disableOriginalConstructor()->getMock(); $args = ['context' => $contextMock, 'customerAccountManagement' => $this->customerAccountManagement, 'resultLayoutFactory' => $this->resultLayoutFactoryMock]; $helperObjectManager = new \Magento\TestFramework\Helper\ObjectManager($this); $this->_testedObject = $helperObjectManager->getObject('Magento\\Customer\\Controller\\Adminhtml\\Index\\Newsletter', $args); }
public function testExecute() { $shipmentId = 1000012; $orderId = 10003; $tracking = []; $shipmentData = ['items' => [], 'send_email' => '']; $shipment = $this->getMock('Magento\\Sales\\Model\\Order\\Shipment', ['load', 'save', 'register', 'getOrder', 'getOrderId', '__wakeup'], [], '', false); $this->request->expects($this->any())->method('getParam')->will($this->returnValueMap([['order_id', null, $orderId], ['shipment_id', null, $shipmentId], ['shipment', null, $shipmentData], ['tracking', null, $tracking]])); $this->shipmentLoader->expects($this->any())->method('setShipmentId')->with($shipmentId); $this->shipmentLoader->expects($this->any())->method('setOrderId')->with($orderId); $this->shipmentLoader->expects($this->any())->method('setShipment')->with($shipmentData); $this->shipmentLoader->expects($this->any())->method('setTracking')->with($tracking); $this->shipmentLoader->expects($this->once())->method('load')->will($this->returnValue($shipment)); $this->session->expects($this->once())->method('getCommentText')->with(true)->will($this->returnValue('')); $this->objectManager->expects($this->atLeastOnce())->method('get')->with('Magento\\Backend\\Model\\Session')->will($this->returnValue($this->session)); $this->view->expects($this->once())->method('loadLayout')->will($this->returnSelf()); $this->view->expects($this->once())->method('renderLayout')->will($this->returnSelf()); $this->view->expects($this->any())->method('getPage')->willReturn($this->resultPageMock); $this->resultPageMock->expects($this->any())->method('getConfig')->willReturn($this->pageConfigMock); $this->pageConfigMock->expects($this->any())->method('getTitle')->willReturn($this->pageTitleMock); $layout = $this->getMock('Magento\\Framework\\View\\Layout\\Element\\Layout', ['getBlock'], [], '', false); $menuBlock = $this->getMock('Magento\\Framework\\View\\Element\\BlockInterface', ['toHtml', 'setActive', 'getMenuModel'], [], '', false); $menuModel = $this->getMockBuilder('Magento\\Backend\\Model\\Menu')->disableOriginalConstructor()->getMock(); $itemId = 'Magento_Sales::sales_order'; $parents = [new \Magento\Framework\DataObject(['title' => 'title1']), new \Magento\Framework\DataObject(['title' => 'title2']), new \Magento\Framework\DataObject(['title' => 'title3'])]; $menuModel->expects($this->once())->method('getParentItems')->with($itemId)->will($this->returnValue($parents)); $menuBlock->expects($this->once())->method('setActive')->with($itemId); $menuBlock->expects($this->once())->method('getMenuModel')->will($this->returnValue($menuModel)); $this->view->expects($this->once())->method('getLayout')->will($this->returnValue($layout)); $layout->expects($this->once())->method('getBlock')->with('menu')->will($this->returnValue($menuBlock)); $this->assertNull($this->newAction->execute()); }
/** * @param string $path * @param array $arguments * @param int $index */ protected function prepareRedirect($path, $arguments, $index) { $this->actionFlag->expects($this->any())->method('get')->with('', 'check_url_settings')->will($this->returnValue(true)); $this->session->expects($this->any())->method('setIsUrlNotice')->with(true); $url = $path . '/' . (!empty($arguments) ? $arguments['shipment_id'] : ''); $this->helper->expects($this->at($index))->method('getUrl')->with($path, $arguments)->will($this->returnValue($url)); $this->response->expects($this->at($index))->method('setRedirect')->with($url); }
/** * @param int $cmId */ protected function prepareRedirect($cmId) { $this->actionFlag->expects($this->once())->method('get')->with('', 'check_url_settings')->will($this->returnValue(true)); $this->session->expects($this->once())->method('setIsUrlNotice')->with(true); $path = 'sales/order_creditmemo/view'; $this->response->expects($this->once())->method('setRedirect')->with($path . '/' . $cmId); $this->helper->expects($this->atLeastOnce())->method('getUrl')->with($path, ['creditmemo_id' => $cmId])->will($this->returnValue($path . '/' . $cmId)); }
/** * Run test execute method (fail print) */ public function testExecuteFail() { $this->shipmentLoaderMock->expects($this->once())->method('load')->will($this->returnValue(false)); $this->shipmentLoaderMock->expects($this->once())->method('load')->will($this->returnValue(false)); $this->actionFlag->expects($this->once())->method('get')->with('', \Magento\Backend\App\AbstractAction::FLAG_IS_URLS_CHECKED)->will($this->returnValue(true)); $this->sessionMock->expects($this->once())->method('setIsUrlNotice')->with(true); $this->assertNull($this->controller->execute()); }
public function testExecute() { $data = ['tmp_name' => 'tmp_name', 'path' => 'path', 'file' => 'file']; $uploader = $this->getMockBuilder('Magento\\MediaStorage\\Model\\File\\Uploader')->disableOriginalConstructor()->getMock(); $resultJson = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\Json')->disableOriginalConstructor()->setMethods(['setData'])->getMock(); $this->request->expects($this->once())->method('getParam')->with('type')->willReturn('samples'); $this->sample->expects($this->once())->method('getBaseTmpPath')->willReturn('base_tmp_path'); $this->uploaderFactory->expects($this->once())->method('create')->willReturn($uploader); $this->fileHelper->expects($this->once())->method('uploadFromTmp')->willReturn($data); $this->storageDatabase->expects($this->once())->method('saveFile'); $this->session->expects($this->once())->method('getName')->willReturn('Name'); $this->session->expects($this->once())->method('getSessionId')->willReturn('SessionId'); $this->session->expects($this->once())->method('getCookieLifetime')->willReturn('CookieLifetime'); $this->session->expects($this->once())->method('getCookiePath')->willReturn('CookiePath'); $this->session->expects($this->once())->method('getCookieDomain')->willReturn('CookieDomain'); $this->resultFactory->expects($this->once())->method('create')->willReturn($resultJson); $resultJson->expects($this->once())->method('setData')->willReturnSelf(); $this->assertEquals($resultJson, $this->upload->execute()); }
public function testUpdateNavigationMode() { $this->_setAdditionalExpectations(); $request = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false); $request->expects($this->once())->method('getPathInfo')->will($this->returnValue('/')); $this->_backendSession->expects($this->at(0))->method('setData')->with('vde_current_url', '/'); $this->_backendSession->expects($this->at(1))->method('setData')->with('vde_current_mode', \Magento\DesignEditor\Model\State::MODE_NAVIGATION); $this->_urlModelFactory->expects($this->once())->method('replaceClassName')->with(self::URL_MODEL_NAVIGATION_MODE_CLASS_NAME); $this->_areaEmulator->expects($this->once())->method('emulateLayoutArea')->with(self::AREA_CODE); $controller = $this->getMock('Magento\\Backend\\App\\Action', [], [], '', false); $this->_model->update(self::AREA_CODE, $request, $controller); }
/** * @param string $path * @param array $arguments * @param int $index */ protected function prepareRedirect($path, $arguments, $index) { $this->actionFlag->expects($this->any()) ->method('get') ->with('', 'check_url_settings') ->will($this->returnValue(true)); $this->session->expects($this->any()) ->method('setIsUrlNotice') ->with(true); $this->resultRedirect->expects($this->at($index)) ->method('setPath') ->with($path, ['shipment_id' => $arguments['shipment_id']]); }
public function testExecuteException() { $queryId = 1; $queryText = 'search'; $this->mockGetRequestData($queryText, $queryId); $this->request->expects($this->at(4))->method('getPost')->with('store_id', false)->willReturn(1); $this->query->expects($this->once())->method('setStoreId'); $this->query->expects($this->once())->method('loadByQueryText')->willThrowException(new \Exception()); $this->messageManager->expects($this->once())->method('addException'); $this->session->expects($this->once())->method('setPageData'); $this->redirect->expects($this->once())->method('setPath')->willReturnSelf(); $this->assertSame($this->redirect, $this->controller->execute()); }
public function testForceAdminPasswordChange() { /** @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject $eventObserverMock */ $eventObserverMock = $this->getMockBuilder('Magento\\Framework\\Event\\Observer')->disableOriginalConstructor()->setMethods([])->getMock(); /** @var \Magento\Framework\Event|\PHPUnit_Framework_MockObject_MockObject */ $eventMock = $this->getMockBuilder('Magento\\Framework\\Event')->disableOriginalConstructor()->setMethods(['getControllerAction', 'getRequest'])->getMock(); $this->configInterfaceMock->expects($this->atLeastOnce())->method('getValue')->willReturn(1); $this->authSessionMock->expects($this->once())->method('isLoggedIn')->willReturn(true); $eventObserverMock->expects($this->atLeastOnce())->method('getEvent')->willReturn($eventMock); /** @var \Magento\Framework\App\Action\Action $controllerMock */ $controllerMock = $this->getMockBuilder('Magento\\Framework\\App\\Action\\AbstractAction')->disableOriginalConstructor()->setMethods(['getRedirect', 'getRequest'])->getMockForAbstractClass(); /** @var \Magento\Framework\App\RequestInterface $requestMock */ $requestMock = $this->getMockBuilder('Magento\\Framework\\App\\RequestInterface')->disableOriginalConstructor()->setMethods(['getFullActionName', 'setDispatched'])->getMockForAbstractClass(); $eventMock->expects($this->once())->method('getControllerAction')->willReturn($controllerMock); $eventMock->expects($this->once())->method('getRequest')->willReturn($requestMock); $this->authSessionMock->expects($this->once())->method('getPciAdminUserIsPasswordExpired')->willReturn(true); $requestMock->expects($this->once())->method('getFullActionName')->willReturn('not_in_array'); $this->authSessionMock->expects($this->once())->method('clearStorage'); $this->sessionMock->expects($this->once())->method('clearStorage'); $this->managerInterfaceMock->expects($this->once())->method('addErrorMessage'); $controllerMock->expects($this->once())->method('getRequest')->willReturn($requestMock); $requestMock->expects($this->once())->method('setDispatched')->willReturn(false); $this->model->execute($eventObserverMock); }
public function testSaveActionThrowsException() { $this->requestMock->expects($this->any())->method('getPostValue')->willReturn(['block_id' => 1]); $this->requestMock->expects($this->atLeastOnce())->method('getParam')->willReturnMap([['block_id', null, 1], ['back', null, true]]); $this->objectManagerMock->expects($this->atLeastOnce())->method('create')->with($this->equalTo('Magento\\Cms\\Model\\Block'))->willReturn($this->blockMock); $this->objectManagerMock->expects($this->atLeastOnce())->method('get')->willReturnMap([['Magento\\Backend\\Model\\Session', $this->sessionMock]]); $this->blockMock->expects($this->any())->method('load')->willReturnSelf(); $this->blockMock->expects($this->any())->method('getId')->willReturn(true); $this->blockMock->expects($this->once())->method('setData'); $this->blockMock->expects($this->once())->method('save')->willThrowException(new \Exception('Error message.')); $this->messageManagerMock->expects($this->any())->method('addSuccess')->with(__('You saved the block.')); $this->messageManagerMock->expects($this->once())->method('addError'); $this->sessionMock->expects($this->atLeastOnce())->method('setFormData')->with(['block_id' => 1]); $this->resultRedirect->expects($this->atLeastOnce())->method('setPath')->with('*/*/edit', ['block_id' => $this->blockId])->willReturnSelf(); $this->assertSame($this->resultRedirect, $this->saveController->execute()); }
/** * @param string $actionName * @return \Magento\Integration\Controller\Adminhtml\Integration * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function _createIntegrationController($actionName) { // Mock Layout passed into constructor $this->_viewMock = $this->getMockBuilder('Magento\\Framework\\App\\ViewInterface')->getMock(); $this->_layoutMock = $this->getMock('Magento\\Framework\\View\\LayoutInterface'); $this->_layoutMergeMock = $this->getMockBuilder('Magento\\Framework\\View\\Model\\Layout\\Merge')->disableOriginalConstructor()->getMock(); $this->_layoutMock->expects($this->any())->method('getUpdate')->will($this->returnValue($this->_layoutMergeMock)); $testElement = new \Magento\Framework\Simplexml\Element('<test>test</test>'); $this->_layoutMock->expects($this->any())->method('getNode')->will($this->returnValue($testElement)); // for _setActiveMenu $this->_viewMock->expects($this->any())->method('getLayout')->will($this->returnValue($this->_layoutMock)); $blockMock = $this->getMockBuilder('Magento\\Backend\\Block\\Menu')->disableOriginalConstructor()->getMock(); $menuMock = $this->getMock('Magento\\Backend\\Model\\Menu', [], [$this->getMock('Psr\\Log\\LoggerInterface')]); $loggerMock = $this->getMockBuilder('Psr\\Log\\LoggerInterface')->getMock(); $loggerMock->expects($this->any())->method('critical')->will($this->returnSelf()); $menuMock->expects($this->any())->method('getParentItems')->will($this->returnValue([])); $blockMock->expects($this->any())->method('getMenuModel')->will($this->returnValue($menuMock)); $this->_layoutMock->expects($this->any())->method('getMessagesBlock')->will($this->returnValue($blockMock)); $this->_layoutMock->expects($this->any())->method('getBlock')->will($this->returnValue($blockMock)); $this->_viewMock->expects($this->any())->method('getPage')->willReturn($this->resultPageMock); $this->resultPageMock->expects($this->any())->method('getConfig')->willReturn($this->viewConfigMock); $this->viewConfigMock->expects($this->any())->method('getTitle')->willReturn($this->pageTitleMock); $this->_escaper->expects($this->any())->method('escapeHtml')->will($this->returnArgument(0)); $this->resultRedirectFactory = $this->getMockBuilder('Magento\\Backend\\Model\\View\\Result\\RedirectFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->resultFactory = $this->getMockBuilder('Magento\\Framework\\Controller\\ResultFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->_authMock->expects($this->any())->method('getUser')->will($this->returnValue($this->_userMock)); $this->_userMock->expects($this->any())->method('load')->willReturn($this->_userMock); $this->_backendSessionMock->expects($this->any())->method('getIntegrationData')->willReturn(['all_resources' => 1]); $contextParameters = ['view' => $this->_viewMock, 'objectManager' => $this->_objectManagerMock, 'session' => $this->_backendSessionMock, 'translator' => $this->_translateModelMock, 'request' => $this->_requestMock, 'response' => $this->_responseMock, 'messageManager' => $this->_messageManager, 'resultRedirectFactory' => $this->resultRedirectFactory, 'resultFactory' => $this->resultFactory, 'auth' => $this->_authMock, 'eventManager' => $this->_eventManagerMock]; $this->_backendActionCtxMock = $this->_objectManagerHelper->getObject('Magento\\Backend\\App\\Action\\Context', $contextParameters); $integrationCollection = $this->getMockBuilder('\\Magento\\Integration\\Model\\ResourceModel\\Integration\\Collection')->disableOriginalConstructor()->setMethods(['addUnsecureUrlsFilter', 'getSize'])->getMock(); $integrationCollection->expects($this->any())->method('addUnsecureUrlsFilter')->will($this->returnValue($integrationCollection)); $integrationCollection->expects($this->any())->method('getSize')->will($this->returnValue(0)); $subControllerParams = ['context' => $this->_backendActionCtxMock, 'integrationService' => $this->_integrationSvcMock, 'oauthService' => $this->_oauthSvcMock, 'registry' => $this->_registryMock, 'logger' => $loggerMock, 'integrationData' => $this->_integrationHelperMock, 'escaper' => $this->_escaper, 'integrationCollection' => $integrationCollection]; /** Create IntegrationController to test */ $controller = $this->_objectManagerHelper->getObject('\\Magento\\Integration\\Controller\\Adminhtml\\Integration\\' . $actionName, $subControllerParams); if ($actionName == 'Save') { $reflection = new \ReflectionClass(get_class($controller)); $reflectionProperty = $reflection->getProperty('securityCookie'); $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($controller, $this->securityCookieMock); } return $controller; }
/** * Run test execute method (no shipment) */ public function testExecuteNoShipment() { $orderId = 1; $shipmentId = 1; $shipment = []; $tracking = []; $this->requestMock->expects($this->at(0))->method('getParam')->with('order_id')->will($this->returnValue($orderId)); $this->requestMock->expects($this->at(1))->method('getParam')->with('shipment_id')->will($this->returnValue($shipmentId)); $this->requestMock->expects($this->at(2))->method('getParam')->with('shipment')->will($this->returnValue($shipment)); $this->requestMock->expects($this->at(3))->method('getParam')->with('tracking')->will($this->returnValue($tracking)); $this->shipmentLoaderMock->expects($this->once())->method('setOrderId')->with($orderId); $this->shipmentLoaderMock->expects($this->once())->method('setShipmentId')->with($shipmentId); $this->shipmentLoaderMock->expects($this->once())->method('setShipment')->with($shipment); $this->shipmentLoaderMock->expects($this->once())->method('setTracking')->with($tracking); $this->shipmentLoaderMock->expects($this->once())->method('load')->will($this->returnValue(false)); $this->actionFlag->expects($this->once())->method('get')->with('', \Magento\Backend\App\AbstractAction::FLAG_IS_URLS_CHECKED)->will($this->returnValue(true)); $this->sessionMock->expects($this->once())->method('setIsUrlNotice')->with(true); $this->assertNull($this->controller->execute()); }
public function testExecuteWithTaxClassAndException() { $taxClass = '3'; $groupId = 0; $code = 'NOT LOGGED IN'; $this->request->expects($this->exactly(3))->method('getParam')->withConsecutive(['tax_class'], ['id'], ['code'])->willReturnOnConsecutiveCalls($taxClass, $groupId, null); $this->resultRedirectFactory->expects($this->once())->method('create')->willReturn($this->resultRedirect); $this->groupRepositoryMock->expects($this->once())->method('getById')->with($groupId)->willReturn($this->group); $this->group->expects($this->once())->method('getCode')->willReturn($code); $this->group->expects($this->once())->method('setCode')->with($code); $this->group->expects($this->once())->method('setTaxClassId')->with($taxClass); $this->groupRepositoryMock->expects($this->once())->method('save')->with($this->group); $this->messageManager->expects($this->once())->method('addSuccess')->with(__('You saved the customer group.')); $exception = new \Exception('Exception'); $this->resultRedirect->expects($this->at(0))->method('setPath')->with('customer/group')->willThrowException($exception); $this->messageManager->expects($this->once())->method('addError')->with('Exception'); $this->dataObjectProcessorMock->expects($this->once())->method('buildOutputDataArray')->with($this->group, '\\Magento\\Customer\\Api\\Data\\GroupInterface')->willReturn(['code' => $code]); $this->session->expects($this->once())->method('setCustomerGroupData')->with(['customer_group_code' => $code]); $this->resultRedirect->expects($this->at(1))->method('setPath')->with('customer/group/edit', ['id' => $groupId]); $this->assertSame($this->resultRedirect, $this->controller->execute()); }
/** * test execute method */ public function testExecute() { $this->requestMock->expects($this->exactly(4))->method('getParam')->will($this->returnValueMap([['order_id', null, 'order_id'], ['creditmemo_id', null, 'creditmemo_id'], ['creditmemo', null, 'creditmemo'], ['invoice_id', null, 'invoice_id']])); $this->creditmemoLoaderMock->expects($this->once())->method('setOrderId')->with($this->equalTo('order_id')); $this->creditmemoLoaderMock->expects($this->once())->method('setCreditmemoId')->with($this->equalTo('creditmemo_id')); $this->creditmemoLoaderMock->expects($this->once())->method('setCreditmemo')->with($this->equalTo('creditmemo')); $this->creditmemoLoaderMock->expects($this->once())->method('setInvoiceId')->with($this->equalTo('invoice_id')); $this->creditmemoLoaderMock->expects($this->once())->method('load')->will($this->returnValue($this->creditmemoMock)); $this->creditmemoMock->expects($this->exactly(2))->method('getInvoice')->will($this->returnValue($this->invoiceMock)); $this->invoiceMock->expects($this->once())->method('getIncrementId')->will($this->returnValue('invoice-increment-id')); $this->titleMock->expects($this->exactly(2))->method('prepend')->will($this->returnValueMap([['Credit Memos', null], ['New Memo for #invoice-increment-id', null], ['item-title', null]])); $this->objectManagerMock->expects($this->once())->method('get')->with($this->equalTo('Magento\\Backend\\Model\\Session'))->will($this->returnValue($this->backendSessionMock)); $this->backendSessionMock->expects($this->once())->method('getCommentText')->with($this->equalTo(true))->will($this->returnValue('comment')); $this->creditmemoMock->expects($this->once())->method('setCommentText')->with($this->equalTo('comment')); $this->resultPageMock->expects($this->any())->method('getConfig')->will($this->returnValue($this->pageConfigMock)); $this->pageConfigMock->expects($this->any())->method('getTitle')->willReturn($this->titleMock); $this->resultPageFactoryMock->expects($this->once())->method('create')->willReturn($this->resultPageMock); $this->resultPageMock->expects($this->once())->method('setActiveMenu')->with('Magento_Sales::sales_order')->willReturnSelf(); $this->resultPageMock->expects($this->atLeastOnce())->method('getConfig')->willReturn($this->pageConfigMock); $this->assertInstanceOf('Magento\\Backend\\Model\\View\\Result\\Page', $this->controller->execute()); }
/** * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecuteWithNewCustomerAndException() { $subscription = 'false'; $postValue = ['customer' => ['coolness' => false, 'disable_auto_group_change' => 'false'], 'subscription' => $subscription]; $filteredData = ['coolness' => false, 'disable_auto_group_change' => 'false']; /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $formMock */ $attributeMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface')->disableOriginalConstructor()->getMock(); $attributeMock->expects($this->once())->method('getAttributeCode')->willReturn('coolness'); $attributeMock->expects($this->once())->method('getFrontendInput')->willReturn('int'); $attributes = [$attributeMock]; $this->requestMock->expects($this->exactly(2))->method('getPostValue')->willReturn($postValue); $this->requestMock->expects($this->exactly(2))->method('getPost')->willReturnMap([['customer', null, $postValue['customer']], ['address', null, null]]); /** @var \Magento\Customer\Model\Metadata\Form|\PHPUnit_Framework_MockObject_MockObject $formMock */ $formMock = $this->getMockBuilder('Magento\\Customer\\Model\\Metadata\\Form')->disableOriginalConstructor()->getMock(); $this->formFactoryMock->expects($this->once())->method('create')->with(\Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'adminhtml_customer', [], false, \Magento\Customer\Model\Metadata\Form::DONT_IGNORE_INVISIBLE)->willReturn($formMock); $formMock->expects($this->once())->method('extractData')->with($this->requestMock, 'customer')->willReturn($filteredData); /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */ $objectMock = $this->getMockBuilder('Magento\\Framework\\DataObject')->disableOriginalConstructor()->getMock(); $this->objectFactoryMock->expects($this->once())->method('create')->with(['data' => $postValue])->willReturn($objectMock); $objectMock->expects($this->once())->method('getData')->with('customer')->willReturn($postValue['customer']); $formMock->expects($this->once())->method('getAttributes')->willReturn($attributes); /** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */ $customerMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock(); $this->customerDataFactoryMock->expects($this->once())->method('create')->willReturn($customerMock); $exception = new \Exception(__('Exception')); $this->managementMock->expects($this->once())->method('createAccount')->with($customerMock, null, '')->willThrowException($exception); $customerMock->expects($this->never())->method('getId'); $this->authorizationMock->expects($this->never())->method('isAllowed'); $this->subscriberFactoryMock->expects($this->never())->method('create'); $this->sessionMock->expects($this->never())->method('unsCustomerData'); $this->registryMock->expects($this->never())->method('register'); $this->messageManagerMock->expects($this->never())->method('addSuccess'); $this->messageManagerMock->expects($this->once())->method('addException')->with($exception, __('Something went wrong while saving the customer.')); $this->sessionMock->expects($this->once())->method('setCustomerData')->with($postValue); /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */ $redirectMock = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\Redirect')->disableOriginalConstructor()->getMock(); $this->redirectFactoryMock->expects($this->once())->method('create')->with([])->willReturn($redirectMock); $redirectMock->expects($this->once())->method('setPath')->with('customer/*/new', ['_current' => true])->willReturn(true); $this->assertEquals($redirectMock, $this->model->execute()); }
public function testInitFormWithCustomerFormData() { $customerId = 1; $subscriberMock = $this->getMock('\\Magento\\Newsletter\\Model\\Subscriber', [], [], '', false); $fieldsetMock = $this->getMock('\\Magento\\Framework\\Data\\Form\\Element\\Fieldset', [], [], '', false); $elementMock = $this->getMock('Magento\\Framework\\Data\\Form\\Element\\Checkbox', ['setIsChecked'], [], '', false); $formMock = $this->getMock('\\Magento\\Framework\\Data\\Form', ['setHtmlIdPrefix', 'addFieldset', 'setValues', 'getElement', 'setForm', 'setParent', 'setBaseUrl'], [], '', false); $this->registryMock->expects($this->exactly(3))->method('registry')->willReturnMap([[RegistryConstants::CURRENT_CUSTOMER_ID, $customerId], ['subscriber', $subscriberMock]]); $this->formFactoryMock->expects($this->once())->method('create')->willReturn($formMock); $formMock->expects($this->once())->method('setHtmlIdPrefix')->with('_newsletter'); $this->subscriberFactoryMock->expects($this->once())->method('create')->willReturn($subscriberMock); $subscriberMock->expects($this->once())->method('loadByCustomerId')->with($customerId)->willReturnSelf(); $formMock->expects($this->once())->method('addFieldset')->willReturn($fieldsetMock); $fieldsetMock->expects($this->once())->method('addField')->willReturn($elementMock); $this->accountManagementMock->expects($this->once())->method('isReadOnly')->with($customerId)->willReturn(false); $subscriberMock->expects($this->once())->method('isSubscribed')->willReturn(false); $this->urlBuilderMock->expects($this->once())->method('getBaseUrl')->willReturn('domain.com'); $this->backendSessionMock->expects($this->once())->method('getCustomerFormData')->willReturn(['customer' => ['entity_id' => $customerId], 'subscription' => true]); $formMock->expects($this->exactly(2))->method('getElement')->willReturnMap([['subscription', $elementMock]]); $elementMock->expects($this->exactly(2))->method('setIsChecked')->willReturnMap([[false], [true]]); $this->assertSame($this->model, $this->model->initForm()); }
/** * test execute method */ public function testExecute() { $this->requestMock->expects($this->exactly(4))->method('getParam')->will($this->returnValueMap([['order_id', null, 'order_id'], ['creditmemo_id', null, 'creditmemo_id'], ['creditmemo', null, 'creditmemo'], ['invoice_id', null, 'invoice_id']])); $this->creditmemoLoaderMock->expects($this->once())->method('setOrderId')->with($this->equalTo('order_id')); $this->creditmemoLoaderMock->expects($this->once())->method('setCreditmemoId')->with($this->equalTo('creditmemo_id')); $this->creditmemoLoaderMock->expects($this->once())->method('setCreditmemo')->with($this->equalTo('creditmemo')); $this->creditmemoLoaderMock->expects($this->once())->method('setInvoiceId')->with($this->equalTo('invoice_id')); $this->creditmemoLoaderMock->expects($this->once())->method('load')->will($this->returnValue($this->creditmemoMock)); $this->creditmemoMock->expects($this->exactly(2))->method('getInvoice')->will($this->returnValue($this->invoiceMock)); $this->invoiceMock->expects($this->once())->method('getIncrementId')->will($this->returnValue('invoice-increment-id')); $this->titleMock->expects($this->exactly(3))->method('add')->will($this->returnValueMap([['Credit Memos', null], ['New Memo for #invoice-increment-id', null], ['item-title', null]])); $this->objectManagerMock->expects($this->once())->method('get')->with($this->equalTo('Magento\\Backend\\Model\\Session'))->will($this->returnValue($this->backendSessionMock)); $this->backendSessionMock->expects($this->once())->method('getCommentText')->with($this->equalTo(true))->will($this->returnValue('comment')); $this->creditmemoMock->expects($this->once())->method('setCommentText')->with($this->equalTo('comment')); $this->viewMock->expects($this->once())->method('loadLayout'); $this->viewMock->expects($this->once())->method('renderLayout'); $this->viewMock->expects($this->once())->method('getLayout')->will($this->returnValue($this->layoutMock)); $this->layoutMock->expects($this->once())->method('getBlock')->with($this->equalTo('menu'))->will($this->returnValue($this->blockMenuMock)); $this->blockMenuMock->expects($this->once())->method('setActive')->with($this->equalTo('Magento_Sales::sales_order')); $this->blockMenuMock->expects($this->once())->method('getMenuModel')->will($this->returnValue($this->modelMenuMock)); $this->modelMenuMock->expects($this->once())->method('getParentItems')->will($this->returnValue([$this->modelMenuItem])); $this->modelMenuItem->expects($this->once())->method('getTitle')->will($this->returnValue('item-title')); $this->assertNull($this->controller->execute()); }
/** * Set expectations in case of \Magento\Framework\Exception\LocalizedException for saveAction method * * @param array $data * @param string $errorMessage */ protected function _setSaveActionExpectationForMageCoreException($data, $errorMessage) { $this->_messageManager->expects($this->once())->method('addError')->with($this->equalTo($errorMessage)); $this->_sessionMock->expects($this->once())->method('setFormData')->with($this->equalTo($data)); }