OrderServiceTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Model\Service;
  7. /**
  8. * Class OrderUnHoldTest
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class OrderServiceTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Sales\Model\Service\OrderService
  16. */
  17. protected $orderService;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Api\OrderRepositoryInterface
  20. */
  21. protected $orderRepositoryMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Api\OrderStatusHistoryRepositoryInterface
  24. */
  25. protected $orderStatusHistoryRepositoryMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\SearchCriteriaBuilder
  28. */
  29. protected $searchCriteriaBuilderMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\SearchCriteria
  32. */
  33. protected $searchCriteriaMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\FilterBuilder
  36. */
  37. protected $filterBuilderMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\Filter
  40. */
  41. protected $filterMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Model\OrderNotifier
  44. */
  45. protected $orderNotifierMock;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Model\Order
  48. */
  49. protected $orderMock;
  50. /**
  51. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Model\Order\Status\History
  52. */
  53. protected $orderStatusHistoryMock;
  54. /**
  55. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Api\Data\OrderStatusHistorySearchResultInterface
  56. */
  57. protected $orderSearchResultMock;
  58. /**
  59. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Event\ManagerInterface
  60. */
  61. protected $eventManagerMock;
  62. /**
  63. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Model\Order\Email\Sender\OrderCommentSender
  64. */
  65. protected $orderCommentSender;
  66. protected function setUp()
  67. {
  68. $this->orderRepositoryMock = $this->getMockBuilder(
  69. \Magento\Sales\Api\OrderRepositoryInterface::class
  70. )
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->orderStatusHistoryRepositoryMock = $this->getMockBuilder(
  74. \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface::class
  75. )
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. $this->searchCriteriaBuilderMock = $this->getMockBuilder(
  79. \Magento\Framework\Api\SearchCriteriaBuilder::class
  80. )
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $this->searchCriteriaMock = $this->getMockBuilder(
  84. \Magento\Framework\Api\SearchCriteria::class
  85. )
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $this->filterBuilderMock = $this->getMockBuilder(
  89. \Magento\Framework\Api\FilterBuilder::class
  90. )
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $this->filterMock = $this->getMockBuilder(
  94. \Magento\Framework\Api\Filter::class
  95. )
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. $this->orderNotifierMock = $this->getMockBuilder(
  99. \Magento\Sales\Model\OrderNotifier::class
  100. )
  101. ->disableOriginalConstructor()
  102. ->getMock();
  103. $this->orderMock = $this->getMockBuilder(
  104. \Magento\Sales\Model\Order::class
  105. )
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. $this->orderStatusHistoryMock = $this->getMockBuilder(
  109. \Magento\Sales\Model\Order\Status\History::class
  110. )
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. $this->orderSearchResultMock = $this->getMockBuilder(
  114. \Magento\Sales\Api\Data\OrderStatusHistorySearchResultInterface::class
  115. )
  116. ->disableOriginalConstructor()
  117. ->getMock();
  118. $this->eventManagerMock = $this->getMockBuilder(
  119. \Magento\Framework\Event\ManagerInterface::class
  120. )
  121. ->disableOriginalConstructor()
  122. ->getMock();
  123. $this->orderCommentSender = $this->getMockBuilder(
  124. \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender::class
  125. )
  126. ->disableOriginalConstructor()
  127. ->getMock();
  128. $this->orderService = new \Magento\Sales\Model\Service\OrderService(
  129. $this->orderRepositoryMock,
  130. $this->orderStatusHistoryRepositoryMock,
  131. $this->searchCriteriaBuilderMock,
  132. $this->filterBuilderMock,
  133. $this->orderNotifierMock,
  134. $this->eventManagerMock,
  135. $this->orderCommentSender
  136. );
  137. }
  138. /**
  139. * test for Order::cancel()
  140. */
  141. public function testCancel()
  142. {
  143. $this->orderRepositoryMock->expects($this->once())
  144. ->method('get')
  145. ->with(123)
  146. ->willReturn($this->orderMock);
  147. $this->orderMock->expects($this->once())
  148. ->method('cancel')
  149. ->willReturn($this->orderMock);
  150. $this->orderMock->expects($this->once())
  151. ->method('canCancel')
  152. ->willReturn(true);
  153. $this->assertTrue($this->orderService->cancel(123));
  154. }
  155. /**
  156. * test for Order::cancel() fail case
  157. */
  158. public function testCancelFailed()
  159. {
  160. $this->orderRepositoryMock->expects($this->once())
  161. ->method('get')
  162. ->with(123)
  163. ->willReturn($this->orderMock);
  164. $this->orderMock->expects($this->never())
  165. ->method('cancel')
  166. ->willReturn($this->orderMock);
  167. $this->orderMock->expects($this->once())
  168. ->method('canCancel')
  169. ->willReturn(false);
  170. $this->assertFalse($this->orderService->cancel(123));
  171. }
  172. public function testGetCommentsList()
  173. {
  174. $this->filterBuilderMock->expects($this->once())
  175. ->method('setField')
  176. ->with('parent_id')
  177. ->willReturnSelf();
  178. $this->filterBuilderMock->expects($this->once())
  179. ->method('setValue')
  180. ->with(123)
  181. ->willReturnSelf();
  182. $this->filterBuilderMock->expects($this->once())
  183. ->method('setConditionType')
  184. ->with('eq')
  185. ->willReturnSelf();
  186. $this->filterBuilderMock->expects($this->once())
  187. ->method('create')
  188. ->willReturn($this->filterMock);
  189. $this->searchCriteriaBuilderMock->expects($this->once())
  190. ->method('addFilters')
  191. ->with([$this->filterMock])
  192. ->willReturn($this->filterBuilderMock);
  193. $this->searchCriteriaBuilderMock->expects($this->once())
  194. ->method('create')
  195. ->willReturn($this->searchCriteriaMock);
  196. $this->orderStatusHistoryRepositoryMock->expects($this->once())
  197. ->method('getList')
  198. ->with($this->searchCriteriaMock)
  199. ->willReturn($this->orderSearchResultMock);
  200. $this->assertEquals($this->orderSearchResultMock, $this->orderService->getCommentsList(123));
  201. }
  202. public function testAddComment()
  203. {
  204. $clearComment = "Comment text here...";
  205. $this->orderRepositoryMock->expects($this->once())
  206. ->method('get')
  207. ->with(123)
  208. ->willReturn($this->orderMock);
  209. $this->orderMock->expects($this->once())
  210. ->method('addStatusHistory')
  211. ->with($this->orderStatusHistoryMock)
  212. ->willReturn($this->orderMock);
  213. $this->orderStatusHistoryMock->expects($this->once())
  214. ->method('getComment')
  215. ->willReturn("<h1>" . $clearComment);
  216. $this->orderRepositoryMock->expects($this->once())
  217. ->method('save')
  218. ->with($this->orderMock)
  219. ->willReturn([]);
  220. $this->orderCommentSender->expects($this->once())
  221. ->method('send')
  222. ->with($this->orderMock, false, $clearComment);
  223. $this->assertTrue($this->orderService->addComment(123, $this->orderStatusHistoryMock));
  224. }
  225. public function testNotify()
  226. {
  227. $this->orderRepositoryMock->expects($this->once())
  228. ->method('get')
  229. ->with(123)
  230. ->willReturn($this->orderMock);
  231. $this->orderNotifierMock->expects($this->once())
  232. ->method('notify')
  233. ->with($this->orderMock)
  234. ->willReturn(true);
  235. $this->assertTrue($this->orderService->notify(123));
  236. }
  237. public function testGetStatus()
  238. {
  239. $this->orderRepositoryMock->expects($this->once())
  240. ->method('get')
  241. ->with(123)
  242. ->willReturn($this->orderMock);
  243. $this->orderMock->expects($this->once())
  244. ->method('getStatus')
  245. ->willReturn('test-status');
  246. $this->assertEquals('test-status', $this->orderService->getStatus(123));
  247. }
  248. public function testHold()
  249. {
  250. $this->orderRepositoryMock->expects($this->once())
  251. ->method('get')
  252. ->with(123)
  253. ->willReturn($this->orderMock);
  254. $this->orderRepositoryMock->expects($this->once())
  255. ->method('save')
  256. ->with($this->orderMock)
  257. ->willReturn($this->orderMock);
  258. $this->orderMock->expects($this->once())
  259. ->method('hold')
  260. ->willReturn($this->orderMock);
  261. $this->assertTrue($this->orderService->hold(123));
  262. }
  263. public function testUnHold()
  264. {
  265. $this->orderRepositoryMock->expects($this->once())
  266. ->method('get')
  267. ->with(123)
  268. ->willReturn($this->orderMock);
  269. $this->orderRepositoryMock->expects($this->once())
  270. ->method('save')
  271. ->with($this->orderMock)
  272. ->willReturn($this->orderMock);
  273. $this->orderMock->expects($this->once())
  274. ->method('unHold')
  275. ->willReturn($this->orderMock);
  276. $this->assertTrue($this->orderService->unHold(123));
  277. }
  278. }