86 $this->requestMock = $this->getMockBuilder(\
Magento\Framework\
App\Request\Http::class)
87 ->disableOriginalConstructor()
89 $this->responseMock = $this->getMockBuilder(\
Magento\Framework\
App\
Response\Http::class)
90 ->disableOriginalConstructor()
93 $this->objectManagerMock = $this->createMock(\
Magento\Framework\ObjectManagerInterface::class);
95 $this->messageManagerMock = $this->getMockBuilder(\
Magento\Framework\Message\Manager::class)
96 ->disableOriginalConstructor()
99 $this->sessionMock = $this->getMockBuilder(\
Magento\Backend\Model\Session::class)
100 ->disableOriginalConstructor()
103 $this->actionFlagMock = $this->getMockBuilder(\
Magento\Framework\
App\ActionFlag::class)
104 ->disableOriginalConstructor()
107 $this->helperMock = $this->getMockBuilder(\
Magento\Backend\Helper\Data::class)
108 ->disableOriginalConstructor()
111 $this->resultRedirectFactoryMock = $this->getMockBuilder(
112 \
Magento\Backend\Model\View\Result\RedirectFactory::class
113 )->disableOriginalConstructor()
114 ->setMethods([
'create'])
117 $this->resultForwardFactoryMock = $this->getMockBuilder(
118 \
Magento\Backend\Model\View\Result\ForwardFactory::class
119 )->disableOriginalConstructor()
120 ->setMethods([
'create'])
123 $contextMock = $this->getMockBuilder(\
Magento\Backend\
App\
Action\Context::class)
124 ->disableOriginalConstructor()
127 $contextMock->expects($this->any())
128 ->method(
'getRequest')
129 ->willReturn($this->requestMock);
130 $contextMock->expects($this->any())
131 ->method(
'getResponse')
132 ->willReturn($this->responseMock);
133 $contextMock->expects($this->any())
134 ->method(
'getObjectManager')
135 ->willReturn($this->objectManagerMock);
136 $contextMock->expects($this->any())
137 ->method(
'getMessageManager')
138 ->willReturn($this->messageManagerMock);
139 $contextMock->expects($this->any())
140 ->method(
'getSession')
141 ->willReturn($this->sessionMock);
142 $contextMock->expects($this->any())
143 ->method(
'getActionFlag')
144 ->willReturn($this->actionFlagMock);
145 $contextMock->expects($this->any())
146 ->method(
'getHelper')
147 ->willReturn($this->helperMock);
148 $contextMock->expects($this->any())
149 ->method(
'getResultRedirectFactory')
150 ->willReturn($this->resultRedirectFactoryMock);
152 $this->invoiceManagement = $this->getMockBuilder(\
Magento\Sales\Api\InvoiceManagementInterface::class)
153 ->disableOriginalConstructor()
155 $this->objectManagerMock->expects($this->any())
157 ->with(\
Magento\Sales\Api\InvoiceManagementInterface::class)
158 ->willReturn($this->invoiceManagement);
159 $this->invoiceRepository = $this->getMockBuilder(InvoiceRepositoryInterface::class)
160 ->disableOriginalConstructor()
161 ->getMockForAbstractClass();
164 \
Magento\Sales\Controller\Adminhtml\Order\Invoice\Capture::class,
166 'context' => $contextMock,
167 'resultForwardFactory' => $this->resultForwardFactoryMock,
174 $this->invoiceRepository
185 $this->requestMock->expects($this->once())
188 ->will($this->returnValue($invoiceId));
190 $orderMock = $this->getMockBuilder(\
Magento\Sales\Model\Order::class)
191 ->disableOriginalConstructor()
192 ->setMethods([
'setIsInProcess',
'__wakeup'])
195 $this->invoiceManagement->expects($this->once())
196 ->method(
'setCapture')
199 $invoiceMock = $this->getMockBuilder(\
Magento\Sales\Model\Order\Invoice::class)
200 ->disableOriginalConstructor()
203 $invoiceMock->expects($this->any())
204 ->method(
'getEntityId')
205 ->will($this->returnValue($invoiceId));
206 $invoiceMock->expects($this->any())
208 ->will($this->returnValue($orderMock));
210 $transactionMock = $this->getMockBuilder(\
Magento\Framework\DB\Transaction::class)
211 ->disableOriginalConstructor()
214 $transactionMock->expects($this->at(0))
215 ->method(
'addObject')
217 ->will($this->returnSelf());
218 $transactionMock->expects($this->at(1))
219 ->method(
'addObject')
221 ->will($this->returnSelf());
222 $transactionMock->expects($this->at(2))
225 $this->messageManagerMock->expects($this->once())
226 ->method(
'addSuccessMessage')
227 ->with(
'The invoice has been captured.');
229 $invoiceMock->expects($this->once())
231 ->will($this->returnValue($invoiceId));
233 $this->invoiceRepository->expects($this->once())
235 ->willReturn($invoiceMock);
237 $this->objectManagerMock->expects($this->at(1))
239 ->with(\
Magento\Framework\DB\Transaction::class)
240 ->will($this->returnValue($transactionMock));
242 $resultRedirect = $this->getMockBuilder(\
Magento\Backend\Model\View\Result\Redirect::class)
243 ->disableOriginalConstructor()
245 $resultRedirect->expects($this->once())->method(
'setPath')->with(
'sales/*/view', [
'invoice_id' => $invoiceId]);
247 $this->resultRedirectFactoryMock->expects($this->once())
249 ->will($this->returnValue($resultRedirect));
251 $this->assertSame($resultRedirect, $this->controller->execute());
261 $this->requestMock->expects($this->once())
264 ->will($this->returnValue($invoiceId));
266 $this->invoiceRepository->expects($this->once())
270 $resultForward = $this->getMockBuilder(\
Magento\Backend\Model\View\Result\Forward::class)
271 ->disableOriginalConstructor()
273 $resultForward->expects($this->once())->method(
'forward')->with((
'noroute'))->will($this->returnSelf());
275 $this->resultForwardFactoryMock->expects($this->once())
277 ->will($this->returnValue($resultForward));
279 $this->assertSame($resultForward, $this->controller->execute());
289 $message =
'Invoice capturing error';
290 $e = new \Magento\Framework\Exception\LocalizedException(
__(
$message));
292 $this->invoiceManagement->expects($this->once())
293 ->method(
'setCapture')
295 ->will($this->throwException($e));
297 $this->requestMock->expects($this->once())
300 ->will($this->returnValue($invoiceId));
302 $invoiceMock = $this->getMockBuilder(\
Magento\Sales\Model\Order\Invoice::class)
303 ->disableOriginalConstructor()
306 $this->messageManagerMock->expects($this->once())
307 ->method(
'addErrorMessage')
310 $invoiceMock->expects($this->once())
312 ->will($this->returnValue($invoiceId));
313 $invoiceMock->expects($this->once())
314 ->method(
'getEntityId')
315 ->will($this->returnValue($invoiceId));
317 $this->invoiceRepository->expects($this->once())
319 ->willReturn($invoiceMock);
321 $resultRedirect = $this->getMockBuilder(\
Magento\Backend\Model\View\Result\Redirect::class)
322 ->disableOriginalConstructor()
324 $resultRedirect->expects($this->once())->method(
'setPath')->with(
'sales/*/view', [
'invoice_id' => $invoiceId]);
326 $this->resultRedirectFactoryMock->expects($this->once())
328 ->will($this->returnValue($resultRedirect));
330 $this->assertSame($resultRedirect, $this->controller->execute());
340 $message =
'Invoice capturing error';
343 $this->requestMock->expects($this->once())
346 ->will($this->returnValue($invoiceId));
348 $this->invoiceManagement->expects($this->once())
349 ->method(
'setCapture')
351 ->will($this->throwException($e));
353 $invoiceMock = $this->getMockBuilder(\
Magento\Sales\Model\Order\Invoice::class)
354 ->disableOriginalConstructor()
357 $this->messageManagerMock->expects($this->once())
358 ->method(
'addErrorMessage')
361 $invoiceMock->expects($this->once())
363 ->will($this->returnValue($invoiceId));
364 $invoiceMock->expects($this->once())
365 ->method(
'getEntityId')
366 ->will($this->returnValue($invoiceId));
368 $this->invoiceRepository->expects($this->once())
370 ->willReturn($invoiceMock);
372 $resultRedirect = $this->getMockBuilder(\
Magento\Backend\Model\View\Result\Redirect::class)
373 ->disableOriginalConstructor()
376 $resultRedirect->expects($this->once())->method(
'setPath')->with(
'sales/*/view', [
'invoice_id' => $invoiceId]);
378 $this->resultRedirectFactoryMock->expects($this->once())
380 ->will($this->returnValue($resultRedirect));
382 $this->assertSame($resultRedirect, $this->controller->execute());
$resultForwardFactoryMock
$resultRedirectFactoryMock
testExecuteModelException()