UpdatingServiceTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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\CaseServices;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Signifyd\Api\CaseRepositoryInterface;
  9. use Magento\Signifyd\Api\Data\CaseInterface;
  10. use Magento\Signifyd\Model\CaseServices\UpdatingService;
  11. use Magento\Signifyd\Model\CommentsHistoryUpdater;
  12. use Magento\Signifyd\Model\MessageGenerators\GeneratorException;
  13. use Magento\Signifyd\Model\MessageGenerators\GeneratorInterface;
  14. use Magento\Signifyd\Model\OrderStateService;
  15. use Magento\Signifyd\Model\SalesOrderGrid\OrderGridUpdater;
  16. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  17. /**
  18. * Contains tests with different negative and positive scenarios for case updating service.
  19. */
  20. class UpdatingServiceTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /**
  23. * @var UpdatingService
  24. */
  25. private $service;
  26. /**
  27. * @var ObjectManager
  28. */
  29. private $objectManager;
  30. /**
  31. * @var GeneratorInterface|MockObject
  32. */
  33. private $messageGenerator;
  34. /**
  35. * @var CaseRepositoryInterface|MockObject
  36. */
  37. private $caseRepository;
  38. /**
  39. * @var CommentsHistoryUpdater|MockObject
  40. */
  41. private $commentsHistoryUpdater;
  42. /**
  43. * @var OrderGridUpdater|MockObject
  44. */
  45. private $orderGridUpdater;
  46. /**
  47. * @var OrderStateService|MockObject
  48. */
  49. private $orderStateService;
  50. /**
  51. * @inheritdoc
  52. */
  53. protected function setUp()
  54. {
  55. $this->objectManager = new ObjectManager($this);
  56. $this->messageGenerator = $this->getMockBuilder(GeneratorInterface::class)
  57. ->disableOriginalConstructor()
  58. ->setMethods(['generate'])
  59. ->getMock();
  60. $this->caseRepository = $this->getMockBuilder(CaseRepositoryInterface::class)
  61. ->disableOriginalConstructor()
  62. ->setMethods(['getByCaseId'])
  63. ->getMockForAbstractClass();
  64. $this->commentsHistoryUpdater = $this->getMockBuilder(CommentsHistoryUpdater::class)
  65. ->disableOriginalConstructor()
  66. ->setMethods(['addComment'])
  67. ->getMock();
  68. $this->orderGridUpdater = $this->getMockBuilder(OrderGridUpdater::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $this->orderStateService = $this->getMockBuilder(OrderStateService::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->service = $this->objectManager->getObject(UpdatingService::class, [
  75. 'messageGenerator' => $this->messageGenerator,
  76. 'caseRepository' => $this->caseRepository,
  77. 'commentsHistoryUpdater' => $this->commentsHistoryUpdater,
  78. 'orderGridUpdater' => $this->orderGridUpdater,
  79. 'orderStateService' => $this->orderStateService
  80. ]);
  81. }
  82. /**
  83. * Checks a test case when Signifyd case is empty entity.
  84. *
  85. * @covers \Magento\Signifyd\Model\CaseServices\UpdatingService::update
  86. * @expectedException \Magento\Framework\Exception\LocalizedException
  87. * @expectedExceptionMessage The case entity should not be empty.
  88. */
  89. public function testUpdateWithEmptyCaseEntity()
  90. {
  91. $data = [];
  92. $caseEntity = $this->withCaseEntity(null, 123, $data);
  93. $this->service->update($caseEntity, $data);
  94. }
  95. /**
  96. * Checks a test case when Signifyd case id is not specified for a case entity.
  97. *
  98. * @covers \Magento\Signifyd\Model\CaseServices\UpdatingService::update
  99. * @expectedException \Magento\Framework\Exception\LocalizedException
  100. * @expectedExceptionMessage The case entity should not be empty.
  101. */
  102. public function testUpdateWithEmptyCaseId()
  103. {
  104. $data = [
  105. 'caseId' => 123
  106. ];
  107. $caseEntity = $this->withCaseEntity(1, null, $data);
  108. $this->service->update($caseEntity, $data);
  109. }
  110. /**
  111. * Checks as test case when service cannot save Signifyd case entity
  112. *
  113. * @covers \Magento\Signifyd\Model\CaseServices\UpdatingService::update
  114. * @expectedException \Magento\Framework\Exception\LocalizedException
  115. * @expectedExceptionMessage Cannot update Case entity.
  116. */
  117. public function testUpdateWithFailedCaseSaving()
  118. {
  119. $caseId = 123;
  120. $data = [
  121. 'caseId' => $caseId,
  122. 'status' => CaseInterface::STATUS_OPEN,
  123. 'orderId' => '10000012',
  124. 'score' => 500
  125. ];
  126. $caseEntity = $this->withCaseEntity(1, $caseId, $data);
  127. $this->caseRepository->expects(self::once())
  128. ->method('save')
  129. ->willThrowException(new \Exception('Something wrong.'));
  130. $this->service->update($caseEntity, $data);
  131. }
  132. /**
  133. * Checks as test case when message generator throws an exception
  134. *
  135. * @covers \Magento\Signifyd\Model\CaseServices\UpdatingService::update
  136. * @expectedException \Magento\Framework\Exception\LocalizedException
  137. * @expectedExceptionMessage Cannot update Case entity.
  138. */
  139. public function testUpdateWithExceptionFromMessageGenerator()
  140. {
  141. $caseId = 123;
  142. $data = [
  143. 'caseId' => $caseId
  144. ];
  145. $caseEntity = $this->withCaseEntity(1, $caseId, $data);
  146. $this->caseRepository->expects(self::never())
  147. ->method('save')
  148. ->with($caseEntity)
  149. ->willReturn($caseEntity);
  150. $this->messageGenerator->expects(self::once())
  151. ->method('generate')
  152. ->with($data)
  153. ->willThrowException(new GeneratorException(__('Cannot generate message.')));
  154. $this->service->update($caseEntity, $data);
  155. }
  156. /**
  157. * Checks a test case when comments history updater throws an exception.
  158. *
  159. * @covers \Magento\Signifyd\Model\CaseServices\UpdatingService::update
  160. * @expectedException \Magento\Framework\Exception\LocalizedException
  161. * @expectedExceptionMessage Cannot update Case entity.
  162. */
  163. public function testUpdateWithFailedCommentSaving()
  164. {
  165. $caseId = 123;
  166. $data = [
  167. 'caseId' => $caseId,
  168. 'orderId' => 1
  169. ];
  170. $caseEntity = $this->withCaseEntity(1, $caseId, $data);
  171. $this->caseRepository->expects(self::once())
  172. ->method('save')
  173. ->with($caseEntity)
  174. ->willReturn($caseEntity);
  175. $this->orderGridUpdater->expects(self::once())
  176. ->method('update')
  177. ->with($data['orderId']);
  178. $message = __('Message is generated.');
  179. $this->messageGenerator->expects(self::once())
  180. ->method('generate')
  181. ->with($data)
  182. ->willReturn($message);
  183. $this->commentsHistoryUpdater->expects(self::once())
  184. ->method('addComment')
  185. ->with($caseEntity, $message)
  186. ->willThrowException(new \Exception('Something wrong'));
  187. $this->service->update($caseEntity, $data);
  188. }
  189. /**
  190. * Checks a test case when Signifyd case entity is successfully updated and message stored in comments history.
  191. *
  192. * @covers \Magento\Signifyd\Model\CaseServices\UpdatingService::update
  193. */
  194. public function testUpdate()
  195. {
  196. $caseId = 123;
  197. $data = [
  198. 'caseId' => $caseId,
  199. 'orderId' => 1
  200. ];
  201. $caseEntity = $this->withCaseEntity(21, $caseId, $data);
  202. $caseEntitySaved = clone $caseEntity;
  203. $caseEntitySaved->expects(self::once())
  204. ->method('getGuaranteeDisposition')
  205. ->willReturn('APPROVED');
  206. $this->caseRepository->expects(self::once())
  207. ->method('save')
  208. ->with($caseEntity)
  209. ->willReturn($caseEntitySaved);
  210. $message = __('Message is generated.');
  211. $this->messageGenerator->expects(self::once())
  212. ->method('generate')
  213. ->with($data)
  214. ->willReturn($message);
  215. $this->orderGridUpdater->expects(self::once())
  216. ->method('update')
  217. ->with($data['orderId']);
  218. $this->commentsHistoryUpdater->expects(self::once())
  219. ->method('addComment')
  220. ->with($caseEntitySaved, $message);
  221. $this->orderStateService->expects(self::once())
  222. ->method('updateByCase')
  223. ->with($caseEntitySaved);
  224. $this->service->update($caseEntity, $data);
  225. }
  226. /**
  227. * Create mock for case entity with common scenarios.
  228. *
  229. * @param $caseEntityId
  230. * @param $caseId
  231. * @param array $data
  232. * @return CaseInterface|MockObject
  233. */
  234. private function withCaseEntity($caseEntityId, $caseId, array $data = [])
  235. {
  236. /** @var CaseInterface|MockObject $caseEntity */
  237. $caseEntity = $this->getMockBuilder(CaseInterface::class)
  238. ->disableOriginalConstructor()
  239. ->setMethods([
  240. 'getEntityId', 'getCaseId', 'getOrderId',
  241. 'setCaseId', 'setStatus', 'setOrderId', 'setScore'
  242. ])
  243. ->getMockForAbstractClass();
  244. $caseEntity->expects(self::any())
  245. ->method('getEntityId')
  246. ->willReturn($caseEntityId);
  247. $caseEntity->expects(self::any())
  248. ->method('getCaseId')
  249. ->willReturn($caseId);
  250. foreach ($data as $property => $value) {
  251. $method = 'set' . ucfirst($property);
  252. if ($property === 'orderId') {
  253. $caseEntity->expects(self::never())
  254. ->method($method);
  255. }
  256. $caseEntity->expects(self::any())
  257. ->method($method)
  258. ->with(self::equalTo($value))
  259. ->willReturnSelf();
  260. $method = 'get' . ucfirst($property);
  261. $caseEntity->expects(self::any())
  262. ->method($method)
  263. ->willReturn($value);
  264. }
  265. return $caseEntity;
  266. }
  267. }