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() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Returning the environment into its initial state. |
||
| 70 | * |
||
| 71 | * @after |
||
| 72 | */ |
||
| 73 | public function tear_down() { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Testing the `/jetpack/v4/remote_authorize` endpoint. |
||
| 88 | */ |
||
| 89 | public function test_remote_authorize() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Testing the `/jetpack/v4/connection` endpoint. |
||
| 150 | */ |
||
| 151 | public function test_connection() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Testing the `/jetpack/v4/connection/plugins` endpoint. |
||
| 169 | */ |
||
| 170 | public function test_connection_plugins() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Testing the `connection/reconnect` endpoint, full reconnect. |
||
| 205 | */ |
||
| 206 | public function test_connection_reconnect_full() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (blog token). |
||
| 225 | */ |
||
| 226 | View Code Duplication | public function test_connection_reconnect_partial_blog_token_success() { |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Testing the `connection/reconnect` endpoint, failed partial reconnect (blog token). |
||
| 242 | */ |
||
| 243 | View Code Duplication | public function test_connection_reconnect_partial_blog_token_fail() { |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (user token). |
||
| 259 | */ |
||
| 260 | public function test_connection_reconnect_partial_user_token_success() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * This filter callback allow us to skip the database query by `Jetpack_Options` to retrieve the option. |
||
| 275 | * |
||
| 276 | * @param array $options List of options already skipping the database request. |
||
| 277 | * |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | public function bypass_raw_options( array $options ) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Intercept the `jetpack.register` API request sent to WP.com, and mock the response. |
||
| 288 | * |
||
| 289 | * @param bool|array $response The existing response. |
||
| 290 | * @param array $args The request arguments. |
||
| 291 | * @param string $url The request URL. |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | public function intercept_register_request( $response, $args, $url ) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. |
||
| 317 | * |
||
| 318 | * @param bool|array $response The existing response. |
||
| 319 | * @param array $args The request arguments. |
||
| 320 | * @param string $url The request URL. |
||
| 321 | * |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | public function intercept_validate_tokens_request_invalid_blog_token( $response, $args, $url ) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid user token" response. |
||
| 334 | * |
||
| 335 | * @param bool|array $response The existing response. |
||
| 336 | * @param array $args The request arguments. |
||
| 337 | * @param string $url The request URL. |
||
| 338 | * |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | public function intercept_validate_tokens_request_invalid_user_token( $response, $args, $url ) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "valid tokens" response. |
||
| 351 | * |
||
| 352 | * @param bool|array $response The existing response. |
||
| 353 | * @param array $args The request arguments. |
||
| 354 | * @param string $url The request URL. |
||
| 355 | * |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | public function intercept_validate_tokens_request_valid_tokens( $response, $args, $url ) { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Build the response for a tokens validation request |
||
| 368 | * |
||
| 369 | * @param string $invalid_token Accepted values: 'blog_token', 'user_token'. |
||
| 370 | * |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | private function build_validate_tokens_response( $invalid_token ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the success response. |
||
| 411 | * |
||
| 412 | * @param bool|array $response The existing response. |
||
| 413 | * @param array $args The request arguments. |
||
| 414 | * @param string $url The request URL. |
||
| 415 | * |
||
| 416 | * @return array |
||
| 417 | */ |
||
| 418 | View Code Duplication | public function intercept_refresh_blog_token_request( $response, $args, $url ) { |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the failure response. |
||
| 435 | * |
||
| 436 | * @param bool|array $response The existing response. |
||
| 437 | * @param array $args The request arguments. |
||
| 438 | * @param string $url The request URL. |
||
| 439 | * |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | View Code Duplication | public function intercept_refresh_blog_token_request_fail( $response, $args, $url ) { |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. |
||
| 459 | * |
||
| 460 | * @param bool|array $response The existing response. |
||
| 461 | * @param array $args The request arguments. |
||
| 462 | * @param string $url The request URL. |
||
| 463 | * |
||
| 464 | * @return array |
||
| 465 | */ |
||
| 466 | public function intercept_auth_token_request( $response, $args, $url ) { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Intercept the `Jetpack_Options` call and mock the values. |
||
| 489 | * |
||
| 490 | * @param mixed $value The current option value. |
||
| 491 | * @param string $name Option name. |
||
| 492 | * |
||
| 493 | * @return mixed |
||
| 494 | */ |
||
| 495 | public function mock_jetpack_options( $value, $name ) { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Build the `connection/reconnect` request object. |
||
| 508 | * |
||
| 509 | * @return WP_REST_Request |
||
| 510 | */ |
||
| 511 | private function build_reconnect_request() { |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Setup the environment to test the reconnection process. |
||
| 520 | * |
||
| 521 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
||
| 522 | */ |
||
| 523 | View Code Duplication | private function setup_reconnect_test( $invalid_token ) { |
|
| 562 | |||
| 563 | /** |
||
| 564 | * Restore the environment after the `reconnect` test has been run. |
||
| 565 | * |
||
| 566 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
||
| 567 | */ |
||
| 568 | View Code Duplication | private function shutdown_reconnect_test( $invalid_token ) { |
|
| 605 | |||
| 606 | } |
||
| 607 |