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_offline_mode when not using any filter |
||
56 | * |
||
57 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
58 | */ |
||
59 | public function test_is_offline_mode_default() { |
||
60 | Functions\when( 'site_url' )->justReturn( $this->site_url ); |
||
61 | Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( false ); |
||
62 | |||
63 | $this->assertFalse( $this->status->is_offline_mode() ); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Test is_offline_mode when using the jetpack_offline_mode filter |
||
68 | * |
||
69 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
70 | */ |
||
71 | public function test_is_offline_mode_filter_true() { |
||
72 | Functions\when( 'site_url' )->justReturn( $this->site_url ); |
||
73 | Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( true ); |
||
74 | |||
75 | $this->assertTrue( $this->status->is_offline_mode() ); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Test when using a bool value for the jetpack_offline_mode filter. |
||
80 | * |
||
81 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
82 | */ |
||
83 | public function test_is_offline_mode_filter_bool() { |
||
84 | Functions\when( 'site_url' )->justReturn( $this->site_url ); |
||
85 | Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( 0 ); |
||
86 | |||
87 | $this->assertFalse( $this->status->is_offline_mode() ); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Test when site url is localhost (dev mode on) |
||
92 | * |
||
93 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
94 | */ |
||
95 | public function test_is_offline_mode_localhost() { |
||
96 | Functions\when( 'site_url' )->justReturn( 'localhost' ); |
||
97 | |||
98 | Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( false ); |
||
99 | |||
100 | $this->assertTrue( $this->status->is_offline_mode() ); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Test when using the constant to set dev mode |
||
105 | * |
||
106 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
107 | * |
||
108 | * @runInSeparateProcess |
||
109 | */ |
||
110 | public function test_is_offline_mode_constant() { |
||
111 | Functions\when( 'site_url' )->justReturn( $this->site_url ); |
||
112 | Filters\expectApplied( 'jetpack_offline_mode' )->once()->with( false )->andReturn( false ); |
||
113 | |||
114 | $constants_mocks = $this->mock_constants( |
||
115 | array( |
||
116 | array( '\\JETPACK_DEV_DEBUG', true ), |
||
117 | ) |
||
118 | ); |
||
119 | |||
120 | $this->assertTrue( $this->status->is_offline_mode() ); |
||
121 | |||
122 | array_map( |
||
123 | function( $mock ) { |
||
124 | $mock->disable(); |
||
125 | }, |
||
126 | $constants_mocks |
||
127 | ); |
||
128 | } |
||
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 | /** |
||
339 | * Tests known local development sites. |
||
340 | * |
||
341 | * @dataProvider get_is_local_site_known_tld |
||
342 | * |
||
343 | * @param string $site_url Site URL. |
||
344 | * @param bool $expected_response Expected response. |
||
345 | */ |
||
346 | public function test_is_local_site_for_known_tld( $site_url, $expected_response ) { |
||
359 | |||
360 | /** |
||
361 | * Known hosting providers. |
||
362 | * |
||
363 | * @return array |
||
364 | */ |
||
365 | public function get_is_local_site_known_tld() { |
||
393 | } |
||
394 |