Skip to content

Commit

Permalink
merge split UPE tests into main gateway - II
Browse files Browse the repository at this point in the history
  • Loading branch information
Timur Karimov committed Jan 2, 2024
1 parent 374b219 commit 4d9bbb6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 99 deletions.
49 changes: 0 additions & 49 deletions tests/unit/payment-methods/test-class-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,55 +354,6 @@ public function tear_down() {
wcpay_get_test_container()->reset_all_replacements();
}

public function test_process_payment_returns_correct_redirect_when_using_saved_payment() {
$order = WC_Helper_Order::create_order();
$_POST = $this->setup_saved_payment_method();
$intent = WC_Helper_Intention::create_intention();

$this->mock_gateway->expects( $this->once() )
->method( 'manage_customer_details_for_order' )
->will(
$this->returnValue( [ wp_get_current_user(), 'cus_123' ] )
);
$this->mock_wcpay_request( Create_And_Confirm_Intention::class, 1, $intent->get_id() )
->expects( $this->once() )
->method( 'format_response' )
->willReturn( $intent );

$this->set_cart_contains_subscription_items( false );

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

$this->assertEquals( 'success', $result['result'] );
$this->assertEquals( $this->return_url, $result['redirect'] );
}

public function test_process_payment_returns_correct_redirect_when_using_payment_request() {
$order = WC_Helper_Order::create_order();
$intent = WC_Helper_Intention::create_intention();
$_POST['payment_request_type'] = 'google_pay';

$this->mock_gateway->expects( $this->once() )
->method( 'manage_customer_details_for_order' )
->will(
$this->returnValue( [ wp_get_current_user(), 'cus_123' ] )
);
$this->mock_wcpay_request( Create_And_Confirm_Intention::class, 1, $intent->get_id() )
->expects( $this->once() )
->method( 'format_response' )
->willReturn( $intent );
$this->set_cart_contains_subscription_items( false );

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

$this->assertEquals( 'success', $result['result'] );
$this->assertEquals( $this->return_url, $result['redirect'] );
}

public function is_proper_intent_used_with_order_returns_false() {
$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() {
$order = WC_Helper_Order::create_order();
$order_id = $order->get_id();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,38 +353,6 @@ public function tear_down() {
wcpay_get_test_container()->reset_all_replacements();
}

public function test_process_payment_returns_correct_redirect_when_using_saved_payment() {
$mock_card_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
$user = wp_get_current_user();
$customer_id = 'cus_mock';

$order = WC_Helper_Order::create_order();
$_POST = $this->setup_saved_payment_method();
$mock_card_payment_gateway->expects( $this->once() )
->method( 'manage_customer_details_for_order' )
->will(
$this->returnValue( [ $user, $customer_id ] )
);
$mock_card_payment_gateway->expects( $this->any() )
->method( 'get_upe_enabled_payment_method_ids' )
->will(
$this->returnValue( [ Payment_Method::CARD ] )
);
$this->mock_wcpay_request( Create_And_Confirm_Intention::class, 1 )
->expects( $this->once() )
->method( 'format_response' )
->willReturn(
WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::PROCESSING ] )
);

$this->set_cart_contains_subscription_items( false );

$result = $mock_card_payment_gateway->process_payment( $order->get_id() );

$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';
$mock_upe_gateway = $this->mock_payment_gateways[ Payment_Method::SEPA ];
Expand Down Expand Up @@ -531,10 +499,6 @@ public function test_process_redirect_payment_intent_succeded() {
$this->assertEquals( Order_Status::PROCESSING, $result_order->get_status() );
}

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

public function test_process_redirect_setup_intent_succeded() {

$order = WC_Helper_Order::create_order();
Expand Down
64 changes: 50 additions & 14 deletions tests/unit/test-class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
/**
* WC_Payment_Gateway_WCPay unit tests.
*/
class Timur_Test extends WCPAY_UnitTestCase {
class WC_Payment_Gateway_WCPay_Test extends WCPAY_UnitTestCase {

const NO_REQUIREMENTS = false;
const PENDING_REQUIREMENTS = true;
Expand Down Expand Up @@ -2377,6 +2377,55 @@ public function test_new_process_payment() {
);
}

public function test_process_payment_returns_correct_redirect() {
$order = WC_Helper_Order::create_order();
$_POST = [ 'wcpay-payment-method' => 'pm_mock' ];

$this->mock_wcpay_request( Create_And_Confirm_Intention::class, 1 )
->expects( $this->once() )
->method( 'format_response' )
->willReturn(
WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::PROCESSING ] )
);

$this->mock_token_service
->expects( $this->once() )
->method( 'add_payment_method_to_user' )
->willReturn( new WC_Payment_Token_CC() );

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

$this->assertEquals( 'success', $result['result'] );
$this->assertEquals( $order->get_checkout_order_received_url(), $result['redirect'] );
}

public function test_process_payment_returns_correct_redirect_when_using_payment_request() {
$order = WC_Helper_Order::create_order();
$_POST['payment_request_type'] = 'google_pay';
$_POST = [ 'wcpay-payment-method' => 'pm_mock' ];

$this->mock_wcpay_request( Create_And_Confirm_Intention::class, 1 )
->expects( $this->once() )
->method( 'format_response' )
->willReturn(
WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::PROCESSING ] )
);

$this->mock_token_service
->expects( $this->once() )
->method( 'add_payment_method_to_user' )
->willReturn( new WC_Payment_Token_CC() );

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

$this->assertEquals( 'success', $result['result'] );
$this->assertEquals( $order->get_checkout_order_received_url(), $result['redirect'] );
}

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

/**
* Sets up the expectation for a certain factor for the new payment
* process to be either set or unset.
Expand Down Expand Up @@ -2496,17 +2545,4 @@ function( $gateway ) use ( $payment_method_id ) {
)
) )[0] ?? null;
}

/**
* Helper function to mock subscriptions for internal UPE payment methods.
*/
private function set_cart_contains_subscription_items( $cart_contains_subscriptions ) {
foreach ( $this->mock_payment_methods as $mock_payment_method ) {
$mock_payment_method->expects( $this->any() )
->method( 'is_subscription_item_in_cart' )
->will(
$this->returnValue( $cart_contains_subscriptions )
);
}
}
}

0 comments on commit 4d9bbb6

Please sign in to comment.