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() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Returning the environment into its initial state. |
||
| 68 | */ |
||
| 69 | public function tearDown() { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Testing the `/jetpack/v4/remote_authorize` endpoint. |
||
| 85 | */ |
||
| 86 | public function test_remote_authorize() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Testing the `/jetpack/v4/connection` endpoint. |
||
| 100 | */ |
||
| 101 | public function test_connection() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Testing the `/jetpack/v4/connection/plugins` endpoint. |
||
| 126 | */ |
||
| 127 | public function test_connection_plugins() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Testing the `connection/reconnect` endpoint, full reconnect. |
||
| 162 | */ |
||
| 163 | public function test_connection_reconnect_full() { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (blog token). |
||
| 180 | */ |
||
| 181 | View Code Duplication | public function test_connection_reconnect_partial_blog_token_success() { |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Testing the `connection/reconnect` endpoint, failed partial reconnect (blog token). |
||
| 197 | */ |
||
| 198 | View Code Duplication | public function test_connection_reconnect_partial_blog_token_fail() { |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (user token). |
||
| 214 | */ |
||
| 215 | public function test_connection_reconnect_partial_user_token_success() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * This filter callback allow us to skip the database query by `Jetpack_Options` to retrieve the option. |
||
| 230 | * |
||
| 231 | * @param array $options List of options already skipping the database request. |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | public function bypass_raw_options( array $options ) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Intercept the `jetpack.register` API request sent to WP.com, and mock the response. |
||
| 243 | * |
||
| 244 | * @param bool|array $response The existing response. |
||
| 245 | * @param array $args The request arguments. |
||
| 246 | * @param string $url The request URL. |
||
| 247 | * |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | public function intercept_register_request( $response, $args, $url ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. |
||
| 272 | * |
||
| 273 | * @param bool|array $response The existing response. |
||
| 274 | * @param array $args The request arguments. |
||
| 275 | * @param string $url The request URL. |
||
| 276 | * |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | public function intercept_validate_tokens_request_invalid_blog_token( $response, $args, $url ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid user token" response. |
||
| 289 | * |
||
| 290 | * @param bool|array $response The existing response. |
||
| 291 | * @param array $args The request arguments. |
||
| 292 | * @param string $url The request URL. |
||
| 293 | * |
||
| 294 | * @return array |
||
| 295 | */ |
||
| 296 | public function intercept_validate_tokens_request_invalid_user_token( $response, $args, $url ) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "valid tokens" response. |
||
| 306 | * |
||
| 307 | * @param bool|array $response The existing response. |
||
| 308 | * @param array $args The request arguments. |
||
| 309 | * @param string $url The request URL. |
||
| 310 | * |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | public function intercept_validate_tokens_request_valid_tokens( $response, $args, $url ) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Build the response for a tokens validation request |
||
| 323 | * |
||
| 324 | * @param string $invalid_token Accepted values: 'blog_token', 'user_token'. |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | private function build_validate_tokens_response( $invalid_token ) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the success response. |
||
| 366 | * |
||
| 367 | * @param bool|array $response The existing response. |
||
| 368 | * @param array $args The request arguments. |
||
| 369 | * @param string $url The request URL. |
||
| 370 | * |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | View Code Duplication | public function intercept_refresh_blog_token_request( $response, $args, $url ) { |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the failure response. |
||
| 390 | * |
||
| 391 | * @param bool|array $response The existing response. |
||
| 392 | * @param array $args The request arguments. |
||
| 393 | * @param string $url The request URL. |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function intercept_refresh_blog_token_request_fail( $response, $args, $url ) { |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Intercept the `Jetpack_Options` call to get `blog_id`, and set a random value. |
||
| 414 | * |
||
| 415 | * @param mixed $value The current option value. |
||
| 416 | * @param string $name Option name. |
||
| 417 | * |
||
| 418 | * @return int |
||
| 419 | */ |
||
| 420 | public function mock_blog_id( $value, $name ) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Intercept the `Jetpack_Options` call to get `user_tokens`, and set a mock value. |
||
| 430 | * |
||
| 431 | * @param mixed $value The current option value. |
||
| 432 | * @param string $name Option name. |
||
| 433 | * |
||
| 434 | * @return int |
||
| 435 | */ |
||
| 436 | public function mock_access_tokens( $value, $name ) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Build the `connection/reconnect` request object. |
||
| 446 | * |
||
| 447 | * @return WP_REST_Request |
||
| 448 | */ |
||
| 449 | private function build_reconnect_request() { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Setup the environment to test the reconnection process. |
||
| 459 | * |
||
| 460 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
||
| 461 | */ |
||
| 462 | private function setup_reconnect_test( $invalid_token ) { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Restore the environment after the `reconnect` test has been run. |
||
| 505 | * |
||
| 506 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
||
| 507 | */ |
||
| 508 | private function shutdown_reconnect_test( $invalid_token ) { |
||
| 546 | |||
| 547 | } |
||
| 548 |