messageManager = $this->createPartialMock( \Magento\Framework\Message\Manager::class, ['addSuccessMessage', 'addErrorMessage'] ); $this->orderCollectionMock = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class); $this->filterMock = $this->createMock(\Magento\Ui\Component\MassAction\Filter::class); $this->orderCollectionFactoryMock = $this->createPartialMock( \Magento\Sales\Model\ResourceModel\Order\CollectionFactory::class, ['create'] ); $this->orderCollectionFactoryMock ->expects($this->once()) ->method('create') ->willReturn($this->orderCollectionMock); $this->resultRedirect = $this->createMock(\Magento\Backend\Model\View\Result\Redirect::class); $resultRedirectFactory = $this->createMock(\Magento\Framework\Controller\ResultFactory::class); $resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirect); $this->controller = $objectManagerHelper->getObject( \Magento\Sales\Controller\Adminhtml\Order\Pdfinvoices::class, [ 'filter' => $this->filterMock, 'resultFactory' => $resultRedirectFactory, 'messageManager' => $this->messageManager ] ); $objectManagerHelper ->setBackwardCompatibleProperty( $this->controller, 'orderCollectionFactory', $this->orderCollectionFactoryMock ); } /** * @throws \Magento\Framework\Exception\LocalizedException */ public function testExecute() { $exception = new \Exception(); $this->filterMock ->expects($this->once()) ->method('getCollection') ->with($this->orderCollectionMock) ->willThrowException($exception); $this->messageManager->expects($this->once())->method('addErrorMessage'); $this->resultRedirect->expects($this->once())->method('setPath')->willReturnSelf(); $this->controller->execute($exception); } }