10
10
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
11
11
use Magento \OfflinePayments \Model \Banktransfer ;
12
12
use Magento \OfflinePayments \Model \Cashondelivery ;
13
+ use Magento \OfflinePayments \Model \Checkmo ;
13
14
use Magento \OfflinePayments \Observer \BeforeOrderPaymentSaveObserver ;
15
+ use Magento \Sales \Model \Order ;
14
16
use Magento \Sales \Model \Order \Payment ;
15
- use PHPUnit_Framework_MockObject_MockObject as MockObject ;
16
- use Magento \ OfflinePayments \ Model \ Checkmo ;
17
+ use PHPUnit \ Framework \ MockObject \ MockObject ;
18
+ use PHPUnit \ Framework \ TestCase ;
17
19
18
- class BeforeOrderPaymentSaveObserverTest extends \PHPUnit \Framework \TestCase
20
+ /**
21
+ * Test class for \Magento\OfflinePayments\Observer\BeforeOrderPaymentSaveObserver
22
+ */
23
+ class BeforeOrderPaymentSaveObserverTest extends TestCase
19
24
{
25
+ private const STORE_ID = 1 ;
26
+
20
27
/**
21
28
* @var BeforeOrderPaymentSaveObserver
22
29
*/
23
- protected $ _model ;
30
+ private $ model ;
24
31
25
32
/**
26
33
* @var Payment|MockObject
27
34
*/
28
- private $ payment ;
35
+ private $ paymentMock ;
29
36
30
37
/**
31
38
* @var Event|MockObject
32
39
*/
33
- private $ event ;
40
+ private $ eventMock ;
34
41
35
42
/**
36
43
* @var Observer|MockObject
37
44
*/
38
- private $ observer ;
45
+ private $ observerMock ;
46
+
47
+ /**
48
+ * @var Order|MockObject
49
+ */
50
+ private $ orderMock ;
39
51
40
52
/**
41
53
* @inheritdoc
42
54
*/
43
55
protected function setUp ()
44
56
{
45
57
$ objectManagerHelper = new ObjectManager ($ this );
46
- $ this ->payment = $ this ->getMockBuilder (Payment::class)
58
+ $ this ->paymentMock = $ this ->getMockBuilder (Payment::class)
47
59
->disableOriginalConstructor ()
48
60
->getMock ();
49
61
50
- $ this ->event = $ this ->getMockBuilder (Event::class)
62
+ $ this ->eventMock = $ this ->getMockBuilder (Event::class)
51
63
->disableOriginalConstructor ()
52
64
->setMethods (['getPayment ' ])
53
65
->getMock ();
54
66
55
- $ this ->event ->expects (self ::once ())
67
+ $ this ->eventMock ->expects (self ::once ())
56
68
->method ('getPayment ' )
57
- ->willReturn ($ this ->payment );
69
+ ->willReturn ($ this ->paymentMock );
58
70
59
- $ this ->observer = $ this ->getMockBuilder (Observer::class)
71
+ $ this ->observerMock = $ this ->getMockBuilder (Observer::class)
60
72
->disableOriginalConstructor ()
61
73
->getMock ();
62
74
63
- $ this ->observer ->expects (self ::once ())
75
+ $ this ->observerMock ->expects (self ::once ())
64
76
->method ('getEvent ' )
65
- ->willReturn ($ this ->event );
77
+ ->willReturn ($ this ->eventMock );
78
+
79
+ $ this ->orderMock = $ this ->getMockBuilder (Order::class)
80
+ ->disableOriginalConstructor ()
81
+ ->getMock ();
82
+ $ this ->orderMock ->method ('getStoreId ' )
83
+ ->willReturn (static ::STORE_ID );
66
84
67
- $ this ->_model = $ objectManagerHelper ->getObject (BeforeOrderPaymentSaveObserver::class);
85
+ $ this ->paymentMock ->method ('getOrder ' )
86
+ ->willReturn ($ this ->orderMock );
87
+
88
+ $ this ->model = $ objectManagerHelper ->getObject (BeforeOrderPaymentSaveObserver::class);
68
89
}
69
90
70
91
/**
92
+ * Checks a case when payment method is either bank transfer or cash on delivery
71
93
* @param string $methodCode
72
94
* @dataProvider dataProviderBeforeOrderPaymentSaveWithInstructions
73
95
*/
74
96
public function testBeforeOrderPaymentSaveWithInstructions ($ methodCode )
75
97
{
76
- $ this ->payment ->expects (self ::once ())
98
+ $ this ->paymentMock ->expects (self ::once ())
77
99
->method ('getMethod ' )
78
100
->willReturn ($ methodCode );
79
- $ this ->payment ->expects (self ::once ())
101
+ $ this ->paymentMock ->expects (self ::once ())
80
102
->method ('setAdditionalInformation ' )
81
103
->with ('instructions ' , 'payment configuration ' );
82
104
$ method = $ this ->getMockBuilder (Banktransfer::class)
83
105
->disableOriginalConstructor ()
84
106
->getMock ();
85
107
86
108
$ method ->expects (self ::once ())
87
- ->method ('getInstructions ' )
109
+ ->method ('getConfigData ' )
110
+ ->with ('instructions ' , static ::STORE_ID )
88
111
->willReturn ('payment configuration ' );
89
- $ this ->payment ->expects (self ::once ())
112
+ $ this ->paymentMock ->expects (self ::once ())
90
113
->method ('getMethodInstance ' )
91
114
->willReturn ($ method );
92
115
93
- $ this ->_model ->execute ($ this ->observer );
116
+ $ this ->model ->execute ($ this ->observerMock );
94
117
}
95
118
96
119
/**
@@ -106,33 +129,37 @@ public function dataProviderBeforeOrderPaymentSaveWithInstructions()
106
129
];
107
130
}
108
131
132
+ /**
133
+ * Checks a case when payment method is Check Money
134
+ */
109
135
public function testBeforeOrderPaymentSaveWithCheckmo ()
110
136
{
111
- $ this ->payment ->expects (self ::exactly (2 ))
137
+ $ this ->paymentMock ->expects (self ::exactly (2 ))
112
138
->method ('getMethod ' )
113
139
->willReturn (Checkmo::PAYMENT_METHOD_CHECKMO_CODE );
114
- $ this ->payment ->expects (self ::exactly (2 ))
140
+ $ this ->paymentMock ->expects (self ::exactly (2 ))
115
141
->method ('setAdditionalInformation ' )
116
142
->willReturnMap (
117
143
[
118
- ['payable_to ' , 'payable to ' , $ this ->payment ],
119
- ['mailing_address ' , 'mailing address ' , $ this ->payment ],
144
+ ['payable_to ' , 'payable to ' , $ this ->paymentMock ],
145
+ ['mailing_address ' , 'mailing address ' , $ this ->paymentMock ],
120
146
]
121
147
);
122
148
123
149
$ method = $ this ->getMockBuilder (Checkmo::class)
124
150
->disableOriginalConstructor ()
125
151
->getMock ();
126
- $ method ->expects (self ::exactly (2 ))
127
- ->method ('getPayableTo ' )
128
- ->willReturn ('payable to ' );
129
- $ method ->expects (self ::exactly (2 ))
130
- ->method ('getMailingAddress ' )
131
- ->willReturn ('mailing address ' );
132
- $ this ->payment ->expects (self ::once ())
152
+ $ method ->method ('getConfigData ' )
153
+ ->willReturnMap (
154
+ [
155
+ ['payable_to ' , static ::STORE_ID , 'payable to ' ],
156
+ ['mailing_address ' , static ::STORE_ID , 'mailing address ' ]
157
+ ]
158
+ );
159
+ $ this ->paymentMock ->expects (self ::once ())
133
160
->method ('getMethodInstance ' )
134
161
->willReturn ($ method );
135
- $ this ->_model ->execute ($ this ->observer );
162
+ $ this ->model ->execute ($ this ->observerMock );
136
163
}
137
164
138
165
/**
@@ -141,36 +168,40 @@ public function testBeforeOrderPaymentSaveWithCheckmo()
141
168
*/
142
169
public function testBeforeOrderPaymentSaveWithCheckmoWithoutConfig ()
143
170
{
144
- $ this ->payment ->expects (self ::exactly (2 ))
171
+ $ this ->paymentMock ->expects (self ::exactly (2 ))
145
172
->method ('getMethod ' )
146
173
->willReturn (Checkmo::PAYMENT_METHOD_CHECKMO_CODE );
147
- $ this ->payment ->expects (self ::never ())
174
+ $ this ->paymentMock ->expects (self ::never ())
148
175
->method ('setAdditionalInformation ' );
149
176
150
177
$ method = $ this ->getMockBuilder (Checkmo::class)
151
178
->disableOriginalConstructor ()
152
179
->getMock ();
153
- $ method ->expects (self ::once ())
154
- ->method ('getPayableTo ' )
155
- ->willReturn (null );
156
- $ method ->expects (self ::once ())
157
- ->method ('getMailingAddress ' )
158
- ->willReturn (null );
159
- $ this ->payment ->expects (self ::once ())
180
+ $ method ->method ('getConfigData ' )
181
+ ->willReturnMap (
182
+ [
183
+ ['payable_to ' , static ::STORE_ID , null ],
184
+ ['mailing_address ' , static ::STORE_ID , null ]
185
+ ]
186
+ );
187
+ $ this ->paymentMock ->expects (self ::once ())
160
188
->method ('getMethodInstance ' )
161
189
->willReturn ($ method );
162
- $ this ->_model ->execute ($ this ->observer );
190
+ $ this ->model ->execute ($ this ->observerMock );
163
191
}
164
192
193
+ /**
194
+ * Checks a case with payment method not handled by observer
195
+ */
165
196
public function testBeforeOrderPaymentSaveWithOthers ()
166
197
{
167
- $ this ->payment ->expects (self ::exactly (2 ))
198
+ $ this ->paymentMock ->expects (self ::exactly (2 ))
168
199
->method ('getMethod ' )
169
200
->willReturn ('somepaymentmethod ' );
170
- $ this ->payment ->expects (self ::never ())
201
+ $ this ->paymentMock ->expects (self ::never ())
171
202
->method ('setAdditionalInformation ' );
172
203
173
- $ this ->_model ->execute ($ this ->observer );
204
+ $ this ->model ->execute ($ this ->observerMock );
174
205
}
175
206
176
207
/**
@@ -179,17 +210,16 @@ public function testBeforeOrderPaymentSaveWithOthers()
179
210
*/
180
211
public function testBeforeOrderPaymentSaveWithInstructionsAlreadySet ($ methodCode )
181
212
{
182
- $ this ->payment
183
- ->method ('getMethod ' )
213
+ $ this ->paymentMock ->method ('getMethod ' )
184
214
->willReturn ($ methodCode );
185
215
186
- $ this ->payment ->expects (self ::once ())
216
+ $ this ->paymentMock ->expects (self ::once ())
187
217
->method ('getAdditionalInformation ' )
188
218
->willReturn ('Test ' );
189
219
190
- $ this ->payment ->expects (self ::never ())
220
+ $ this ->paymentMock ->expects (self ::never ())
191
221
->method ('setAdditionalInformation ' );
192
222
193
- $ this ->_model ->execute ($ this ->observer );
223
+ $ this ->model ->execute ($ this ->observerMock );
194
224
}
195
225
}
0 commit comments