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