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 |
||
| 21 | class Test_Status extends TestCase { |
||
| 22 | /** |
||
| 23 | * Default site URL. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $site_url = 'https://yourjetpack.blog'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Setup before running any of the tests. |
||
| 31 | */ |
||
| 32 | public static function setUpBeforeClass() { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Test setup. |
||
| 40 | */ |
||
| 41 | public function setUp() { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Test teardown. |
||
| 48 | */ |
||
| 49 | public function tearDown() { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Test is_development_mode when not using any filter |
||
| 56 | * |
||
| 57 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 58 | */ |
||
| 59 | public function test_is_development_mode_default() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Test is_development_mode when using the jetpack_development_mode filter |
||
| 68 | * |
||
| 69 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 70 | */ |
||
| 71 | public function test_is_development_mode_filter_true() { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Test when using a bool value for the jetpack_development_mode filter. |
||
| 80 | * |
||
| 81 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 82 | */ |
||
| 83 | public function test_is_development_mode_filter_bool() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Test when site url is localhost (dev mode on) |
||
| 92 | * |
||
| 93 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 94 | */ |
||
| 95 | public function test_is_development_mode_localhost() { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Test when using the constant to set dev mode |
||
| 105 | * |
||
| 106 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 107 | * |
||
| 108 | * @runInSeparateProcess |
||
| 109 | */ |
||
| 110 | public function test_is_development_mode_constant() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Test for is_multi_network with a single site |
||
| 132 | * |
||
| 133 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 134 | */ |
||
| 135 | public function test_is_multi_network_not_multisite() { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Test is_multi_network with a multisite install |
||
| 143 | * |
||
| 144 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 145 | */ |
||
| 146 | public function test_is_multi_network_when_single_network() { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Test is_multi_network when multiple networks |
||
| 157 | * |
||
| 158 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function test_is_multi_network_when_multiple_networks() { |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Test cached is_single_user_site |
||
| 171 | * |
||
| 172 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 173 | */ |
||
| 174 | public function test_is_single_user_site_with_transient() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Test is_single_user_site |
||
| 185 | * |
||
| 186 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 187 | */ |
||
| 188 | View Code Duplication | public function test_is_single_user_site_with_one_user() { |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Test is_single_user_site with multiple users |
||
| 200 | * |
||
| 201 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 202 | */ |
||
| 203 | View Code Duplication | public function test_is_single_user_site_with_multiple_users() { |
|
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * Mock a global function with particular arguments and make it return a certain value. |
||
| 216 | * |
||
| 217 | * @param string $function_name Name of the function. |
||
| 218 | * @param array $args Array of argument sets, last value of each set is used as a return value. |
||
| 219 | * @return phpmock\Mock The mock object. |
||
| 220 | */ |
||
| 221 | protected function mock_function_with_args( $function_name, $args = array() ) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Mock a set of constants. |
||
| 243 | * |
||
| 244 | * @param array $constants Array of sets with constants and their respective values. |
||
| 245 | * @return phpmock\Mock The mock object. |
||
| 246 | */ |
||
| 247 | protected function mock_constants( $constants = array() ) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Mock $wpdb->get_var() and make it return a certain value. |
||
| 260 | * |
||
| 261 | * @param mixed $return_value Return value of the function. |
||
| 262 | * |
||
| 263 | * PHPUnit\Framework\MockObject\MockObject The mock object. |
||
| 264 | */ |
||
| 265 | protected function mock_wpdb_get_var( $return_value = null ) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Clean up the existing $wpdb->get_var() mock. |
||
| 282 | */ |
||
| 283 | protected function clean_mock_wpdb_get_var() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Tests a WP Engine staging site URL. |
||
| 290 | * |
||
| 291 | * @author kraftbj |
||
| 292 | * @covers is_staging_site |
||
| 293 | * @since 3.9.0 |
||
| 294 | */ |
||
| 295 | public function test_is_staging_site_will_report_staging_for_wpengine_sites_by_url() { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Tests known staging sites. |
||
| 302 | * |
||
| 303 | * @dataProvider get_is_staging_site_known_hosting_providers_data |
||
| 304 | * |
||
| 305 | * @param string $site_url Site URL. |
||
| 306 | */ |
||
| 307 | public function test_is_staging_site_for_known_hosting_providers( $site_url ) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Known hosting providers. |
||
| 318 | * |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | public function get_is_staging_site_known_hosting_providers_data() { |
||
| 337 | } |
||
| 338 |