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:
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 22 | class Test_REST_Endpoints extends TestCase { |
||
| 23 | |||
| 24 | const BLOG_TOKEN = 'new.blogtoken'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * REST Server object. |
||
| 28 | * |
||
| 29 | * @var WP_REST_Server |
||
| 30 | */ |
||
| 31 | private $server; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The original hostname to restore after tests are finished. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $api_host_original; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Setting up the test. |
||
| 42 | */ |
||
| 43 | public function setUp() { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Returning the environment into its initial state. |
||
| 67 | */ |
||
| 68 | public function tearDown() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Testing the `/jetpack/v4/remote_authorize` endpoint. |
||
| 83 | */ |
||
| 84 | public function test_remote_authorize() { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Testing the `/jetpack/v4/connection` endpoint. |
||
| 98 | */ |
||
| 99 | public function test_connection() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Testing the `/jetpack/v4/connection/plugins` endpoint. |
||
| 124 | */ |
||
| 125 | public function test_connection_plugins() { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Testing the `connection/reconnect` endpoint, full reconnect. |
||
| 160 | */ |
||
| 161 | public function test_connection_reconnect_full() { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (blog token). |
||
| 178 | */ |
||
| 179 | View Code Duplication | public function test_connection_reconnect_partial_blog_token_success() { |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Testing the `connection/reconnect` endpoint, failed partial reconnect (blog token). |
||
| 195 | */ |
||
| 196 | View Code Duplication | public function test_connection_reconnect_partial_blog_token_fail() { |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (user token). |
||
| 212 | */ |
||
| 213 | public function test_connection_reconnect_partial_user_token_success() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * This filter callback allow us to skip the database query by `Jetpack_Options` to retrieve the option. |
||
| 228 | * |
||
| 229 | * @param array $options List of options already skipping the database request. |
||
| 230 | * |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | public function bypass_raw_options( array $options ) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Intercept the `jetpack.register` API request sent to WP.com, and mock the response. |
||
| 241 | * |
||
| 242 | * @param bool|array $response The existing response. |
||
| 243 | * @param array $args The request arguments. |
||
| 244 | * @param string $url The request URL. |
||
| 245 | * |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | public function intercept_register_request( $response, $args, $url ) { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. |
||
| 270 | * |
||
| 271 | * @param bool|array $response The existing response. |
||
| 272 | * @param array $args The request arguments. |
||
| 273 | * @param string $url The request URL. |
||
| 274 | * |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | public function intercept_validate_tokens_request_invalid_blog_token( $response, $args, $url ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid user token" response. |
||
| 287 | * |
||
| 288 | * @param bool|array $response The existing response. |
||
| 289 | * @param array $args The request arguments. |
||
| 290 | * @param string $url The request URL. |
||
| 291 | * |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function intercept_validate_tokens_request_invalid_user_token( $response, $args, $url ) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "valid tokens" response. |
||
| 304 | * |
||
| 305 | * @param bool|array $response The existing response. |
||
| 306 | * @param array $args The request arguments. |
||
| 307 | * @param string $url The request URL. |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | public function intercept_validate_tokens_request_valid_tokens( $response, $args, $url ) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Build the response for a tokens validation request |
||
| 321 | * |
||
| 322 | * @param string $invalid_token Accepted values: 'blog_token', 'user_token'. |
||
| 323 | * |
||
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | private function build_validate_tokens_response( $invalid_token ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the success response. |
||
| 364 | * |
||
| 365 | * @param bool|array $response The existing response. |
||
| 366 | * @param array $args The request arguments. |
||
| 367 | * @param string $url The request URL. |
||
| 368 | * |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | View Code Duplication | public function intercept_refresh_blog_token_request( $response, $args, $url ) { |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the failure response. |
||
| 388 | * |
||
| 389 | * @param bool|array $response The existing response. |
||
| 390 | * @param array $args The request arguments. |
||
| 391 | * @param string $url The request URL. |
||
| 392 | * |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | View Code Duplication | public function intercept_refresh_blog_token_request_fail( $response, $args, $url ) { |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Intercept the `Jetpack_Options` call to get `blog_id`, and set a random value. |
||
| 412 | * |
||
| 413 | * @param mixed $value The current option value. |
||
| 414 | * @param string $name Option name. |
||
| 415 | * |
||
| 416 | * @return int |
||
| 417 | */ |
||
| 418 | public function mock_blog_id( $value, $name ) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Intercept the `Jetpack_Options` call to get `user_tokens`, and set a mock value. |
||
| 428 | * |
||
| 429 | * @param mixed $value The current option value. |
||
| 430 | * @param string $name Option name. |
||
| 431 | * |
||
| 432 | * @return int |
||
| 433 | */ |
||
| 434 | public function mock_access_tokens( $value, $name ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Build the `connection/reconnect` request object. |
||
| 444 | * |
||
| 445 | * @return WP_REST_Request |
||
| 446 | */ |
||
| 447 | private function build_reconnect_request() { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Setup the environment to test the reconnection process. |
||
| 457 | * |
||
| 458 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
||
| 459 | */ |
||
| 460 | private function setup_reconnect_test( $invalid_token ) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Restore the environment after the `reconnect` test has been run. |
||
| 503 | * |
||
| 504 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
||
| 505 | */ |
||
| 506 | private function shutdown_reconnect_test( $invalid_token ) { |
||
| 544 | |||
| 545 | } |
||
| 546 |