Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CaptureTest.php
Go to the documentation of this file.
1 <?php
7 
11 
17 class CaptureTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $objectManagerMock;
23 
27  protected $requestMock;
28 
32  protected $responseMock;
33 
38 
42  protected $sessionMock;
43 
47  protected $actionFlagMock;
48 
52  protected $helperMock;
53 
58 
63 
67  protected $controller;
68 
72  protected $invoiceManagement;
73 
77  protected $invoiceRepository;
78 
82  protected function setUp()
83  {
84  $objectManager = new ObjectManager($this);
85 
86  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
87  ->disableOriginalConstructor()
88  ->getMock();
89  $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92 
93  $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
94 
95  $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\Manager::class)
96  ->disableOriginalConstructor()
97  ->getMock();
98 
99  $this->sessionMock = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
100  ->disableOriginalConstructor()
101  ->getMock();
102 
103  $this->actionFlagMock = $this->getMockBuilder(\Magento\Framework\App\ActionFlag::class)
104  ->disableOriginalConstructor()
105  ->getMock();
106 
107  $this->helperMock = $this->getMockBuilder(\Magento\Backend\Helper\Data::class)
108  ->disableOriginalConstructor()
109  ->getMock();
110 
111  $this->resultRedirectFactoryMock = $this->getMockBuilder(
112  \Magento\Backend\Model\View\Result\RedirectFactory::class
113  )->disableOriginalConstructor()
114  ->setMethods(['create'])
115  ->getMock();
116 
117  $this->resultForwardFactoryMock = $this->getMockBuilder(
118  \Magento\Backend\Model\View\Result\ForwardFactory::class
119  )->disableOriginalConstructor()
120  ->setMethods(['create'])
121  ->getMock();
122 
123  $contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
124  ->disableOriginalConstructor()
125  ->setMethods([])
126  ->getMock();
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);
151 
152  $this->invoiceManagement = $this->getMockBuilder(\Magento\Sales\Api\InvoiceManagementInterface::class)
153  ->disableOriginalConstructor()
154  ->getMock();
155  $this->objectManagerMock->expects($this->any())
156  ->method('get')
157  ->with(\Magento\Sales\Api\InvoiceManagementInterface::class)
158  ->willReturn($this->invoiceManagement);
159  $this->invoiceRepository = $this->getMockBuilder(InvoiceRepositoryInterface::class)
160  ->disableOriginalConstructor()
161  ->getMockForAbstractClass();
162 
163  $this->controller = $objectManager->getObject(
164  \Magento\Sales\Controller\Adminhtml\Order\Invoice\Capture::class,
165  [
166  'context' => $contextMock,
167  'resultForwardFactory' => $this->resultForwardFactoryMock,
168  ]
169  );
170 
171  $objectManager->setBackwardCompatibleProperty(
172  $this->controller,
173  'invoiceRepository',
174  $this->invoiceRepository
175  );
176  }
177 
181  public function testExecute()
182  {
183  $invoiceId = 2;
184 
185  $this->requestMock->expects($this->once())
186  ->method('getParam')
187  ->with('invoice_id')
188  ->will($this->returnValue($invoiceId));
189 
190  $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
191  ->disableOriginalConstructor()
192  ->setMethods(['setIsInProcess', '__wakeup'])
193  ->getMock();
194 
195  $this->invoiceManagement->expects($this->once())
196  ->method('setCapture')
197  ->with($invoiceId);
198 
199  $invoiceMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
200  ->disableOriginalConstructor()
201  ->setMethods([])
202  ->getMock();
203  $invoiceMock->expects($this->any())
204  ->method('getEntityId')
205  ->will($this->returnValue($invoiceId));
206  $invoiceMock->expects($this->any())
207  ->method('getOrder')
208  ->will($this->returnValue($orderMock));
209 
210  $transactionMock = $this->getMockBuilder(\Magento\Framework\DB\Transaction::class)
211  ->disableOriginalConstructor()
212  ->setMethods([])
213  ->getMock();
214  $transactionMock->expects($this->at(0))
215  ->method('addObject')
216  ->with($invoiceMock)
217  ->will($this->returnSelf());
218  $transactionMock->expects($this->at(1))
219  ->method('addObject')
220  ->with($orderMock)
221  ->will($this->returnSelf());
222  $transactionMock->expects($this->at(2))
223  ->method('save');
224 
225  $this->messageManagerMock->expects($this->once())
226  ->method('addSuccessMessage')
227  ->with('The invoice has been captured.');
228 
229  $invoiceMock->expects($this->once())
230  ->method('getId')
231  ->will($this->returnValue($invoiceId));
232 
233  $this->invoiceRepository->expects($this->once())
234  ->method('get')
235  ->willReturn($invoiceMock);
236 
237  $this->objectManagerMock->expects($this->at(1))
238  ->method('create')
239  ->with(\Magento\Framework\DB\Transaction::class)
240  ->will($this->returnValue($transactionMock));
241 
242  $resultRedirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
243  ->disableOriginalConstructor()
244  ->getMock();
245  $resultRedirect->expects($this->once())->method('setPath')->with('sales/*/view', ['invoice_id' => $invoiceId]);
246 
247  $this->resultRedirectFactoryMock->expects($this->once())
248  ->method('create')
249  ->will($this->returnValue($resultRedirect));
250 
251  $this->assertSame($resultRedirect, $this->controller->execute());
252  }
253 
257  public function testExecuteNoInvoice()
258  {
259  $invoiceId = 2;
260 
261  $this->requestMock->expects($this->once())
262  ->method('getParam')
263  ->with('invoice_id')
264  ->will($this->returnValue($invoiceId));
265 
266  $this->invoiceRepository->expects($this->once())
267  ->method('get')
268  ->willReturn(null);
269 
270  $resultForward = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Forward::class)
271  ->disableOriginalConstructor()
272  ->getMock();
273  $resultForward->expects($this->once())->method('forward')->with(('noroute'))->will($this->returnSelf());
274 
275  $this->resultForwardFactoryMock->expects($this->once())
276  ->method('create')
277  ->will($this->returnValue($resultForward));
278 
279  $this->assertSame($resultForward, $this->controller->execute());
280  }
281 
285  public function testExecuteModelException()
286  {
287  $invoiceId = 2;
288 
289  $message = 'Invoice capturing error';
290  $e = new \Magento\Framework\Exception\LocalizedException(__($message));
291 
292  $this->invoiceManagement->expects($this->once())
293  ->method('setCapture')
294  ->with($invoiceId)
295  ->will($this->throwException($e));
296 
297  $this->requestMock->expects($this->once())
298  ->method('getParam')
299  ->with('invoice_id')
300  ->will($this->returnValue($invoiceId));
301 
302  $invoiceMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
303  ->disableOriginalConstructor()
304  ->getMock();
305 
306  $this->messageManagerMock->expects($this->once())
307  ->method('addErrorMessage')
308  ->with($message);
309 
310  $invoiceMock->expects($this->once())
311  ->method('getId')
312  ->will($this->returnValue($invoiceId));
313  $invoiceMock->expects($this->once())
314  ->method('getEntityId')
315  ->will($this->returnValue($invoiceId));
316 
317  $this->invoiceRepository->expects($this->once())
318  ->method('get')
319  ->willReturn($invoiceMock);
320 
321  $resultRedirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
322  ->disableOriginalConstructor()
323  ->getMock();
324  $resultRedirect->expects($this->once())->method('setPath')->with('sales/*/view', ['invoice_id' => $invoiceId]);
325 
326  $this->resultRedirectFactoryMock->expects($this->once())
327  ->method('create')
328  ->will($this->returnValue($resultRedirect));
329 
330  $this->assertSame($resultRedirect, $this->controller->execute());
331  }
332 
336  public function testExecuteException()
337  {
338  $invoiceId = 2;
339 
340  $message = 'Invoice capturing error';
341  $e = new \Exception($message);
342 
343  $this->requestMock->expects($this->once())
344  ->method('getParam')
345  ->with('invoice_id')
346  ->will($this->returnValue($invoiceId));
347 
348  $this->invoiceManagement->expects($this->once())
349  ->method('setCapture')
350  ->with($invoiceId)
351  ->will($this->throwException($e));
352 
353  $invoiceMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
354  ->disableOriginalConstructor()
355  ->getMock();
356 
357  $this->messageManagerMock->expects($this->once())
358  ->method('addErrorMessage')
359  ->with($message);
360 
361  $invoiceMock->expects($this->once())
362  ->method('getId')
363  ->will($this->returnValue($invoiceId));
364  $invoiceMock->expects($this->once())
365  ->method('getEntityId')
366  ->will($this->returnValue($invoiceId));
367 
368  $this->invoiceRepository->expects($this->once())
369  ->method('get')
370  ->willReturn($invoiceMock);
371 
372  $resultRedirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
373  ->disableOriginalConstructor()
374  ->setMethods([])
375  ->getMock();
376  $resultRedirect->expects($this->once())->method('setPath')->with('sales/*/view', ['invoice_id' => $invoiceId]);
377 
378  $this->resultRedirectFactoryMock->expects($this->once())
379  ->method('create')
380  ->will($this->returnValue($resultRedirect));
381 
382  $this->assertSame($resultRedirect, $this->controller->execute());
383  }
384 }
$objectManager
Definition: bootstrap.php:17
__()
Definition: __.php:13
$message