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 
10 
16 class VoidActionTest extends \PHPUnit\Framework\TestCase
17 {
21  protected $requestMock;
22 
26  protected $responseMock;
27 
31  protected $titleMock;
32 
36  protected $objectManagerMock;
37 
42 
46  protected $actionFlagMock;
47 
51  protected $sessionMock;
52 
56  protected $helperMock;
57 
61  protected $controller;
62 
67 
72 
76  protected $invoiceManagement;
77 
81  protected $invoiceRepository;
82 
87  protected function setUp()
88  {
89  $objectManager = new ObjectManager($this);
90 
91  $this->titleMock = $this->getMockBuilder(\Magento\Framework\App\Action\Title::class)
92  ->disableOriginalConstructor()
93  ->setMethods([])
94  ->getMock();
95 
96  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
97  ->disableOriginalConstructor()
98  ->setMethods([])
99  ->getMock();
100  $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
101  ->disableOriginalConstructor()
102  ->setMethods([])
103  ->getMock();
104 
105  $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
106 
107  $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\Manager::class)
108  ->disableOriginalConstructor()
109  ->setMethods([])
110  ->getMock();
111 
112  $this->actionFlagMock = $this->getMockBuilder(\Magento\Framework\App\ActionFlag::class)
113  ->disableOriginalConstructor()
114  ->setMethods([])
115  ->getMock();
116 
117  $this->helperMock = $this->getMockBuilder(\Magento\Backend\Helper\Data::class)
118  ->disableOriginalConstructor()
119  ->setMethods([])
120  ->getMock();
121 
122  $this->sessionMock = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
123  ->disableOriginalConstructor()
124  ->setMethods([])
125  ->getMock();
126 
127  $this->resultRedirectFactoryMock = $this->getMockBuilder(
128  \Magento\Backend\Model\View\Result\RedirectFactory::class
129  )->disableOriginalConstructor()
130  ->setMethods(['create'])
131  ->getMock();
132 
133  $this->resultForwardFactoryMock = $this->getMockBuilder(
134  \Magento\Backend\Model\View\Result\ForwardFactory::class
135  )->disableOriginalConstructor()
136  ->setMethods(['create'])
137  ->getMock();
138 
139  $this->invoiceManagement = $this->getMockBuilder(\Magento\Sales\Api\InvoiceManagementInterface::class)
140  ->disableOriginalConstructor()
141  ->getMock();
142  $this->objectManagerMock->expects($this->any())
143  ->method('get')
144  ->with(\Magento\Sales\Api\InvoiceManagementInterface::class)
145  ->willReturn($this->invoiceManagement);
146 
147  $contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
148  ->disableOriginalConstructor()
149  ->setMethods(
150  [
151  'getRequest',
152  'getResponse',
153  'getObjectManager',
154  'getTitle',
155  'getSession',
156  'getHelper',
157  'getActionFlag',
158  'getMessageManager',
159  'getResultRedirectFactory'
160  ]
161  )
162  ->getMock();
163  $contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
164  $contextMock->expects($this->any())->method('getResponse')->willReturn($this->responseMock);
165  $contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock);
166  $contextMock->expects($this->any())->method('getMessageManager')->willReturn($this->messageManagerMock);
167  $contextMock->expects($this->any())->method('getTitle')->willReturn($this->titleMock);
168  $contextMock->expects($this->any())->method('getActionFlag')->willReturn($this->actionFlagMock);
169  $contextMock->expects($this->any())->method('getSession')->willReturn($this->sessionMock);
170  $contextMock->expects($this->any())->method('getHelper')->willReturn($this->helperMock);
171  $contextMock->expects($this->any())
172  ->method('getResultRedirectFactory')
173  ->willReturn($this->resultRedirectFactoryMock);
174 
175  $this->invoiceRepository = $this->getMockBuilder(InvoiceRepositoryInterface::class)
176  ->disableOriginalConstructor()
177  ->getMockForAbstractClass();
178 
179  $this->controller = $objectManager->getObject(
180  \Magento\Sales\Controller\Adminhtml\Order\Invoice\VoidAction::class,
181  [
182  'context' => $contextMock,
183  'resultForwardFactory' => $this->resultForwardFactoryMock
184  ]
185  );
186 
187  $objectManager->setBackwardCompatibleProperty(
188  $this->controller,
189  'invoiceRepository',
190  $this->invoiceRepository
191  );
192  }
193 
197  public function testExecute()
198  {
199  $invoiceId = 2;
200 
201  $this->requestMock->expects($this->once())
202  ->method('getParam')
203  ->with('invoice_id')
204  ->will($this->returnValue($invoiceId));
205 
206  $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
207  ->disableOriginalConstructor()
208  ->setMethods(['setIsInProcess', '__wakeup'])
209  ->getMock();
210 
211  $this->invoiceManagement->expects($this->once())
212  ->method('setVoid')
213  ->with($invoiceId)
214  ->willReturn(true);
215 
216  $invoiceMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
217  ->disableOriginalConstructor()
218  ->getMock();
219  $invoiceMock->expects($this->any())
220  ->method('getEntityId')
221  ->will($this->returnValue($invoiceId));
222  $invoiceMock->expects($this->any())
223  ->method('getOrder')
224  ->will($this->returnValue($orderMock));
225  $invoiceMock->expects($this->once())
226  ->method('getId')
227  ->will($this->returnValue($invoiceId));
228 
229  $transactionMock = $this->getMockBuilder(\Magento\Framework\DB\Transaction::class)
230  ->disableOriginalConstructor()
231  ->getMock();
232  $transactionMock->expects($this->at(0))
233  ->method('addObject')
234  ->with($invoiceMock)
235  ->will($this->returnSelf());
236  $transactionMock->expects($this->at(1))
237  ->method('addObject')
238  ->with($orderMock)
239  ->will($this->returnSelf());
240  $transactionMock->expects($this->at(2))
241  ->method('save');
242 
243  $this->invoiceRepository->expects($this->once())
244  ->method('get')
245  ->willReturn($invoiceMock);
246 
247  $this->objectManagerMock->expects($this->at(1))
248  ->method('create')
249  ->with(\Magento\Framework\DB\Transaction::class)
250  ->will($this->returnValue($transactionMock));
251 
252  $this->messageManagerMock->expects($this->once())
253  ->method('addSuccessMessage')
254  ->with('The invoice has been voided.');
255 
256  $resultRedirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
257  ->disableOriginalConstructor()
258  ->setMethods([])
259  ->getMock();
260  $resultRedirect->expects($this->once())->method('setPath')->with('sales/*/view', ['invoice_id' => $invoiceId]);
261 
262  $this->resultRedirectFactoryMock->expects($this->once())
263  ->method('create')
264  ->will($this->returnValue($resultRedirect));
265 
266  $this->assertSame($resultRedirect, $this->controller->execute());
267  }
268 
272  public function testExecuteNoInvoice()
273  {
274  $invoiceId = 2;
275 
276  $this->requestMock->expects($this->once())
277  ->method('getParam')
278  ->with('invoice_id')
279  ->will($this->returnValue($invoiceId));
280 
281  $this->invoiceRepository->expects($this->once())
282  ->method('get')
283  ->willReturn(null);
284 
285  $this->messageManagerMock->expects($this->never())
286  ->method('addErrorMessage');
287  $this->messageManagerMock->expects($this->never())
288  ->method('addSuccessMessage');
289 
290  $resultForward = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Forward::class)
291  ->disableOriginalConstructor()
292  ->setMethods([])
293  ->getMock();
294  $resultForward->expects($this->once())->method('forward')->with(('noroute'))->will($this->returnSelf());
295 
296  $this->resultForwardFactoryMock->expects($this->once())
297  ->method('create')
298  ->will($this->returnValue($resultForward));
299 
300  $this->assertSame($resultForward, $this->controller->execute());
301  }
302 
306  public function testExecuteModelException()
307  {
308  $invoiceId = 2;
309  $message = 'test message';
310  $e = new \Magento\Framework\Exception\LocalizedException(__($message));
311 
312  $this->requestMock->expects($this->once())
313  ->method('getParam')
314  ->with('invoice_id')
315  ->will($this->returnValue($invoiceId));
316 
317  $this->invoiceManagement->expects($this->once())
318  ->method('setVoid')
319  ->with($invoiceId)
320  ->will($this->throwException($e));
321 
322  $invoiceMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
323  ->disableOriginalConstructor()
324  ->getMock();
325  $invoiceMock->expects($this->once())
326  ->method('getEntityId')
327  ->will($this->returnValue($invoiceId));
328  $invoiceMock->expects($this->once())
329  ->method('getId')
330  ->will($this->returnValue($invoiceId));
331 
332  $this->invoiceRepository->expects($this->once())
333  ->method('get')
334  ->willReturn($invoiceMock);
335 
336  $this->messageManagerMock->expects($this->once())
337  ->method('addErrorMessage');
338 
339  $resultRedirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
340  ->disableOriginalConstructor()
341  ->setMethods([])
342  ->getMock();
343  $resultRedirect->expects($this->once())->method('setPath')->with('sales/*/view', ['invoice_id' => $invoiceId]);
344 
345  $this->resultRedirectFactoryMock->expects($this->once())
346  ->method('create')
347  ->will($this->returnValue($resultRedirect));
348 
349  $this->assertSame($resultRedirect, $this->controller->execute());
350  }
351 }
$objectManager
Definition: bootstrap.php:17
__()
Definition: __.php:13
$message