Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Test_REST_Endpoints often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Test_REST_Endpoints, and based on these observations, apply Extract Interface, too.
1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
21 | class Test_REST_Endpoints extends TestCase { |
||
22 | |||
23 | const BLOG_TOKEN = 'new.blogtoken'; |
||
24 | const BLOG_ID = 42; |
||
25 | const USER_ID = 111; |
||
26 | |||
27 | /** |
||
28 | * REST Server object. |
||
29 | * |
||
30 | * @var WP_REST_Server |
||
31 | */ |
||
32 | private $server; |
||
33 | |||
34 | /** |
||
35 | * The original hostname to restore after tests are finished. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $api_host_original; |
||
40 | |||
41 | /** |
||
42 | * Setting up the test. |
||
43 | * |
||
44 | * @before |
||
45 | */ |
||
46 | public function set_up() { |
||
68 | |||
69 | /** |
||
70 | * Returning the environment into its initial state. |
||
71 | * |
||
72 | * @after |
||
73 | */ |
||
74 | public function tear_down() { |
||
86 | |||
87 | /** |
||
88 | * Testing the `/jetpack/v4/remote_authorize` endpoint. |
||
89 | */ |
||
90 | public function test_remote_authorize() { |
||
148 | |||
149 | /** |
||
150 | * Testing the `/jetpack/v4/connection` endpoint. |
||
151 | */ |
||
152 | public function test_connection() { |
||
167 | |||
168 | /** |
||
169 | * Testing the `/jetpack/v4/connection` endpoint jetpack_connection_status filter. |
||
170 | */ |
||
171 | public function test_connection_jetpack_connection_status_filter() { |
||
190 | |||
191 | /** |
||
192 | * Testing the `/jetpack/v4/connection/plugins` endpoint. |
||
193 | */ |
||
194 | public function test_connection_plugins() { |
||
226 | |||
227 | /** |
||
228 | * Testing the `connection/reconnect` endpoint, full reconnect. |
||
229 | */ |
||
230 | public function test_connection_reconnect_full() { |
||
246 | |||
247 | /** |
||
248 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (blog token). |
||
249 | */ |
||
250 | View Code Duplication | public function test_connection_reconnect_partial_blog_token_success() { |
|
263 | |||
264 | /** |
||
265 | * Testing the `connection/reconnect` endpoint, failed partial reconnect (blog token). |
||
266 | */ |
||
267 | View Code Duplication | public function test_connection_reconnect_partial_blog_token_fail() { |
|
280 | |||
281 | /** |
||
282 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (user token). |
||
283 | */ |
||
284 | public function test_connection_reconnect_partial_user_token_success() { |
||
296 | |||
297 | /** |
||
298 | * Testing the `connection/reconnect` endpoint, site_connection (full reconnect). |
||
299 | */ |
||
300 | public function test_connection_reconnect_site_connection() { |
||
301 | add_filter( 'jetpack_options', array( $this, 'mock_jetpack_site_connection_options' ), 10, 2 ); |
||
302 | add_filter( 'jetpack_connection_disconnect_site_wpcom', '__return_false' ); |
||
303 | add_filter( 'pre_http_request', array( static::class, 'intercept_register_request' ), 10, 3 ); |
||
304 | |||
305 | $response = $this->server->dispatch( $this->build_reconnect_request() ); |
||
306 | $data = $response->get_data(); |
||
307 | |||
308 | remove_filter( 'pre_http_request', array( static::class, 'intercept_register_request' ), 10 ); |
||
309 | remove_filter( 'jetpack_connection_disconnect_site_wpcom', '__return_false' ); |
||
310 | remove_filter( 'jetpack_options', array( $this, 'mock_jetpack_site_connection_options' ) ); |
||
311 | |||
312 | $this->assertEquals( 200, $response->get_status() ); |
||
313 | $this->assertEquals( 'completed', $data['status'] ); |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * Testing the `connection/reconnect` endpoint when the token validation request fails. |
||
318 | */ |
||
319 | public function test_connection_reconnect_when_token_validation_request_fails() { |
||
335 | |||
336 | /** |
||
337 | * Testing the `connection/register` endpoint. |
||
338 | */ |
||
339 | public function test_connection_register() { |
||
355 | |||
356 | /** |
||
357 | * This filter callback allow us to skip the database query by `Jetpack_Options` to retrieve the option. |
||
358 | * |
||
359 | * @param array $options List of options already skipping the database request. |
||
360 | * |
||
361 | * @return array |
||
362 | */ |
||
363 | public function bypass_raw_options( array $options ) { |
||
368 | |||
369 | /** |
||
370 | * Intercept the `jetpack.register` API request sent to WP.com, and mock the response. |
||
371 | * |
||
372 | * @param bool|array $response The existing response. |
||
373 | * @param array $args The request arguments. |
||
374 | * @param string $url The request URL. |
||
375 | * |
||
376 | * @return array |
||
377 | */ |
||
378 | public static function intercept_register_request( $response, $args, $url ) { |
||
397 | |||
398 | /** |
||
399 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. |
||
400 | * |
||
401 | * @param bool|array $response The existing response. |
||
402 | * @param array $args The request arguments. |
||
403 | * @param string $url The request URL. |
||
404 | * |
||
405 | * @return array |
||
406 | */ |
||
407 | public function intercept_validate_tokens_request_invalid_blog_token( $response, $args, $url ) { |
||
414 | |||
415 | /** |
||
416 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid user token" response. |
||
417 | * |
||
418 | * @param bool|array $response The existing response. |
||
419 | * @param array $args The request arguments. |
||
420 | * @param string $url The request URL. |
||
421 | * |
||
422 | * @return array |
||
423 | */ |
||
424 | public function intercept_validate_tokens_request_invalid_user_token( $response, $args, $url ) { |
||
431 | |||
432 | /** |
||
433 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "valid tokens" response. |
||
434 | * |
||
435 | * @param bool|array $response The existing response. |
||
436 | * @param array $args The request arguments. |
||
437 | * @param string $url The request URL. |
||
438 | * |
||
439 | * @return array |
||
440 | */ |
||
441 | public function intercept_validate_tokens_request_valid_tokens( $response, $args, $url ) { |
||
448 | |||
449 | /** |
||
450 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock failed response. |
||
451 | * |
||
452 | * @param bool|array $response The existing response. |
||
453 | * @param array $args The request arguments. |
||
454 | * @param string $url The request URL. |
||
455 | * |
||
456 | * @return array |
||
457 | */ |
||
458 | View Code Duplication | public function intercept_validate_tokens_request_failed( $response, $args, $url ) { |
|
472 | |||
473 | /** |
||
474 | * Build the response for a tokens validation request |
||
475 | * |
||
476 | * @param string $invalid_token Accepted values: 'blog_token', 'user_token'. |
||
477 | * |
||
478 | * @return array |
||
479 | */ |
||
480 | private function build_validate_tokens_response( $invalid_token ) { |
||
515 | |||
516 | /** |
||
517 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the success response. |
||
518 | * |
||
519 | * @param bool|array $response The existing response. |
||
520 | * @param array $args The request arguments. |
||
521 | * @param string $url The request URL. |
||
522 | * |
||
523 | * @return array |
||
524 | */ |
||
525 | View Code Duplication | public function intercept_refresh_blog_token_request( $response, $args, $url ) { |
|
539 | |||
540 | /** |
||
541 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the failure response. |
||
542 | * |
||
543 | * @param bool|array $response The existing response. |
||
544 | * @param array $args The request arguments. |
||
545 | * @param string $url The request URL. |
||
546 | * |
||
547 | * @return array |
||
548 | */ |
||
549 | View Code Duplication | public function intercept_refresh_blog_token_request_fail( $response, $args, $url ) { |
|
563 | |||
564 | /** |
||
565 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. |
||
566 | * |
||
567 | * @param bool|array $response The existing response. |
||
568 | * @param array $args The request arguments. |
||
569 | * @param string $url The request URL. |
||
570 | * |
||
571 | * @return array |
||
572 | */ |
||
573 | public function intercept_auth_token_request( $response, $args, $url ) { |
||
593 | |||
594 | /** |
||
595 | * Intercept the `Jetpack_Options` call and mock the values. |
||
596 | * Site level / user-less connection set-up. |
||
597 | * |
||
598 | * @param mixed $value The current option value. |
||
599 | * @param string $name Option name. |
||
600 | * |
||
601 | * @return mixed |
||
602 | */ |
||
603 | public function mock_jetpack_site_connection_options( $value, $name ) { |
||
604 | switch ( $name ) { |
||
605 | case 'blog_token': |
||
606 | return self::BLOG_TOKEN; |
||
607 | case 'id': |
||
608 | return self::BLOG_ID; |
||
609 | } |
||
610 | |||
611 | return $value; |
||
612 | } |
||
613 | |||
614 | /** |
||
615 | * Intercept the `Jetpack_Options` call and mock the values. |
||
616 | * Full connection set-up. |
||
617 | * |
||
618 | * @param mixed $value The current option value. |
||
619 | * @param string $name Option name. |
||
620 | * |
||
621 | * @return mixed |
||
622 | */ |
||
623 | public function mock_jetpack_options( $value, $name ) { |
||
639 | |||
640 | /** |
||
641 | * Build the `connection/reconnect` request object. |
||
642 | * |
||
643 | * @return WP_REST_Request |
||
644 | */ |
||
645 | private function build_reconnect_request() { |
||
651 | |||
652 | /** |
||
653 | * Setup the environment to test the reconnection process. |
||
654 | * |
||
655 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
||
656 | */ |
||
657 | private function setup_reconnect_test( $invalid_token ) { |
||
707 | |||
708 | /** |
||
709 | * Restore the environment after the `reconnect` test has been run. |
||
710 | * |
||
711 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
||
712 | */ |
||
713 | private function shutdown_reconnect_test( $invalid_token ) { |
||
760 | |||
761 | } |
||
762 |