-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathAdyenPaymentMethodDataAssignObserverTest.php
More file actions
160 lines (132 loc) · 5.54 KB
/
AdyenPaymentMethodDataAssignObserverTest.php
File metadata and controls
160 lines (132 loc) · 5.54 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
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
namespace Adyen\Payment\Test\Unit\Observer;
use Adyen\Payment\Helper\StateData;
use Adyen\Payment\Helper\Util\CheckoutStateDataValidator;
use Adyen\Payment\Helper\Util\DataArrayValidator;
use Adyen\Payment\Helper\Vault;
use Adyen\Payment\Model\ResourceModel\StateData\Collection;
use Adyen\Payment\Observer\AdyenPaymentMethodDataAssignObserver;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
use Magento\Framework\Event;
use Magento\Framework\Event\Observer;
use Magento\Payment\Model\InfoInterface;
use Magento\Payment\Observer\AbstractDataAssignObserver;
use Magento\Quote\Api\Data\PaymentInterface;
use PHPUnit\Framework\MockObject\MockObject;
use \Magento\Framework\DataObject;
use Magento\Quote\Model\Quote\Payment;
class AdyenPaymentMethodDataAssignObserverTest extends AbstractAdyenTestCase
{
private MockObject $checkoutStateDataValidator;
private MockObject $stateDataCollection;
private MockObject $stateData;
private MockObject $vaultHelper;
private AdyenPaymentMethodDataAssignObserver $observer;
protected function setUp(): void
{
$this->checkoutStateDataValidator = $this->createMock(CheckoutStateDataValidator::class);
$this->stateDataCollection = $this->createMock(Collection::class);
$this->stateData = $this->createMock(StateData::class);
$this->vaultHelper = $this->createMock(Vault::class);
$this->paymentInfo = $this->createMock(Payment::class);
$this->observer = new AdyenPaymentMethodDataAssignObserver(
$this->checkoutStateDataValidator,
$this->stateDataCollection,
$this->stateData,
$this->vaultHelper
);
}
public function testExecuteWithNoAdditionalData()
{
$dataObject = new DataObject();
$paymentInfo = $this->createMock(Payment::class);
$observer = $this->getPreparedObserverWithMap([
[AbstractDataAssignObserver::DATA_CODE, $dataObject],
[AbstractDataAssignObserver::MODEL_CODE, $paymentInfo],
]);
$this->observer->execute($observer);
}
public function testExecuteWithValidAdditionalData()
{
$additionalData = [
'brand_code' => 'visa',
'stateData' => json_encode(['paymentMethod' => ['type' => 'scheme']]),
'recurringProcessingModel' => 'Subscription',
];
$dataObject = new DataObject([
PaymentInterface::KEY_ADDITIONAL_DATA => $additionalData,
]);
$observer = $this->getPreparedObserverWithMap([
[AbstractDataAssignObserver::DATA_CODE, $dataObject],
[AbstractDataAssignObserver::MODEL_CODE, $this->paymentInfo],
]);
$this->paymentInfo->expects($this->once())->method('unsAdditionalInformation')->with('cc_type');
$this->paymentInfo->method('getData')
->with('quote_id')
->willReturn(1);
$this->vaultHelper->expects($this->once())
->method('validateRecurringProcessingModel')
->with('Subscription')
->willReturn(true);
$this->paymentInfo->expects($this->exactly(2))->method('setAdditionalInformation');
// Execute the observer logic
$this->observer->execute($observer);
}
public function testExecuteWithInvalidRecurringProcessingModel()
{
$additionalData = [
'brand_code' => 'visa',
'stateData' => json_encode(['paymentMethod' => ['type' => 'scheme']]),
'recurringProcessingModel' => 'invalid',
];
$dataObject = new \Magento\Framework\DataObject([
PaymentInterface::KEY_ADDITIONAL_DATA => $additionalData,
]);
$this->paymentInfo->expects($this->atLeastOnce())
->method('unsAdditionalInformation')
->withConsecutive(['cc_type'], ['recurringProcessingModel']);
$this->paymentInfo->expects($this->any())->method('getData')
->with('quote_id')->willReturn(123);
$observer = $this->getPreparedObserverWithMap([
[AbstractDataAssignObserver::DATA_CODE, $dataObject],
[AbstractDataAssignObserver::MODEL_CODE, $this->paymentInfo],
]);
$this->vaultHelper->expects($this->once())->method('validateRecurringProcessingModel')
->with('invalid')->willReturn(false);
$this->observer->execute($observer);
}
public function testExecuteWithStateDataFromDatabase()
{
$this->paymentInfo->expects($this->any())->method('getData')
->with('quote_id')->willReturn(123);
$dataObject = new \Magento\Framework\DataObject();
$observer = $this->getPreparedObserverWithMap([
[AbstractDataAssignObserver::DATA_CODE, $dataObject],
[AbstractDataAssignObserver::MODEL_CODE, $this->paymentInfo],
]);
$this->observer->execute($observer);
}
/**
* @param array $returnMap
* @return MockObject|Observer
*/
private function getPreparedObserverWithMap(array $returnMap): Observer|MockObject
{
$observer = $this->getMockBuilder(Observer::class)
->disableOriginalConstructor()
->getMock();
$event = $this->getMockBuilder(Event::class)
->disableOriginalConstructor()
->getMock();
$observer->expects(static::atLeastOnce())
->method('getEvent')
->willReturn($event);
$event->expects(static::atLeastOnce())
->method('getDataByKey')
->willReturnMap(
$returnMap
);
return $observer;
}
}