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 |
||
22 | class Test_REST_Endpoints extends TestCase { |
||
23 | |||
24 | const BLOG_TOKEN = 'new.blogtoken'; |
||
25 | const BLOG_ID = 42; |
||
26 | const USER_ID = 111; |
||
27 | |||
28 | /** |
||
29 | * REST Server object. |
||
30 | * |
||
31 | * @var WP_REST_Server |
||
32 | */ |
||
33 | private $server; |
||
34 | |||
35 | /** |
||
36 | * The original hostname to restore after tests are finished. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $api_host_original; |
||
41 | |||
42 | /** |
||
43 | * Setting up the test. |
||
44 | * |
||
45 | * @before |
||
46 | */ |
||
47 | public function set_up() { |
||
69 | |||
70 | /** |
||
71 | * Returning the environment into its initial state. |
||
72 | * |
||
73 | * @after |
||
74 | */ |
||
75 | public function tear_down() { |
||
87 | |||
88 | /** |
||
89 | * Testing the `/jetpack/v4/remote_authorize` endpoint. |
||
90 | */ |
||
91 | public function test_remote_authorize() { |
||
149 | |||
150 | /** |
||
151 | * Testing the `/jetpack/v4/connection` endpoint. |
||
152 | */ |
||
153 | public function test_connection() { |
||
168 | |||
169 | /** |
||
170 | * Testing the `/jetpack/v4/connection` endpoint jetpack_connection_status filter. |
||
171 | */ |
||
172 | public function test_connection_jetpack_connection_status_filter() { |
||
191 | |||
192 | /** |
||
193 | * Testing the `/jetpack/v4/connection/plugins` endpoint. |
||
194 | */ |
||
195 | public function test_connection_plugins() { |
||
227 | |||
228 | /** |
||
229 | * Testing the `connection/reconnect` endpoint, full reconnect. |
||
230 | */ |
||
231 | public function test_connection_reconnect_full() { |
||
247 | |||
248 | /** |
||
249 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (blog token). |
||
250 | */ |
||
251 | View Code Duplication | public function test_connection_reconnect_partial_blog_token_success() { |
|
264 | |||
265 | /** |
||
266 | * Testing the `connection/reconnect` endpoint, failed partial reconnect (blog token). |
||
267 | */ |
||
268 | View Code Duplication | public function test_connection_reconnect_partial_blog_token_fail() { |
|
281 | |||
282 | /** |
||
283 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (user token). |
||
284 | */ |
||
285 | public function test_connection_reconnect_partial_user_token_success() { |
||
297 | |||
298 | /** |
||
299 | * Testing the `connection/reconnect` endpoint, site_connection (full reconnect). |
||
300 | */ |
||
301 | public function test_connection_reconnect_site_connection() { |
||
302 | add_filter( 'jetpack_options', array( $this, 'mock_jetpack_site_connection_options' ), 10, 2 ); |
||
303 | add_filter( 'jetpack_connection_disconnect_site_wpcom', '__return_false' ); |
||
304 | add_filter( 'pre_http_request', array( static::class, 'intercept_register_request' ), 10, 3 ); |
||
305 | |||
306 | $response = $this->server->dispatch( $this->build_reconnect_request() ); |
||
307 | $data = $response->get_data(); |
||
308 | |||
309 | remove_filter( 'pre_http_request', array( static::class, 'intercept_register_request' ), 10 ); |
||
310 | remove_filter( 'jetpack_connection_disconnect_site_wpcom', '__return_false' ); |
||
311 | remove_filter( 'jetpack_options', array( $this, 'mock_jetpack_site_connection_options' ) ); |
||
312 | |||
313 | $this->assertEquals( 200, $response->get_status() ); |
||
314 | $this->assertEquals( 'completed', $data['status'] ); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Testing the `connection/reconnect` endpoint when the token validation request fails. |
||
319 | */ |
||
320 | public function test_connection_reconnect_when_token_validation_request_fails() { |
||
336 | |||
337 | /** |
||
338 | * Testing the `connection/register` endpoint. |
||
339 | */ |
||
340 | View Code Duplication | public function test_connection_register() { |
|
363 | |||
364 | /** |
||
365 | * Testing the `connection/register` endpoint with allow_inplace_authorization as true. |
||
366 | */ |
||
367 | public function test_connection_register_allow_inplace() { |
||
387 | |||
388 | /** |
||
389 | * Testing the `connection/register` endpoint with alternate_authorization_url |
||
390 | */ |
||
391 | View Code Duplication | public function test_connection_register_with_alternate_auth_url() { |
|
414 | |||
415 | /** |
||
416 | * This filter callback allow us to skip the database query by `Jetpack_Options` to retrieve the option. |
||
417 | * |
||
418 | * @param array $options List of options already skipping the database request. |
||
419 | * |
||
420 | * @return array |
||
421 | */ |
||
422 | public function bypass_raw_options( array $options ) { |
||
427 | |||
428 | /** |
||
429 | * Intercept the `jetpack.register` API request sent to WP.com, and mock the response. |
||
430 | * |
||
431 | * @param bool|array $response The existing response. |
||
432 | * @param array $args The request arguments. |
||
433 | * @param string $url The request URL. |
||
434 | * |
||
435 | * @return array |
||
436 | */ |
||
437 | public static function intercept_register_request( $response, $args, $url ) { |
||
444 | |||
445 | /** |
||
446 | * Intercept the `jetpack.register` API request sent to WP.com, and mock the response with allow_inplace_authorization as true. |
||
447 | * |
||
448 | * @param bool|array $response The existing response. |
||
449 | * @param array $args The request arguments. |
||
450 | * @param string $url The request URL. |
||
451 | * |
||
452 | * @return array |
||
453 | */ |
||
454 | public static function intercept_register_request_with_allow_inplace( $response, $args, $url ) { |
||
461 | |||
462 | /** |
||
463 | * Intercept the `jetpack.register` API request sent to WP.com, and mock the response with a value in alternate_authorization_url key. |
||
464 | * |
||
465 | * @param bool|array $response The existing response. |
||
466 | * @param array $args The request arguments. |
||
467 | * @param string $url The request URL. |
||
468 | * |
||
469 | * @return array |
||
470 | */ |
||
471 | public static function intercept_register_request_with_alternate_auth_url( $response, $args, $url ) { |
||
478 | |||
479 | /** |
||
480 | * Gets a mocked REST response from jetpack.register WPCOM endpoint |
||
481 | * |
||
482 | * @param boolean $allow_inplace_authorization the value of allow_inplace_authorization returned by the server. |
||
483 | * @param string $alternate_authorization_url the value of alternate_authorization_url returned by the server. |
||
484 | * @return array |
||
485 | */ |
||
486 | private static function get_register_request_mock_response( $allow_inplace_authorization = false, $alternate_authorization_url = '' ) { |
||
503 | |||
504 | /** |
||
505 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. |
||
506 | * |
||
507 | * @param bool|array $response The existing response. |
||
508 | * @param array $args The request arguments. |
||
509 | * @param string $url The request URL. |
||
510 | * |
||
511 | * @return array |
||
512 | */ |
||
513 | public function intercept_validate_tokens_request_invalid_blog_token( $response, $args, $url ) { |
||
520 | |||
521 | /** |
||
522 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid user token" response. |
||
523 | * |
||
524 | * @param bool|array $response The existing response. |
||
525 | * @param array $args The request arguments. |
||
526 | * @param string $url The request URL. |
||
527 | * |
||
528 | * @return array |
||
529 | */ |
||
530 | public function intercept_validate_tokens_request_invalid_user_token( $response, $args, $url ) { |
||
537 | |||
538 | /** |
||
539 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "valid tokens" response. |
||
540 | * |
||
541 | * @param bool|array $response The existing response. |
||
542 | * @param array $args The request arguments. |
||
543 | * @param string $url The request URL. |
||
544 | * |
||
545 | * @return array |
||
546 | */ |
||
547 | public function intercept_validate_tokens_request_valid_tokens( $response, $args, $url ) { |
||
554 | |||
555 | /** |
||
556 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock failed response. |
||
557 | * |
||
558 | * @param bool|array $response The existing response. |
||
559 | * @param array $args The request arguments. |
||
560 | * @param string $url The request URL. |
||
561 | * |
||
562 | * @return array |
||
563 | */ |
||
564 | View Code Duplication | public function intercept_validate_tokens_request_failed( $response, $args, $url ) { |
|
578 | |||
579 | /** |
||
580 | * Build the response for a tokens validation request |
||
581 | * |
||
582 | * @param string $invalid_token Accepted values: 'blog_token', 'user_token'. |
||
583 | * |
||
584 | * @return array |
||
585 | */ |
||
586 | private function build_validate_tokens_response( $invalid_token ) { |
||
621 | |||
622 | /** |
||
623 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the success response. |
||
624 | * |
||
625 | * @param bool|array $response The existing response. |
||
626 | * @param array $args The request arguments. |
||
627 | * @param string $url The request URL. |
||
628 | * |
||
629 | * @return array |
||
630 | */ |
||
631 | View Code Duplication | public function intercept_refresh_blog_token_request( $response, $args, $url ) { |
|
645 | |||
646 | /** |
||
647 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the failure response. |
||
648 | * |
||
649 | * @param bool|array $response The existing response. |
||
650 | * @param array $args The request arguments. |
||
651 | * @param string $url The request URL. |
||
652 | * |
||
653 | * @return array |
||
654 | */ |
||
655 | View Code Duplication | public function intercept_refresh_blog_token_request_fail( $response, $args, $url ) { |
|
669 | |||
670 | /** |
||
671 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. |
||
672 | * |
||
673 | * @param bool|array $response The existing response. |
||
674 | * @param array $args The request arguments. |
||
675 | * @param string $url The request URL. |
||
676 | * |
||
677 | * @return array |
||
678 | */ |
||
679 | public function intercept_auth_token_request( $response, $args, $url ) { |
||
699 | |||
700 | /** |
||
701 | * Intercept the `Jetpack_Options` call and mock the values. |
||
702 | * Site level / user-less connection set-up. |
||
703 | * |
||
704 | * @param mixed $value The current option value. |
||
705 | * @param string $name Option name. |
||
706 | * |
||
707 | * @return mixed |
||
708 | */ |
||
709 | public function mock_jetpack_site_connection_options( $value, $name ) { |
||
710 | switch ( $name ) { |
||
711 | case 'blog_token': |
||
712 | return self::BLOG_TOKEN; |
||
713 | case 'id': |
||
714 | return self::BLOG_ID; |
||
715 | } |
||
716 | |||
717 | return $value; |
||
718 | } |
||
719 | |||
720 | /** |
||
721 | * Intercept the `Jetpack_Options` call and mock the values. |
||
722 | * Full connection set-up. |
||
723 | * |
||
724 | * @param mixed $value The current option value. |
||
725 | * @param string $name Option name. |
||
726 | * |
||
727 | * @return mixed |
||
728 | */ |
||
729 | public function mock_jetpack_options( $value, $name ) { |
||
745 | |||
746 | /** |
||
747 | * Build the `connection/reconnect` request object. |
||
748 | * |
||
749 | * @return WP_REST_Request |
||
750 | */ |
||
751 | private function build_reconnect_request() { |
||
757 | |||
758 | /** |
||
759 | * Setup the environment to test the reconnection process. |
||
760 | * |
||
761 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
||
762 | */ |
||
763 | private function setup_reconnect_test( $invalid_token ) { |
||
813 | |||
814 | /** |
||
815 | * Restore the environment after the `reconnect` test has been run. |
||
816 | * |
||
817 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
||
818 | */ |
||
819 | private function shutdown_reconnect_test( $invalid_token ) { |
||
866 | |||
867 | } |
||
868 |