CreateGuaranteeAbilityTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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\Guarantee;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Framework\Intl\DateTimeFactory;
  9. use Magento\Sales\Api\Data\OrderInterface;
  10. use Magento\Sales\Api\OrderRepositoryInterface;
  11. use Magento\Sales\Model\Order;
  12. use Magento\Signifyd\Api\Data\CaseInterface;
  13. use Magento\Signifyd\Model\CaseManagement;
  14. use Magento\Signifyd\Model\Guarantee\CreateGuaranteeAbility;
  15. /**
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. */
  18. class CreateGuaranteeAbilityTest extends \PHPUnit\Framework\TestCase
  19. {
  20. /**
  21. * @var DateTimeFactory
  22. */
  23. private $dateTimeFactory;
  24. /**
  25. * @var OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $orderRepository;
  28. /**
  29. * @var CaseManagement|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $caseManagement;
  32. /**
  33. * @var CreateGuaranteeAbility
  34. */
  35. private $createGuaranteeAbility;
  36. /**
  37. * @inheritdoc
  38. */
  39. protected function setUp()
  40. {
  41. $this->dateTimeFactory = new DateTimeFactory();
  42. $this->orderRepository = $this->getMockBuilder(OrderRepositoryInterface::class)
  43. ->getMockForAbstractClass();
  44. $this->caseManagement = $this->getMockBuilder(CaseManagement::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->createGuaranteeAbility = new CreateGuaranteeAbility(
  48. $this->caseManagement,
  49. $this->orderRepository,
  50. $this->dateTimeFactory
  51. );
  52. }
  53. public function testIsAvailableSuccess()
  54. {
  55. $orderId = 123;
  56. $orderCreatedAt = $this->getDateAgo(6);
  57. /** @var CaseInterface|\PHPUnit_Framework_MockObject_MockObject $case */
  58. $case = $this->getMockBuilder(CaseInterface::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $case->expects($this->once())
  62. ->method('isGuaranteeEligible')
  63. ->willReturn(true);
  64. $this->caseManagement->expects($this->once())
  65. ->method('getByOrderId')
  66. ->with($orderId)
  67. ->willReturn($case);
  68. /** @var OrderInterface|\PHPUnit_Framework_MockObject_MockObject $order */
  69. $order = $this->getMockBuilder(OrderInterface::class)
  70. ->getMockForAbstractClass();
  71. $order->expects($this->once())
  72. ->method('getState')
  73. ->willReturn(Order::STATE_COMPLETE);
  74. $order->expects($this->once())
  75. ->method('getCreatedAt')
  76. ->willReturn($orderCreatedAt);
  77. $this->orderRepository->expects($this->once())
  78. ->method('get')
  79. ->with($orderId)
  80. ->willReturn($order);
  81. $this->assertTrue($this->createGuaranteeAbility->isAvailable($orderId));
  82. }
  83. /**
  84. * Tests case when Case entity doesn't exist for order
  85. */
  86. public function testIsAvailableWithNullCase()
  87. {
  88. $orderId = 123;
  89. $this->caseManagement->expects($this->once())
  90. ->method('getByOrderId')
  91. ->with($orderId)
  92. ->willReturn(null);
  93. $this->assertFalse($this->createGuaranteeAbility->isAvailable($orderId));
  94. }
  95. /**
  96. * Tests case when GuaranteeEligible for Case is false
  97. */
  98. public function testIsAvailableWithGuarantyEligibleFalse()
  99. {
  100. $orderId = 123;
  101. /** @var CaseInterface|\PHPUnit_Framework_MockObject_MockObject $case */
  102. $case = $this->getMockBuilder(CaseInterface::class)
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. $case->expects($this->once())
  106. ->method('isGuaranteeEligible')
  107. ->willReturn(false);
  108. $this->caseManagement->expects($this->once())
  109. ->method('getByOrderId')
  110. ->with($orderId)
  111. ->willReturn($case);
  112. $this->assertFalse($this->createGuaranteeAbility->isAvailable($orderId));
  113. }
  114. /**
  115. * Tests case when GuaranteeEligible for Case is false
  116. */
  117. public function testIsAvailableWithNullOrder()
  118. {
  119. $orderId = 123;
  120. /** @var CaseInterface|\PHPUnit_Framework_MockObject_MockObject $case */
  121. $case = $this->getMockBuilder(CaseInterface::class)
  122. ->disableOriginalConstructor()
  123. ->getMock();
  124. $case->expects($this->once())
  125. ->method('isGuaranteeEligible')
  126. ->willReturn(true);
  127. $this->caseManagement->expects($this->once())
  128. ->method('getByOrderId')
  129. ->with($orderId)
  130. ->willReturn($case);
  131. $this->orderRepository->expects($this->once())
  132. ->method('get')
  133. ->with($orderId)
  134. ->willThrowException(new NoSuchEntityException());
  135. $this->assertFalse($this->createGuaranteeAbility->isAvailable($orderId));
  136. }
  137. /**
  138. * Tests case when order has Canceled Or Closed states.
  139. *
  140. * @param string $state
  141. * @dataProvider isAvailableWithCanceledOrderDataProvider
  142. */
  143. public function testIsAvailableWithCanceledOrder($state)
  144. {
  145. $orderId = 123;
  146. /** @var CaseInterface|\PHPUnit_Framework_MockObject_MockObject $case */
  147. $case = $this->getMockBuilder(CaseInterface::class)
  148. ->disableOriginalConstructor()
  149. ->getMock();
  150. $case->expects($this->once())
  151. ->method('isGuaranteeEligible')
  152. ->willReturn(true);
  153. $this->caseManagement->expects($this->once())
  154. ->method('getByOrderId')
  155. ->with($orderId)
  156. ->willReturn($case);
  157. /** @var OrderInterface|\PHPUnit_Framework_MockObject_MockObject $order */
  158. $order = $this->getMockBuilder(OrderInterface::class)
  159. ->getMockForAbstractClass();
  160. $order->expects($this->once())
  161. ->method('getState')
  162. ->willReturn($state);
  163. $this->orderRepository->expects($this->once())
  164. ->method('get')
  165. ->with($orderId)
  166. ->willReturn($order);
  167. $this->assertFalse($this->createGuaranteeAbility->isAvailable($orderId));
  168. }
  169. /**
  170. * @return array
  171. */
  172. public function isAvailableWithCanceledOrderDataProvider()
  173. {
  174. return [
  175. [Order::STATE_CANCELED], [Order::STATE_CLOSED]
  176. ];
  177. }
  178. public function testIsAvailableWithOldOrder()
  179. {
  180. $orderId = 123;
  181. $orderCreatedAt = $this->getDateAgo(8);
  182. /** @var CaseInterface|\PHPUnit_Framework_MockObject_MockObject $case */
  183. $case = $this->getMockBuilder(CaseInterface::class)
  184. ->disableOriginalConstructor()
  185. ->getMock();
  186. $case->expects($this->once())
  187. ->method('isGuaranteeEligible')
  188. ->willReturn(true);
  189. $this->caseManagement->expects($this->once())
  190. ->method('getByOrderId')
  191. ->with($orderId)
  192. ->willReturn($case);
  193. /** @var OrderInterface|\PHPUnit_Framework_MockObject_MockObject $order */
  194. $order = $this->getMockBuilder(OrderInterface::class)
  195. ->getMockForAbstractClass();
  196. $order->expects($this->once())
  197. ->method('getState')
  198. ->willReturn(Order::STATE_COMPLETE);
  199. $order->expects($this->once())
  200. ->method('getCreatedAt')
  201. ->willReturn($orderCreatedAt);
  202. $this->orderRepository->expects($this->once())
  203. ->method('get')
  204. ->with($orderId)
  205. ->willReturn($order);
  206. $this->assertFalse($this->createGuaranteeAbility->isAvailable($orderId));
  207. }
  208. /**
  209. * Returns date N days ago
  210. *
  211. * @param int $days number of days that will be deducted from the current date
  212. * @return string
  213. */
  214. private function getDateAgo($days)
  215. {
  216. $createdAtTime = $this->dateTimeFactory->create('now', new \DateTimeZone('UTC'));
  217. $createdAtTime->sub(new \DateInterval('P' . $days . 'D'));
  218. return $createdAtTime->format('Y-m-d h:i:s');
  219. }
  220. }