@@ -14,6 +14,11 @@ class CreditmemoSenderTest extends \PHPUnit_Framework_TestCase
14
14
*/
15
15
protected $ sender ;
16
16
17
+ /**
18
+ * @var \Magento\Sales\Model\Order\Email\Sender|\PHPUnit_Framework_MockObject_MockObject
19
+ */
20
+ protected $ senderMock ;
21
+
17
22
/**
18
23
* @var \PHPUnit_Framework_MockObject_MockObject
19
24
*/
@@ -40,7 +45,7 @@ class CreditmemoSenderTest extends \PHPUnit_Framework_TestCase
40
45
protected $ orderMock ;
41
46
42
47
/**
43
- * @var \PHPUnit_Framework_MockObject_MockObject
48
+ * @var \Magento\Sales\Model\Order\Creditmemo|\ PHPUnit_Framework_MockObject_MockObject
44
49
*/
45
50
protected $ creditmemoMock ;
46
51
/**
@@ -53,8 +58,23 @@ class CreditmemoSenderTest extends \PHPUnit_Framework_TestCase
53
58
*/
54
59
protected $ creditmemoResource ;
55
60
61
+ /**
62
+ * Global configuration storage mock.
63
+ *
64
+ * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
65
+ */
66
+ protected $ globalConfig ;
67
+
56
68
protected function setUp ()
57
69
{
70
+ $ this ->senderMock = $ this ->getMock (
71
+ 'Magento\Sales\Model\Order\Email\Sender ' ,
72
+ ['send ' , 'sendCopyTo ' ],
73
+ [],
74
+ '' ,
75
+ false
76
+ );
77
+
58
78
$ this ->senderBuilderFactoryMock = $ this ->getMock (
59
79
'\Magento\Sales\Model\Order\Email\SenderBuilderFactory ' ,
60
80
['create ' ],
@@ -76,7 +96,15 @@ protected function setUp()
76
96
77
97
$ this ->creditmemoResource = $ this ->getMock (
78
98
'\Magento\Sales\Model\Resource\Order\Creditmemo ' ,
99
+ ['saveAttribute ' ],
79
100
[],
101
+ '' ,
102
+ false
103
+ );
104
+
105
+ $ this ->globalConfig = $ this ->getMock (
106
+ 'Magento\Framework\App\Config ' ,
107
+ ['getValue ' ],
80
108
[],
81
109
'' ,
82
110
false
@@ -123,13 +151,17 @@ protected function setUp()
123
151
'' ,
124
152
false
125
153
);
126
- $ this ->orderMock ->expects ($ this ->once ())
154
+ $ this ->orderMock ->expects ($ this ->any ())
127
155
->method ('getPayment ' )
128
156
->will ($ this ->returnValue ($ paymentInfoMock ));
129
157
130
158
$ this ->creditmemoMock = $ this ->getMock (
131
159
'\Magento\Sales\Model\Order\Creditmemo ' ,
132
- ['getStore ' , '__wakeup ' , 'getOrder ' ],
160
+ [
161
+ 'getStore ' , '__wakeup ' , 'getOrder ' ,
162
+ 'setSendEmail ' , 'setEmailSent ' , 'getCustomerNoteNotify ' ,
163
+ 'getCustomerNote '
164
+ ],
133
165
[],
134
166
'' ,
135
167
false
@@ -146,122 +178,117 @@ protected function setUp()
146
178
$ this ->identityContainerMock ,
147
179
$ this ->senderBuilderFactoryMock ,
148
180
$ this ->paymentHelper ,
149
- $ this ->creditmemoResource
181
+ $ this ->creditmemoResource ,
182
+ $ this ->globalConfig
150
183
);
151
184
}
152
185
153
- public function testSendFalse ()
154
- {
155
- $ result = $ this ->sender ->send ($ this ->creditmemoMock );
156
- $ this ->assertFalse ($ result );
157
- }
158
-
159
- public function testSendTrueWithCustomerCopy ()
186
+ /**
187
+ * @param int $configValue
188
+ * @param bool|null $forceSyncMode
189
+ * @param bool|null $customerNoteNotify
190
+ * @param bool|null $emailSendingResult
191
+ * @dataProvider sendDataProvider
192
+ * @return void
193
+ */
194
+ public function testSend ($ configValue , $ forceSyncMode , $ customerNoteNotify , $ emailSendingResult )
160
195
{
161
- $ billingAddress = 'billing_address ' ;
162
196
$ comment = 'comment_test ' ;
197
+ $ billingAddress = 'billing_address ' ;
198
+ $ configPath = 'sales_email/general/async_sending ' ;
163
199
164
- $ this ->orderMock ->expects ($ this ->once ())
165
- ->method ('getCustomerIsGuest ' )
166
- ->will ($ this ->returnValue (false ));
167
- $ this ->orderMock ->expects ($ this ->any ())
168
- ->method ('getBillingAddress ' )
169
- ->will ($ this ->returnValue ($ billingAddress ));
170
-
171
- $ this ->identityContainerMock ->expects ($ this ->once ())
172
- ->method ('isEnabled ' )
173
- ->will ($ this ->returnValue (true ));
174
- $ this ->templateContainerMock ->expects ($ this ->once ())
175
- ->method ('setTemplateVars ' )
176
- ->with (
177
- $ this ->equalTo (
178
- [
179
- 'order ' => $ this ->orderMock ,
180
- 'invoice ' => $ this ->creditmemoMock ,
181
- 'comment ' => $ comment ,
182
- 'billing ' => $ billingAddress ,
183
- 'payment_html ' => 'payment ' ,
184
- 'store ' => $ this ->storeMock ,
185
- ]
186
- )
187
- );
188
- $ paymentInfoMock = $ this ->getMock (
189
- '\Magento\Payment\Model\Info ' ,
190
- [],
191
- [],
192
- '' ,
193
- false
194
- );
195
- $ this ->orderMock ->expects ($ this ->once ())
196
- ->method ('getPayment ' )
197
- ->will ($ this ->returnValue ($ paymentInfoMock ));
200
+ $ this ->creditmemoMock ->expects ($ this ->once ())
201
+ ->method ('setSendEmail ' )
202
+ ->with (true );
198
203
199
- $ senderMock = $ this ->getMock (
200
- 'Magento\Sales\Model\Order\Email\Sender ' ,
201
- ['send ' , 'sendCopyTo ' ],
202
- [],
203
- '' ,
204
- false
205
- );
206
- $ senderMock ->expects ($ this ->once ())
207
- ->method ('send ' );
208
- $ senderMock ->expects ($ this ->never ())
209
- ->method ('sendCopyTo ' );
204
+ $ this ->globalConfig ->expects ($ this ->once ())
205
+ ->method ('getValue ' )
206
+ ->with ($ configPath )
207
+ ->willReturn ($ configValue );
210
208
211
- $ this ->senderBuilderFactoryMock ->expects ($ this ->once ())
212
- ->method ('create ' )
213
- ->will ($ this ->returnValue ($ senderMock ));
209
+ if (!$ configValue || $ forceSyncMode ) {
210
+ $ this ->orderMock ->expects ($ this ->any ())
211
+ ->method ('getBillingAddress ' )
212
+ ->willReturn ($ billingAddress );
214
213
215
- $ result = $ this ->sender -> send ($ this ->creditmemoMock , true , $ comment );
216
- $ this -> assertTrue ( $ result );
217
- }
214
+ $ this ->creditmemoMock -> expects ($ this ->once ())
215
+ -> method ( ' getCustomerNoteNotify ' )
216
+ -> willReturn ( $ customerNoteNotify );
218
217
219
- public function testSendTrueWithoutCustomerCopy ()
220
- {
221
- $ billingAddress = 'billing_address ' ;
222
- $ comment = 'comment_test ' ;
218
+ $ this ->creditmemoMock ->expects ($ this ->any ())
219
+ ->method ('getCustomerNote ' )
220
+ ->willReturn ($ comment );
223
221
224
- $ this ->orderMock ->expects ($ this ->once ())
225
- ->method ('getCustomerIsGuest ' )
226
- ->will ($ this ->returnValue (false ));
227
- $ this ->orderMock ->expects ($ this ->any ())
228
- ->method ('getBillingAddress ' )
229
- ->will ($ this ->returnValue ($ billingAddress ));
230
-
231
- $ this ->identityContainerMock ->expects ($ this ->once ())
232
- ->method ('isEnabled ' )
233
- ->will ($ this ->returnValue (true ));
234
- $ this ->templateContainerMock ->expects ($ this ->once ())
235
- ->method ('setTemplateVars ' )
236
- ->with (
237
- $ this ->equalTo (
222
+ $ this ->templateContainerMock ->expects ($ this ->once ())
223
+ ->method ('setTemplateVars ' )
224
+ ->with (
238
225
[
239
226
'order ' => $ this ->orderMock ,
240
227
'invoice ' => $ this ->creditmemoMock ,
228
+ 'comment ' => $ customerNoteNotify ? $ comment : '' ,
241
229
'billing ' => $ billingAddress ,
242
230
'payment_html ' => 'payment ' ,
243
- 'comment ' => $ comment ,
244
231
'store ' => $ this ->storeMock ,
245
232
]
246
- )
247
- );
248
- $ senderMock = $ this ->getMock (
249
- 'Magento\Sales\Model\Order\Email\Sender ' ,
250
- ['send ' , 'sendCopyTo ' ],
251
- [],
252
- '' ,
253
- false
254
- );
255
- $ senderMock ->expects ($ this ->never ())
256
- ->method ('send ' );
257
- $ senderMock ->expects ($ this ->once ())
258
- ->method ('sendCopyTo ' );
233
+ );
234
+
235
+ $ this ->identityContainerMock ->expects ($ this ->once ())
236
+ ->method ('isEnabled ' )
237
+ ->willReturn ($ emailSendingResult );
238
+
239
+ if ($ emailSendingResult ) {
240
+ $ this ->senderBuilderFactoryMock ->expects ($ this ->once ())
241
+ ->method ('create ' )
242
+ ->willReturn ($ this ->senderMock );
243
+
244
+ $ this ->senderMock ->expects ($ this ->once ())->method ('send ' );
245
+
246
+ $ this ->senderMock ->expects ($ this ->once ())->method ('sendCopyTo ' );
259
247
260
- $ this ->senderBuilderFactoryMock ->expects ($ this ->once ())
261
- ->method ('create ' )
262
- -> will ( $ this -> returnValue ( $ senderMock ) );
248
+ $ this ->creditmemoMock ->expects ($ this ->once ())
249
+ ->method ('setEmailSent ' )
250
+ -> with ( true );
263
251
264
- $ result = $ this ->sender ->send ($ this ->creditmemoMock , false , $ comment );
265
- $ this ->assertTrue ($ result );
252
+ $ this ->creditmemoResource ->expects ($ this ->once ())
253
+ ->method ('saveAttribute ' )
254
+ ->with ($ this ->creditmemoMock , ['send_email ' , 'email_sent ' ]);
255
+
256
+ $ this ->assertTrue (
257
+ $ this ->sender ->send ($ this ->creditmemoMock )
258
+ );
259
+ } else {
260
+ $ this ->creditmemoResource ->expects ($ this ->once ())
261
+ ->method ('saveAttribute ' )
262
+ ->with ($ this ->creditmemoMock , 'send_email ' );
263
+
264
+ $ this ->assertFalse (
265
+ $ this ->sender ->send ($ this ->creditmemoMock )
266
+ );
267
+ }
268
+ } else {
269
+ $ this ->creditmemoResource ->expects ($ this ->once ())
270
+ ->method ('saveAttribute ' )
271
+ ->with ($ this ->creditmemoMock , 'send_email ' );
272
+
273
+ $ this ->assertFalse (
274
+ $ this ->sender ->send ($ this ->creditmemoMock )
275
+ );
276
+ }
277
+ }
278
+
279
+ /**
280
+ * @return array
281
+ */
282
+ public function sendDataProvider ()
283
+ {
284
+ return [
285
+ [0 , 0 , 1 , true ],
286
+ [0 , 0 , 0 , true ],
287
+ [0 , 0 , 1 , false ],
288
+ [0 , 0 , 0 , false ],
289
+ [0 , 1 , 1 , true ],
290
+ [0 , 1 , 0 , true ],
291
+ [1 , null , null , null ]
292
+ ];
266
293
}
267
294
}
0 commit comments