Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateQtyTest.php
Go to the documentation of this file.
1 <?php
7 
9 
16 class UpdateQtyTest extends \PHPUnit\Framework\TestCase
17 {
21  protected $viewMock;
22 
26  protected $resultPageMock;
27 
31  protected $pageConfigMock;
32 
36  protected $objectManagerMock;
37 
41  protected $requestMock;
42 
46  protected $responseMock;
47 
51  protected $titleMock;
52 
56  protected $controller;
57 
62 
67 
72 
77 
83  protected function setUp()
84  {
85  $objectManager = new ObjectManager($this);
86 
87  $this->titleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
88  ->disableOriginalConstructor()
89  ->getMock();
90  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
91  ->disableOriginalConstructor()
92  ->setMethods([])
93  ->getMock();
94  $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
95  ->disableOriginalConstructor()
96  ->getMock();
97  $this->resultPageMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Page::class)
98  ->setMethods([])
99  ->disableOriginalConstructor()
100  ->getMock();
101  $this->pageConfigMock = $this->getMockBuilder(\Magento\Framework\View\Page\Config::class)
102  ->disableOriginalConstructor()
103  ->getMock();
104 
105  $this->viewMock = $this->getMockBuilder(\Magento\Framework\App\ViewInterface::class)
106  ->disableOriginalConstructor()
107  ->getMock();
108 
109  $this->viewMock->expects($this->any())->method('loadLayout')->will($this->returnSelf());
110 
111  $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
112 
113  $this->pageConfigMock->expects($this->any())->method('getTitle')->will($this->returnValue($this->titleMock));
114 
115  $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
116  ->disableOriginalConstructor()
117  ->getMock();
118 
119  $contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
120  ->disableOriginalConstructor()
121  ->setMethods(
122  [
123  'getRequest',
124  'getResponse',
125  'getObjectManager',
126  'getTitle',
127  'getSession',
128  'getHelper',
129  'getActionFlag',
130  'getMessageManager',
131  'getResultRedirectFactory',
132  'getView'
133  ]
134  )
135  ->getMock();
136  $contextMock->expects($this->any())
137  ->method('getRequest')
138  ->will($this->returnValue($this->requestMock));
139  $contextMock->expects($this->any())
140  ->method('getResponse')
141  ->will($this->returnValue($this->responseMock));
142  $contextMock->expects($this->any())
143  ->method('getTitle')
144  ->will($this->returnValue($this->titleMock));
145  $contextMock->expects($this->any())
146  ->method('getView')
147  ->will($this->returnValue($this->viewMock));
148  $contextMock->expects($this->any())
149  ->method('getObjectManager')
150  ->will($this->returnValue($this->objectManagerMock));
151 
152  $this->resultPageFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
153  ->disableOriginalConstructor()
154  ->setMethods(['create'])
155  ->getMock();
156 
157  $this->resultRawFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\RawFactory::class)
158  ->disableOriginalConstructor()
159  ->setMethods(['create'])
160  ->getMock();
161 
162  $this->resultJsonFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class)
163  ->disableOriginalConstructor()
164  ->setMethods(['create'])
165  ->getMock();
166 
167  $this->invoiceServiceMock = $this->getMockBuilder(\Magento\Sales\Model\Service\InvoiceService::class)
168  ->disableOriginalConstructor()
169  ->getMock();
170 
171  $this->controller = $objectManager->getObject(
172  \Magento\Sales\Controller\Adminhtml\Order\Invoice\UpdateQty::class,
173  [
174  'context' => $contextMock,
175  'resultPageFactory' => $this->resultPageFactoryMock,
176  'resultRawFactory' => $this->resultRawFactoryMock,
177  'resultJsonFactory' => $this->resultJsonFactoryMock,
178  'invoiceService' => $this->invoiceServiceMock
179  ]
180  );
181  }
182 
188  public function testExecute()
189  {
190  $orderId = 1;
191  $invoiceData = ['comment_text' => 'test'];
192  $response = 'test data';
193 
194  $this->requestMock->expects($this->at(0))
195  ->method('getParam')
196  ->with('order_id')
197  ->will($this->returnValue($orderId));
198  $this->requestMock->expects($this->at(1))
199  ->method('getParam')
200  ->with('invoice', [])
201  ->will($this->returnValue($invoiceData));
202 
203  $invoiceMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
204  ->disableOriginalConstructor()
205  ->setMethods([])
206  ->getMock();
207  $invoiceMock->expects($this->once())
208  ->method('getTotalQty')
209  ->willReturn(2);
210 
211  $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
212  ->disableOriginalConstructor()
213  ->setMethods(['load', 'getId', 'canInvoice'])
214  ->getMock();
215  $orderMock->expects($this->once())
216  ->method('load')
217  ->with($orderId)
218  ->willReturnSelf();
219  $orderMock->expects($this->once())
220  ->method('getId')
221  ->willReturn($orderId);
222  $orderMock->expects($this->once())
223  ->method('canInvoice')
224  ->willReturn(true);
225 
226  $this->invoiceServiceMock->expects($this->once())
227  ->method('prepareInvoice')
228  ->with($orderMock, [])
229  ->willReturn($invoiceMock);
230 
231  $this->objectManagerMock->expects($this->at(0))
232  ->method('create')
233  ->with(\Magento\Sales\Model\Order::class)
234  ->willReturn($orderMock);
235 
236  $blockItemMock = $this->getMockBuilder(\Magento\Sales\Block\Order\Items::class)
237  ->disableOriginalConstructor()
238  ->setMethods([])
239  ->getMock();
240  $blockItemMock->expects($this->once())
241  ->method('toHtml')
242  ->will($this->returnValue($response));
243 
244  $layoutMock = $this->getMockBuilder(\Magento\Framework\View\Layout::class)
245  ->disableOriginalConstructor()
246  ->setMethods([])
247  ->getMock();
248  $layoutMock->expects($this->once())
249  ->method('getBlock')
250  ->with('order_items')
251  ->will($this->returnValue($blockItemMock));
252 
253  $this->resultPageMock->expects($this->once())
254  ->method('getLayout')
255  ->will($this->returnValue($layoutMock));
256  $this->resultPageMock->expects($this->once())
257  ->method('getConfig')
258  ->will($this->returnValue($this->pageConfigMock));
259 
260  $this->pageConfigMock->expects($this->once())->method('getTitle')->will($this->returnValue($this->titleMock));
261 
262  $this->resultPageFactoryMock->expects($this->once())
263  ->method('create')
264  ->will($this->returnValue($this->resultPageMock));
265 
266  $resultRaw = $this->getMockBuilder(\Magento\Framework\Controller\Result\Raw::class)
267  ->disableOriginalConstructor()
268  ->setMethods([])
269  ->getMock();
270  $resultRaw->expects($this->once())->method('setContents')->with($response);
271 
272  $this->resultRawFactoryMock->expects($this->once())->method('create')->will($this->returnValue($resultRaw));
273 
274  $this->assertSame($resultRaw, $this->controller->execute());
275  }
276 
282  public function testExecuteModelException()
283  {
284  $message = 'The order no longer exists.';
285  $response = ['error' => true, 'message' => $message];
286 
287  $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
288  ->disableOriginalConstructor()
289  ->setMethods(['load', 'getId', 'canInvoice'])
290  ->getMock();
291  $orderMock->expects($this->once())
292  ->method('load')
293  ->will($this->returnSelf());
294  $orderMock->expects($this->once())
295  ->method('getId')
296  ->willReturn(null);
297  $this->objectManagerMock->expects($this->at(0))
298  ->method('create')
299  ->with(\Magento\Sales\Model\Order::class)
300  ->willReturn($orderMock);
301 
302  $this->titleMock->expects($this->never())
303  ->method('prepend')
304  ->with('Invoices');
305 
307  $resultJsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
308  ->disableOriginalConstructor()
309  ->setMethods([])
310  ->getMock();
311  $resultJsonMock->expects($this->once())->method('setData')->with($response);
312 
313  $this->resultJsonFactoryMock->expects($this->once())
314  ->method('create')
315  ->will($this->returnValue($resultJsonMock));
316 
317  $this->assertSame($resultJsonMock, $this->controller->execute());
318  }
319 
325  public function testExecuteException()
326  {
327  $message = 'The order no longer exists.';
328  $response = ['error' => true, 'message' => $message];
329 
330  $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
331  ->disableOriginalConstructor()
332  ->setMethods(['load', 'getId', 'canInvoice'])
333  ->getMock();
334  $orderMock->expects($this->once())
335  ->method('load')
336  ->will($this->returnSelf());
337  $orderMock->expects($this->once())
338  ->method('getId')
339  ->willReturn(null);
340  $this->objectManagerMock->expects($this->at(0))
341  ->method('create')
342  ->with(\Magento\Sales\Model\Order::class)
343  ->willReturn($orderMock);
344 
345  $this->titleMock->expects($this->never())
346  ->method('prepend')
347  ->with('Invoices');
348 
350  $resultJsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
351  ->disableOriginalConstructor()
352  ->setMethods([])
353  ->getMock();
354  $resultJsonMock->expects($this->once())->method('setData')->with($response);
355 
356  $this->resultJsonFactoryMock->expects($this->once())
357  ->method('create')
358  ->will($this->returnValue($resultJsonMock));
359 
360  $this->assertSame($resultJsonMock, $this->controller->execute());
361  }
362 }
$response
Definition: 404.php:11
$objectManager
Definition: bootstrap.php:17
$message