EmailTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Controller\Adminhtml\Order;
  7. use Magento\Framework\App\Action\Context;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. use Magento\Sales\Controller\Adminhtml\Order\Email;
  10. /**
  11. * Class EmailTest
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. * @package Magento\Sales\Controller\Adminhtml\Order
  14. */
  15. class EmailTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var Email
  19. */
  20. protected $orderEmail;
  21. /**
  22. * @var Context|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $context;
  25. /**
  26. * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $resultRedirect;
  29. /**
  30. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $request;
  33. /**
  34. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $response;
  37. /**
  38. * @var \Magento\Framework\Message\Manager|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $messageManager;
  41. /**
  42. * @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $objectManager;
  45. /**
  46. * @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $session;
  49. /**
  50. * @var \Magento\Framework\App\ActionFlag|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. protected $actionFlag;
  53. /**
  54. * @var \Magento\Backend\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. protected $helper;
  57. /**
  58. * @var \Magento\Sales\Api\OrderManagementInterface|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. protected $orderManagementMock;
  61. /**
  62. * @var \Magento\Sales\Api\OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  63. */
  64. protected $orderRepositoryMock;
  65. /**
  66. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  67. */
  68. protected $loggerMock;
  69. /**
  70. * @var \Magento\Sales\Api\Data\OrderInterface|\PHPUnit_Framework_MockObject_MockObject
  71. */
  72. protected $orderMock;
  73. /**
  74. * Test setup
  75. */
  76. protected function setUp()
  77. {
  78. $objectManagerHelper = new ObjectManagerHelper($this);
  79. $this->context = $this->createPartialMock(\Magento\Backend\App\Action\Context::class, [
  80. 'getRequest',
  81. 'getResponse',
  82. 'getMessageManager',
  83. 'getRedirect',
  84. 'getObjectManager',
  85. 'getSession',
  86. 'getActionFlag',
  87. 'getHelper',
  88. 'getResultRedirectFactory'
  89. ]);
  90. $this->orderManagementMock = $this->getMockBuilder(\Magento\Sales\Api\OrderManagementInterface::class)
  91. ->getMockForAbstractClass();
  92. $this->orderRepositoryMock = $this->getMockBuilder(\Magento\Sales\Api\OrderRepositoryInterface::class)
  93. ->getMockForAbstractClass();
  94. $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  95. ->getMockForAbstractClass();
  96. $resultRedirectFactory = $this->createPartialMock(
  97. \Magento\Backend\Model\View\Result\RedirectFactory::class,
  98. ['create']
  99. );
  100. $this->response = $this->createPartialMock(
  101. \Magento\Framework\App\ResponseInterface::class,
  102. ['setRedirect', 'sendResponse']
  103. );
  104. $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  105. ->disableOriginalConstructor()->getMock();
  106. $this->messageManager = $this->createPartialMock(
  107. \Magento\Framework\Message\Manager::class,
  108. ['addSuccessMessage', 'addErrorMessage']
  109. );
  110. $this->orderMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderInterface::class)
  111. ->getMockForAbstractClass();
  112. $this->session = $this->createPartialMock(\Magento\Backend\Model\Session::class, ['setIsUrlNotice']);
  113. $this->actionFlag = $this->createPartialMock(\Magento\Framework\App\ActionFlag::class, ['get', 'set']);
  114. $this->helper = $this->createPartialMock(\Magento\Backend\Helper\Data::class, ['getUrl']);
  115. $this->resultRedirect = $this->createMock(\Magento\Backend\Model\View\Result\Redirect::class);
  116. $resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirect);
  117. $this->context->expects($this->once())->method('getMessageManager')->willReturn($this->messageManager);
  118. $this->context->expects($this->once())->method('getRequest')->willReturn($this->request);
  119. $this->context->expects($this->once())->method('getResponse')->willReturn($this->response);
  120. $this->context->expects($this->once())->method('getObjectManager')->willReturn($this->objectManager);
  121. $this->context->expects($this->once())->method('getSession')->willReturn($this->session);
  122. $this->context->expects($this->once())->method('getActionFlag')->willReturn($this->actionFlag);
  123. $this->context->expects($this->once())->method('getHelper')->willReturn($this->helper);
  124. $this->context->expects($this->once())->method('getResultRedirectFactory')->willReturn($resultRedirectFactory);
  125. $this->orderEmail = $objectManagerHelper->getObject(
  126. \Magento\Sales\Controller\Adminhtml\Order\Email::class,
  127. [
  128. 'context' => $this->context,
  129. 'request' => $this->request,
  130. 'response' => $this->response,
  131. 'orderManagement' => $this->orderManagementMock,
  132. 'orderRepository' => $this->orderRepositoryMock,
  133. 'logger' => $this->loggerMock
  134. ]
  135. );
  136. }
  137. /**
  138. * testEmail
  139. */
  140. public function testEmail()
  141. {
  142. $orderId = 10000031;
  143. $this->request->expects($this->once())
  144. ->method('getParam')
  145. ->with('order_id')
  146. ->will($this->returnValue($orderId));
  147. $this->orderRepositoryMock->expects($this->once())
  148. ->method('get')
  149. ->with($orderId)
  150. ->willReturn($this->orderMock);
  151. $this->orderMock->expects($this->atLeastOnce())
  152. ->method('getEntityId')
  153. ->will($this->returnValue($orderId));
  154. $this->orderManagementMock->expects($this->once())
  155. ->method('notify')
  156. ->with($orderId)
  157. ->willReturn(true);
  158. $this->messageManager->expects($this->once())
  159. ->method('addSuccessMessage')
  160. ->with('You sent the order email.');
  161. $this->resultRedirect->expects($this->once())
  162. ->method('setPath')
  163. ->with('sales/order/view', ['order_id' => $orderId])
  164. ->willReturnSelf();
  165. $this->assertInstanceOf(
  166. \Magento\Backend\Model\View\Result\Redirect::class,
  167. $this->orderEmail->execute()
  168. );
  169. $this->assertEquals($this->response, $this->orderEmail->getResponse());
  170. }
  171. /**
  172. * testEmailNoOrderId
  173. */
  174. public function testEmailNoOrderId()
  175. {
  176. $this->request->expects($this->once())
  177. ->method('getParam')
  178. ->with('order_id')
  179. ->will($this->returnValue(null));
  180. $this->orderRepositoryMock->expects($this->once())
  181. ->method('get')
  182. ->with(null)
  183. ->willThrowException(
  184. new \Magento\Framework\Exception\NoSuchEntityException(
  185. __("The entity that was requested doesn't exist. Verify the entity and try again.")
  186. )
  187. );
  188. $this->messageManager->expects($this->once())
  189. ->method('addErrorMessage')
  190. ->with('This order no longer exists.');
  191. $this->actionFlag->expects($this->once())
  192. ->method('set')
  193. ->with('', 'no-dispatch', true)
  194. ->will($this->returnValue(true));
  195. $this->resultRedirect->expects($this->once())
  196. ->method('setPath')
  197. ->with('sales/*/')
  198. ->willReturnSelf();
  199. $this->assertInstanceOf(
  200. \Magento\Backend\Model\View\Result\Redirect::class,
  201. $this->orderEmail->execute()
  202. );
  203. }
  204. }