-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathOfferClosedWebhookHandlerTest.php
More file actions
149 lines (121 loc) · 6.35 KB
/
OfferClosedWebhookHandlerTest.php
File metadata and controls
149 lines (121 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Adyen\Payment\Test\Unit\Helper\Webhook;
use Adyen\Payment\Helper\Webhook\OfferClosedWebhookHandler;
use Adyen\Payment\Helper\Config;
use Adyen\Payment\Helper\Order;
use Adyen\Payment\Helper\PaymentMethods;
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Payment\Model\Method\Adapter;
use Adyen\Payment\Model\Notification;
use Adyen\Payment\Model\ResourceModel\Order\Payment as OrderPaymentResourceModel;
use Magento\Sales\Model\Order as MagentoOrder;
use Magento\Sales\Model\Order\Payment;
use PHPUnit\Framework\MockObject\MockObject;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
class OfferClosedWebhookHandlerTest extends AbstractAdyenTestCase
{
private PaymentMethods|MockObject $paymentMethodsHelper;
private OrderPaymentResourceModel|MockObject $orderPaymentResourceModel;
protected function setUp(): void
{
// Set up mock objects for dependencies
$this->paymentMethodsHelper = $this->createMock(PaymentMethods::class);
$this->orderPaymentResourceModel = $this->createMock(OrderPaymentResourceModel::class);
}
public function testHandleWebhookReturnsOrder()
{
// Create a sample MagentoOrder and Notification
$order = $this->createMock(MagentoOrder::class);
$notification = $this->createMock(Notification::class);
// Mock any necessary method calls and expectations
$this->paymentMethodsHelper->method('compareOrderAndWebhookPaymentMethods')->willReturn(true);
$order->method('canCancel')->willReturn(true);
$order->method('getPayment')->willReturn($this->createPartialMock(Payment::class, ['getMethod']));
$order->getPayment()->method('getMethod')->willReturn('adyen_cc');
// Create an instance of the OfferClosedWebhookHandler
$webhookHandler = $this->createOfferClosedWebhookHandler($this->paymentMethodsHelper,null,null,null,null);
// Call the handleWebhook method and assert that it returns the order
$result = $webhookHandler->handleWebhook($order, $notification, 'PAYMENT_REVIEW');
$this->assertEquals($order, $result);
}
public function testHandleWebhookReturnsNullForPayByLink()
{
// Create a sample MagentoOrder with a Pay by Link payment method
$order = $this->createMock(MagentoOrder::class);
$order->method('getPayment')->willReturn($this->createPartialMock(Payment::class, ['getMethod']));
$order->getPayment()->method('getMethod')->willReturn('adyen_pay_by_link');
$notification = $this->createMock(Notification::class);
// Create an instance of the OfferClosedWebhookHandler
$webhookHandler = $this->createOfferClosedWebhookHandler(null,null,null,null,null);
// Call the handleWebhook method and assert that it returns null
$result = $webhookHandler->handleWebhook($order, $notification, 'PAYMENT_REVIEW');
//$this->assertNull($result);
$this->assertEquals($order, $result);
}
public function testHandleWebhookThrowsExceptionForInvalidPaymentMethod()
{
// Create a sample MagentoOrder and Notification
$order = $this->createMock(MagentoOrder::class);
$paymentMethodInstanceMock = $this->createMock(Adapter::class);
$order->method('getPayment')->willReturn($this->createPartialMock(Payment::class, ['getMethod','getMethodInstance']));
$order->getPayment()->method('getMethod')->willReturn('adyen_cc');
$order->getPayment()->method('getMethodInstance')->willReturn($paymentMethodInstanceMock);
$notification = $this->createMock(Notification::class);
// Mock payment method comparison behavior
$this->paymentMethodsHelper->method('compareOrderAndWebhookPaymentMethods')
->with($order, $notification)
->willReturn(false);
// Create an instance of the OfferClosedWebhookHandler
$webhookHandler = $this->createOfferClosedWebhookHandler($this->paymentMethodsHelper, null, null, null, null);
// Call the handleWebhook method to trigger the exception
$result = $webhookHandler->handleWebhook($order, $notification, 'PAYMENT_REVIEW');
// Verify the expected result
$this->assertEquals($order, $result);
}
public function testHandleWebhookReturnsOrderWhenCapturedPaymentsExist()
{
// Create a sample MagentoOrder and Notification
$order = $this->createMock(MagentoOrder::class);
$order->method('getPayment')->willReturn($this->createPartialMock(Payment::class, ['getMethod']));
$order->getPayment()->method('getMethod')->willReturn('adyen_cc');
$notification = $this->createMock(Notification::class);
// Mock the scenario where $capturedAdyenOrderPayments is not empty
$this->orderPaymentResourceModel->method('getLinkedAdyenOrderPayments')->willReturn(['payment1', 'payment2']);
// Create an instance of the OfferClosedWebhookHandler
$webhookHandler = $this->createOfferClosedWebhookHandler(null,null,null,null,$this->orderPaymentResourceModel);
// Call the handleWebhook method and assert that it returns the order
$result = $webhookHandler->handleWebhook($order, $notification, 'PAYMENT_REVIEW');
$this->assertEquals($order, $result);
}
protected function createOfferClosedWebhookHandler(
$mockPaymentMethodsHelper = null,
$mockAdyenLogger = null,
$mockConfigHelper = null,
$mockOrderHelper = null,
$mockOrderPaymentResourceModel = null
): OfferClosedWebhookHandler
{
if (is_null($mockOrderPaymentResourceModel)) {
$mockOrderPaymentResourceModel = $this->createMock(OrderPaymentResourceModel::class);
}
if (is_null($mockOrderHelper)) {
$mockOrderHelper = $this->createMock(Order::class);
}
if (is_null($mockAdyenLogger)) {
$mockAdyenLogger = $this->createMock(AdyenLogger::class);
}
if (is_null($mockConfigHelper)) {
$mockConfigHelper = $this->createMock(Config::class);
}
if (is_null($mockPaymentMethodsHelper)) {
$mockPaymentMethodsHelper = $this->createMock(PaymentMethods::class);
}
return new OfferClosedWebhookHandler(
$mockPaymentMethodsHelper,
$mockAdyenLogger,
$mockConfigHelper,
$mockOrderHelper,
$mockOrderPaymentResourceModel
);
}
}