From 52c2e9d6f10cbe633a21ec3b15a17add63eb64df Mon Sep 17 00:00:00 2001 From: khushboos Date: Wed, 30 Oct 2024 14:52:18 +0100 Subject: [PATCH] Adding Unit Test for OrderClosedWebhookNotification --- .../Webhook/OrderClosedWebhookHandlerTest.php | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 Test/Unit/Helper/Webhook/OrderClosedWebhookHandlerTest.php diff --git a/Test/Unit/Helper/Webhook/OrderClosedWebhookHandlerTest.php b/Test/Unit/Helper/Webhook/OrderClosedWebhookHandlerTest.php new file mode 100644 index 0000000000..b5d1168053 --- /dev/null +++ b/Test/Unit/Helper/Webhook/OrderClosedWebhookHandlerTest.php @@ -0,0 +1,112 @@ +adyenOrderPaymentHelperMock = $this->createMock(AdyenOrderPayment::class); + $this->orderHelperMock = $this->createMock(OrderHelper::class); + $this->configHelperMock = $this->createMock(Config::class); + $this->adyenLoggerMock = $this->createMock(AdyenLogger::class); + $this->adyenOrderPaymentCollectionFactoryMock = $this->createMock(OrderPaymentCollectionFactory::class); + + $this->orderClosedWebhookHandler = new OrderClosedWebhookHandler( + $this->adyenOrderPaymentHelperMock, + $this->orderHelperMock, + $this->configHelperMock, + $this->adyenOrderPaymentCollectionFactoryMock, + $this->adyenLoggerMock + ); + } + + public function testHandleWebhookSuccessfulNotificationWithMatchingAdyenOrderPayment() + { + $orderMock = $this->createMock(MagentoOrder::class); + $notificationMock = $this->createMock(Notification::class); + //$adyenOrderPaymentMock = $this->createMock(OrderPaymentInterface::class); + $adyenOrderPaymentMock = $this->createConfiguredMock(Order\Payment::class, [ + 'getId' => 123 + ]); + $adyenOrderPaymentCollectionMock = $this->createMock(\Magento\Framework\Data\Collection\AbstractDb::class); + + $notificationMock->expects($this->once())->method('isSuccessful')->willReturn(true); + $notificationMock->expects($this->once())->method('getAdditionalData')->willReturn(serialize(['order-1-pspReference' => 'testPspReference'])); + + $adyenOrderPaymentCollectionMock->expects($this->once())->method('addFieldToFilter')->willReturnSelf(); + $adyenOrderPaymentCollectionMock->expects($this->once())->method('getFirstItem')->willReturn($adyenOrderPaymentMock); + + $this->adyenOrderPaymentCollectionFactoryMock->expects($this->once())->method('create')->willReturn($adyenOrderPaymentCollectionMock); + + $this->adyenLoggerMock->expects($this->once())->method('addAdyenNotification') + ->with('Updated adyen_order_payment with order status 1 for pspReference testPspReference', [ + 'pspReference' => 'testPspReference', + 'status' => 1, + 'merchantReference' => $notificationMock->getMerchantReference() + ]); + + $orderMock->expects($this->once())->method('addCommentToStatusHistory') + ->with(__('This order has been successfully completed.')); + + $result = $this->orderClosedWebhookHandler->handleWebhook($orderMock, $notificationMock, 'completed'); + $this->assertSame($orderMock, $result); + } + + public function testHandleWebhookUnsuccessfulNotificationWithRefund() + { + $orderMock = $this->createMock(MagentoOrder::class); + $notificationMock = $this->createMock(Notification::class); + $adyenOrderPaymentMock = $this->createMock(OrderPaymentInterface::class); + $adyenOrderPaymentCollectionMock = $this->createMock(\Magento\Framework\Data\Collection\AbstractDb::class); + + $notificationMock->expects($this->once())->method('isSuccessful')->willReturn(false); + + $orderMock->expects($this->once())->method('getPayment')->willReturn( + $this->createConfiguredMock(MagentoOrderPaymentInterface::class, ['getEntityId' => 123]) + ); + + $adyenOrderPaymentCollectionMock->method('addFieldToFilter') + ->willReturnSelf(); + + $adyenOrderPaymentCollectionMock->expects($this->once())->method('getItems')->willReturn([$adyenOrderPaymentMock]); + + $this->adyenOrderPaymentCollectionFactoryMock->expects($this->once())->method('create')->willReturn($adyenOrderPaymentCollectionMock); + + $this->adyenOrderPaymentHelperMock->expects($this->once())->method('refundFullyAdyenOrderPayment')->with($adyenOrderPaymentMock); + + $orderMock->expects($this->once())->method('addCommentToStatusHistory') + ->with(__('All the funds captured/settled will be refunded by Adyen.')); + + $this->adyenLoggerMock->expects($this->once())->method('addAdyenNotification') + ->with('All the funds captured/settled will be refunded by Adyen.', [ + 'pspReference' => $notificationMock->getPspreference(), + 'merchantReference' => $notificationMock->getMerchantReference() + ]); + + $this->orderHelperMock->method('holdCancelOrder')->with($orderMock, true); + + $this->configHelperMock->method('getNotificationsCanCancel')->with($orderMock->getStoreId())->willReturn(true); + + $result = $this->orderClosedWebhookHandler->handleWebhook($orderMock, $notificationMock, 'completed'); + $this->assertSame($orderMock, $result); + } +}