Skip to content

Commit

Permalink
Updating to serializer in unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
khushboo-singhvi committed Oct 30, 2024
1 parent 27dbd8f commit c6afd0b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
6 changes: 4 additions & 2 deletions Helper/Webhook/OrderClosedWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ public function handleWebhook(
string $transitionState
): MagentoOrder {
$additionalData = $notification->getAdditionalData();
if (is_string($additionalData)) {
if (!empty($additionalData)) {
$additionalData = $this->serializer->unserialize($additionalData);
}

if ($notification->isSuccessful()) {
foreach ($additionalData as $key => $value) {
// Check if the key matches the pattern "order-X-pspReference"
if (preg_match('/^order-(\d+)-pspReference$/', $key, $matches)) {
$orderIndex = (int) $matches[1]; // Get the order number, e.g., 1, 2
$orderIndex = (int)$matches[1]; // Get the order number, e.g., 1, 2
$pspReference = $value;
$sortValue = $orderIndex; // Set status based on order index

Expand Down Expand Up @@ -121,6 +122,7 @@ public function handleWebhook(
}
}
}

$order->addCommentToStatusHistory(__('This order has been successfully completed.'));
} else {
/** @var OrderPaymentInterface $orderPayment */
Expand Down
26 changes: 18 additions & 8 deletions Test/Unit/Helper/Webhook/OrderClosedWebhookHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Magento\Framework\Serialize\SerializerInterface;
use PHPUnit\Framework\TestCase;
use Adyen\Payment\Helper\Webhook\OrderClosedWebhookHandler;
use Adyen\Payment\Helper\AdyenOrderPayment;
Expand All @@ -21,6 +22,7 @@ class OrderClosedWebhookHandlerTest extends TestCase
private $configHelperMock;
private $adyenLoggerMock;
private $adyenOrderPaymentCollectionFactoryMock;
private $serializerMock;

protected function setUp(): void
{
Expand All @@ -29,35 +31,43 @@ protected function setUp(): void
$this->configHelperMock = $this->createMock(Config::class);
$this->adyenLoggerMock = $this->createMock(AdyenLogger::class);
$this->adyenOrderPaymentCollectionFactoryMock = $this->createMock(OrderPaymentCollectionFactory::class);
$this->serializerMock = $this->createMock(SerializerInterface::class);

$this->orderClosedWebhookHandler = new OrderClosedWebhookHandler(
$this->adyenOrderPaymentHelperMock,
$this->orderHelperMock,
$this->configHelperMock,
$this->adyenOrderPaymentCollectionFactoryMock,
$this->adyenLoggerMock
$this->adyenLoggerMock,
$this->serializerMock
);
}

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);

$additionalDataString = 'a:1:{s:20:"order-1-pspReference";s:16:"testPspReference";}';
$additionalData = array (
'order-1-pspReference' => 'testPspReference',
);
$notificationMock->expects($this->once())->method('isSuccessful')->willReturn(true);
$notificationMock->expects($this->once())->method('getAdditionalData')->willReturn(serialize(['order-1-pspReference' => 'testPspReference']));
$notificationMock->expects($this->once())->method('getAdditionalData')->willReturn($additionalDataString);
$this->serializerMock
->method('unserialize')
->with($additionalDataString)
->willReturn($additionalData);

$adyenOrderPaymentCollectionMock->expects($this->once())->method('addFieldToFilter')->willReturnSelf();
$adyenOrderPaymentCollectionMock->expects($this->once())->method('getFirstItem')->willReturn($adyenOrderPaymentMock);
$adyenOrderPaymentCollectionMock->method('addFieldToFilter')->willReturnSelf();
$adyenOrderPaymentCollectionMock->method('getFirstItem')->willReturn($adyenOrderPaymentMock);

$this->adyenOrderPaymentCollectionFactoryMock->expects($this->once())->method('create')->willReturn($adyenOrderPaymentCollectionMock);
$this->adyenOrderPaymentCollectionFactoryMock->method('create')->willReturn($adyenOrderPaymentCollectionMock);

$this->adyenLoggerMock->expects($this->once())->method('addAdyenNotification')
$this->adyenLoggerMock->method('addAdyenNotification')
->with('Updated adyen_order_payment with order status 1 for pspReference testPspReference', [
'pspReference' => 'testPspReference',
'status' => 1,
Expand Down

0 comments on commit c6afd0b

Please sign in to comment.