WC_Stripe_Payment_Gateway_Test::updateOrderMeta()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * These tests make assertions against abstract class WC_Stripe_Payment_Gateway
4
 *
5
 */
6
7
8
class WC_Stripe_Payment_Gateway_Test extends WP_UnitTestCase {
9
	/**
10
	 * Gateway under test.
11
	 *
12
	 * @var WC_Gateway_Stripe
13
	 */
14
	private $gateway;
15
16
	/**
17
	 * Sets up things all tests need.
18
	 */
19
	public function setUp() {
20
		parent::setUp();
21
22
		$this->gateway = new WC_Gateway_Stripe();
23
	}
24
25
	/**
26
	 * Helper function to update test order meta data
27
	 */
28
	private function updateOrderMeta( $order, $key, $value ) {
29
		$order->update_meta_data( $key, $value );
30
	}
31
32
	/**
33
	 * Tests false is returned if payment intent is not set in the order.
34
	 */
35
	public function test_default_get_payment_intent_from_order() {
36
		$order = WC_Helper_Order::create_order();
37
		$intent = $this->gateway->get_intent_from_order( $order );
38
		$this->assertFalse( $intent );
39
	}
40
41
	/**
42
	 * Tests if payment intent is fetched from Stripe API.
43
	 */
44
	public function test_success_get_payment_intent_from_order() {
45
		$order = WC_Helper_Order::create_order();
46
		$this->updateOrderMeta( $order, '_stripe_intent_id', 'pi_123' );
47
		$expected_intent = ( object ) [ 'id' => 'pi_123' ];
48 View Code Duplication
		$callback = function( $preempt, $request_args, $url ) use ( $expected_intent ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
			$response = [
50
				'headers' 	=> [],
51
				'body'		=> json_encode( $expected_intent ),
52
				'response'	=> [
53
					'code' 		=> 200,
54
					'message' 	=> 'OK',
55
				],
56
			];
57
58
			$this->assertEquals( 'GET', $request_args['method'] );
59
			$this->assertStringEndsWith( 'payment_intents/pi_123', $url );
60
61
			return $response;
62
		};
63
64
		add_filter( 'pre_http_request', $callback, 10, 3);
65
66
		$intent = $this->gateway->get_intent_from_order( $order );
67
		$this->assertEquals( $expected_intent, $intent );
68
69
		remove_filter( 'pre_http_request', $callback );
70
	}
71
72
	/**
73
	 * Tests if false is returned when error is returned from Stripe API.
74
	 */
75
	public function test_error_get_payment_intent_from_order() {
76
		$order = WC_Helper_Order::create_order();
77
		$this->updateOrderMeta( $order, '_stripe_intent_id', 'pi_123' );
78
		$response_error = ( object ) [
79
			'error' => [
80
				'code' 		=> 'resource_missing',
81
				'message' 	=> 'error_message'
82
			]
83
		];
84 View Code Duplication
		$callback = function( $preempt, $request_args, $url ) use ( $response_error ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
			$response = [
86
				'headers' 	=> [],
87
				'body'		=> json_encode( $response_error ),
88
				'response'	=> [
89
					'code' 		=> 404,
90
					'message' 	=> 'ERR',
91
				],
92
			];
93
94
			$this->assertEquals( 'GET', $request_args['method'] );
95
			$this->assertStringEndsWith( 'payment_intents/pi_123', $url );
96
97
			return $response;
98
		};
99
100
		add_filter( 'pre_http_request', $callback, 10, 3);
101
102
		$intent = $this->gateway->get_intent_from_order( $order );
103
		$this->assertFalse( $intent );
104
105
		remove_filter( 'pre_http_request', $callback );
106
	}
107
}
108