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 |
||
24 | class ManagerTest extends TestCase { |
||
25 | |||
26 | use Options, Hooks; |
||
27 | |||
28 | /** |
||
29 | * Temporary stack for `wp_redirect`. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $arguments_stack = array(); |
||
34 | |||
35 | const DEFAULT_TEST_CAPS = array( 'default_test_caps' ); |
||
36 | |||
37 | /** |
||
38 | * Initialize the object before running the test method. |
||
39 | */ |
||
40 | public function setUp() { |
||
82 | |||
83 | /** |
||
84 | * Clean up the testing environment. |
||
85 | */ |
||
86 | public function tearDown() { |
||
91 | |||
92 | /** |
||
93 | * Test the `is_active` functionality when connected. |
||
94 | * |
||
95 | * @covers Automattic\Jetpack\Connection\Manager::is_active |
||
96 | */ |
||
97 | public function test_is_active_when_connected() { |
||
108 | |||
109 | /** |
||
110 | * Test the `is_active` functionality when not connected. |
||
111 | * |
||
112 | * @covers Automattic\Jetpack\Connection\Manager::is_active |
||
113 | */ |
||
114 | public function test_is_active_when_not_connected() { |
||
121 | |||
122 | /** |
||
123 | * Test the `api_url` generation. |
||
124 | * |
||
125 | * @covers Automattic\Jetpack\Connection\Manager::api_url |
||
126 | */ |
||
127 | public function test_api_url_defaults() { |
||
140 | |||
141 | /** |
||
142 | * Testing the ability of the api_url method to follow set constants and filters. |
||
143 | * |
||
144 | * @covers Automattic\Jetpack\Connection\Manager::api_url |
||
145 | */ |
||
146 | public function test_api_url_uses_constants_and_filters() { |
||
197 | |||
198 | /** |
||
199 | * Test the `is_user_connected` functionality. |
||
200 | * |
||
201 | * @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
||
202 | */ |
||
203 | public function test_is_user_connected_with_default_user_id_logged_out() { |
||
208 | |||
209 | /** |
||
210 | * Test the `is_user_connected` functionality. |
||
211 | * |
||
212 | * @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
||
213 | */ |
||
214 | public function test_is_user_connected_with_false_user_id_logged_out() { |
||
219 | |||
220 | /** |
||
221 | * Test the `is_user_connected` functionality |
||
222 | * |
||
223 | * @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
||
224 | */ |
||
225 | public function test_is_user_connected_with_user_id_logged_out_not_connected() { |
||
233 | |||
234 | |||
235 | /** |
||
236 | * Test the `is_user_connected` functionality. |
||
237 | * |
||
238 | * @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
||
239 | */ |
||
240 | View Code Duplication | public function test_is_user_connected_with_default_user_id_logged_in() { |
|
252 | |||
253 | /** |
||
254 | * Test the `is_user_connected` functionality. |
||
255 | * |
||
256 | * @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
||
257 | */ |
||
258 | View Code Duplication | public function test_is_user_connected_with_user_id_logged_in() { |
|
270 | |||
271 | /** |
||
272 | * Unit test for the "Delete all tokens" functionality. |
||
273 | * |
||
274 | * @covers Automattic\Jetpack\Connection\Manager::delete_all_connection_tokens |
||
275 | * @throws MockEnabledException PHPUnit wasn't able to enable mock functions. |
||
276 | */ |
||
277 | View Code Duplication | public function test_delete_all_connection_tokens() { |
|
294 | |||
295 | /** |
||
296 | * Unit test for the "Disconnect from WP" functionality. |
||
297 | * |
||
298 | * @covers Automattic\Jetpack\Connection\Manager::disconnect_site_wpcom |
||
299 | * @throws MockEnabledException PHPUnit wasn't able to enable mock functions. |
||
300 | */ |
||
301 | View Code Duplication | public function test_disconnect_site_wpcom() { |
|
318 | |||
319 | /** |
||
320 | * Test the `jetpack_connection_custom_caps' method. |
||
321 | * |
||
322 | * @covers Automattic\Jetpack\Connection\Manager::jetpack_connection_custom_caps |
||
323 | * @dataProvider jetpack_connection_custom_caps_data_provider |
||
324 | * |
||
325 | * @param bool $in_dev_mode Whether development mode is active. |
||
326 | * @param string $custom_cap The custom capability that is being tested. |
||
327 | * @param array $expected_caps The expected output. |
||
328 | */ |
||
329 | public function test_jetpack_connection_custom_caps( $in_dev_mode, $custom_cap, $expected_caps ) { |
||
342 | |||
343 | /** |
||
344 | * Data provider test_jetpack_connection_custom_caps. |
||
345 | * |
||
346 | * Structure of the test data arrays: |
||
347 | * [0] => 'in_dev_mode' boolean Whether development mode is active. |
||
348 | * [1] => 'custom_cap' string The custom capability that is being tested. |
||
349 | * [2] => 'expected_caps' array The expected output of the call to jetpack_connection_custom_caps. |
||
350 | */ |
||
351 | public function jetpack_connection_custom_caps_data_provider() { |
||
366 | |||
367 | /** |
||
368 | * Mock a global function and make it return a certain value. |
||
369 | * |
||
370 | * @param string $function_name Name of the function. |
||
371 | * @param mixed $return_value Return value of the function. |
||
372 | * @param string $namespace The namespace of the function. |
||
373 | * |
||
374 | * @return Mock The mock object. |
||
375 | * @throws MockEnabledException PHPUnit wasn't able to enable mock functions. |
||
376 | */ |
||
377 | View Code Duplication | protected function mock_function( $function_name, $return_value = null, $namespace = __NAMESPACE__ ) { |
|
392 | |||
393 | } |
||
394 |