Skip to content

Commit

Permalink
further tests cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Timur Karimov committed Dec 27, 2023
1 parent 2122fd9 commit 5105bfb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ public function set_up() {
$order_service = new WC_Payments_Order_Service( $this->createMock( WC_Payments_API_Client::class ) );
$action_scheduler_service = new WC_Payments_Action_Scheduler_Service( $mock_api_client, $order_service );
$mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
$mock_payment_method = $this->getMockBuilder( CC_Payment_Method::class )
->setConstructorArgs( [ $token_service ] )
->onlyMethods( [ 'is_subscription_item_in_cart' ] )
->getMock();
$mock_payment_method = $this->createMock( CC_Payment_Method::class );

$this->gateway = new WC_Payment_Gateway_WCPay(
$mock_api_client,
Expand Down
198 changes: 25 additions & 173 deletions tests/unit/payment-methods/test-class-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use WCPay\Constants\Intent_Status;
use WCPay\Core\Server\Request\Get_Intention;
use WCPay\Core\Server\Request\Get_Setup_Intention;
use WCPay\Core\Server\Request\Update_Intention;
use WCPay\Exceptions\Process_Payment_Exception;
use WCPay\WooPay\WooPay_Utilities;
use WCPay\Session_Rate_Limiter;
Expand Down Expand Up @@ -54,7 +53,7 @@ class UPE_Payment_Gateway_Test extends WCPAY_UnitTestCase {
*
* @var WC_Payment_Gateway_WCPay
*/
private $mock_upe_gateway;
private $mock_gateway;

/**
* Mock WC_Payments_Customer_Service.
Expand Down Expand Up @@ -182,7 +181,7 @@ public function set_up() {
// Note that we cannot use createStub here since it's not defined in PHPUnit 6.5.
$this->mock_api_client = $this->getMockBuilder( 'WC_Payments_API_Client' )
->disableOriginalConstructor()
->setMethods(
->onlyMethods(
[
'get_payment_method',
'is_server_connected',
Expand Down Expand Up @@ -221,7 +220,7 @@ public function set_up() {
// Arrange: Mock WC_Payments_Customer_Service so its methods aren't called directly.
$this->mock_token_service = $this->getMockBuilder( 'WC_Payments_Token_Service' )
->disableOriginalConstructor()
->setMethods( [ 'add_payment_method_to_user' ] )
->onlyMethods( [ 'add_payment_method_to_user' ] )
->getMock();

// Arrange: Mock WC_Payments_Action_Scheduler_Service so its methods aren't called directly.
Expand Down Expand Up @@ -254,7 +253,7 @@ public function set_up() {
foreach ( $payment_method_classes as $payment_method_class ) {
$mock_payment_method = $this->getMockBuilder( $payment_method_class )
->setConstructorArgs( [ $this->mock_token_service ] )
->setMethods( [ 'is_subscription_item_in_cart', 'get_icon' ] )
->onlyMethods( [ 'is_subscription_item_in_cart', 'get_icon' ] )
->getMock();
$this->mock_payment_methods[ $mock_payment_method->get_id() ] = $mock_payment_method;
}
Expand All @@ -265,7 +264,7 @@ public function set_up() {
$this->mock_api_client,
]
)
->setMethods(
->onlyMethods(
[
'get_payment_method_id_for_order',
]
Expand All @@ -280,7 +279,7 @@ public function set_up() {

// Arrange: Mock WC_Payment_Gateway_WCPay so that some of its methods can be
// mocked, and their return values can be used for testing.
$this->mock_upe_gateway = $this->getMockBuilder( WC_Payment_Gateway_WCPay::class )
$this->mock_gateway = $this->getMockBuilder( WC_Payment_Gateway_WCPay::class )
->setConstructorArgs(
[
$this->mock_api_client,
Expand Down Expand Up @@ -309,13 +308,13 @@ public function set_up() {
->getMock();

// Arrange: Set the return value of get_return_url() so it can be used in a test later.
$this->mock_upe_gateway
$this->mock_gateway
->expects( $this->any() )
->method( 'get_return_url' )
->will(
$this->returnValue( $this->return_url )
);
$this->mock_upe_gateway
$this->mock_gateway
->expects( $this->any() )
->method( 'parent_process_payment' )
->will(
Expand Down Expand Up @@ -359,7 +358,8 @@ public function test_process_payment_returns_correct_redirect_when_using_payment
$order = WC_Helper_Order::create_order();
$intent = WC_Helper_Intention::create_intention();
$_POST['payment_request_type'] = 'google_pay';
$this->mock_upe_gateway->expects( $this->once() )

$this->mock_gateway->expects( $this->once() )
->method( 'manage_customer_details_for_order' )
->will(
$this->returnValue( [ wp_get_current_user(), 'cus_123' ] )
Expand All @@ -368,147 +368,16 @@ public function test_process_payment_returns_correct_redirect_when_using_payment
->expects( $this->once() )
->method( 'format_response' )
->willReturn( $intent );

$this->set_cart_contains_subscription_items( false );

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

$this->mock_upe_gateway
->expects( $this->never() )
->method( 'manage_customer_details_for_order' );
$this->assertEquals( 'success', $result['result'] );
$this->assertEquals( $this->return_url, $result['redirect'] );
}

public function test_upe_process_payment_check_session_order_redirect_to_previous_order() {
$_POST['wc_payment_intent_id'] = 'pi_mock';

$response = [
'dummy_result' => 'xyz',
];

// Arrange the order is being processed.
$current_order = WC_Helper_Order::create_order();
$current_order_id = $current_order->get_id();

// Arrange the DPPS to return an order from the session.
$this->mock_dpps->expects( $this->once() )
->method( 'check_against_session_processing_order' )
->with( wc_get_order( $current_order ) )
->willReturn( $response );

// Assert: no call to the server to confirm the payment.
$this->mock_wcpay_request( Update_Intention::class, 0, 'pi_XXXXX' );

// Act: process the order but redirect to the previous/session paid order.
$result = $this->mock_upe_gateway->process_payment( $current_order_id );

// Assert: the result of check_against_session_processing_order.
$this->assertSame( $response, $result );
}

public function test_upe_process_payment_check_session_with_failed_intent_then_order_id_saved_to_session() {
$_POST['wc_payment_intent_id'] = 'pi_mock';
$this->mock_upe_gateway->expects( $this->once() )
->method( 'manage_customer_details_for_order' )
->will(
$this->returnValue( [ wp_get_current_user(), 'cus_123' ] )
);

// Arrange the order is being processed.
$current_order = WC_Helper_Order::create_order();
$current_order_id = $current_order->get_id();

// Arrange a failed intention.
$intent = WC_Helper_Intention::create_intention( [ 'status' => 'failed' ] );

// Assert.
$update_request = $this->mock_wcpay_request( Create_And_Confirm_Intention::class, 1, $intent->get_id() );
$update_request->expects( $this->once() )
->method( 'format_response' )
->willReturn( $intent );

// Arrange the DPPS not to return an order from the session.
$this->mock_dpps->expects( $this->once() )
->method( 'check_against_session_processing_order' )
->with( wc_get_order( $current_order ) )
->willReturn( null );

// Assert: maybe_update_session_processing_order takes action and its value is kept.
$this->mock_dpps->expects( $this->once() )
->method( 'maybe_update_session_processing_order' )
->with( $current_order_id );

// Act: process the order but redirect to the previous/session paid order.
$this->mock_upe_gateway->process_payment( $current_order_id );
}

public function test_upe_process_payment_check_session_and_continue_processing() {
$_POST['wc_payment_intent_id'] = 'pi_mock';

$this->mock_upe_gateway->expects( $this->once() )
->method( 'manage_customer_details_for_order' )
->will(
$this->returnValue( [ wp_get_current_user(), 'cus_123' ] )
);

// Arrange the order is being processed.
$order = WC_Helper_Order::create_order();
$order_id = $order->get_id();

// Arrange a successful intention.
$intent = WC_Helper_Intention::create_intention();

// Arrange the DPPS not to return an order from the session.
$this->mock_dpps->expects( $this->once() )
->method( 'check_against_session_processing_order' )
->with( wc_get_order( $order ) )
->willReturn( null );

// Assert: Order is removed from the session.
$this->mock_dpps->expects( $this->once() )
->method( 'remove_session_processing_order' )
->with( $order_id );

// Assert: the payment process continues.
$this->mock_wcpay_request( Create_And_Confirm_Intention::class, 1, $intent->get_id() )
->expects( $this->once() )
->method( 'format_response' )
->willReturn( $intent );

// Act.
$this->mock_upe_gateway->process_payment( $order_id );
}

public function test_upe_check_payment_intent_attached_to_order_succeeded_return_redirection() {
$_POST['wc_payment_intent_id'] = 'pi_mock';

$response = [
'dummy_result' => 'xyz',
];

// Arrange order.
$order = WC_Helper_Order::create_order();
$order_id = $order->get_id();

// Arrange the DPPS to return a prepared response.
$this->mock_dpps->expects( $this->once() )
->method( 'check_payment_intent_attached_to_order_succeeded' )
->with( wc_get_order( $order ) )
->willReturn( $response );

// Assert: no more call to the server to update the intention.
$this->mock_wcpay_request( Update_Intention::class, 0 );

// Act: process the order but redirect to the order.
$result = $this->mock_upe_gateway->process_payment( $order_id );

// Assert: the result of check_intent_attached_to_order_succeeded.
$this->assertSame( $response, $result );
}

public function is_proper_intent_used_with_order_returns_false() {
$this->assertFalse( $this->mock_upe_gateway->is_proper_intent_used_with_order( WC_Helper_Order::create_order(), 'wrong_intent_id' ) );
$this->assertFalse( $this->mock_gateway->is_proper_intent_used_with_order( WC_Helper_Order::create_order(), 'wrong_intent_id' ) );
}

public function test_process_redirect_payment_intent_processing() {
Expand All @@ -534,7 +403,7 @@ public function test_process_redirect_payment_intent_processing() {
]
);

$this->mock_upe_gateway->expects( $this->once() )
$this->mock_gateway->expects( $this->once() )
->method( 'manage_customer_details_for_order' )
->will(
$this->returnValue( [ $user, $customer_id ] )
Expand All @@ -548,7 +417,7 @@ public function test_process_redirect_payment_intent_processing() {

$this->set_cart_contains_subscription_items( false );

$this->mock_upe_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
$this->mock_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );

$result_order = wc_get_order( $order_id );
$note = wc_get_order_notes(
Expand Down Expand Up @@ -590,7 +459,7 @@ public function test_process_redirect_payment_intent_succeded() {
]
);

$this->mock_upe_gateway->expects( $this->once() )
$this->mock_gateway->expects( $this->once() )
->method( 'manage_customer_details_for_order' )
->will(
$this->returnValue( [ $user, $customer_id ] )
Expand All @@ -604,7 +473,7 @@ public function test_process_redirect_payment_intent_succeded() {

$this->set_cart_contains_subscription_items( false );

$this->mock_upe_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
$this->mock_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );

$result_order = wc_get_order( $order_id );

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

\PHPUnit_Utils::call_method(
$this->mock_upe_gateway,
$this->mock_gateway,
'validate_order_id_received_vs_intent_meta_order_id',
[ $order, $intent_metadata ]
);
Expand All @@ -635,7 +504,7 @@ public function test_validate_order_id_received_vs_intent_meta_order_id_returnin
$intent_metadata = [ 'order_id' => (string) ( $order->get_id() ) ];

$res = \PHPUnit_Utils::call_method(
$this->mock_upe_gateway,
$this->mock_gateway,
'validate_order_id_received_vs_intent_meta_order_id',
[ $order, $intent_metadata ]
);
Expand Down Expand Up @@ -721,7 +590,7 @@ public function test_correct_payment_method_title_for_order() {
];

foreach ( $charge_payment_method_details as $i => $payment_method_details ) {
$this->mock_upe_gateway->set_payment_method_title_for_order( $order, $payment_method_details['type'], $payment_method_details );
$this->mock_gateway->set_payment_method_title_for_order( $order, $payment_method_details['type'], $payment_method_details );
$this->assertEquals( $expected_payment_method_titles[ $i ], $order->get_payment_method_title() );
}
}
Expand Down Expand Up @@ -1019,24 +888,7 @@ public function test_create_token_from_setup_intent_adds_token() {
$this->returnValue( $mock_token )
);

$this->assertEquals( $mock_token, $this->mock_upe_gateway->create_token_from_setup_intent( $mock_setup_intent_id, $mock_user ) );
}

public function test_process_payment_rejects_with_cached_minimum_acount() {
$order = WC_Helper_Order::create_order();
$order->set_currency( 'USD' );
$order->set_total( 0.45 );
$order->save();

set_transient( 'wcpay_minimum_amount_usd', '50', DAY_IN_SECONDS );
$_POST['wc_payment_intent_id'] = 'pi_mock';

// Make sure that the payment was not actually processed.
$price = wp_strip_all_tags( html_entity_decode( wc_price( 0.5, [ 'currency' => 'USD' ] ) ) );
$message = 'The selected payment method requires a total amount of at least ' . $price . '.';
$this->expectException( Exception::class );
$this->expectExceptionMessage( $message );
$this->mock_upe_gateway->process_payment( $order->get_id() );
$this->assertEquals( $mock_token, $this->mock_gateway->create_token_from_setup_intent( $mock_setup_intent_id, $mock_user ) );
}

public function test_exception_will_be_thrown_if_phone_number_is_invalid() {
Expand All @@ -1045,20 +897,20 @@ public function test_exception_will_be_thrown_if_phone_number_is_invalid() {
$order->save();
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Invalid phone number.' );
$this->mock_upe_gateway->process_payment( $order->get_id() );
$this->mock_gateway->process_payment( $order->get_id() );
}

public function test_remove_link_payment_method_if_card_disabled() {
$this->mock_upe_gateway->settings['upe_enabled_payment_method_ids'] = [ 'link' ];
$this->mock_gateway->settings['upe_enabled_payment_method_ids'] = [ 'link' ];

$this->mock_upe_gateway
$this->mock_gateway
->expects( $this->once() )
->method( 'get_upe_enabled_payment_method_statuses' )
->will(
$this->returnValue( [ 'link_payments' => [ 'status' => 'active' ] ] )
);

$this->assertSame( $this->mock_upe_gateway->get_payment_method_ids_enabled_at_checkout(), [] );
$this->assertSame( $this->mock_gateway->get_payment_method_ids_enabled_at_checkout(), [] );
}

/**
Expand Down Expand Up @@ -1143,7 +995,7 @@ private function set_get_upe_enabled_payment_method_statuses_return_value( $retu
],
];
}
$this->mock_upe_gateway
$this->mock_gateway
->expects( $this->any() )
->method( 'get_upe_enabled_payment_method_statuses' )
->will( $this->returnValue( $return_value ) );
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/test-class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
use WCPay\Core\Server\Request\Get_Charge;
use WCPay\Core\Server\Request\Get_Intention;
use WCPay\Core\Server\Request\Get_Setup_Intention;
use WCPay\Core\Server\Request\Update_Intention;
use WCPay\Constants\Order_Status;
use WCPay\Constants\Payment_Type;
use WCPay\Constants\Intent_Status;
use WCPay\Duplicate_Payment_Prevention_Service;
use WCPay\Exceptions\Amount_Too_Small_Exception;
Expand Down

0 comments on commit 5105bfb

Please sign in to comment.