Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
annemirasol committed Jan 23, 2025
1 parent 096e5c8 commit cbb602c
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,8 @@ private function process_payment_with_confirmation_token( int $order_id ) {
try {
$payment_information = $this->prepare_payment_information_from_request( $order );

$this->validate_selected_payment_method_type( $payment_information, $order->get_billing_country() );

$this->set_customer_id_for_order( $order, $payment_information['customer'] );

if ( $this->is_payment_needed( $order->get_id() ) ) {
Expand All @@ -969,7 +971,6 @@ private function process_payment_with_confirmation_token( int $order_id ) {

$selected_payment_type = $payment_information['selected_payment_type'];

// TODO: We want to set the payment method title to 'Amazon Pay', if applicable.
$this->set_payment_method_title_for_order( $order, $selected_payment_type );

$return_url = $this->get_return_url( $order );
Expand Down
156 changes: 123 additions & 33 deletions tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,10 @@ public function test_process_payment_returns_valid_response() {

/**
* Test basic checkout process_payment flow with deferred intent.
*
* @dataProvider provide_process_payment_deferred_intent_returns_valid_response
*/
public function test_process_payment_deferred_intent_returns_valid_response() {
public function test_process_payment_deferred_intent_returns_valid_response( $post_vars ) {
$customer_id = 'cus_mock';
$order = WC_Helper_Order::create_order();
$currency = $order->get_currency();
Expand All @@ -382,11 +384,7 @@ public function test_process_payment_deferred_intent_returns_valid_response() {
);

// Set the appropriate POST flag to trigger a deferred intent request.
$_POST = [
'payment_method' => 'stripe',
'wc-stripe-payment-method' => 'pm_mock',
'wc-stripe-is-deferred-intent' => '1',
];
$_POST = $post_vars;

$this->mock_gateway->intent_controller
->expects( $this->once() )
Expand All @@ -408,6 +406,30 @@ public function test_process_payment_deferred_intent_returns_valid_response() {
$this->assertEquals( self::MOCK_RETURN_URL, $response['redirect'] );
}

/**
* Provider for `test_process_payment_deferred_intent_returns_valid_response`.
*/
public function provide_process_payment_deferred_intent_returns_valid_response() {
return [
'with-payment-method' => [
[
'payment_method' => 'stripe',
'wc-stripe-payment-method' => 'pm_mock',
'wc-stripe-confirmation-token' => '',
'wc-stripe-is-deferred-intent' => '1',
],
],
'with-confirmation-token' => [
[
'payment_method' => 'stripe',
'wc-stripe-payment-method' => '',
'wc-stripe-confirmation-token' => 'ctoken_mock',
'wc-stripe-is-deferred-intent' => '1',
],
],
];
}

/**
* Test SCA/3DS checkout process_payment flow with deferred intent.
*/
Expand Down Expand Up @@ -589,8 +611,10 @@ public function provide_process_payment_deferred_intent_with_required_action_for

/**
* Exception handling of the process_payment flow with deferred intent.
*
* @dataProvider provide_process_payment_deferred_intent_handles_exception
*/
public function test_process_payment_deferred_intent_handles_exception() {
public function test_process_payment_deferred_intent_handles_exception( $post_vars ) {
$payment_intent_id = 'pi_mock';
$customer_id = 'cus_mock';
$order = WC_Helper_Order::create_order();
Expand All @@ -609,11 +633,7 @@ public function test_process_payment_deferred_intent_handles_exception() {
],
];

$_POST = [
'payment_method' => 'stripe',
'wc-stripe-payment-method' => 'pm_mock',
'wc-stripe-is-deferred-intent' => '1',
];
$_POST = $post_vars;

$this->mock_gateway->intent_controller
->expects( $this->once() )
Expand All @@ -637,7 +657,34 @@ public function test_process_payment_deferred_intent_handles_exception() {
$this->assertEquals( 'failed', $processed_order->get_status() );
}

public function test_process_payment_deferred_intent_bails_with_empty_payment_type() {
/**
* Provider for `test_process_payment_deferred_intent_handles_exception`.
*/
public function provide_process_payment_deferred_intent_handles_exception() {
return [
'with-payment-method' => [
[
'payment_method' => 'stripe',
'wc-stripe-payment-method' => 'pm_mock',
'wc-stripe-confirmation-token' => '',
'wc-stripe-is-deferred-intent' => '1',
],
],
'with-confirmation-token' => [
[
'payment_method' => 'stripe',
'wc-stripe-payment-method' => '',
'wc-stripe-confirmation-token' => 'ctoken_mock',
'wc-stripe-is-deferred-intent' => '1',
],
],
];
}

/**
* @dataProvider provide_process_payment_deferred_intent_bails_with_empty_payment_type
*/
public function test_process_payment_deferred_intent_bails_with_empty_payment_type( $post_vars ) {
$payment_intent_id = 'pi_mock';
$customer_id = 'cus_mock';
$order = WC_Helper_Order::create_order();
Expand All @@ -656,11 +703,7 @@ public function test_process_payment_deferred_intent_bails_with_empty_payment_ty
],
];

$_POST = [
'payment_method' => '',
'wc-stripe-payment-method' => 'pm_mock',
'wc-stripe-is-deferred-intent' => '1',
];
$_POST = $post_vars;

$this->mock_gateway->intent_controller
->expects( $this->never() )
Expand All @@ -683,7 +726,34 @@ public function test_process_payment_deferred_intent_bails_with_empty_payment_ty
$this->assertEquals( 'failed', $processed_order->get_status() );
}

public function test_process_payment_deferred_intent_bails_with_invalid_payment_type() {
/**
* Provider for `test_process_payment_deferred_intent_bails_with_empty_payment_type`.
*/
public function provide_process_payment_deferred_intent_bails_with_empty_payment_type() {
return [
'with-payment-method' => [
[
'payment_method' => '',
'wc-stripe-payment-method' => 'pm_mock',
'wc-stripe-confirmation-token' => '',
'wc-stripe-is-deferred-intent' => '1',
],
],
'with-confirmation-token' => [
[
'payment_method' => '',
'wc-stripe-payment-method' => '',
'wc-stripe-confirmation-token' => 'ctoken_mock',
'wc-stripe-is-deferred-intent' => '1',
],
],
];
}

/**
* @dataProvider provide_process_payment_deferred_intent_bails_with_invalid_payment_type
*/
public function test_process_payment_deferred_intent_bails_with_invalid_payment_type( $post_vars ) {
$payment_intent_id = 'pi_mock';
$customer_id = 'cus_mock';
$order = WC_Helper_Order::create_order();
Expand All @@ -702,11 +772,7 @@ public function test_process_payment_deferred_intent_bails_with_invalid_payment_
],
];

$_POST = [
'payment_method' => 'some_invalid_type',
'wc-stripe-payment-method' => 'pm_mock',
'wc-stripe-is-deferred-intent' => '1',
];
$_POST = $post_vars;

$this->mock_gateway->intent_controller
->expects( $this->never() )
Expand All @@ -729,6 +795,30 @@ public function test_process_payment_deferred_intent_bails_with_invalid_payment_
$this->assertEquals( 'failed', $processed_order->get_status() );
}

/**
* Provider for `test_process_payment_deferred_intent_bails_with_invalid_payment_type`.
*/
public function provide_process_payment_deferred_intent_bails_with_invalid_payment_type() {
return [
'with-payment-method' => [
[
'payment_method' => 'some_invalid_type',
'wc-stripe-payment-method' => 'pm_mock',
'wc-stripe-confirmation-token' => '',
'wc-stripe-is-deferred-intent' => '1',
],
],
'with-confirmation-token' => [
[
'payment_method' => 'some_invalid_type',
'wc-stripe-payment-method' => '',
'wc-stripe-confirmation-token' => 'ctoken_mock',
'wc-stripe-is-deferred-intent' => '1',
],
],
];
}

/**
* Test basic redirect payment processed correctly.
*/
Expand Down Expand Up @@ -1026,7 +1116,7 @@ public function test_checkout_saves_sepa_generated_payment_method_to_order() {
'captured' => true,
'status' => 'succeeded',
'payment_method_details' => [
'type' => WC_Stripe_Payment_Methods::BANCONTACT,
'type' => WC_Stripe_Payment_Methods::BANCONTACT,
WC_Stripe_Payment_Methods::BANCONTACT => [
'generated_sepa_debit' => $generated_payment_method_id,
],
Expand Down Expand Up @@ -2283,11 +2373,11 @@ public function test_process_payment_creates_new_intent_when_existing_intent_fai
// Create a mock failed payment intent that would be attached to the order
$mock_failed_intent = (object) wp_parse_args(
[
'id' => 'pi_mock_failed',
'payment_method' => 'pm_mock',
'status' => WC_Stripe_Intent_Status::CANCELED,
'id' => 'pi_mock_failed',
'payment_method' => 'pm_mock',
'status' => WC_Stripe_Intent_Status::CANCELED,
'payment_method_types' => [ WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID ],
'charges' => (object) [
'charges' => (object) [
'data' => [],
],
],
Expand All @@ -2297,11 +2387,11 @@ public function test_process_payment_creates_new_intent_when_existing_intent_fai
// Create a mock successful payment intent that will be created
$mock_success_intent = (object) wp_parse_args(
[
'id' => 'pi_mock_new',
'payment_method' => 'pm_mock',
'status' => WC_Stripe_Intent_Status::SUCCEEDED,
'id' => 'pi_mock_new',
'payment_method' => 'pm_mock',
'status' => WC_Stripe_Intent_Status::SUCCEEDED,
'payment_method_types' => [ WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID ],
'charges' => (object) [
'charges' => (object) [
'data' => [
(object) [
'id' => 'ch_mock',
Expand Down

0 comments on commit cbb602c

Please sign in to comment.