Skip to content

Commit d256f60

Browse files
author
Timur Karimov
committed
further tests cleanup
1 parent 2122fd9 commit d256f60

13 files changed

+48
-315
lines changed

tests/unit/admin/test-class-wc-rest-payments-tos-controller.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ public function set_up() {
6464
$order_service = new WC_Payments_Order_Service( $this->createMock( WC_Payments_API_Client::class ) );
6565
$action_scheduler_service = new WC_Payments_Action_Scheduler_Service( $mock_api_client, $order_service );
6666
$mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
67-
$mock_payment_method = $this->getMockBuilder( CC_Payment_Method::class )
68-
->setConstructorArgs( [ $token_service ] )
69-
->onlyMethods( [ 'is_subscription_item_in_cart' ] )
70-
->getMock();
67+
$mock_payment_method = $this->createMock( CC_Payment_Method::class );
7168

7269
$this->gateway = new WC_Payment_Gateway_WCPay(
7370
$mock_api_client,

tests/unit/payment-methods/test-class-upe-payment-gateway.php

Lines changed: 38 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use WCPay\Constants\Intent_Status;
1414
use WCPay\Core\Server\Request\Get_Intention;
1515
use WCPay\Core\Server\Request\Get_Setup_Intention;
16-
use WCPay\Core\Server\Request\Update_Intention;
1716
use WCPay\Exceptions\Process_Payment_Exception;
1817
use WCPay\WooPay\WooPay_Utilities;
1918
use WCPay\Session_Rate_Limiter;
@@ -54,7 +53,7 @@ class UPE_Payment_Gateway_Test extends WCPAY_UnitTestCase {
5453
*
5554
* @var WC_Payment_Gateway_WCPay
5655
*/
57-
private $mock_upe_gateway;
56+
private $mock_gateway;
5857

5958
/**
6059
* Mock WC_Payments_Customer_Service.
@@ -182,7 +181,7 @@ public function set_up() {
182181
// Note that we cannot use createStub here since it's not defined in PHPUnit 6.5.
183182
$this->mock_api_client = $this->getMockBuilder( 'WC_Payments_API_Client' )
184183
->disableOriginalConstructor()
185-
->setMethods(
184+
->onlyMethods(
186185
[
187186
'get_payment_method',
188187
'is_server_connected',
@@ -221,7 +220,7 @@ public function set_up() {
221220
// Arrange: Mock WC_Payments_Customer_Service so its methods aren't called directly.
222221
$this->mock_token_service = $this->getMockBuilder( 'WC_Payments_Token_Service' )
223222
->disableOriginalConstructor()
224-
->setMethods( [ 'add_payment_method_to_user' ] )
223+
->onlyMethods( [ 'add_payment_method_to_user' ] )
225224
->getMock();
226225

227226
// Arrange: Mock WC_Payments_Action_Scheduler_Service so its methods aren't called directly.
@@ -254,7 +253,7 @@ public function set_up() {
254253
foreach ( $payment_method_classes as $payment_method_class ) {
255254
$mock_payment_method = $this->getMockBuilder( $payment_method_class )
256255
->setConstructorArgs( [ $this->mock_token_service ] )
257-
->setMethods( [ 'is_subscription_item_in_cart', 'get_icon' ] )
256+
->onlyMethods( [ 'is_subscription_item_in_cart', 'get_icon' ] )
258257
->getMock();
259258
$this->mock_payment_methods[ $mock_payment_method->get_id() ] = $mock_payment_method;
260259
}
@@ -265,7 +264,7 @@ public function set_up() {
265264
$this->mock_api_client,
266265
]
267266
)
268-
->setMethods(
267+
->onlyMethods(
269268
[
270269
'get_payment_method_id_for_order',
271270
]
@@ -280,7 +279,7 @@ public function set_up() {
280279

281280
// Arrange: Mock WC_Payment_Gateway_WCPay so that some of its methods can be
282281
// 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 )
284283
->setConstructorArgs(
285284
[
286285
$this->mock_api_client,
@@ -309,13 +308,13 @@ public function set_up() {
309308
->getMock();
310309

311310
// 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
313312
->expects( $this->any() )
314313
->method( 'get_return_url' )
315314
->will(
316315
$this->returnValue( $this->return_url )
317316
);
318-
$this->mock_upe_gateway
317+
$this->mock_gateway
319318
->expects( $this->any() )
320319
->method( 'parent_process_payment' )
321320
->will(
@@ -355,11 +354,12 @@ public function tear_down() {
355354
wcpay_get_test_container()->reset_all_replacements();
356355
}
357356

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() )
363363
->method( 'manage_customer_details_for_order' )
364364
->will(
365365
$this->returnValue( [ wp_get_current_user(), 'cus_123' ] )
@@ -371,144 +371,36 @@ public function test_process_payment_returns_correct_redirect_when_using_payment
371371

372372
$this->set_cart_contains_subscription_items( false );
373373

374-
$result = $this->mock_upe_gateway->process_payment( $order->get_id() );
374+
$result = $this->mock_gateway->process_payment( $order->get_id() );
375375

376-
$this->mock_upe_gateway
377-
->expects( $this->never() )
378-
->method( 'manage_customer_details_for_order' );
379376
$this->assertEquals( 'success', $result['result'] );
380377
$this->assertEquals( $this->return_url, $result['redirect'] );
381378
}
382379

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';
448384

449-
$this->mock_upe_gateway->expects( $this->once() )
385+
$this->mock_gateway->expects( $this->once() )
450386
->method( 'manage_customer_details_for_order' )
451387
->will(
452388
$this->returnValue( [ wp_get_current_user(), 'cus_123' ] )
453389
);
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.
474390
$this->mock_wcpay_request( Create_And_Confirm_Intention::class, 1, $intent->get_id() )
475391
->expects( $this->once() )
476392
->method( 'format_response' )
477393
->willReturn( $intent );
394+
$this->set_cart_contains_subscription_items( false );
478395

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() );
505397

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'] );
508400
}
509401

510402
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' ) );
512404
}
513405

514406
public function test_process_redirect_payment_intent_processing() {
@@ -534,7 +426,7 @@ public function test_process_redirect_payment_intent_processing() {
534426
]
535427
);
536428

537-
$this->mock_upe_gateway->expects( $this->once() )
429+
$this->mock_gateway->expects( $this->once() )
538430
->method( 'manage_customer_details_for_order' )
539431
->will(
540432
$this->returnValue( [ $user, $customer_id ] )
@@ -548,7 +440,7 @@ public function test_process_redirect_payment_intent_processing() {
548440

549441
$this->set_cart_contains_subscription_items( false );
550442

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 );
552444

553445
$result_order = wc_get_order( $order_id );
554446
$note = wc_get_order_notes(
@@ -590,7 +482,7 @@ public function test_process_redirect_payment_intent_succeded() {
590482
]
591483
);
592484

593-
$this->mock_upe_gateway->expects( $this->once() )
485+
$this->mock_gateway->expects( $this->once() )
594486
->method( 'manage_customer_details_for_order' )
595487
->will(
596488
$this->returnValue( [ $user, $customer_id ] )
@@ -604,7 +496,7 @@ public function test_process_redirect_payment_intent_succeded() {
604496

605497
$this->set_cart_contains_subscription_items( false );
606498

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 );
608500

609501
$result_order = wc_get_order( $order_id );
610502

@@ -624,7 +516,7 @@ public function test_validate_order_id_received_vs_intent_meta_order_id_throw_ex
624516
$this->expectExceptionMessage( "We're not able to process this payment due to the order ID mismatch. Please try again later." );
625517

626518
\PHPUnit_Utils::call_method(
627-
$this->mock_upe_gateway,
519+
$this->mock_gateway,
628520
'validate_order_id_received_vs_intent_meta_order_id',
629521
[ $order, $intent_metadata ]
630522
);
@@ -635,7 +527,7 @@ public function test_validate_order_id_received_vs_intent_meta_order_id_returnin
635527
$intent_metadata = [ 'order_id' => (string) ( $order->get_id() ) ];
636528

637529
$res = \PHPUnit_Utils::call_method(
638-
$this->mock_upe_gateway,
530+
$this->mock_gateway,
639531
'validate_order_id_received_vs_intent_meta_order_id',
640532
[ $order, $intent_metadata ]
641533
);
@@ -721,7 +613,7 @@ public function test_correct_payment_method_title_for_order() {
721613
];
722614

723615
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 );
725617
$this->assertEquals( $expected_payment_method_titles[ $i ], $order->get_payment_method_title() );
726618
}
727619
}
@@ -1019,24 +911,7 @@ public function test_create_token_from_setup_intent_adds_token() {
1019911
$this->returnValue( $mock_token )
1020912
);
1021913

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 ) );
1040915
}
1041916

1042917
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() {
1045920
$order->save();
1046921
$this->expectException( Exception::class );
1047922
$this->expectExceptionMessage( 'Invalid phone number.' );
1048-
$this->mock_upe_gateway->process_payment( $order->get_id() );
923+
$this->mock_gateway->process_payment( $order->get_id() );
1049924
}
1050925

1051926
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' ];
1053928

1054-
$this->mock_upe_gateway
929+
$this->mock_gateway
1055930
->expects( $this->once() )
1056931
->method( 'get_upe_enabled_payment_method_statuses' )
1057932
->will(
1058933
$this->returnValue( [ 'link_payments' => [ 'status' => 'active' ] ] )
1059934
);
1060935

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(), [] );
1062937
}
1063938

1064939
/**
@@ -1143,7 +1018,7 @@ private function set_get_upe_enabled_payment_method_statuses_return_value( $retu
11431018
],
11441019
];
11451020
}
1146-
$this->mock_upe_gateway
1021+
$this->mock_gateway
11471022
->expects( $this->any() )
11481023
->method( 'get_upe_enabled_payment_method_statuses' )
11491024
->will( $this->returnValue( $return_value ) );

0 commit comments

Comments
 (0)