PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-sales/Test/Unit/Controller/Adminhtml/Order/MassCancelTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 292 lines | 184 code | 47 blank | 61 comment | 0 complexity | feba315442be588d6439daee3f56f66e 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;
  7. use Magento\Framework\App\Action\Context;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. /**
  10. * Class MassCancelTest
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class MassCancelTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @var \Magento\Sales\Controller\Adminhtml\Order\MassCancel
  17. */
  18. protected $massAction;
  19. /**
  20. * @var Context|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $contextMock;
  23. /**
  24. * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $resultRedirectMock;
  27. /**
  28. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $requestMock;
  31. /**
  32. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $responseMock;
  35. /**
  36. * @var \Magento\Framework\Message\Manager|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $messageManagerMock;
  39. /**
  40. * @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $objectManagerMock;
  43. /**
  44. * @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $sessionMock;
  47. /**
  48. * @var \Magento\Framework\App\ActionFlag|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $actionFlagMock;
  51. /**
  52. * @var \Magento\Backend\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected $helperMock;
  55. /**
  56. * @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
  57. */
  58. protected $orderMock;
  59. /**
  60. * @var \Magento\Sales\Model\ResourceModel\Order\Collection|\PHPUnit_Framework_MockObject_MockObject
  61. */
  62. protected $orderCollectionMock;
  63. /**
  64. * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  65. */
  66. protected $orderCollectionFactoryMock;
  67. /**
  68. * @var \Magento\Ui\Component\MassAction\Filter|\PHPUnit_Framework_MockObject_MockObject
  69. */
  70. protected $filterMock;
  71. public function setUp()
  72. {
  73. $objectManagerHelper = new ObjectManagerHelper($this);
  74. $this->contextMock = $this->getMock('Magento\Backend\App\Action\Context', [], [], '', false);
  75. $this->messageManagerMock = $this->getMock('Magento\Framework\Message\Manager', [], [], '', false);
  76. $this->responseMock = $this->getMock('Magento\Framework\App\ResponseInterface', [], [], '', false);
  77. $this->requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
  78. $this->objectManagerMock = $this->getMock(
  79. 'Magento\Framework\ObjectManager\ObjectManager',
  80. [],
  81. [],
  82. '',
  83. false
  84. );
  85. $resultRedirectFactory = $this->getMock(
  86. 'Magento\Backend\Model\View\Result\RedirectFactory',
  87. [],
  88. [],
  89. '',
  90. false
  91. );
  92. $this->orderCollectionMock = $this->getMockBuilder('Magento\Sales\Model\ResourceModel\Order\Collection')
  93. ->disableOriginalConstructor()
  94. ->getMock();
  95. $resourceCollection = 'Magento\Sales\Model\ResourceModel\Order\CollectionFactory';
  96. $this->orderCollectionFactoryMock = $this->getMockBuilder($resourceCollection)
  97. ->disableOriginalConstructor()
  98. ->setMethods(['create'])
  99. ->getMock();
  100. $this->sessionMock = $this->getMock('Magento\Backend\Model\Session', ['setIsUrlNotice'], [], '', false);
  101. $this->actionFlagMock = $this->getMock('Magento\Framework\App\ActionFlag', ['get', 'set'], [], '', false);
  102. $this->helperMock = $this->getMock('\Magento\Backend\Helper\Data', ['getUrl'], [], '', false);
  103. $this->resultRedirectMock = $this->getMock('Magento\Backend\Model\View\Result\Redirect', [], [], '', false);
  104. $resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirectMock);
  105. $redirectMock = $this->getMockBuilder('Magento\Backend\Model\View\Result\Redirect')
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. $resultFactoryMock = $this->getMockBuilder('Magento\Framework\Controller\ResultFactory')
  109. ->disableOriginalConstructor()
  110. ->getMock();
  111. $resultFactoryMock->expects($this->any())
  112. ->method('create')
  113. ->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
  114. ->willReturn($redirectMock);
  115. $this->contextMock->expects($this->once())->method('getMessageManager')->willReturn($this->messageManagerMock);
  116. $this->contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock);
  117. $this->contextMock->expects($this->once())->method('getResponse')->willReturn($this->responseMock);
  118. $this->contextMock->expects($this->once())->method('getObjectManager')->willReturn($this->objectManagerMock);
  119. $this->contextMock->expects($this->once())->method('getSession')->willReturn($this->sessionMock);
  120. $this->contextMock->expects($this->once())->method('getActionFlag')->willReturn($this->actionFlagMock);
  121. $this->contextMock->expects($this->once())->method('getHelper')->willReturn($this->helperMock);
  122. $this->contextMock->expects($this->once())
  123. ->method('getResultRedirectFactory')
  124. ->willReturn($resultRedirectFactory);
  125. $this->contextMock->expects($this->any())
  126. ->method('getResultFactory')
  127. ->willReturn($resultFactoryMock);
  128. $this->filterMock = $this->getMock('Magento\Ui\Component\MassAction\Filter', [], [], '', false);
  129. $this->filterMock->expects($this->once())
  130. ->method('getCollection')
  131. ->with($this->orderCollectionMock)
  132. ->willReturn($this->orderCollectionMock);
  133. $this->orderCollectionFactoryMock->expects($this->once())
  134. ->method('create')
  135. ->willReturn($this->orderCollectionMock);
  136. $this->massAction = $objectManagerHelper->getObject(
  137. 'Magento\Sales\Controller\Adminhtml\Order\MassCancel',
  138. [
  139. 'context' => $this->contextMock,
  140. 'filter' => $this->filterMock,
  141. 'collectionFactory' => $this->orderCollectionFactoryMock
  142. ]
  143. );
  144. }
  145. /**
  146. * Test for selected orders
  147. * Two orders, only $order1 can be canceled
  148. */
  149. public function testExecuteCanCancelOneOrder()
  150. {
  151. $order1 = $this->getMockBuilder('Magento\Sales\Model\Order')
  152. ->disableOriginalConstructor()
  153. ->getMock();
  154. $order2 = $this->getMockBuilder('Magento\Sales\Model\Order')
  155. ->disableOriginalConstructor()
  156. ->getMock();
  157. $orders = [$order1, $order2];
  158. $countOrders = count($orders);
  159. $this->orderCollectionMock->expects($this->any())
  160. ->method('getItems')
  161. ->willReturn($orders);
  162. $order1->expects($this->once())
  163. ->method('canCancel')
  164. ->willReturn(true);
  165. $order1->expects($this->once())
  166. ->method('cancel');
  167. $order1->expects($this->once())
  168. ->method('save');
  169. $this->orderCollectionMock->expects($this->once())
  170. ->method('count')
  171. ->willReturn($countOrders);
  172. $order2->expects($this->once())
  173. ->method('canCancel')
  174. ->willReturn(false);
  175. $this->messageManagerMock->expects($this->once())
  176. ->method('addError')
  177. ->with('1 order(s) cannot be canceled.');
  178. $this->messageManagerMock->expects($this->once())
  179. ->method('addSuccess')
  180. ->with('We canceled 1 order(s).');
  181. $this->resultRedirectMock->expects($this->once())
  182. ->method('setPath')
  183. ->with('sales/*/')
  184. ->willReturnSelf();
  185. $this->massAction->execute();
  186. }
  187. /**
  188. * Test for excluded orders
  189. * Two orders could't be canceled
  190. */
  191. public function testExcludedCannotCancelOrders()
  192. {
  193. $order1 = $this->getMockBuilder('Magento\Sales\Model\Order')
  194. ->disableOriginalConstructor()
  195. ->getMock();
  196. $order2 = $this->getMockBuilder('Magento\Sales\Model\Order')
  197. ->disableOriginalConstructor()
  198. ->getMock();
  199. $orders = [$order1, $order2];
  200. $countOrders = count($orders);
  201. $this->orderCollectionMock->expects($this->any())
  202. ->method('getItems')
  203. ->willReturn([$order1, $order2]);
  204. $order1->expects($this->once())
  205. ->method('canCancel')
  206. ->willReturn(false);
  207. $this->orderCollectionMock->expects($this->once())
  208. ->method('count')
  209. ->willReturn($countOrders);
  210. $order2->expects($this->once())
  211. ->method('canCancel')
  212. ->willReturn(false);
  213. $this->messageManagerMock->expects($this->once())
  214. ->method('addError')
  215. ->with('You cannot cancel the order(s).');
  216. $this->resultRedirectMock->expects($this->once())
  217. ->method('setPath')
  218. ->with('sales/*/')
  219. ->willReturnSelf();
  220. $this->massAction->execute();
  221. }
  222. /**
  223. * Order throws exception while canceling
  224. */
  225. public function testException()
  226. {
  227. $exception = new \Exception('Can not cancel');
  228. $order1 = $this->getMockBuilder('Magento\Sales\Model\Order')
  229. ->disableOriginalConstructor()
  230. ->getMock();
  231. $this->orderCollectionMock->expects($this->any())
  232. ->method('getItems')
  233. ->willReturn([$order1]);
  234. $order1->expects($this->once())
  235. ->method('canCancel')
  236. ->willReturn(true);
  237. $order1->expects($this->once())
  238. ->method('cancel')
  239. ->willThrowException($exception);
  240. $this->messageManagerMock->expects($this->once())
  241. ->method('addError')
  242. ->with('Can not cancel');
  243. $this->massAction->execute();
  244. }
  245. }