Conditions | 7 |
Paths | 1 |
Total Lines | 172 |
Lines | 4 |
Ratio | 2.33 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
78 | public function test_renewal_successful() { |
||
79 | // Arrange: Some variables we'll use later. |
||
80 | $renewal_order = WC_Helper_Order::create_order(); |
||
81 | $amount = 20; // WC Subs sends an amount to be used, instead of using the order amount. |
||
82 | $stripe_amount = WC_Stripe_Helper::get_stripe_amount( $amount ); |
||
83 | $currency = strtolower( $renewal_order->get_currency() ); |
||
84 | $customer = 'cus_123abc'; |
||
85 | $source = 'src_123abc'; |
||
86 | $statement_descriptor = WC_Stripe_Helper::clean_statement_descriptor( $this->statement_descriptor ); |
||
87 | $should_retry = false; |
||
88 | $previous_error = false; |
||
89 | $payments_intents_api_endpoint = 'https://api.stripe.com/v1/payment_intents'; |
||
90 | $urls_used = array(); |
||
91 | |||
92 | $renewal_order->set_payment_method( 'stripe' ); |
||
93 | |||
94 | // Arrange: Mock prepare_order_source() so that we have a customer and source. |
||
95 | $this->wc_stripe_subs_compat |
||
96 | ->expects( $this->any() ) |
||
97 | ->method( 'prepare_order_source' ) |
||
98 | ->will( |
||
99 | $this->returnValue( |
||
100 | (object) array( |
||
101 | 'token_id' => false, |
||
102 | 'customer' => $customer, |
||
103 | 'source' => $source, |
||
104 | 'source_object' => (object) array(), |
||
105 | ) |
||
106 | ) |
||
107 | ); |
||
108 | |||
109 | // Arrange: Add filter that will return a mocked HTTP response for the payment_intent call. |
||
110 | // Note: There are assertions in the callback function. |
||
111 | $pre_http_request_response_callback = function( $preempt, $request_args, $url ) use ( |
||
112 | $renewal_order, |
||
113 | $stripe_amount, |
||
114 | $currency, |
||
115 | $customer, |
||
116 | $source, |
||
117 | $statement_descriptor, |
||
118 | $payments_intents_api_endpoint, |
||
119 | &$urls_used |
||
120 | ) { |
||
121 | // Add all urls to array so we can later make assertions about which endpoints were used. |
||
122 | array_push( $urls_used, $url ); |
||
123 | |||
124 | // Continue without mocking the request if it's not the endpoint we care about. |
||
125 | if ( $payments_intents_api_endpoint !== $url ) { |
||
126 | return false; |
||
127 | } |
||
128 | |||
129 | // Assert: the request method is POST. |
||
130 | $this->assertArrayHasKey( 'method', $request_args ); |
||
131 | $this->assertSame( 'POST', $request_args['method'] ); |
||
132 | |||
133 | // Assert: the request has a body. |
||
134 | $this->assertArrayHasKey( 'body', $request_args ); |
||
135 | |||
136 | // Assert: the request body contains these values. |
||
137 | $expected_request_body_values = array( |
||
138 | 'source' => $source, |
||
139 | 'amount' => $stripe_amount, |
||
140 | 'currency' => $currency, |
||
141 | 'payment_method_types' => array( 'card' ), |
||
142 | 'customer' => $customer, |
||
143 | 'off_session' => 'true', |
||
144 | 'confirm' => 'true', |
||
145 | 'confirmation_method' => 'automatic', |
||
146 | 'statement_descriptor' => $statement_descriptor, |
||
147 | ); |
||
148 | View Code Duplication | foreach ( $expected_request_body_values as $key => $value ) { |
|
|
|||
149 | $this->assertArrayHasKey( $key, $request_args['body'] ); |
||
150 | $this->assertSame( $value, $request_args['body'][ $key ] ); |
||
151 | } |
||
152 | |||
153 | // Assert: the request body contains these keys, without checking for their value. |
||
154 | $expected_request_body_keys = array( |
||
155 | 'description', |
||
156 | 'metadata', |
||
157 | ); |
||
158 | foreach ( $expected_request_body_keys as $key ) { |
||
159 | $this->assertArrayHasKey( $key, $request_args['body'] ); |
||
160 | } |
||
161 | |||
162 | // Assert: the body metadata has these values. |
||
163 | $order_id = (string) $renewal_order->get_id(); |
||
164 | $expected_metadata_values = array( |
||
165 | 'order_id' => $order_id, |
||
166 | 'payment_type' => 'recurring', |
||
167 | ); |
||
168 | foreach ( $expected_metadata_values as $key => $value ) { |
||
169 | $this->assertArrayHasKey( $key, $request_args['body']['metadata'] ); |
||
170 | $this->assertSame( $value, $request_args['body']['metadata'][ $key ] ); |
||
171 | } |
||
172 | |||
173 | // Assert: the body metadata has these keys, without checking for their value. |
||
174 | $expected_metadata_keys = array( |
||
175 | 'customer_name', |
||
176 | 'customer_email', |
||
177 | 'site_url', |
||
178 | ); |
||
179 | foreach ( $expected_metadata_keys as $key ) { |
||
180 | $this->assertArrayHasKey( $key, $request_args['body']['metadata'] ); |
||
181 | } |
||
182 | |||
183 | // Assert: the request body does not contains these keys. |
||
184 | $expected_missing_request_body_keys = array( |
||
185 | 'capture', // No need to capture with a payment intent. |
||
186 | 'capture_method', // The default ('automatic') is what we want in this case, so we leave it off. |
||
187 | 'expand[]', |
||
188 | ); |
||
189 | foreach ( $expected_missing_request_body_keys as $key ) { |
||
190 | $this->assertArrayNotHasKey( $key, $request_args['body'] ); |
||
191 | } |
||
192 | |||
193 | // Arrange: return dummy content as the response. |
||
194 | return array( |
||
195 | 'headers' => array(), |
||
196 | // Too bad we aren't dynamically setting things 'cus_123abc' when using this file. |
||
197 | 'body' => file_get_contents( 'tests/phpunit/dummy-data/subscription_renewal_response_success.json' ), |
||
198 | 'response' => array( |
||
199 | 'code' => 200, |
||
200 | 'message' => 'OK', |
||
201 | ), |
||
202 | 'cookies' => array(), |
||
203 | 'filename' => null, |
||
204 | ); |
||
205 | }; |
||
206 | |||
207 | add_filter( 'pre_http_request', $pre_http_request_response_callback, 10, 3 ); |
||
208 | |||
209 | // Arrange: Make sure to check that an action we care about was called |
||
210 | // by hooking into it. |
||
211 | $mock_action_process_payment = new MockAction(); |
||
212 | add_action( |
||
213 | 'wc_gateway_stripe_process_payment', |
||
214 | [ &$mock_action_process_payment, 'action' ] |
||
215 | ); |
||
216 | |||
217 | // Act: call process_subscription_payment(). |
||
218 | // We need to use `wc_stripe_subs_compat` here because we mocked this class earlier. |
||
219 | $result = $this->wc_stripe_subs_compat->process_subscription_payment( 20, $renewal_order, $should_retry, $previous_error ); |
||
220 | |||
221 | // Assert: nothing was returned. |
||
222 | $this->assertEquals( $result, null ); |
||
223 | |||
224 | // Assert that we saved the payment intent to the order. |
||
225 | $order_id = $renewal_order->get_id(); |
||
226 | $order = wc_get_order( $order_id ); |
||
227 | $order_data = $order->get_meta( '_stripe_intent_id' ); |
||
228 | |||
229 | $this->assertEquals( $order_data, 'pi_123abc' ); |
||
230 | |||
231 | // Transaction ID was saved to order. |
||
232 | $order_transaction_id = $order->get_transaction_id(); |
||
233 | $this->assertEquals( $order_transaction_id, 'ch_123abc' ); |
||
234 | |||
235 | // Assert: the order was marked as processing (this is done in process_response()). |
||
236 | $this->assertEquals( $order->get_status(), 'processing' ); |
||
237 | |||
238 | // Assert: called payment intents. |
||
239 | $this->assertTrue( in_array( $payments_intents_api_endpoint, $urls_used ) ); |
||
240 | |||
241 | // Assert: Our hook was called once. |
||
242 | $this->assertEquals( 1, $mock_action_process_payment->get_call_count() ); |
||
243 | |||
244 | // Assert: Only our hook was called. |
||
245 | $this->assertEquals( array( 'wc_gateway_stripe_process_payment' ), $mock_action_process_payment->get_tags() ); |
||
246 | |||
247 | // Clean up. |
||
248 | remove_filter( 'pre_http_request', array( $this, 'pre_http_request_response_success' ) ); |
||
249 | } |
||
250 | |||
365 |
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.