OrderStateServiceTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Test\Unit\Model;
  7. use Magento\Sales\Api\OrderManagementInterface;
  8. use Magento\Sales\Model\Order;
  9. use Magento\Sales\Model\OrderFactory;
  10. use Magento\Signifyd\Api\Data\CaseInterface;
  11. use Magento\Signifyd\Model\CommentsHistoryUpdater;
  12. use Magento\Signifyd\Model\OrderStateService;
  13. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  14. class OrderStateServiceTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var int
  18. */
  19. private static $orderId = 123;
  20. /**
  21. * @var OrderFactory|MockObject
  22. */
  23. private $orderFactory;
  24. /**
  25. * @var OrderManagementInterface|MockObject
  26. */
  27. private $orderManagement;
  28. /**
  29. * @var CommentsHistoryUpdater|MockObject
  30. */
  31. private $commentsHistoryUpdater;
  32. /**
  33. * @var CaseInterface|MockObject
  34. */
  35. private $caseEntity;
  36. /**
  37. * @var Order|MockObject
  38. */
  39. private $order;
  40. /**
  41. * @var OrderStateService
  42. */
  43. private $orderStateService;
  44. /**
  45. * @inheritdoc
  46. */
  47. protected function setUp()
  48. {
  49. $this->orderManagement = $this->getMockBuilder(OrderManagementInterface::class)
  50. ->getMockForAbstractClass();
  51. $this->commentsHistoryUpdater = $this->getMockBuilder(CommentsHistoryUpdater::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->orderFactory = $this->getMockBuilder(OrderFactory::class)
  55. ->setMethods(['create'])
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->order = $this->getMockBuilder(Order::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->order->expects($this->once())
  62. ->method('load')
  63. ->willReturnSelf();
  64. $this->orderFactory->expects($this->once())
  65. ->method('create')
  66. ->willReturn($this->order);
  67. $this->caseEntity = $this->getMockBuilder(CaseInterface::class)
  68. ->disableOriginalConstructor()
  69. ->getMockForAbstractClass();
  70. $this->caseEntity->expects($this->once())
  71. ->method('getOrderId')
  72. ->willReturn(self::$orderId);
  73. $this->orderStateService = new OrderStateService(
  74. $this->orderFactory,
  75. $this->orderManagement,
  76. $this->commentsHistoryUpdater
  77. );
  78. }
  79. /**
  80. * Tests update order state flow when case guarantee disposition is PENDING.
  81. *
  82. * @param bool $canHold
  83. * @param bool $hold
  84. * @param int $addCommentCall
  85. * @dataProvider updateByCaseWithGuaranteePendingDataProvider
  86. */
  87. public function testUpdateByCaseWithGuaranteePending($canHold, $hold, $addCommentCall)
  88. {
  89. $this->caseEntity->expects($this->once())
  90. ->method('getGuaranteeDisposition')
  91. ->willReturn(CaseInterface::GUARANTEE_PENDING);
  92. $this->order->expects($this->any())
  93. ->method('canHold')
  94. ->willReturn($canHold);
  95. $this->orderManagement->expects($this->any())
  96. ->method('hold')
  97. ->willReturn($hold);
  98. $this->commentsHistoryUpdater->expects($this->exactly($addCommentCall))
  99. ->method('addComment')
  100. ->with(
  101. $this->caseEntity,
  102. __('Awaiting the Signifyd guarantee disposition.'),
  103. Order::STATE_HOLDED
  104. );
  105. $this->orderStateService->updateByCase($this->caseEntity);
  106. }
  107. /**
  108. * @return array
  109. */
  110. public function updateByCaseWithGuaranteePendingDataProvider()
  111. {
  112. return [
  113. ['canHold' => true, 'hold' => true, 'addCommentCall' => 1],
  114. ['canHold' => false, 'hold' => true, 'addCommentCall' => 0],
  115. ['canHold' => true, 'hold' => false, 'addCommentCall' => 0],
  116. ];
  117. }
  118. /**
  119. * Tests update order state flow when case guarantee disposition is APPROVED.
  120. *
  121. * @param bool $canUnhold
  122. * @param int $unholdCall
  123. * @dataProvider updateByCaseWithGuaranteeApprovedDataProvider
  124. */
  125. public function testUpdateByCaseWithGuaranteeApproved($canUnhold, $unholdCall)
  126. {
  127. $this->caseEntity->expects($this->once())
  128. ->method('getGuaranteeDisposition')
  129. ->willReturn(CaseInterface::GUARANTEE_APPROVED);
  130. $this->order->expects($this->any())
  131. ->method('canUnhold')
  132. ->willReturn($canUnhold);
  133. $this->orderManagement->expects($this->exactly($unholdCall))
  134. ->method('unHold');
  135. $this->commentsHistoryUpdater->expects($this->never())
  136. ->method('addComment');
  137. $this->orderStateService->updateByCase($this->caseEntity);
  138. }
  139. /**
  140. * @return array
  141. */
  142. public function updateByCaseWithGuaranteeApprovedDataProvider()
  143. {
  144. return [
  145. ['canUnhold' => true, 'unholdCall' => 1],
  146. ['canUnhold' => false, 'unholdCall' => 0]
  147. ];
  148. }
  149. /**
  150. * Tests update order state flow when case guarantee disposition is DECLINED.
  151. *
  152. * @param bool $canHold
  153. * @param int $holdCall
  154. * @dataProvider updateByCaseWithGuaranteeDeclinedDataProvider
  155. */
  156. public function testUpdateByCaseWithGuaranteeDeclined($canHold, $holdCall)
  157. {
  158. $this->caseEntity->expects($this->once())
  159. ->method('getGuaranteeDisposition')
  160. ->willReturn(CaseInterface::GUARANTEE_DECLINED);
  161. $this->order->expects($this->any())
  162. ->method('canHold')
  163. ->willReturn($canHold);
  164. $this->orderManagement->expects($this->exactly($holdCall))
  165. ->method('hold');
  166. $this->commentsHistoryUpdater->expects($this->never())
  167. ->method('addComment');
  168. $this->orderStateService->updateByCase($this->caseEntity);
  169. }
  170. /**
  171. * @return array
  172. */
  173. public function updateByCaseWithGuaranteeDeclinedDataProvider()
  174. {
  175. return [
  176. ['canHold' => true, 'holdCall' => 1],
  177. ['canHold' => false, 'holdCall' => 0]
  178. ];
  179. }
  180. }