Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
VoidActionTest.php
Go to the documentation of this file.
1 <?php
7 
14 class VoidActionTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $controller;
20 
24  protected $contextMock;
25 
29  protected $loaderMock;
30 
34  protected $senderMock;
35 
39  protected $requestMock;
40 
44  protected $responseMock;
45 
49  protected $objectManagerMock;
50 
54  protected $sessionMock;
55 
59  protected $actionFlagMock;
60 
64  protected $creditmemoMock;
65 
70 
74  protected $helperMock;
75 
80 
85 
90 
94  protected $resultForwardMock;
95 
99  protected function setUp()
100  {
101  $this->creditmemoMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Creditmemo::class)
102  ->disableOriginalConstructor()
103  ->setMethods(['getInvoice', 'getOrder', 'cancel', 'getId', 'void', '__wakeup'])
104  ->getMock();
105  $titleMock = $this->getMockBuilder(\Magento\Framework\App\Action\Title::class)
106  ->disableOriginalConstructor()
107  ->getMock();
108  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
109  ->disableOriginalConstructor()
110  ->getMock();
111  $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
112  ->disableOriginalConstructor()
113  ->getMock();
114  $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
115  $this->helperMock = $this->getMockBuilder(\Magento\Backend\Helper\Data::class)
116  ->disableOriginalConstructor()
117  ->getMock();
118  $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\Manager::class)
119  ->disableOriginalConstructor()
120  ->getMock();
121  $this->sessionMock = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
122  ->disableOriginalConstructor()
123  ->getMock();
124  $this->actionFlagMock = $this->getMockBuilder(\Magento\Framework\App\ActionFlag::class)
125  ->disableOriginalConstructor()
126  ->getMock();
127  $this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
128  ->setMethods(
129  [
130  'getRequest',
131  'getResponse',
132  'getObjectManager',
133  'getTitle',
134  'getSession',
135  'getHelper',
136  'getActionFlag',
137  'getMessageManager',
138  'getResultRedirectFactory'
139  ]
140  )
141  ->disableOriginalConstructor()
142  ->getMock();
143  $this->loaderMock = $this->getMockBuilder(\Magento\Sales\Controller\Adminhtml\Order\CreditmemoLoader::class)
144  ->disableOriginalConstructor()
145  ->getMock();
146  $this->senderMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Email\Sender\CreditmemoSender::class)
147  ->disableOriginalConstructor()
148  ->getMock();
149  $this->resultRedirectFactoryMock = $this->getMockBuilder(
150  \Magento\Backend\Model\View\Result\RedirectFactory::class
151  )->disableOriginalConstructor()
152  ->setMethods(['create'])
153  ->getMock();
154  $this->resultForwardFactoryMock = $this->getMockBuilder(
155  \Magento\Backend\Model\View\Result\ForwardFactory::class
156  )->disableOriginalConstructor()
157  ->setMethods(['create'])
158  ->getMock();
159  $this->resultRedirectMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
160  ->disableOriginalConstructor()
161  ->getMock();
162  $this->resultForwardMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Forward::class)
163  ->disableOriginalConstructor()
164  ->getMock();
165 
166  $this->contextMock->expects($this->any())
167  ->method('getRequest')
168  ->willReturn($this->requestMock);
169  $this->contextMock->expects($this->any())
170  ->method('getResponse')
171  ->willReturn($this->responseMock);
172  $this->contextMock->expects($this->any())
173  ->method('getActionFlag')
174  ->willReturn($this->actionFlagMock);
175  $this->contextMock->expects($this->any())
176  ->method('getHelper')
177  ->willReturn($this->helperMock);
178  $this->contextMock->expects($this->any())
179  ->method('getSession')
180  ->willReturn($this->sessionMock);
181  $this->contextMock->expects($this->any())
182  ->method('getObjectManager')
183  ->willReturn($this->objectManagerMock);
184  $this->contextMock->expects($this->any())
185  ->method('getTitle')
186  ->willReturn($titleMock);
187  $this->contextMock->expects($this->any())
188  ->method('getMessageManager')
189  ->willReturn($this->messageManagerMock);
190  $this->contextMock->expects($this->any())
191  ->method('getResultRedirectFactory')
192  ->willReturn($this->resultRedirectFactoryMock);
193 
194  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
195  $this->controller = $objectManager->getObject(
196  \Magento\Sales\Controller\Adminhtml\Order\Creditmemo\VoidAction::class,
197  [
198  'context' => $this->contextMock,
199  'creditmemoLoader' => $this->loaderMock,
200  'resultForwardFactory' => $this->resultForwardFactoryMock
201  ]
202  );
203  }
204 
208  public function testExecuteNoCreditmemo()
209  {
210  $this->requestMock->expects($this->any())
211  ->method('getParam')
212  ->withAnyParameters()
213  ->willReturnArgument(0);
214  $this->loaderMock->expects($this->once())
215  ->method('load')
216  ->willReturn(false);
217  $this->resultForwardFactoryMock->expects($this->once())
218  ->method('create')
219  ->willReturn($this->resultForwardMock);
220  $this->resultForwardMock->expects($this->once())
221  ->method('forward')
222  ->with('noroute')
223  ->willReturnSelf();
224 
225  $this->assertInstanceOf(
226  \Magento\Backend\Model\View\Result\Forward::class,
227  $this->controller->execute()
228  );
229  }
230 
234  public function testExecuteModelException()
235  {
236  $id = 123;
237  $message = 'Model exception';
238  $e = new \Magento\Framework\Exception\LocalizedException(__($message));
239 
240  $this->requestMock->expects($this->any())
241  ->method('getParam')
242  ->withAnyParameters()
243  ->willReturnArgument(0);
244  $this->creditmemoMock->expects($this->once())
245  ->method('void')
246  ->willThrowException($e);
247  $this->loaderMock->expects($this->once())
248  ->method('load')
249  ->willReturn($this->creditmemoMock);
250  $this->resultRedirectFactoryMock->expects($this->once())
251  ->method('create')
252  ->willReturn($this->resultRedirectMock);
253  $this->creditmemoMock->expects($this->atLeastOnce())
254  ->method('getId')
255  ->willReturn($id);
256  $this->resultRedirectMock->expects($this->once())
257  ->method('setPath')
258  ->with('sales/*/view', ['creditmemo_id' => $id])
259  ->willReturnSelf();
260 
261  $this->assertInstanceOf(
262  \Magento\Backend\Model\View\Result\Redirect::class,
263  $this->controller->execute()
264  );
265  }
266 
270  public function testExecuteException()
271  {
272  $id = 321;
273  $message = 'Model exception';
274  $e = new \Exception($message);
275 
276  $this->requestMock->expects($this->any())
277  ->method('getParam')
278  ->withAnyParameters()
279  ->willReturnArgument(0);
280  $this->creditmemoMock->expects($this->once())
281  ->method('void')
282  ->willThrowException($e);
283  $this->loaderMock->expects($this->once())
284  ->method('load')
285  ->willReturn($this->creditmemoMock);
286  $this->resultRedirectFactoryMock->expects($this->once())
287  ->method('create')
288  ->willReturn($this->resultRedirectMock);
289  $this->creditmemoMock->expects($this->atLeastOnce())
290  ->method('getId')
291  ->willReturn($id);
292  $this->resultRedirectMock->expects($this->once())
293  ->method('setPath')
294  ->with('sales/*/view', ['creditmemo_id' => $id])
295  ->willReturnSelf();
296 
297  $this->assertInstanceOf(
298  \Magento\Backend\Model\View\Result\Redirect::class,
299  $this->controller->execute()
300  );
301  }
302 
306  public function testExecute()
307  {
308  $id = '111';
309 
310  $transactionMock = $this->getMockBuilder(\Magento\Framework\DB\Transaction::class)
311  ->disableOriginalConstructor()
312  ->getMock();
313  $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
314  ->disableOriginalConstructor()
315  ->getMock();
316  $invoiceMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
317  ->disableOriginalConstructor()
318  ->getMock();
319 
320  $this->requestMock->expects($this->any())
321  ->method('getParam')
322  ->withAnyParameters()
323  ->willReturnArgument(0);
324  $this->loaderMock->expects($this->once())
325  ->method('load')
326  ->willReturn($this->creditmemoMock);
327  $this->objectManagerMock->expects($this->once())
328  ->method('create')
329  ->with(\Magento\Framework\DB\Transaction::class)
330  ->willReturn($transactionMock);
331  $this->creditmemoMock->expects($this->any())
332  ->method('getOrder')
333  ->willReturn($orderMock);
334  $this->creditmemoMock->expects($this->any())
335  ->method('getInvoice')
336  ->willReturn($invoiceMock);
337  $this->messageManagerMock->expects($this->once())
338  ->method('addSuccessMessage')
339  ->with('You voided the credit memo.');
340  $this->resultRedirectFactoryMock->expects($this->once())
341  ->method('create')
342  ->willReturn($this->resultRedirectMock);
343  $this->creditmemoMock->expects($this->atLeastOnce())
344  ->method('getId')
345  ->willReturn($id);
346  $this->resultRedirectMock->expects($this->once())
347  ->method('setPath')
348  ->with('sales/*/view', ['creditmemo_id' => $id])
349  ->willReturnSelf();
350 
351  $this->assertInstanceOf(
352  \Magento\Backend\Model\View\Result\Redirect::class,
353  $this->controller->execute()
354  );
355  }
356 }
$objectManager
Definition: bootstrap.php:17
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$message