/app/code/Magento/Sales/Test/Unit/Controller/Adminhtml/Order/Invoice/CaptureTest.php

https://gitlab.com/crazybutterfly815/magento2 · PHP · 390 lines · 263 code · 67 blank · 60 comment · 0 complexity · 17eced8deeb518acd73739b4ede1de0b MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Controller\Adminhtml\Order\Invoice;
  7. use Magento\Backend\App\Action;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\Sales\Api\InvoiceRepositoryInterface;
  10. /**
  11. * Class CaptureTest
  12. * @package Magento\Sales\Controller\Adminhtml\Order\Invoice
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class CaptureTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $objectManagerMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $requestMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $responseMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $messageManagerMock;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $sessionMock;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $actionFlagMock;
  41. /**
  42. * @var \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $helperMock;
  45. /**
  46. * @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $resultRedirectFactoryMock;
  49. /**
  50. * @var \Magento\Backend\Model\View\Result\ForwardFactory|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. protected $resultForwardFactoryMock;
  53. /**
  54. * @var \Magento\Sales\Controller\Adminhtml\Order\Invoice\Capture
  55. */
  56. protected $controller;
  57. /**
  58. * @var \PHPUnit_Framework_MockObject_MockObject
  59. */
  60. protected $invoiceManagement;
  61. /**
  62. * @var InvoiceRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  63. */
  64. protected $invoiceRepository;
  65. /**
  66. * @return void
  67. */
  68. protected function setUp()
  69. {
  70. $objectManager = new ObjectManager($this);
  71. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  72. ->disableOriginalConstructor()
  73. ->setMethods([])
  74. ->getMock();
  75. $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http::class)
  76. ->disableOriginalConstructor()
  77. ->setMethods([])
  78. ->getMock();
  79. $this->objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class);
  80. $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\Manager::class)
  81. ->disableOriginalConstructor()
  82. ->setMethods([])
  83. ->getMock();
  84. $this->sessionMock = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
  85. ->disableOriginalConstructor()
  86. ->setMethods([])
  87. ->getMock();
  88. $this->actionFlagMock = $this->getMockBuilder(\Magento\Framework\App\ActionFlag::class)
  89. ->disableOriginalConstructor()
  90. ->setMethods([])
  91. ->getMock();
  92. $this->helperMock = $this->getMockBuilder(\Magento\Backend\Helper\Data::class)
  93. ->disableOriginalConstructor()
  94. ->setMethods([])
  95. ->getMock();
  96. $this->resultRedirectFactoryMock = $this->getMockBuilder(
  97. \Magento\Backend\Model\View\Result\RedirectFactory::class
  98. )->disableOriginalConstructor()
  99. ->setMethods(['create'])
  100. ->getMock();
  101. $this->resultForwardFactoryMock = $this->getMockBuilder(
  102. \Magento\Backend\Model\View\Result\ForwardFactory::class
  103. )->disableOriginalConstructor()
  104. ->setMethods(['create'])
  105. ->getMock();
  106. $contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
  107. ->disableOriginalConstructor()
  108. ->setMethods([])
  109. ->getMock();
  110. $contextMock->expects($this->any())
  111. ->method('getRequest')
  112. ->willReturn($this->requestMock);
  113. $contextMock->expects($this->any())
  114. ->method('getResponse')
  115. ->willReturn($this->responseMock);
  116. $contextMock->expects($this->any())
  117. ->method('getObjectManager')
  118. ->willReturn($this->objectManagerMock);
  119. $contextMock->expects($this->any())
  120. ->method('getMessageManager')
  121. ->willReturn($this->messageManagerMock);
  122. $contextMock->expects($this->any())
  123. ->method('getSession')
  124. ->willReturn($this->sessionMock);
  125. $contextMock->expects($this->any())
  126. ->method('getActionFlag')
  127. ->willReturn($this->actionFlagMock);
  128. $contextMock->expects($this->any())
  129. ->method('getHelper')
  130. ->willReturn($this->helperMock);
  131. $contextMock->expects($this->any())
  132. ->method('getResultRedirectFactory')
  133. ->willReturn($this->resultRedirectFactoryMock);
  134. $this->invoiceManagement = $this->getMockBuilder(\Magento\Sales\Api\InvoiceManagementInterface::class)
  135. ->disableOriginalConstructor()
  136. ->getMock();
  137. $this->objectManagerMock->expects($this->any())
  138. ->method('get')
  139. ->with(\Magento\Sales\Api\InvoiceManagementInterface::class)
  140. ->willReturn($this->invoiceManagement);
  141. $this->invoiceRepository = $this->getMockBuilder(InvoiceRepositoryInterface::class)
  142. ->disableOriginalConstructor()
  143. ->getMockForAbstractClass();
  144. $this->controller = $objectManager->getObject(
  145. \Magento\Sales\Controller\Adminhtml\Order\Invoice\Capture::class,
  146. [
  147. 'context' => $contextMock,
  148. 'resultForwardFactory' => $this->resultForwardFactoryMock,
  149. ]
  150. );
  151. $objectManager->setBackwardCompatibleProperty(
  152. $this->controller,
  153. 'invoiceRepository',
  154. $this->invoiceRepository
  155. );
  156. }
  157. /**
  158. * @return void
  159. */
  160. public function testExecute()
  161. {
  162. $invoiceId = 2;
  163. $this->requestMock->expects($this->once())
  164. ->method('getParam')
  165. ->with('invoice_id')
  166. ->will($this->returnValue($invoiceId));
  167. $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  168. ->disableOriginalConstructor()
  169. ->setMethods(['setIsInProcess', '__wakeup'])
  170. ->getMock();
  171. $this->invoiceManagement->expects($this->once())
  172. ->method('setCapture')
  173. ->with($invoiceId);
  174. $invoiceMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
  175. ->disableOriginalConstructor()
  176. ->setMethods([])
  177. ->getMock();
  178. $invoiceMock->expects($this->any())
  179. ->method('getEntityId')
  180. ->will($this->returnValue($invoiceId));
  181. $invoiceMock->expects($this->any())
  182. ->method('getOrder')
  183. ->will($this->returnValue($orderMock));
  184. $transactionMock = $this->getMockBuilder(\Magento\Framework\DB\Transaction::class)
  185. ->disableOriginalConstructor()
  186. ->setMethods([])
  187. ->getMock();
  188. $transactionMock->expects($this->at(0))
  189. ->method('addObject')
  190. ->with($invoiceMock)
  191. ->will($this->returnSelf());
  192. $transactionMock->expects($this->at(1))
  193. ->method('addObject')
  194. ->with($orderMock)
  195. ->will($this->returnSelf());
  196. $transactionMock->expects($this->at(2))
  197. ->method('save');
  198. $this->messageManagerMock->expects($this->once())
  199. ->method('addSuccess')
  200. ->with('The invoice has been captured.');
  201. $invoiceMock->expects($this->once())
  202. ->method('getId')
  203. ->will($this->returnValue($invoiceId));
  204. $this->invoiceRepository->expects($this->once())
  205. ->method('get')
  206. ->willReturn($invoiceMock);
  207. $this->objectManagerMock->expects($this->at(1))
  208. ->method('create')
  209. ->with(\Magento\Framework\DB\Transaction::class)
  210. ->will($this->returnValue($transactionMock));
  211. $resultRedirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  212. ->disableOriginalConstructor()
  213. ->getMock();
  214. $resultRedirect->expects($this->once())->method('setPath')->with('sales/*/view', ['invoice_id' => $invoiceId]);
  215. $this->resultRedirectFactoryMock->expects($this->once())
  216. ->method('create')
  217. ->will($this->returnValue($resultRedirect));
  218. $this->assertSame($resultRedirect, $this->controller->execute());
  219. }
  220. /**
  221. * @return void
  222. */
  223. public function testExecuteNoInvoice()
  224. {
  225. $invoiceId = 2;
  226. $this->requestMock->expects($this->once())
  227. ->method('getParam')
  228. ->with('invoice_id')
  229. ->will($this->returnValue($invoiceId));
  230. $this->invoiceRepository->expects($this->once())
  231. ->method('get')
  232. ->willReturn(null);
  233. $resultForward = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Forward::class)
  234. ->disableOriginalConstructor()
  235. ->getMock();
  236. $resultForward->expects($this->once())->method('forward')->with(('noroute'))->will($this->returnSelf());
  237. $this->resultForwardFactoryMock->expects($this->once())
  238. ->method('create')
  239. ->will($this->returnValue($resultForward));
  240. $this->assertSame($resultForward, $this->controller->execute());
  241. }
  242. /**
  243. * @return void
  244. */
  245. public function testExecuteModelException()
  246. {
  247. $invoiceId = 2;
  248. $message = 'Invoice capturing error';
  249. $e = new \Magento\Framework\Exception\LocalizedException(__($message));
  250. $this->invoiceManagement->expects($this->once())
  251. ->method('setCapture')
  252. ->with($invoiceId)
  253. ->will($this->throwException($e));
  254. $this->requestMock->expects($this->once())
  255. ->method('getParam')
  256. ->with('invoice_id')
  257. ->will($this->returnValue($invoiceId));
  258. $invoiceMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
  259. ->disableOriginalConstructor()
  260. ->getMock();
  261. $this->messageManagerMock->expects($this->once())
  262. ->method('addError')
  263. ->with($message);
  264. $invoiceMock->expects($this->once())
  265. ->method('getId')
  266. ->will($this->returnValue($invoiceId));
  267. $invoiceMock->expects($this->once())
  268. ->method('getEntityId')
  269. ->will($this->returnValue($invoiceId));
  270. $this->invoiceRepository->expects($this->once())
  271. ->method('get')
  272. ->willReturn($invoiceMock);
  273. $resultRedirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  274. ->disableOriginalConstructor()
  275. ->getMock();
  276. $resultRedirect->expects($this->once())->method('setPath')->with('sales/*/view', ['invoice_id' => $invoiceId]);
  277. $this->resultRedirectFactoryMock->expects($this->once())
  278. ->method('create')
  279. ->will($this->returnValue($resultRedirect));
  280. $this->assertSame($resultRedirect, $this->controller->execute());
  281. }
  282. /**
  283. * @return void
  284. */
  285. public function testExecuteException()
  286. {
  287. $invoiceId = 2;
  288. $message = 'Invoice capturing error';
  289. $e = new \Exception($message);
  290. $this->requestMock->expects($this->once())
  291. ->method('getParam')
  292. ->with('invoice_id')
  293. ->will($this->returnValue($invoiceId));
  294. $this->invoiceManagement->expects($this->once())
  295. ->method('setCapture')
  296. ->with($invoiceId)
  297. ->will($this->throwException($e));
  298. $invoiceMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
  299. ->disableOriginalConstructor()
  300. ->getMock();
  301. $this->messageManagerMock->expects($this->once())
  302. ->method('addError')
  303. ->with($message);
  304. $invoiceMock->expects($this->once())
  305. ->method('getId')
  306. ->will($this->returnValue($invoiceId));
  307. $invoiceMock->expects($this->once())
  308. ->method('getEntityId')
  309. ->will($this->returnValue($invoiceId));
  310. $this->invoiceRepository->expects($this->once())
  311. ->method('get')
  312. ->willReturn($invoiceMock);
  313. $resultRedirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  314. ->disableOriginalConstructor()
  315. ->setMethods([])
  316. ->getMock();
  317. $resultRedirect->expects($this->once())->method('setPath')->with('sales/*/view', ['invoice_id' => $invoiceId]);
  318. $this->resultRedirectFactoryMock->expects($this->once())
  319. ->method('create')
  320. ->will($this->returnValue($resultRedirect));
  321. $this->assertSame($resultRedirect, $this->controller->execute());
  322. }
  323. }