/app/code/Magento/Sales/Test/Unit/Controller/Adminhtml/Invoice/AbstractInvoice/EmailTest.php

https://gitlab.com/crazybutterfly815/magento2 · PHP · 258 lines · 174 code · 32 blank · 52 comment · 0 complexity · 315bed406b142a5ef92e35933643afe2 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\Invoice\AbstractInvoice;
  7. use \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\Email;
  8. use Magento\Framework\App\Action\Context;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  10. /**
  11. * Class EmailTest
  12. *
  13. * @package Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class EmailTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @var Email
  20. */
  21. protected $invoiceEmail;
  22. /**
  23. * @var Context|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $context;
  26. /**
  27. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $request;
  30. /**
  31. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $response;
  34. /**
  35. * @var \Magento\Framework\Message\Manager|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $messageManager;
  38. /**
  39. * @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $objectManager;
  42. /**
  43. * @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $session;
  46. /**
  47. * @var \Magento\Framework\App\ActionFlag|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $actionFlag;
  50. /**
  51. * @var \Magento\Backend\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $helper;
  54. /**
  55. * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $resultRedirect;
  58. /**
  59. * @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. protected $resultRedirectFactory;
  62. /**
  63. * @var \Magento\Backend\Model\View\Result\Forward|\PHPUnit_Framework_MockObject_MockObject
  64. */
  65. protected $resultForward;
  66. /**
  67. * @var \Magento\Backend\Model\View\Result\ForwardFactory|\PHPUnit_Framework_MockObject_MockObject
  68. */
  69. protected $resultForwardFactory;
  70. /**
  71. * @var \PHPUnit_Framework_MockObject_MockObject
  72. */
  73. protected $invoiceManagement;
  74. protected function setUp()
  75. {
  76. $objectManagerHelper = new ObjectManagerHelper($this);
  77. $this->context = $this->getMock(\Magento\Backend\App\Action\Context::class, [], [], '', false);
  78. $this->response = $this->getMock(\Magento\Framework\App\ResponseInterface::class, [], [], '', false);
  79. $this->request = $this->getMock(\Magento\Framework\App\RequestInterface::class, [], [], '', false);
  80. $this->objectManager = $this->getMock(\Magento\Framework\ObjectManager\ObjectManager::class, [], [], '', false);
  81. $this->messageManager = $this->getMock(\Magento\Framework\Message\Manager::class, [], [], '', false);
  82. $this->session = $this->getMock(\Magento\Backend\Model\Session::class, ['setIsUrlNotice'], [], '', false);
  83. $this->actionFlag = $this->getMock(\Magento\Framework\App\ActionFlag::class, [], [], '', false);
  84. $this->helper = $this->getMock(\Magento\Backend\Helper\Data::class, [], [], '', false);
  85. $this->resultRedirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $this->resultRedirectFactory = $this->getMockBuilder(\Magento\Backend\Model\View\Result\RedirectFactory::class)
  89. ->disableOriginalConstructor()
  90. ->setMethods(['create'])
  91. ->getMock();
  92. $this->context->expects($this->once())
  93. ->method('getMessageManager')
  94. ->willReturn($this->messageManager);
  95. $this->context->expects($this->once())
  96. ->method('getObjectManager')
  97. ->willReturn($this->objectManager);
  98. $this->context->expects($this->once())
  99. ->method('getSession')
  100. ->willReturn($this->session);
  101. $this->context->expects($this->once())
  102. ->method('getActionFlag')
  103. ->willReturn($this->actionFlag);
  104. $this->context->expects($this->once())
  105. ->method('getRequest')
  106. ->willReturn($this->request);
  107. $this->context->expects($this->once())
  108. ->method('getResponse')
  109. ->willReturn($this->response);
  110. $this->context->expects($this->once())
  111. ->method('getHelper')
  112. ->willReturn($this->helper);
  113. $this->context->expects($this->once())
  114. ->method('getResultRedirectFactory')
  115. ->willReturn($this->resultRedirectFactory);
  116. $this->invoiceManagement = $this->getMockBuilder(\Magento\Sales\Api\InvoiceManagementInterface::class)
  117. ->disableOriginalConstructor()
  118. ->getMock();
  119. $this->resultForward = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Forward::class)
  120. ->disableOriginalConstructor()
  121. ->getMock();
  122. $this->resultForwardFactory = $this->getMockBuilder(\Magento\Backend\Model\View\Result\ForwardFactory::class)
  123. ->disableOriginalConstructor()
  124. ->setMethods(['create'])
  125. ->getMock();
  126. $this->invoiceEmail = $objectManagerHelper->getObject(
  127. \Magento\Sales\Controller\Adminhtml\Order\Invoice\Email::class,
  128. [
  129. 'context' => $this->context,
  130. 'resultForwardFactory' => $this->resultForwardFactory,
  131. ]
  132. );
  133. }
  134. public function testEmail()
  135. {
  136. $invoiceId = 10000031;
  137. $orderId = 100000030;
  138. $invoiceClassName = \Magento\Sales\Model\Order\Invoice::class;
  139. $cmNotifierClassName = \Magento\Sales\Api\InvoiceManagementInterface::class;
  140. $invoice = $this->getMock($invoiceClassName, [], [], '', false);
  141. $invoice->expects($this->once())
  142. ->method('getEntityId')
  143. ->willReturn($invoiceId);
  144. $order = $this->getMock(\Magento\Sales\Model\Order::class, [], [], '', false);
  145. $order->expects($this->once())
  146. ->method('getId')
  147. ->willReturn($orderId);
  148. $this->request->expects($this->once())
  149. ->method('getParam')
  150. ->with('invoice_id')
  151. ->willReturn($invoiceId);
  152. $invoiceRepository = $this->getMockBuilder(\Magento\Sales\Api\InvoiceRepositoryInterface::class)
  153. ->disableOriginalConstructor()
  154. ->getMock();
  155. $invoiceRepository->expects($this->any())
  156. ->method('get')
  157. ->willReturn($invoice);
  158. $this->objectManager->expects($this->at(0))
  159. ->method('create')
  160. ->with(\Magento\Sales\Api\InvoiceRepositoryInterface::class)
  161. ->willReturn($invoiceRepository);
  162. $invoice->expects($this->once())
  163. ->method('getOrder')
  164. ->willReturn($order);
  165. $this->objectManager->expects($this->at(1))
  166. ->method('create')
  167. ->with($cmNotifierClassName)
  168. ->willReturn($this->invoiceManagement);
  169. $this->invoiceManagement->expects($this->once())
  170. ->method('notify')
  171. ->with($invoiceId)
  172. ->willReturn(true);
  173. $this->messageManager->expects($this->once())
  174. ->method('addSuccess')
  175. ->with('You sent the message.');
  176. $this->resultRedirectFactory->expects($this->atLeastOnce())
  177. ->method('create')
  178. ->willReturn($this->resultRedirect);
  179. $this->resultRedirect->expects($this->once())
  180. ->method('setPath')
  181. ->with('sales/invoice/view', ['order_id' => $orderId, 'invoice_id' => $invoiceId])
  182. ->willReturnSelf();
  183. $this->assertInstanceOf(\Magento\Backend\Model\View\Result\Redirect::class, $this->invoiceEmail->execute());
  184. }
  185. public function testEmailNoInvoiceId()
  186. {
  187. $this->request->expects($this->once())
  188. ->method('getParam')
  189. ->with('invoice_id')
  190. ->willReturn(null);
  191. $this->resultForwardFactory->expects($this->any())
  192. ->method('create')
  193. ->willReturn($this->resultForward);
  194. $this->resultForward->expects($this->once())
  195. ->method('forward')
  196. ->with('noroute')
  197. ->willReturnSelf();
  198. $this->assertInstanceOf(\Magento\Backend\Model\View\Result\Forward::class, $this->invoiceEmail->execute());
  199. }
  200. public function testEmailNoInvoice()
  201. {
  202. $invoiceId = 10000031;
  203. $this->request->expects($this->once())
  204. ->method('getParam')
  205. ->with('invoice_id')
  206. ->willReturn($invoiceId);
  207. $invoiceRepository = $this->getMockBuilder(\Magento\Sales\Api\InvoiceRepositoryInterface::class)
  208. ->disableOriginalConstructor()
  209. ->getMock();
  210. $invoiceRepository->expects($this->any())
  211. ->method('get')
  212. ->willReturn(null);
  213. $this->objectManager->expects($this->at(0))
  214. ->method('create')
  215. ->with(\Magento\Sales\Api\InvoiceRepositoryInterface::class)
  216. ->willReturn($invoiceRepository);
  217. $this->resultForwardFactory->expects($this->any())
  218. ->method('create')
  219. ->willReturn($this->resultForward);
  220. $this->resultForward->expects($this->once())
  221. ->method('forward')
  222. ->with('noroute')
  223. ->willReturnSelf();
  224. $this->assertInstanceOf(\Magento\Backend\Model\View\Result\Forward::class, $this->invoiceEmail->execute());
  225. }
  226. }