HandlerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Controller\Webhooks;
  7. use Magento\TestFramework\TestCase\AbstractController;
  8. use Magento\Signifyd\Model\SignifydGateway\Response\WebhookRequest;
  9. use Magento\Signifyd\Api\CaseRepositoryInterface;
  10. use Magento\Signifyd\Api\Data\CaseInterface;
  11. use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
  12. use Magento\Sales\Api\OrderRepositoryInterface;
  13. /**
  14. * Class tests handling webhook post from Signifyd service.
  15. */
  16. class HandlerTest extends AbstractController
  17. {
  18. /**
  19. * @var string
  20. */
  21. private static $entryPoint = 'signifyd/webhooks/handler';
  22. /**
  23. * Tests handling webhook message of cases/rescore type.
  24. * Checks updated case entity and comment in order history.
  25. *
  26. * @covers \Magento\Signifyd\Controller\Webhooks\Handler::execute
  27. * @magentoConfigFixture current_store fraud_protection/signifyd/active 1
  28. * @magentoConfigFixture current_store fraud_protection/signifyd/api_key ApFZZvxGgIxuP8BazSm3v8eGN
  29. * @magentoDataFixture Magento/Signifyd/_files/case.php
  30. */
  31. public function testExecuteSuccess()
  32. {
  33. $caseId = 123;
  34. $webhookRequest = $this->getWebhookRequest();
  35. $this->_objectManager->addSharedInstance($webhookRequest, WebhookRequest::class);
  36. $this->dispatch(self::$entryPoint);
  37. /** @var CaseRepositoryInterface $caseManagement */
  38. $caseRepository = $this->_objectManager->get(CaseRepositoryInterface::class);
  39. /** @var CaseInterface $caseEntity */
  40. $caseEntity = $caseRepository->getByCaseId($caseId);
  41. $orderEntityId = $caseEntity->getOrderId();
  42. self::assertNotEmpty($caseEntity);
  43. self::assertEquals('2017-01-06 12:47:03', $caseEntity->getCreatedAt());
  44. self::assertEquals('2017-01-06 12:47:03', $caseEntity->getUpdatedAt());
  45. self::assertEquals('Magento', $caseEntity->getAssociatedTeam()['teamName']);
  46. self::assertEquals(true, $caseEntity->isGuaranteeEligible());
  47. self::assertEquals(CaseInterface::STATUS_OPEN, $caseEntity->getStatus());
  48. self::assertEquals($orderEntityId, $caseEntity->getOrderId());
  49. /** @var OrderRepositoryInterface $orderRepository */
  50. $orderRepository = $this->_objectManager->get(OrderRepositoryInterface::class);
  51. $order = $orderRepository->get($caseEntity->getOrderId());
  52. $histories = $order->getStatusHistories();
  53. self::assertNotEmpty($histories);
  54. /** @var OrderStatusHistoryInterface $caseCreationComment */
  55. $caseComment = array_pop($histories);
  56. self::assertInstanceOf(OrderStatusHistoryInterface::class, $caseComment);
  57. self::assertEquals(
  58. "Case Update: New score for the order is 384. Previous score was 553.",
  59. $caseComment->getComment()
  60. );
  61. $this->_objectManager->removeSharedInstance(WebhookRequest::class);
  62. }
  63. /**
  64. * Tests handling webhook message of cases/test type.
  65. * Controller should response with code 200.
  66. *
  67. * @covers \Magento\Signifyd\Controller\Webhooks\Handler::execute
  68. * @magentoConfigFixture current_store fraud_protection/signifyd/active 1
  69. */
  70. public function testExecuteTestSuccess()
  71. {
  72. $webhookRequest = $this->getTestWebhookRequest();
  73. $this->_objectManager->addSharedInstance($webhookRequest, WebhookRequest::class);
  74. $this->dispatch(self::$entryPoint);
  75. $this->assertEquals(200, $this->getResponse()->getHttpResponseCode());
  76. $this->_objectManager->removeSharedInstance(WebhookRequest::class);
  77. }
  78. /**
  79. * Returns mocked WebhookRequest
  80. *
  81. * @return WebhookRequest|\PHPUnit\Framework\MockObject_MockObject
  82. */
  83. private function getWebhookRequest()
  84. {
  85. $webhookRequest = $this->getMockBuilder(WebhookRequest::class)
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $webhookRequest->expects($this->any())
  89. ->method('getBody')
  90. ->willReturn(file_get_contents(__DIR__ . '/../../_files/webhook_body.json'));
  91. $webhookRequest->expects($this->any())
  92. ->method('getEventTopic')
  93. ->willReturn('cases/rescore');
  94. $webhookRequest->expects($this->any())
  95. ->method('getHash')
  96. ->willReturn('m/X29RcHWPSCDPgQuSXjnyTfKISJDopcdGbVsRLeqy8=');
  97. return $webhookRequest;
  98. }
  99. /**
  100. * Returns mocked test WebhookRequest
  101. *
  102. * @return WebhookRequest|\PHPUnit\Framework\MockObject_MockObject
  103. */
  104. private function getTestWebhookRequest()
  105. {
  106. $webhookRequest = $this->getMockBuilder(WebhookRequest::class)
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $webhookRequest->expects($this->any())
  110. ->method('getBody')
  111. ->willReturn(file_get_contents(__DIR__ . '/../../_files/webhook_body.json'));
  112. $webhookRequest->expects($this->any())
  113. ->method('getEventTopic')
  114. ->willReturn('cases/test');
  115. $webhookRequest->expects($this->any())
  116. ->method('getHash')
  117. ->willReturn('wyG0r9mOmv1IqVlN6ZqJ5sgA635yKW6lbSsqlYF2b8U=');
  118. return $webhookRequest;
  119. }
  120. }