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 
14 class UpdateQtyTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $controller;
20 
24  protected $contextMock;
25 
29  protected $loaderMock;
30 
34  protected $requestMock;
35 
39  protected $responseMock;
40 
44  protected $objectManagerMock;
45 
49  protected $creditmemoMock;
50 
55 
59  protected $sessionMock;
60 
64  protected $actionFlagMock;
65 
69  protected $helperMock;
70 
75 
80 
85 
89  protected $resultPageMock;
90 
94  protected $resultJsonMock;
95 
99  protected $resultRawMock;
100 
106  protected function setUp()
107  {
108  $this->creditmemoMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Creditmemo::class)
109  ->disableOriginalConstructor()
110  ->setMethods(['getInvoice', 'getOrder', 'cancel', 'getId', '__wakeup'])
111  ->getMock();
112  $titleMock = $this->getMockBuilder(\Magento\Framework\App\Action\Title::class)
113  ->disableOriginalConstructor()
114  ->getMock();
115  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
116  ->disableOriginalConstructor()
117  ->getMock();
118  $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
119  ->disableOriginalConstructor()
120  ->getMock();
121  $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
122  $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\Manager::class)
123  ->disableOriginalConstructor()
124  ->getMock();
125  $this->sessionMock = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
126  ->disableOriginalConstructor()
127  ->getMock();
128  $this->helperMock = $this->getMockBuilder(\Magento\Backend\Helper\Data::class)
129  ->disableOriginalConstructor()
130  ->getMock();
131  $this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
132  ->setMethods(
133  [
134  'getRequest',
135  'getResponse',
136  'getObjectManager',
137  'getTitle',
138  'getSession',
139  'getHelper',
140  'getActionFlag',
141  'getMessageManager',
142  'getResultRedirectFactory'
143  ]
144  )
145  ->disableOriginalConstructor()
146  ->getMock();
147  $this->contextMock->expects($this->any())
148  ->method('getHelper')
149  ->will($this->returnValue($this->helperMock));
150  $this->actionFlagMock = $this->getMockBuilder(\Magento\Framework\App\ActionFlag::class)
151  ->disableOriginalConstructor()
152  ->getMock();
153  $this->contextMock->expects($this->any())
154  ->method('getSession')
155  ->will($this->returnValue($this->sessionMock));
156  $this->contextMock->expects($this->any())
157  ->method('getActionFlag')
158  ->will($this->returnValue($this->actionFlagMock));
159  $this->contextMock->expects($this->any())
160  ->method('getRequest')
161  ->will($this->returnValue($this->requestMock));
162  $this->contextMock->expects($this->any())
163  ->method('getResponse')
164  ->will($this->returnValue($this->responseMock));
165  $this->contextMock->expects($this->any())
166  ->method('getObjectManager')
167  ->will($this->returnValue($this->objectManagerMock));
168  $this->contextMock->expects($this->any())
169  ->method('getTitle')
170  ->will($this->returnValue($titleMock));
171  $this->contextMock->expects($this->any())
172  ->method('getMessageManager')
173  ->will($this->returnValue($this->messageManagerMock));
174  $this->loaderMock = $this->getMockBuilder(\Magento\Sales\Controller\Adminhtml\Order\CreditmemoLoader::class)
175  ->disableOriginalConstructor()
176  ->getMock();
177  $this->resultPageFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
178  ->disableOriginalConstructor()
179  ->setMethods(['create'])
180  ->getMock();
181  $this->resultJsonFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class)
182  ->disableOriginalConstructor()
183  ->setMethods(['create'])
184  ->getMock();
185  $this->resultRawFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\RawFactory::class)
186  ->disableOriginalConstructor()
187  ->setMethods(['create'])
188  ->getMock();
189  $this->resultPageMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Page::class)
190  ->disableOriginalConstructor()
191  ->getMock();
192  $this->resultJsonMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
193  ->disableOriginalConstructor()
194  ->getMock();
195  $this->resultRawMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Raw::class)
196  ->disableOriginalConstructor()
197  ->getMock();
198 
199  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
200  $this->controller = $objectManager->getObject(
201  \Magento\Sales\Controller\Adminhtml\Order\Creditmemo\UpdateQty::class,
202  [
203  'context' => $this->contextMock,
204  'creditmemoLoader' => $this->loaderMock,
205  'resultPageFactory' => $this->resultPageFactoryMock,
206  'resultJsonFactory' => $this->resultJsonFactoryMock,
207  'resultRawFactory' => $this->resultRawFactoryMock
208  ]
209  );
210  }
211 
217  public function testExecuteModelException()
218  {
219  $message = 'Model exception';
220  $e = new \Magento\Framework\Exception\LocalizedException(__($message));
221  $response = ['error' => true, 'message' => $message];
222 
223  $this->requestMock->expects($this->any())
224  ->method('getParam')
225  ->willReturnArgument(0);
226  $this->loaderMock->expects($this->once())
227  ->method('load')
228  ->willThrowException($e);
229  $this->resultJsonFactoryMock->expects($this->once())
230  ->method('create')
231  ->willReturn($this->resultJsonMock);
232  $this->resultJsonMock->expects($this->once())
233  ->method('setData')
234  ->with($response)
235  ->willReturnSelf();
236 
237  $this->assertInstanceOf(
238  \Magento\Framework\Controller\Result\Json::class,
239  $this->controller->execute()
240  );
241  }
242 
248  public function testExecuteException()
249  {
250  $message = 'We can\'t update the item\'s quantity right now.';
251  $e = new \Exception($message);
252  $response = ['error' => true, 'message' => $message];
253 
254  $this->requestMock->expects($this->any())
255  ->method('getParam')
256  ->willReturnArgument(0);
257  $this->loaderMock->expects($this->once())
258  ->method('load')
259  ->willThrowException($e);
260  $this->resultJsonFactoryMock->expects($this->once())
261  ->method('create')
262  ->willReturn($this->resultJsonMock);
263  $this->resultJsonMock->expects($this->once())
264  ->method('setData')
265  ->with($response)
266  ->willReturnSelf();
267 
268  $this->assertInstanceOf(
269  \Magento\Framework\Controller\Result\Json::class,
270  $this->controller->execute()
271  );
272  }
273 
279  public function testExecute()
280  {
281  $response = 'output';
282 
283  $this->requestMock->expects($this->any())
284  ->method('getParam')
285  ->withAnyParameters()
286  ->willReturnArgument(0);
287 
288  $layoutMock = $this->getMockBuilder(\Magento\Framework\View\Layout::class)
289  ->disableOriginalConstructor()
290  ->getMock();
291  $blockMock = $this->getMockBuilder(\Magento\Sales\Block\Order\Items::class)
292  ->disableOriginalConstructor()
293  ->getMock();
294 
295  $this->resultPageFactoryMock->expects($this->once())
296  ->method('create')
297  ->willReturn($this->resultPageMock);
298  $this->resultPageMock->expects($this->atLeastOnce())
299  ->method('getLayout')
300  ->willReturn($layoutMock);
301  $blockMock->expects($this->once())
302  ->method('toHtml')
303  ->willReturn($response);
304  $layoutMock->expects($this->once())
305  ->method('getBlock')
306  ->with('order_items')
307  ->willReturn($blockMock);
308  $this->resultRawFactoryMock->expects($this->once())
309  ->method('create')
310  ->willReturn($this->resultRawMock);
311  $this->resultRawMock->expects($this->once())
312  ->method('setContents')
313  ->with($response)
314  ->willReturnSelf();
315 
316  $this->assertInstanceOf(
317  \Magento\Framework\Controller\Result\Raw::class,
318  $this->controller->execute()
319  );
320  }
321 }
$response
Definition: 404.php:11
$objectManager
Definition: bootstrap.php:17
__()
Definition: __.php:13
$message