@@ -135,6 +135,9 @@ function ($value) {
135
135
'canCapture ' ,
136
136
'canRefund ' ,
137
137
'canOrder ' ,
138
+ 'order ' ,
139
+ 'isInitializeNeeded ' ,
140
+ 'initialize ' ,
138
141
]
139
142
)
140
143
->getMock ();
@@ -152,7 +155,9 @@ function ($value) {
152
155
'getBaseGrandTotal ' ,
153
156
'getShippingAmount ' ,
154
157
'getBaseShippingAmount ' ,
155
- 'getBaseTotalRefunded '
158
+ 'getBaseTotalRefunded ' ,
159
+ 'getItemsCollection ' ,
160
+ 'getOrder ' ,
156
161
]
157
162
)
158
163
->getMock ();
@@ -175,8 +180,12 @@ function ($value) {
175
180
'getInvoiceCollection ' ,
176
181
'addRelatedObject ' ,
177
182
'getState ' ,
183
+ 'getStatus ' ,
178
184
'addStatusHistoryComment ' ,
179
185
'registerCancellation ' ,
186
+ 'getCustomerNote ' ,
187
+ 'prepareInvoice ' ,
188
+ 'getPaymentsCollection ' ,
180
189
]
181
190
)
182
191
->getMock ();
@@ -278,6 +287,173 @@ public function testPlace()
278
287
$ this ->assertEquals ($ this ->payment , $ this ->payment ->place ());
279
288
}
280
289
290
+ public function testPlaceActionOrder ()
291
+ {
292
+ $ newOrderStatus = 'new_status ' ;
293
+ $ customerNote = 'blabla ' ;
294
+ $ sum = 10 ;
295
+ $ this ->orderMock ->expects ($ this ->any ())->method ('getTotalDue ' )->willReturn ($ sum );
296
+ $ this ->orderMock ->expects ($ this ->any ())->method ('getBaseTotalDue ' )->willReturn ($ sum );
297
+ $ this ->helperMock ->expects ($ this ->once ())
298
+ ->method ('getMethodInstance ' )
299
+ ->will ($ this ->returnValue ($ this ->paymentMethodMock ));
300
+ $ this ->paymentMethodMock ->expects ($ this ->once ())
301
+ ->method ('getConfigPaymentAction ' )
302
+ ->willReturn (\Magento \Payment \Model \Method \AbstractMethod::ACTION_ORDER );
303
+ $ this ->paymentMethodMock ->expects ($ this ->any ())
304
+ ->method ('getConfigData ' )
305
+ ->with ('order_status ' , null )
306
+ ->willReturn ($ newOrderStatus );
307
+ $ this ->mockGetDefaultStatus (Order::STATE_PROCESSING , $ newOrderStatus , ['first ' , 'second ' ]);
308
+ $ this ->orderMock ->expects ($ this ->any ())
309
+ ->method ('setState ' )
310
+ ->with (Order::STATE_PROCESSING )
311
+ ->willReturnSelf ();
312
+ $ this ->orderMock ->expects ($ this ->any ())
313
+ ->method ('setStatus ' )
314
+ ->with ($ newOrderStatus )
315
+ ->willReturnSelf ();
316
+ $ this ->paymentMethodMock ->expects ($ this ->once ())
317
+ ->method ('getConfigPaymentAction ' )
318
+ ->willReturn (null );
319
+ $ this ->orderMock ->expects ($ this ->once ())->method ('getBaseCurrency ' )->willReturn ($ this ->currencyMock );
320
+ $ this ->currencyMock ->method ('formatTxt ' )->willReturn ($ sum );
321
+ $ this ->paymentMethodMock ->expects ($ this ->once ())
322
+ ->method ('order ' )
323
+ ->with ($ this ->payment , $ sum )
324
+ ->willReturnSelf ();
325
+ $ this ->eventManagerMock ->expects ($ this ->at (0 ))
326
+ ->method ('dispatch ' )
327
+ ->with ('sales_order_payment_place_start ' , ['payment ' => $ this ->payment ]);
328
+ $ this ->eventManagerMock ->expects ($ this ->at (1 ))
329
+ ->method ('dispatch ' )
330
+ ->with ('sales_order_payment_place_end ' , ['payment ' => $ this ->payment ]);
331
+ $ statusHistory = $ this ->getMockForAbstractClass (
332
+ 'Magento\Sales\Api\Data\OrderStatusHistoryInterface '
333
+ );
334
+ $ this ->orderMock ->expects ($ this ->any ())->method ('getCustomerNote ' )->willReturn ($ customerNote );
335
+ $ this ->orderMock ->expects ($ this ->any ())
336
+ ->method ('addStatusHistoryComment ' )
337
+ ->withConsecutive (
338
+ [__ ('Ordered amount of %1 ' , $ sum )],
339
+ [$ customerNote ]
340
+ )
341
+ ->willReturn ($ statusHistory );
342
+ $ this ->orderMock ->expects ($ this ->any ())
343
+ ->method ('setIsCustomerNotified ' )
344
+ ->with (true )
345
+ ->willReturn ($ statusHistory );
346
+ $ this ->assertEquals ($ this ->payment , $ this ->payment ->place ());
347
+ }
348
+
349
+ public function testPlaceActionAuthorizeInitializeNeeded ()
350
+ {
351
+ $ newOrderStatus = 'new_status ' ;
352
+ $ customerNote = 'blabla ' ;
353
+ $ sum = 10 ;
354
+ $ this ->orderMock ->expects ($ this ->any ())->method ('getBaseGrandTotal ' )->willReturn ($ sum );
355
+ $ this ->orderMock ->expects ($ this ->any ())->method ('getTotalDue ' )->willReturn ($ sum );
356
+ $ this ->orderMock ->expects ($ this ->any ())->method ('getBaseTotalDue ' )->willReturn ($ sum );
357
+ $ this ->helperMock ->expects ($ this ->once ())
358
+ ->method ('getMethodInstance ' )
359
+ ->will ($ this ->returnValue ($ this ->paymentMethodMock ));
360
+ $ this ->paymentMethodMock ->expects ($ this ->once ())
361
+ ->method ('getConfigPaymentAction ' )
362
+ ->willReturn (\Magento \Payment \Model \Method \AbstractMethod::ACTION_AUTHORIZE );
363
+ $ this ->paymentMethodMock ->expects ($ this ->any ())
364
+ ->method ('getConfigData ' )
365
+ ->withConsecutive (
366
+ ['order_status ' ],
367
+ ['payment_action ' ]
368
+ )->willReturn ($ newOrderStatus );
369
+ $ this ->paymentMethodMock ->expects ($ this ->once ())->method ('isInitializeNeeded ' )->willReturn (true );
370
+ $ this ->paymentMethodMock ->expects ($ this ->once ())->method ('initialize ' );
371
+ $ this ->mockGetDefaultStatus (Order::STATE_NEW , $ newOrderStatus , ['first ' , 'second ' ]);
372
+ $ this ->orderMock ->expects ($ this ->any ())
373
+ ->method ('setState ' )
374
+ ->with (Order::STATE_NEW )
375
+ ->willReturnSelf ();
376
+ $ this ->orderMock ->expects ($ this ->any ())
377
+ ->method ('setStatus ' )
378
+ ->with ($ newOrderStatus )
379
+ ->willReturnSelf ();
380
+ $ this ->paymentMethodMock ->expects ($ this ->once ())
381
+ ->method ('getConfigPaymentAction ' )
382
+ ->willReturn (null );
383
+ $ this ->eventManagerMock ->expects ($ this ->at (0 ))
384
+ ->method ('dispatch ' )
385
+ ->with ('sales_order_payment_place_start ' , ['payment ' => $ this ->payment ]);
386
+ $ this ->eventManagerMock ->expects ($ this ->at (1 ))
387
+ ->method ('dispatch ' )
388
+ ->with ('sales_order_payment_place_end ' , ['payment ' => $ this ->payment ]);
389
+ $ statusHistory = $ this ->getMockForAbstractClass (
390
+ 'Magento\Sales\Api\Data\OrderStatusHistoryInterface '
391
+ );
392
+ $ this ->orderMock ->expects ($ this ->any ())->method ('getCustomerNote ' )->willReturn ($ customerNote );
393
+ $ this ->orderMock ->expects ($ this ->any ())
394
+ ->method ('addStatusHistoryComment ' )
395
+ ->withConsecutive (
396
+ [$ customerNote ],
397
+ [__ ('Authorized amount of %1 ' , $ sum )]
398
+ )
399
+ ->willReturn ($ statusHistory );
400
+ $ this ->orderMock ->expects ($ this ->any ())
401
+ ->method ('setIsCustomerNotified ' )
402
+ ->with (true )
403
+ ->willReturn ($ statusHistory );
404
+ $ this ->assertEquals ($ this ->payment , $ this ->payment ->place ());
405
+ }
406
+
407
+ public function testPlaceActionAuthorizeFraud ()
408
+ {
409
+ $ newOrderStatus = 'new_status ' ;
410
+ $ customerNote = 'blabla ' ;
411
+ $ sum = 10 ;
412
+ $ this ->orderMock ->expects ($ this ->any ())->method ('getTotalDue ' )->willReturn ($ sum );
413
+ $ this ->orderMock ->expects ($ this ->any ())->method ('getBaseTotalDue ' )->willReturn ($ sum );
414
+ $ this ->helperMock ->expects ($ this ->once ())
415
+ ->method ('getMethodInstance ' )
416
+ ->will ($ this ->returnValue ($ this ->paymentMethodMock ));
417
+ $ this ->paymentMethodMock ->expects ($ this ->once ())
418
+ ->method ('getConfigPaymentAction ' )
419
+ ->willReturn (\Magento \Payment \Model \Method \AbstractMethod::ACTION_AUTHORIZE );
420
+ $ this ->paymentMethodMock ->expects ($ this ->any ())
421
+ ->method ('getConfigData ' )
422
+ ->with ('order_status ' , null )
423
+ ->willReturn ($ newOrderStatus );
424
+ $ statusHistory = $ this ->getMockForAbstractClass (
425
+ 'Magento\Sales\Api\Data\OrderStatusHistoryInterface '
426
+ );
427
+ $ this ->orderMock ->expects ($ this ->any ())->method ('getCustomerNote ' )->willReturn ($ customerNote );
428
+ $ this ->orderMock ->expects ($ this ->any ())
429
+ ->method ('addStatusHistoryComment ' )
430
+ ->withConsecutive (
431
+ [__ ('Order is suspended as its authorizing amount %1 is suspected to be fraudulent. ' , $ sum )]
432
+ )
433
+ ->willReturn ($ statusHistory );
434
+ $ this ->mockGetDefaultStatus (Order::STATE_PROCESSING , Order::STATUS_FRAUD , ['first ' , 'second ' ]);
435
+ $ this ->orderMock ->expects ($ this ->any ())
436
+ ->method ('setState ' )
437
+ ->with (Order::STATE_PROCESSING )
438
+ ->willReturnSelf ();
439
+ $ this ->orderMock ->expects ($ this ->any ())
440
+ ->method ('setStatus ' )
441
+ ->withConsecutive (
442
+ [Order::STATUS_FRAUD ]
443
+ )->willReturnSelf ();
444
+ $ this ->orderMock ->expects ($ this ->atLeastOnce ())
445
+ ->method ('getStatus ' )
446
+ ->willReturn (Order::STATUS_FRAUD );
447
+ $ this ->paymentMethodMock ->expects ($ this ->once ())
448
+ ->method ('getConfigPaymentAction ' )
449
+ ->willReturn (null );
450
+ $ this ->orderMock ->expects ($ this ->once ())->method ('getBaseCurrency ' )->willReturn ($ this ->currencyMock );
451
+ $ this ->currencyMock ->method ('formatTxt ' )->willReturn ($ sum );
452
+ $ this ->assertEquals ($ this ->payment , $ this ->payment ->place ());
453
+ //maybe we don't need write authorised sum when fraud was detected
454
+ $ this ->assertEquals ($ sum , $ this ->payment ->getAmountAuthorized ());
455
+ }
456
+
281
457
public function testAuthorize ()
282
458
{
283
459
$ storeID = 1 ;
@@ -1110,6 +1286,12 @@ public function testCanCaptureAuthorizationTransaction()
1110
1286
$ this ->assertTrue ($ this ->payment ->canCapture ());
1111
1287
}
1112
1288
1289
+ public function testCannotCapture ()
1290
+ {
1291
+ $ this ->paymentMethodMock ->expects ($ this ->once ())->method ('canCapture ' )->willReturn (false );
1292
+ $ this ->assertFalse ($ this ->payment ->canCapture ());
1293
+ }
1294
+
1113
1295
public function testPay ()
1114
1296
{
1115
1297
$ expects = [
@@ -1456,7 +1638,7 @@ protected function mockGetDefaultStatus($state, $status, $allStatuses = [])
1456
1638
if (!empty ($ allStatuses )) {
1457
1639
$ orderConfigMock ->expects ($ this ->any ())
1458
1640
->method ('getStateStatuses ' )
1459
- ->with (Order:: STATE_NEW )
1641
+ ->with ($ state )
1460
1642
->will ($ this ->returnValue ($ allStatuses ));
1461
1643
}
1462
1644
0 commit comments