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.NotHyphenatedLowercase |
||
15 | class ManagerIntegrationTest extends \WorDBless\BaseTestCase { |
||
16 | |||
17 | /** |
||
18 | * The connection manager. |
||
19 | * |
||
20 | * @var Manager |
||
21 | */ |
||
22 | private $manager; |
||
23 | |||
24 | /** |
||
25 | * Initialize the object before running the test method. |
||
26 | * |
||
27 | * @before |
||
28 | */ |
||
29 | public function set_up() { |
||
32 | |||
33 | /** |
||
34 | * Test the `is_connected' method. |
||
35 | * |
||
36 | * @covers Automattic\Jetpack\Connection\Manager::is_connected |
||
37 | * @dataProvider is_connected_data_provider |
||
38 | * |
||
39 | * @param object|boolean $blog_token The blog token. False if the blog token does not exist. |
||
40 | * @param int|boolean $blog_id The blog id. False if the blog id does not exist. |
||
41 | * @param boolean $expected_output The expected output. |
||
42 | */ |
||
43 | public function test_is_connected( $blog_token, $blog_id, $expected_output ) { |
||
58 | |||
59 | /** |
||
60 | * Data provider for test_is_connected. |
||
61 | * |
||
62 | * Structure of the test data arrays: |
||
63 | * [0] => 'blog_token' object|boolean The blog token or false if the blog token does not exist. |
||
64 | * [1] => 'blog_id' int|boolean The blog id or false if the blog id does not exist. |
||
65 | * [2] => 'expected_output' boolean The expected output of the call to is_connected. |
||
66 | */ |
||
67 | public function is_connected_data_provider() { |
||
76 | |||
77 | /** |
||
78 | * Test get_connected_users |
||
79 | */ |
||
80 | public function test_get_connected_users() { |
||
112 | |||
113 | /** |
||
114 | * Test get_connection_owner and is_owner |
||
115 | */ |
||
116 | public function test_get_connection_owner_and_has_connected_owner() { |
||
156 | |||
157 | /** |
||
158 | * Test has_connected_user and has_connected_admin |
||
159 | */ |
||
160 | public function test_has_connected_user_and_has_connected_admin() { |
||
207 | |||
208 | /** |
||
209 | * Test is_connection_owner |
||
210 | */ |
||
211 | public function test_is_connection_owner() { |
||
245 | |||
246 | /** |
||
247 | * Test get_access_token method |
||
248 | * |
||
249 | * @dataProvider get_access_token_data_provider |
||
250 | * |
||
251 | * @param bool|string $create_blog_token The blog token to be created. |
||
252 | * @param bool|array $create_user_tokens The user tokens to be created. |
||
253 | * @param bool|int $master_user The ID of the master user to be defined. |
||
254 | * @param bool|int $user_id_query The user ID that will be used to fetch the token. |
||
255 | * @param bool|string $token_key_query The token_key that will be used to fetch the token. |
||
256 | * @param bool|string $expected_error_code If an error is expected, the error code. |
||
257 | * @param bool|object $expected_token If success is expected, the expected token object. |
||
258 | * @return void |
||
259 | */ |
||
260 | public function test_get_access_token( $create_blog_token, $create_user_tokens, $master_user, $user_id_query, $token_key_query, $expected_error_code, $expected_token ) { |
||
295 | |||
296 | /** |
||
297 | * Data provider for test_get_access_token |
||
298 | * |
||
299 | * @return array |
||
300 | */ |
||
301 | public function get_access_token_data_provider() { |
||
456 | |||
457 | /** |
||
458 | * Make sure we don´t change how we return errors |
||
459 | */ |
||
460 | public function test_get_access_token_suppress_errors() { |
||
464 | |||
465 | /** |
||
466 | * Test the `is_site_connection' method. |
||
467 | * |
||
468 | * @covers Automattic\Jetpack\Connection\Manager::is_site_connection |
||
469 | * @dataProvider data_provider_for_test_is_site_connection |
||
470 | * |
||
471 | * @param boolean $is_connected If the blog is connected. |
||
472 | * @param boolean $has_connected_user If the blog has a connected user. |
||
473 | * @param boolean $master_user_option_is_set If the master_user option is set. |
||
474 | * @param boolean $expected The expected output. |
||
475 | */ |
||
476 | public function test_is_site_connection( $is_connected, $has_connected_user, $master_user_option_is_set, $expected ) { |
||
477 | $id_admin = wp_insert_user( |
||
478 | array( |
||
479 | 'user_login' => 'admin', |
||
480 | 'user_pass' => 'pass', |
||
481 | 'role' => 'administrator', |
||
482 | ) |
||
483 | ); |
||
484 | |||
485 | if ( $is_connected ) { |
||
486 | \Jetpack_Options::update_option( 'id', 1234 ); |
||
487 | \Jetpack_Options::update_option( 'blog_token', 'asdasd.123123' ); |
||
488 | } else { |
||
489 | \Jetpack_Options::delete_option( 'blog_token' ); |
||
490 | \Jetpack_Options::delete_option( 'id' ); |
||
491 | } |
||
492 | |||
493 | if ( $has_connected_user ) { |
||
494 | \Jetpack_Options::update_option( |
||
495 | 'user_tokens', |
||
496 | array( |
||
497 | $id_admin => 'asd123', |
||
498 | ) |
||
499 | ); |
||
500 | } else { |
||
501 | \Jetpack_Options::delete_option( 'user_tokens' ); |
||
502 | } |
||
503 | |||
504 | if ( $master_user_option_is_set ) { |
||
505 | \Jetpack_Options::update_option( 'master_user', $id_admin ); |
||
506 | } else { |
||
507 | \Jetpack_Options::delete_option( 'master_user' ); |
||
508 | } |
||
509 | |||
510 | $this->assertEquals( $expected, $this->manager->is_site_connection() ); |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Data provider for test_is_site_connection. |
||
515 | * |
||
516 | * Structure of the test data arrays: |
||
517 | * [0] => 'is_connected' boolean If the blog is connected. |
||
518 | * [1] => 'has_connected_user' boolean If the blog has a connected user. |
||
519 | * [2] => 'master_user_option_is_set' boolean If the master_user option is set. |
||
520 | * [3] => 'expected' boolean The expected output of the call to is_site_connection. |
||
521 | */ |
||
522 | public function data_provider_for_test_is_site_connection() { |
||
523 | |||
524 | return array( |
||
525 | 'connected, has connected_user, master_user option is set' => array( true, true, true, false ), |
||
526 | 'not connected, has connected_user, master_user option is set' => array( false, true, true, false ), |
||
527 | 'connected, no connected_user, master_user option is set' => array( true, false, true, false ), |
||
528 | 'not connected, no connected_user, master_user option is set' => array( false, false, true, false ), |
||
529 | 'not connected, has connected_user, master_user option is not set' => array( false, true, false, false ), |
||
530 | 'not connected, no connected_user, master_user option is not set' => array( false, false, false, false ), |
||
531 | 'connected, has connected_user, master_user option is not set' => array( true, true, false, false ), |
||
532 | 'connected, no connected_user, master_user option is not set' => array( true, false, false, true ), |
||
533 | ); |
||
534 | } |
||
535 | |||
536 | /** |
||
537 | * Test the `try_registration()` method. |
||
538 | * |
||
539 | * @see Manager::try_registration() |
||
540 | */ |
||
541 | public function test_try_registration() { |
||
553 | |||
554 | /** |
||
555 | * Test that User tokens behave according to expectations after attempting to disconnect a user. |
||
556 | * |
||
557 | * @covers Automattic\Jetpack\Connection\Manager::disconnect_user |
||
558 | * @dataProvider get_disconnect_user_outcomes |
||
559 | * |
||
560 | * @param bool $remote_response Response from the unlink_user XML-RPC request. |
||
561 | * @param int $expected_user_token_count Number of user tokens left on site after Manager::disconnect_user has completed. |
||
562 | */ |
||
563 | public function test_disconnect_user( $remote_response, $expected_user_token_count ) { |
||
618 | |||
619 | /** |
||
620 | * Intercept the disconnect user API request sent to WP.com, and mock success response. |
||
621 | * |
||
622 | * @param bool|array $response The existing response. |
||
623 | * @param array $args The request arguments. |
||
624 | * @param string $url The request URL. |
||
625 | * |
||
626 | * @return array |
||
627 | */ |
||
628 | View Code Duplication | public function intercept_disconnect_success( $response, $args, $url ) { |
|
648 | |||
649 | /** |
||
650 | * Intercept the disconnect user API request sent to WP.com, and mock failure response. |
||
651 | * |
||
652 | * @param bool|array $response The existing response. |
||
653 | * @param array $args The request arguments. |
||
654 | * @param string $url The request URL. |
||
655 | * |
||
656 | * @return array |
||
657 | */ |
||
658 | View Code Duplication | public function intercept_disconnect_failure( $response, $args, $url ) { |
|
678 | |||
679 | /** |
||
680 | * Data for test_disconnect_user |
||
681 | * |
||
682 | * @return array |
||
683 | */ |
||
684 | public function get_disconnect_user_outcomes() { |
||
696 | } |
||
697 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.