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 |
||
17 | class Test_Blocks extends TestCase { |
||
18 | use \Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains; |
||
19 | |||
20 | /** |
||
21 | * Test block name. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | public $block_name = 'jetpack/apple'; |
||
26 | |||
27 | /** |
||
28 | * Setup runs before each test. |
||
29 | * |
||
30 | * @before |
||
31 | */ |
||
32 | public function set_up() { |
||
36 | |||
37 | /** |
||
38 | * Teardown runs after each test. |
||
39 | * |
||
40 | * @after |
||
41 | */ |
||
42 | public function tear_down() { |
||
46 | |||
47 | /** |
||
48 | * Test the different inputs and matching output for Classes. |
||
49 | * |
||
50 | * @since 9.0.0 |
||
51 | * |
||
52 | * @covers Automattic\Jetpack\Blocks::classes |
||
53 | */ |
||
54 | public function test_block_classes() { |
||
74 | |||
75 | /** |
||
76 | * Test for invalid alignment values. |
||
77 | * |
||
78 | * @since 9.0.0 |
||
79 | * |
||
80 | * @covers Automattic\Jetpack\Blocks::classes |
||
81 | */ |
||
82 | public function test_block_classes_invalid_align() { |
||
88 | |||
89 | /** |
||
90 | * Test whether we can detect an AMP view. |
||
91 | * |
||
92 | * @since 9.0.0 |
||
93 | * |
||
94 | * @covers Automattic\Jetpack\Blocks::is_amp_request |
||
95 | */ |
||
96 | public function test_is_amp_request() { |
||
104 | |||
105 | /** |
||
106 | * Test whether we can detect an AMP view. |
||
107 | * |
||
108 | * @since 9.0.0 |
||
109 | * |
||
110 | * @covers Automattic\Jetpack\Blocks::is_amp_request |
||
111 | */ |
||
112 | public function test_is_not_amp_request() { |
||
115 | |||
116 | /** |
||
117 | * Test WordPress and Gutenberg version requirements. |
||
118 | * |
||
119 | * @covers Automattic\Jetpack\Blocks::is_gutenberg_version_available |
||
120 | */ |
||
121 | public function test_returns_false_if_core_wp_version_less_than_minimum() { |
||
131 | |||
132 | /** |
||
133 | * Test WordPress and Gutenberg version requirements. |
||
134 | * |
||
135 | * @covers Automattic\Jetpack\Blocks::is_gutenberg_version_available |
||
136 | */ |
||
137 | public function test_returns_true_if_core_wp_version_greater_or_equal_to_minimum() { |
||
147 | |||
148 | /** |
||
149 | * Testing removing the Jetpack prefix from a block slug. |
||
150 | * |
||
151 | * @covers Automattic\Jetpack\Blocks::remove_extension_prefix |
||
152 | * |
||
153 | * @dataProvider get_extension_name_provider |
||
154 | * |
||
155 | * @param string $extension_slug Block / Extension name. |
||
156 | * @param string $expected_short_slug Extension name without Jetpack prefix. |
||
157 | */ |
||
158 | public function test_remove_extension_prefix( $extension_slug, $expected_short_slug ) { |
||
163 | |||
164 | /** |
||
165 | * Get different possible block names. |
||
166 | * |
||
167 | * Data provider for test_remove_extension_prefix. |
||
168 | */ |
||
169 | public function get_extension_name_provider() { |
||
185 | |||
186 | /** |
||
187 | * Test to ensure that an extension is returned as registered. |
||
188 | * |
||
189 | * @covers Automattic\Jetpack\Blocks::is_registered |
||
190 | */ |
||
191 | public function test_is_extension_registered() { |
||
197 | |||
198 | /** |
||
199 | * Ensure blocks cannot be registered twice. |
||
200 | * |
||
201 | * @covers Automattic\Jetpack\Blocks::jetpack_register_block |
||
202 | */ |
||
203 | public function test_jetpack_register_block_twice() { |
||
207 | |||
208 | /** |
||
209 | * Test to ensure blocks without a Jetpack prefix are registered, but with a jetpack prefix. |
||
210 | * |
||
211 | * @expectedIncorrectUsage Automattic\Jetpack\Blocks::jetpack_register_block |
||
212 | * @covers Automattic\Jetpack\Blocks::jetpack_register_block |
||
213 | */ |
||
214 | public function test_jetpack_register_block_without_jetpack() { |
||
218 | |||
219 | /** |
||
220 | * Test that by default we are not running in a Jetpack plugin context. |
||
221 | * |
||
222 | * @since 9.6.0 |
||
223 | * |
||
224 | * @covers Automattic\Jetpack\Blocks::is_standalone_block |
||
225 | */ |
||
226 | public function test_is_standalone_block() { |
||
229 | |||
230 | /** |
||
231 | * Test that we are running in a Jetpack plugin context, and not |
||
232 | * as a standalone block. |
||
233 | * |
||
234 | * @since 9.6.0 |
||
235 | * |
||
236 | * @covers Automattic\Jetpack\Blocks::is_standalone_block |
||
237 | */ |
||
238 | public function test_is_not_standalone_block() { |
||
246 | |||
247 | /** |
||
248 | * Test to ensure registering a Jetpack block does not add in an editor style dependency, |
||
249 | * when the Jetpack_Gutenberg class is not available. |
||
250 | * |
||
251 | * @since 9.6.0 |
||
252 | * |
||
253 | * @covers Automattic\Jetpack\Blocks::jetpack_register_block |
||
254 | */ |
||
255 | public function test_jetpack_register_block_without_editor_style() { |
||
260 | |||
261 | /** |
||
262 | * Test to ensure registering a Jetpack block adds in an editor style dependency, |
||
263 | * when the Jetpack_Gutenberg class is available. |
||
264 | * |
||
265 | * @since 9.6.0 |
||
266 | * |
||
267 | * @covers Automattic\Jetpack\Blocks::jetpack_register_block |
||
268 | */ |
||
269 | View Code Duplication | public function test_jetpack_register_block_with_editor_style() { |
|
280 | |||
281 | /** |
||
282 | * Test to ensure registering a Jetpack block does not override an existing dependency, |
||
283 | * when the Jetpack_Gutenberg class is available. |
||
284 | * |
||
285 | * @since 9.6.0 |
||
286 | * |
||
287 | * @covers Automattic\Jetpack\Blocks::jetpack_register_block |
||
288 | */ |
||
289 | View Code Duplication | public function test_jetpack_register_block_with_existing_editor_style() { |
|
304 | } |
||
305 |