context = $this->createPartialMock(\Magento\Backend\App\Action\Context::class, [ 'getRequest', 'getResponse', 'getMessageManager', 'getRedirect', 'getObjectManager', 'getSession', 'getActionFlag', 'getHelper', 'getResultRedirectFactory' ]); $this->orderManagementMock = $this->getMockBuilder(\Magento\Sales\Api\OrderManagementInterface::class) ->getMockForAbstractClass(); $this->orderRepositoryMock = $this->getMockBuilder(\Magento\Sales\Api\OrderRepositoryInterface::class) ->getMockForAbstractClass(); $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class) ->getMockForAbstractClass(); $resultRedirectFactory = $this->createPartialMock( \Magento\Backend\Model\View\Result\RedirectFactory::class, ['create'] ); $this->response = $this->createPartialMock( \Magento\Framework\App\ResponseInterface::class, ['setRedirect', 'sendResponse'] ); $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class) ->disableOriginalConstructor()->getMock(); $this->messageManager = $this->createPartialMock( \Magento\Framework\Message\Manager::class, ['addSuccessMessage', 'addErrorMessage'] ); $this->orderMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderInterface::class) ->getMockForAbstractClass(); $this->session = $this->createPartialMock(\Magento\Backend\Model\Session::class, ['setIsUrlNotice']); $this->actionFlag = $this->createPartialMock(\Magento\Framework\App\ActionFlag::class, ['get', 'set']); $this->helper = $this->createPartialMock(\Magento\Backend\Helper\Data::class, ['getUrl']); $this->resultRedirect = $this->createMock(\Magento\Backend\Model\View\Result\Redirect::class); $resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirect); $this->context->expects($this->once())->method('getMessageManager')->willReturn($this->messageManager); $this->context->expects($this->once())->method('getRequest')->willReturn($this->request); $this->context->expects($this->once())->method('getResponse')->willReturn($this->response); $this->context->expects($this->once())->method('getObjectManager')->willReturn($this->objectManager); $this->context->expects($this->once())->method('getSession')->willReturn($this->session); $this->context->expects($this->once())->method('getActionFlag')->willReturn($this->actionFlag); $this->context->expects($this->once())->method('getHelper')->willReturn($this->helper); $this->context->expects($this->once())->method('getResultRedirectFactory')->willReturn($resultRedirectFactory); $this->orderEmail = $objectManagerHelper->getObject( \Magento\Sales\Controller\Adminhtml\Order\Email::class, [ 'context' => $this->context, 'request' => $this->request, 'response' => $this->response, 'orderManagement' => $this->orderManagementMock, 'orderRepository' => $this->orderRepositoryMock, 'logger' => $this->loggerMock ] ); } /** * testEmail */ public function testEmail() { $orderId = 10000031; $this->request->expects($this->once()) ->method('getParam') ->with('order_id') ->will($this->returnValue($orderId)); $this->orderRepositoryMock->expects($this->once()) ->method('get') ->with($orderId) ->willReturn($this->orderMock); $this->orderMock->expects($this->atLeastOnce()) ->method('getEntityId') ->will($this->returnValue($orderId)); $this->orderManagementMock->expects($this->once()) ->method('notify') ->with($orderId) ->willReturn(true); $this->messageManager->expects($this->once()) ->method('addSuccessMessage') ->with('You sent the order email.'); $this->resultRedirect->expects($this->once()) ->method('setPath') ->with('sales/order/view', ['order_id' => $orderId]) ->willReturnSelf(); $this->assertInstanceOf( \Magento\Backend\Model\View\Result\Redirect::class, $this->orderEmail->execute() ); $this->assertEquals($this->response, $this->orderEmail->getResponse()); } /** * testEmailNoOrderId */ public function testEmailNoOrderId() { $this->request->expects($this->once()) ->method('getParam') ->with('order_id') ->will($this->returnValue(null)); $this->orderRepositoryMock->expects($this->once()) ->method('get') ->with(null) ->willThrowException( new \Magento\Framework\Exception\NoSuchEntityException( __("The entity that was requested doesn't exist. Verify the entity and try again.") ) ); $this->messageManager->expects($this->once()) ->method('addErrorMessage') ->with('This order no longer exists.'); $this->actionFlag->expects($this->once()) ->method('set') ->with('', 'no-dispatch', true) ->will($this->returnValue(true)); $this->resultRedirect->expects($this->once()) ->method('setPath') ->with('sales/*/') ->willReturnSelf(); $this->assertInstanceOf( \Magento\Backend\Model\View\Result\Redirect::class, $this->orderEmail->execute() ); } }