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 | public function setUp() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Returning the environment into its initial state. |
||
| 71 | */ |
||
| 72 | public function tearDown() { |
||
| 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() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Testing the `/jetpack/v4/connection/plugins` endpoint. |
||
| 177 | */ |
||
| 178 | public function test_connection_plugins() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Testing the `connection/reconnect` endpoint, full reconnect. |
||
| 213 | */ |
||
| 214 | public function test_connection_reconnect_full() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (blog token). |
||
| 233 | */ |
||
| 234 | View Code Duplication | public function test_connection_reconnect_partial_blog_token_success() { |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Testing the `connection/reconnect` endpoint, failed partial reconnect (blog token). |
||
| 250 | */ |
||
| 251 | View Code Duplication | public function test_connection_reconnect_partial_blog_token_fail() { |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (user token). |
||
| 267 | */ |
||
| 268 | public function test_connection_reconnect_partial_user_token_success() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * This filter callback allow us to skip the database query by `Jetpack_Options` to retrieve the option. |
||
| 283 | * |
||
| 284 | * @param array $options List of options already skipping the database request. |
||
| 285 | * |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function bypass_raw_options( array $options ) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Intercept the `jetpack.register` API request sent to WP.com, and mock the response. |
||
| 296 | * |
||
| 297 | * @param bool|array $response The existing response. |
||
| 298 | * @param array $args The request arguments. |
||
| 299 | * @param string $url The request URL. |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public function intercept_register_request( $response, $args, $url ) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. |
||
| 325 | * |
||
| 326 | * @param bool|array $response The existing response. |
||
| 327 | * @param array $args The request arguments. |
||
| 328 | * @param string $url The request URL. |
||
| 329 | * |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | public function intercept_validate_tokens_request_invalid_blog_token( $response, $args, $url ) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid user token" response. |
||
| 342 | * |
||
| 343 | * @param bool|array $response The existing response. |
||
| 344 | * @param array $args The request arguments. |
||
| 345 | * @param string $url The request URL. |
||
| 346 | * |
||
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | public function intercept_validate_tokens_request_invalid_user_token( $response, $args, $url ) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "valid tokens" response. |
||
| 359 | * |
||
| 360 | * @param bool|array $response The existing response. |
||
| 361 | * @param array $args The request arguments. |
||
| 362 | * @param string $url The request URL. |
||
| 363 | * |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | public function intercept_validate_tokens_request_valid_tokens( $response, $args, $url ) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Build the response for a tokens validation request |
||
| 376 | * |
||
| 377 | * @param string $invalid_token Accepted values: 'blog_token', 'user_token'. |
||
| 378 | * |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | private function build_validate_tokens_response( $invalid_token ) { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the success response. |
||
| 419 | * |
||
| 420 | * @param bool|array $response The existing response. |
||
| 421 | * @param array $args The request arguments. |
||
| 422 | * @param string $url The request URL. |
||
| 423 | * |
||
| 424 | * @return array |
||
| 425 | */ |
||
| 426 | View Code Duplication | public function intercept_refresh_blog_token_request( $response, $args, $url ) { |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the failure response. |
||
| 443 | * |
||
| 444 | * @param bool|array $response The existing response. |
||
| 445 | * @param array $args The request arguments. |
||
| 446 | * @param string $url The request URL. |
||
| 447 | * |
||
| 448 | * @return array |
||
| 449 | */ |
||
| 450 | View Code Duplication | public function intercept_refresh_blog_token_request_fail( $response, $args, $url ) { |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. |
||
| 467 | * |
||
| 468 | * @param bool|array $response The existing response. |
||
| 469 | * @param array $args The request arguments. |
||
| 470 | * @param string $url The request URL. |
||
| 471 | * |
||
| 472 | * @return array |
||
| 473 | */ |
||
| 474 | public function intercept_auth_token_request( $response, $args, $url ) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Intercept the `Jetpack_Options` call and mock the values. |
||
| 497 | * |
||
| 498 | * @param mixed $value The current option value. |
||
| 499 | * @param string $name Option name. |
||
| 500 | * |
||
| 501 | * @return mixed |
||
| 502 | */ |
||
| 503 | public function mock_jetpack_options( $value, $name ) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Build the `connection/reconnect` request object. |
||
| 516 | * |
||
| 517 | * @return WP_REST_Request |
||
| 518 | */ |
||
| 519 | private function build_reconnect_request() { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Setup the environment to test the reconnection process. |
||
| 529 | * |
||
| 530 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
||
| 531 | */ |
||
| 532 | View Code Duplication | private function setup_reconnect_test( $invalid_token ) { |
|
| 571 | |||
| 572 | /** |
||
| 573 | * Restore the environment after the `reconnect` test has been run. |
||
| 574 | * |
||
| 575 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
||
| 576 | */ |
||
| 577 | View Code Duplication | private function shutdown_reconnect_test( $invalid_token ) { |
|
| 614 | |||
| 615 | } |
||
| 616 |