CreationServiceTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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\Signifyd\Api\CaseManagementInterface;
  8. use Magento\Signifyd\Api\Data\CaseInterface;
  9. use Magento\Signifyd\Model\CaseServices\UpdatingServiceFactory;
  10. use Magento\Signifyd\Model\CaseServices\UpdatingServiceInterface;
  11. use Magento\Signifyd\Model\Guarantee\CreateGuaranteeAbility;
  12. use Magento\Signifyd\Model\Guarantee\CreationService;
  13. use Magento\Signifyd\Model\SignifydGateway\Gateway;
  14. use Magento\Signifyd\Model\SignifydGateway\GatewayException;
  15. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  16. use \PHPUnit\Framework\TestCase as TestCase;
  17. use Psr\Log\LoggerInterface;
  18. class CreationServiceTest extends TestCase
  19. {
  20. /**
  21. * @var CreationService|MockObject
  22. */
  23. private $service;
  24. /**
  25. * @var CaseManagementInterface|MockObject
  26. */
  27. private $caseManagement;
  28. /**
  29. * @var UpdatingServiceInterface|MockObject
  30. */
  31. private $caseUpdatingService;
  32. /**
  33. * @var Gateway|MockObject
  34. */
  35. private $gateway;
  36. /**
  37. * @var LoggerInterface|MockObject
  38. */
  39. private $logger;
  40. /**
  41. * @var CreateGuaranteeAbility|MockObject
  42. */
  43. private $createGuaranteeAbility;
  44. /**
  45. * @inheritdoc
  46. */
  47. protected function setUp()
  48. {
  49. $this->caseManagement = $this->getMockBuilder(CaseManagementInterface::class)
  50. ->getMockForAbstractClass();
  51. $caseUpdatingServiceFactory = $this->getMockBuilder(UpdatingServiceFactory::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->caseUpdatingService = $this->getMockBuilder(UpdatingServiceInterface::class)
  55. ->getMockForAbstractClass();
  56. $caseUpdatingServiceFactory
  57. ->method('create')
  58. ->willReturn($this->caseUpdatingService);
  59. $this->gateway = $this->getMockBuilder(Gateway::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->createGuaranteeAbility = $this->getMockBuilder(CreateGuaranteeAbility::class)
  63. ->disableOriginalConstructor()
  64. ->setMethods(['isAvailable'])
  65. ->getMock();
  66. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  67. ->getMockForAbstractClass();
  68. $this->service = new CreationService(
  69. $this->caseManagement,
  70. $caseUpdatingServiceFactory,
  71. $this->gateway,
  72. $this->createGuaranteeAbility,
  73. $this->logger
  74. );
  75. }
  76. /**
  77. * Checks a test case, when guarantee ability checker does not allow to submit case for a guarantee.
  78. *
  79. * @covers \Magento\Signifyd\Model\Guarantee\CreationService::createForOrder
  80. */
  81. public function testCreateForOrderWithNotEligibleCase()
  82. {
  83. $orderId = 1;
  84. $this->createGuaranteeAbility->expects(self::once())
  85. ->method('isAvailable')
  86. ->with($orderId)
  87. ->willReturn(false);
  88. $this->caseManagement->expects(self::never())
  89. ->method('getByOrderId');
  90. $this->gateway->expects(self::never())
  91. ->method('submitCaseForGuarantee');
  92. $result = $this->service->createForOrder($orderId);
  93. self::assertFalse($result);
  94. }
  95. public function testCreateForOrderWitCase()
  96. {
  97. $dummyOrderId = 1;
  98. $dummyCaseId = 42;
  99. $this->withCaseEntityExistsForOrderId(
  100. $dummyOrderId,
  101. [
  102. 'caseId' => $dummyCaseId,
  103. ]
  104. );
  105. $this->gateway
  106. ->expects($this->once())
  107. ->method('submitCaseForGuarantee');
  108. $this->service->createForOrder($dummyOrderId);
  109. }
  110. public function testCreateForOrderWithGatewayFailure()
  111. {
  112. $dummyOrderId = 1;
  113. $dummyCaseId = 42;
  114. $dummyGatewayFailureMessage = 'Everything fails sometimes';
  115. $this->withCaseEntityExistsForOrderId(
  116. $dummyOrderId,
  117. [
  118. 'caseId' => $dummyCaseId,
  119. ]
  120. );
  121. $this->withGatewayFailure($dummyGatewayFailureMessage);
  122. $this->logger
  123. ->expects($this->once())
  124. ->method('error')
  125. ->with($this->equalTo($dummyGatewayFailureMessage));
  126. $this->caseUpdatingService
  127. ->expects($this->never())
  128. ->method('update');
  129. $result = $this->service->createForOrder($dummyOrderId);
  130. $this->assertEquals(
  131. false,
  132. $result,
  133. 'Service should return false in case of gateway failure'
  134. );
  135. }
  136. public function testCreateForOrderWithGatewaySuccess()
  137. {
  138. $dummyOrderId = 1;
  139. $dummyCaseId = 42;
  140. $dummyGuaranteeDisposition = 'foo';
  141. $caseEntity = $this->withCaseEntityExistsForOrderId(
  142. $dummyOrderId,
  143. [
  144. 'caseId' => $dummyCaseId,
  145. ]
  146. );
  147. $this->withGatewaySuccess($dummyGuaranteeDisposition);
  148. $this->caseUpdatingService
  149. ->expects($this->once())
  150. ->method('update')
  151. ->with($caseEntity, $this->equalTo([
  152. 'caseId' => $dummyCaseId,
  153. 'guaranteeDisposition' => $dummyGuaranteeDisposition,
  154. ]));
  155. $this->service->createForOrder($dummyOrderId);
  156. }
  157. public function testCreateForOrderWithCaseUpdate()
  158. {
  159. $dummyOrderId = 1;
  160. $dummyCaseId = 42;
  161. $dummyGuaranteeDisposition = 'foo';
  162. $this->withCaseEntityExistsForOrderId(
  163. $dummyOrderId,
  164. [
  165. 'caseId' => $dummyCaseId,
  166. ]
  167. );
  168. $this->withGatewaySuccess($dummyGuaranteeDisposition);
  169. $result = $this->service->createForOrder($dummyOrderId);
  170. $this->assertEquals(
  171. true,
  172. $result,
  173. 'Service should return true in case if case update service is called'
  174. );
  175. }
  176. /**
  177. * @param $orderId
  178. * @param array $caseData
  179. * @return MockObject
  180. */
  181. private function withCaseEntityExistsForOrderId($orderId, array $caseData = [])
  182. {
  183. $this->createGuaranteeAbility->expects(self::once())
  184. ->method('isAvailable')
  185. ->with(self::equalTo($orderId))
  186. ->willReturn(true);
  187. $dummyCaseEntity = $this->getMockBuilder(CaseInterface::class)
  188. ->getMockForAbstractClass();
  189. foreach ($caseData as $caseProperty => $casePropertyValue) {
  190. $dummyCaseEntity
  191. ->method('get' . ucfirst($caseProperty))
  192. ->willReturn($casePropertyValue);
  193. }
  194. $this->caseManagement
  195. ->method('getByOrderId')
  196. ->with($this->equalTo($orderId))
  197. ->willReturn($dummyCaseEntity);
  198. return $dummyCaseEntity;
  199. }
  200. /**
  201. * @param $failureMessage
  202. */
  203. private function withGatewayFailure($failureMessage)
  204. {
  205. $this->gateway
  206. ->method('submitCaseForGuarantee')
  207. ->willThrowException(new GatewayException($failureMessage));
  208. }
  209. /**
  210. * @param $gatewayResult
  211. */
  212. private function withGatewaySuccess($gatewayResult)
  213. {
  214. $this->gateway
  215. ->method('submitCaseForGuarantee')
  216. ->willReturn($gatewayResult);
  217. }
  218. }