13
13
use WCPay \Constants \Intent_Status ;
14
14
use WCPay \Core \Server \Request \Get_Intention ;
15
15
use WCPay \Core \Server \Request \Get_Setup_Intention ;
16
- use WCPay \Core \Server \Request \Update_Intention ;
17
16
use WCPay \Exceptions \Process_Payment_Exception ;
18
17
use WCPay \WooPay \WooPay_Utilities ;
19
18
use WCPay \Session_Rate_Limiter ;
@@ -54,7 +53,7 @@ class UPE_Payment_Gateway_Test extends WCPAY_UnitTestCase {
54
53
*
55
54
* @var WC_Payment_Gateway_WCPay
56
55
*/
57
- private $ mock_upe_gateway ;
56
+ private $ mock_gateway ;
58
57
59
58
/**
60
59
* Mock WC_Payments_Customer_Service.
@@ -182,7 +181,7 @@ public function set_up() {
182
181
// Note that we cannot use createStub here since it's not defined in PHPUnit 6.5.
183
182
$ this ->mock_api_client = $ this ->getMockBuilder ( 'WC_Payments_API_Client ' )
184
183
->disableOriginalConstructor ()
185
- ->setMethods (
184
+ ->onlyMethods (
186
185
[
187
186
'get_payment_method ' ,
188
187
'is_server_connected ' ,
@@ -221,7 +220,7 @@ public function set_up() {
221
220
// Arrange: Mock WC_Payments_Customer_Service so its methods aren't called directly.
222
221
$ this ->mock_token_service = $ this ->getMockBuilder ( 'WC_Payments_Token_Service ' )
223
222
->disableOriginalConstructor ()
224
- ->setMethods ( [ 'add_payment_method_to_user ' ] )
223
+ ->onlyMethods ( [ 'add_payment_method_to_user ' ] )
225
224
->getMock ();
226
225
227
226
// Arrange: Mock WC_Payments_Action_Scheduler_Service so its methods aren't called directly.
@@ -254,7 +253,7 @@ public function set_up() {
254
253
foreach ( $ payment_method_classes as $ payment_method_class ) {
255
254
$ mock_payment_method = $ this ->getMockBuilder ( $ payment_method_class )
256
255
->setConstructorArgs ( [ $ this ->mock_token_service ] )
257
- ->setMethods ( [ 'is_subscription_item_in_cart ' , 'get_icon ' ] )
256
+ ->onlyMethods ( [ 'is_subscription_item_in_cart ' , 'get_icon ' ] )
258
257
->getMock ();
259
258
$ this ->mock_payment_methods [ $ mock_payment_method ->get_id () ] = $ mock_payment_method ;
260
259
}
@@ -265,7 +264,7 @@ public function set_up() {
265
264
$ this ->mock_api_client ,
266
265
]
267
266
)
268
- ->setMethods (
267
+ ->onlyMethods (
269
268
[
270
269
'get_payment_method_id_for_order ' ,
271
270
]
@@ -280,7 +279,7 @@ public function set_up() {
280
279
281
280
// Arrange: Mock WC_Payment_Gateway_WCPay so that some of its methods can be
282
281
// mocked, and their return values can be used for testing.
283
- $ this ->mock_upe_gateway = $ this ->getMockBuilder ( WC_Payment_Gateway_WCPay::class )
282
+ $ this ->mock_gateway = $ this ->getMockBuilder ( WC_Payment_Gateway_WCPay::class )
284
283
->setConstructorArgs (
285
284
[
286
285
$ this ->mock_api_client ,
@@ -309,13 +308,13 @@ public function set_up() {
309
308
->getMock ();
310
309
311
310
// Arrange: Set the return value of get_return_url() so it can be used in a test later.
312
- $ this ->mock_upe_gateway
311
+ $ this ->mock_gateway
313
312
->expects ( $ this ->any () )
314
313
->method ( 'get_return_url ' )
315
314
->will (
316
315
$ this ->returnValue ( $ this ->return_url )
317
316
);
318
- $ this ->mock_upe_gateway
317
+ $ this ->mock_gateway
319
318
->expects ( $ this ->any () )
320
319
->method ( 'parent_process_payment ' )
321
320
->will (
@@ -355,11 +354,12 @@ public function tear_down() {
355
354
wcpay_get_test_container ()->reset_all_replacements ();
356
355
}
357
356
358
- public function test_process_payment_returns_correct_redirect_when_using_payment_request () {
359
- $ order = WC_Helper_Order::create_order ();
360
- $ intent = WC_Helper_Intention::create_intention ();
361
- $ _POST ['payment_request_type ' ] = 'google_pay ' ;
362
- $ this ->mock_upe_gateway ->expects ( $ this ->once () )
357
+ public function test_process_payment_returns_correct_redirect_when_using_saved_payment () {
358
+ $ order = WC_Helper_Order::create_order ();
359
+ $ _POST = $ this ->setup_saved_payment_method ();
360
+ $ intent = WC_Helper_Intention::create_intention ();
361
+
362
+ $ this ->mock_gateway ->expects ( $ this ->once () )
363
363
->method ( 'manage_customer_details_for_order ' )
364
364
->will (
365
365
$ this ->returnValue ( [ wp_get_current_user (), 'cus_123 ' ] )
@@ -371,144 +371,36 @@ public function test_process_payment_returns_correct_redirect_when_using_payment
371
371
372
372
$ this ->set_cart_contains_subscription_items ( false );
373
373
374
- $ result = $ this ->mock_upe_gateway ->process_payment ( $ order ->get_id () );
374
+ $ result = $ this ->mock_gateway ->process_payment ( $ order ->get_id () );
375
375
376
- $ this ->mock_upe_gateway
377
- ->expects ( $ this ->never () )
378
- ->method ( 'manage_customer_details_for_order ' );
379
376
$ this ->assertEquals ( 'success ' , $ result ['result ' ] );
380
377
$ this ->assertEquals ( $ this ->return_url , $ result ['redirect ' ] );
381
378
}
382
379
383
- public function test_upe_process_payment_check_session_order_redirect_to_previous_order () {
384
- $ _POST ['wc_payment_intent_id ' ] = 'pi_mock ' ;
385
-
386
- $ response = [
387
- 'dummy_result ' => 'xyz ' ,
388
- ];
389
-
390
- // Arrange the order is being processed.
391
- $ current_order = WC_Helper_Order::create_order ();
392
- $ current_order_id = $ current_order ->get_id ();
393
-
394
- // Arrange the DPPS to return an order from the session.
395
- $ this ->mock_dpps ->expects ( $ this ->once () )
396
- ->method ( 'check_against_session_processing_order ' )
397
- ->with ( wc_get_order ( $ current_order ) )
398
- ->willReturn ( $ response );
399
-
400
- // Assert: no call to the server to confirm the payment.
401
- $ this ->mock_wcpay_request ( Update_Intention::class, 0 , 'pi_XXXXX ' );
402
-
403
- // Act: process the order but redirect to the previous/session paid order.
404
- $ result = $ this ->mock_upe_gateway ->process_payment ( $ current_order_id );
405
-
406
- // Assert: the result of check_against_session_processing_order.
407
- $ this ->assertSame ( $ response , $ result );
408
- }
409
-
410
- public function test_upe_process_payment_check_session_with_failed_intent_then_order_id_saved_to_session () {
411
- $ _POST ['wc_payment_intent_id ' ] = 'pi_mock ' ;
412
- $ this ->mock_upe_gateway ->expects ( $ this ->once () )
413
- ->method ( 'manage_customer_details_for_order ' )
414
- ->will (
415
- $ this ->returnValue ( [ wp_get_current_user (), 'cus_123 ' ] )
416
- );
417
-
418
- // Arrange the order is being processed.
419
- $ current_order = WC_Helper_Order::create_order ();
420
- $ current_order_id = $ current_order ->get_id ();
421
-
422
- // Arrange a failed intention.
423
- $ intent = WC_Helper_Intention::create_intention ( [ 'status ' => 'failed ' ] );
424
-
425
- // Assert.
426
- $ update_request = $ this ->mock_wcpay_request ( Create_And_Confirm_Intention::class, 1 , $ intent ->get_id () );
427
- $ update_request ->expects ( $ this ->once () )
428
- ->method ( 'format_response ' )
429
- ->willReturn ( $ intent );
430
-
431
- // Arrange the DPPS not to return an order from the session.
432
- $ this ->mock_dpps ->expects ( $ this ->once () )
433
- ->method ( 'check_against_session_processing_order ' )
434
- ->with ( wc_get_order ( $ current_order ) )
435
- ->willReturn ( null );
436
-
437
- // Assert: maybe_update_session_processing_order takes action and its value is kept.
438
- $ this ->mock_dpps ->expects ( $ this ->once () )
439
- ->method ( 'maybe_update_session_processing_order ' )
440
- ->with ( $ current_order_id );
441
-
442
- // Act: process the order but redirect to the previous/session paid order.
443
- $ this ->mock_upe_gateway ->process_payment ( $ current_order_id );
444
- }
445
-
446
- public function test_upe_process_payment_check_session_and_continue_processing () {
447
- $ _POST ['wc_payment_intent_id ' ] = 'pi_mock ' ;
380
+ public function test_process_payment_returns_correct_redirect_when_using_payment_request () {
381
+ $ order = WC_Helper_Order::create_order ();
382
+ $ intent = WC_Helper_Intention::create_intention ();
383
+ $ _POST ['payment_request_type ' ] = 'google_pay ' ;
448
384
449
- $ this ->mock_upe_gateway ->expects ( $ this ->once () )
385
+ $ this ->mock_gateway ->expects ( $ this ->once () )
450
386
->method ( 'manage_customer_details_for_order ' )
451
387
->will (
452
388
$ this ->returnValue ( [ wp_get_current_user (), 'cus_123 ' ] )
453
389
);
454
-
455
- // Arrange the order is being processed.
456
- $ order = WC_Helper_Order::create_order ();
457
- $ order_id = $ order ->get_id ();
458
-
459
- // Arrange a successful intention.
460
- $ intent = WC_Helper_Intention::create_intention ();
461
-
462
- // Arrange the DPPS not to return an order from the session.
463
- $ this ->mock_dpps ->expects ( $ this ->once () )
464
- ->method ( 'check_against_session_processing_order ' )
465
- ->with ( wc_get_order ( $ order ) )
466
- ->willReturn ( null );
467
-
468
- // Assert: Order is removed from the session.
469
- $ this ->mock_dpps ->expects ( $ this ->once () )
470
- ->method ( 'remove_session_processing_order ' )
471
- ->with ( $ order_id );
472
-
473
- // Assert: the payment process continues.
474
390
$ this ->mock_wcpay_request ( Create_And_Confirm_Intention::class, 1 , $ intent ->get_id () )
475
391
->expects ( $ this ->once () )
476
392
->method ( 'format_response ' )
477
393
->willReturn ( $ intent );
394
+ $ this ->set_cart_contains_subscription_items ( false );
478
395
479
- // Act.
480
- $ this ->mock_upe_gateway ->process_payment ( $ order_id );
481
- }
482
-
483
- public function test_upe_check_payment_intent_attached_to_order_succeeded_return_redirection () {
484
- $ _POST ['wc_payment_intent_id ' ] = 'pi_mock ' ;
485
-
486
- $ response = [
487
- 'dummy_result ' => 'xyz ' ,
488
- ];
489
-
490
- // Arrange order.
491
- $ order = WC_Helper_Order::create_order ();
492
- $ order_id = $ order ->get_id ();
493
-
494
- // Arrange the DPPS to return a prepared response.
495
- $ this ->mock_dpps ->expects ( $ this ->once () )
496
- ->method ( 'check_payment_intent_attached_to_order_succeeded ' )
497
- ->with ( wc_get_order ( $ order ) )
498
- ->willReturn ( $ response );
499
-
500
- // Assert: no more call to the server to update the intention.
501
- $ this ->mock_wcpay_request ( Update_Intention::class, 0 );
502
-
503
- // Act: process the order but redirect to the order.
504
- $ result = $ this ->mock_upe_gateway ->process_payment ( $ order_id );
396
+ $ result = $ this ->mock_gateway ->process_payment ( $ order ->get_id () );
505
397
506
- // Assert: the result of check_intent_attached_to_order_succeeded.
507
- $ this ->assertSame ( $ response , $ result );
398
+ $ this -> assertEquals ( ' success ' , $ result[ ' result ' ] );
399
+ $ this ->assertEquals ( $ this -> return_url , $ result[ ' redirect ' ] );
508
400
}
509
401
510
402
public function is_proper_intent_used_with_order_returns_false () {
511
- $ this ->assertFalse ( $ this ->mock_upe_gateway ->is_proper_intent_used_with_order ( WC_Helper_Order::create_order (), 'wrong_intent_id ' ) );
403
+ $ this ->assertFalse ( $ this ->mock_gateway ->is_proper_intent_used_with_order ( WC_Helper_Order::create_order (), 'wrong_intent_id ' ) );
512
404
}
513
405
514
406
public function test_process_redirect_payment_intent_processing () {
@@ -534,7 +426,7 @@ public function test_process_redirect_payment_intent_processing() {
534
426
]
535
427
);
536
428
537
- $ this ->mock_upe_gateway ->expects ( $ this ->once () )
429
+ $ this ->mock_gateway ->expects ( $ this ->once () )
538
430
->method ( 'manage_customer_details_for_order ' )
539
431
->will (
540
432
$ this ->returnValue ( [ $ user , $ customer_id ] )
@@ -548,7 +440,7 @@ public function test_process_redirect_payment_intent_processing() {
548
440
549
441
$ this ->set_cart_contains_subscription_items ( false );
550
442
551
- $ this ->mock_upe_gateway ->process_redirect_payment ( $ order , $ intent_id , $ save_payment_method );
443
+ $ this ->mock_gateway ->process_redirect_payment ( $ order , $ intent_id , $ save_payment_method );
552
444
553
445
$ result_order = wc_get_order ( $ order_id );
554
446
$ note = wc_get_order_notes (
@@ -590,7 +482,7 @@ public function test_process_redirect_payment_intent_succeded() {
590
482
]
591
483
);
592
484
593
- $ this ->mock_upe_gateway ->expects ( $ this ->once () )
485
+ $ this ->mock_gateway ->expects ( $ this ->once () )
594
486
->method ( 'manage_customer_details_for_order ' )
595
487
->will (
596
488
$ this ->returnValue ( [ $ user , $ customer_id ] )
@@ -604,7 +496,7 @@ public function test_process_redirect_payment_intent_succeded() {
604
496
605
497
$ this ->set_cart_contains_subscription_items ( false );
606
498
607
- $ this ->mock_upe_gateway ->process_redirect_payment ( $ order , $ intent_id , $ save_payment_method );
499
+ $ this ->mock_gateway ->process_redirect_payment ( $ order , $ intent_id , $ save_payment_method );
608
500
609
501
$ result_order = wc_get_order ( $ order_id );
610
502
@@ -624,7 +516,7 @@ public function test_validate_order_id_received_vs_intent_meta_order_id_throw_ex
624
516
$ this ->expectExceptionMessage ( "We're not able to process this payment due to the order ID mismatch. Please try again later. " );
625
517
626
518
\PHPUnit_Utils::call_method (
627
- $ this ->mock_upe_gateway ,
519
+ $ this ->mock_gateway ,
628
520
'validate_order_id_received_vs_intent_meta_order_id ' ,
629
521
[ $ order , $ intent_metadata ]
630
522
);
@@ -635,7 +527,7 @@ public function test_validate_order_id_received_vs_intent_meta_order_id_returnin
635
527
$ intent_metadata = [ 'order_id ' => (string ) ( $ order ->get_id () ) ];
636
528
637
529
$ res = \PHPUnit_Utils::call_method (
638
- $ this ->mock_upe_gateway ,
530
+ $ this ->mock_gateway ,
639
531
'validate_order_id_received_vs_intent_meta_order_id ' ,
640
532
[ $ order , $ intent_metadata ]
641
533
);
@@ -721,7 +613,7 @@ public function test_correct_payment_method_title_for_order() {
721
613
];
722
614
723
615
foreach ( $ charge_payment_method_details as $ i => $ payment_method_details ) {
724
- $ this ->mock_upe_gateway ->set_payment_method_title_for_order ( $ order , $ payment_method_details ['type ' ], $ payment_method_details );
616
+ $ this ->mock_gateway ->set_payment_method_title_for_order ( $ order , $ payment_method_details ['type ' ], $ payment_method_details );
725
617
$ this ->assertEquals ( $ expected_payment_method_titles [ $ i ], $ order ->get_payment_method_title () );
726
618
}
727
619
}
@@ -1019,24 +911,7 @@ public function test_create_token_from_setup_intent_adds_token() {
1019
911
$ this ->returnValue ( $ mock_token )
1020
912
);
1021
913
1022
- $ this ->assertEquals ( $ mock_token , $ this ->mock_upe_gateway ->create_token_from_setup_intent ( $ mock_setup_intent_id , $ mock_user ) );
1023
- }
1024
-
1025
- public function test_process_payment_rejects_with_cached_minimum_acount () {
1026
- $ order = WC_Helper_Order::create_order ();
1027
- $ order ->set_currency ( 'USD ' );
1028
- $ order ->set_total ( 0.45 );
1029
- $ order ->save ();
1030
-
1031
- set_transient ( 'wcpay_minimum_amount_usd ' , '50 ' , DAY_IN_SECONDS );
1032
- $ _POST ['wc_payment_intent_id ' ] = 'pi_mock ' ;
1033
-
1034
- // Make sure that the payment was not actually processed.
1035
- $ price = wp_strip_all_tags ( html_entity_decode ( wc_price ( 0.5 , [ 'currency ' => 'USD ' ] ) ) );
1036
- $ message = 'The selected payment method requires a total amount of at least ' . $ price . '. ' ;
1037
- $ this ->expectException ( Exception::class );
1038
- $ this ->expectExceptionMessage ( $ message );
1039
- $ this ->mock_upe_gateway ->process_payment ( $ order ->get_id () );
914
+ $ this ->assertEquals ( $ mock_token , $ this ->mock_gateway ->create_token_from_setup_intent ( $ mock_setup_intent_id , $ mock_user ) );
1040
915
}
1041
916
1042
917
public function test_exception_will_be_thrown_if_phone_number_is_invalid () {
@@ -1045,20 +920,20 @@ public function test_exception_will_be_thrown_if_phone_number_is_invalid() {
1045
920
$ order ->save ();
1046
921
$ this ->expectException ( Exception::class );
1047
922
$ this ->expectExceptionMessage ( 'Invalid phone number. ' );
1048
- $ this ->mock_upe_gateway ->process_payment ( $ order ->get_id () );
923
+ $ this ->mock_gateway ->process_payment ( $ order ->get_id () );
1049
924
}
1050
925
1051
926
public function test_remove_link_payment_method_if_card_disabled () {
1052
- $ this ->mock_upe_gateway ->settings ['upe_enabled_payment_method_ids ' ] = [ 'link ' ];
927
+ $ this ->mock_gateway ->settings ['upe_enabled_payment_method_ids ' ] = [ 'link ' ];
1053
928
1054
- $ this ->mock_upe_gateway
929
+ $ this ->mock_gateway
1055
930
->expects ( $ this ->once () )
1056
931
->method ( 'get_upe_enabled_payment_method_statuses ' )
1057
932
->will (
1058
933
$ this ->returnValue ( [ 'link_payments ' => [ 'status ' => 'active ' ] ] )
1059
934
);
1060
935
1061
- $ this ->assertSame ( $ this ->mock_upe_gateway ->get_payment_method_ids_enabled_at_checkout (), [] );
936
+ $ this ->assertSame ( $ this ->mock_gateway ->get_payment_method_ids_enabled_at_checkout (), [] );
1062
937
}
1063
938
1064
939
/**
@@ -1143,7 +1018,7 @@ private function set_get_upe_enabled_payment_method_statuses_return_value( $retu
1143
1018
],
1144
1019
];
1145
1020
}
1146
- $ this ->mock_upe_gateway
1021
+ $ this ->mock_gateway
1147
1022
->expects ( $ this ->any () )
1148
1023
->method ( 'get_upe_enabled_payment_method_statuses ' )
1149
1024
->will ( $ this ->returnValue ( $ return_value ) );
0 commit comments